From db3fe3739d32800512114c2b3c260e4db8c5e598 Mon Sep 17 00:00:00 2001 From: Andy Lindsay Date: Tue, 10 Jul 2018 14:23:23 -0700 Subject: [PATCH] simpletext support for recent wifi code examples For #173, added putStrWithNpcVals and sscanAfterStr functions. Temperature from OpenWeathermap updated to search for an exact match to the JSON attribute. --- .../WiFi/Temperature from OpenWeatherMap.c | 75 ++++++++---------- .../libsimpletext/cmm/libsimpletext.a | Bin 51850 -> 54128 bytes .../libsimpletext/libsimpletext.side | 3 + .../libsimpletext/putStrWithNpcVals.c | 65 +++++++++++++++ .../TextDevices/libsimpletext/scanAfterStr.c | 1 + .../TextDevices/libsimpletext/simpletext.h | 33 ++++++++ .../TextDevices/libsimpletext/sscanAfterStr.c | 53 +++++++++++++ version.txt | 2 +- 8 files changed, 190 insertions(+), 42 deletions(-) create mode 100644 Learn/Simple Libraries/TextDevices/libsimpletext/putStrWithNpcVals.c create mode 100644 Learn/Simple Libraries/TextDevices/libsimpletext/scanAfterStr.c create mode 100644 Learn/Simple Libraries/TextDevices/libsimpletext/sscanAfterStr.c diff --git a/Learn/Examples/Network/WiFi/Temperature from OpenWeatherMap.c b/Learn/Examples/Network/WiFi/Temperature from OpenWeatherMap.c index dfd88fc8..438cf61c 100644 --- a/Learn/Examples/Network/WiFi/Temperature from OpenWeatherMap.c +++ b/Learn/Examples/Network/WiFi/Temperature from OpenWeatherMap.c @@ -19,11 +19,10 @@ match the COM control circuit you choose. This application does not make the Wi-Fi module serve - and monitor a page. Instead, it grabs text from this - page on the Internet: - www-eng-x.llnl.gov//documents/a_document.txt + and monitor a page. Instead, it gets weather information + over the Internet from openweathermap.com - Note: This example relies on the 0.8 version of the wifi library. + Note: This example relies on the 0.82 version of the wifi library. Updates may change some function behaviors in later releases. */ @@ -32,7 +31,7 @@ int event, id, handle; char str[1024]; -char wifi_event; +float temp, degC, degF; int main() { @@ -40,7 +39,9 @@ int main() //wifi_start(9, 8, 115200, USB_PGM_TERM); wifi_setBuffer(str, sizeof(str)); - pause(5000); + print("Waiting 10 s..."); + pause(10000); + print("done!\r\r"); while(1) { @@ -50,7 +51,7 @@ int main() pause(2000); - print("tcpHandle = %d\r", tcpHandle); + print("tcpHandle: %d\r", tcpHandle); pause(2000); @@ -65,55 +66,47 @@ int main() "Host: api.openweathermap.org\r\n"\ "Connection: keep-alive\r\n"\ "Accept: *" "/" "*\r\n\r\n"; - - int size = strlen(request); - print("GET req size: %d\r", size); + int length = strlen(request); + + print("GET request length: %d\r", length); + print("GET request\r======================================\r"); + putStrWithNpcVals(request); + print("\r------------------------------------\rEnd of GET request\r\r\r"); pause(2000); wifi_print(TCP, tcpHandle, "%s", request); - event = wifi_event; pause(2000); - size = strlen(str); - print("size = %d", size); + print("WX serial response: %s\r\r", str); pause(2000); wifi_scan(TCP, tcpHandle, "%s", str); - for(int n = 0; n < sizeof(str); n++) - { - if(str[n] <= 'z' && str[n] >= ' ') - { - print("%c", str[n]); - } - else if(str[n] == 0) - { - print("[%d]", str[n]); - break; - } - else if(str[n] == '\n') - { - print("\r", str[n]); - } - else - { - print("[%d]", str[n]); - } - } - char *loc = strstr(str, "temp"); - print("\rloc = %d\r", loc); - float temp = 0; - sscan(loc+5, "%f", &temp); - float degC = temp -273.15; + + length = strlen(str); + print("Server response length: %d\r", length); + print("Server response to GET request\r"); + print("======================================\r"); + putStrWithNpcVals(str); + print("\r------------------------------------\r"); + print( "End of server response to GET request\r\r"); + + temp = 0; + sscanAfterStr(str, "\"temp\":", "%f", &temp); + print("temp = %6.2f deg K\r", temp); + + wifi_disconnect(tcpHandle); + degC = temp -273.15; print("temp = %6.2f deg C\r", degC); - float degF = degC * 9.0 / 5.0 + 32.0; + degF = degC * 9.0 / 5.0 + 32.0; print("temp = %6.2f deg C\r", degF); - wifi_disconnect(tcpHandle); - print("\rdelay..."); pause(20000); print("done!\r\r\r"); } } + + + diff --git a/Learn/Simple Libraries/TextDevices/libsimpletext/cmm/libsimpletext.a b/Learn/Simple Libraries/TextDevices/libsimpletext/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wR*8`40`Yxu$wrPApp zJ!5srp7UM|$wX(ymdfdQF}>dv2~DINAd`Zb>Fi!XL~C*~PdkQHCwPkvTB`#&jN zcB0TNrxg?5r;3tmFj42VT3&qfi_dXjgVYWdgs7Re|5jlOW#wtL`Ns(MFxH6tg$}aLe z%SZ+$^$Nz7k~<+ixu0WF??oj)c{;g7J%OBpT>7TI2()u9LJUfdi6^a7!hd8@&0T%b zp!Syxe1jhAaQ-3uI6JdwDi2xpmRQqC(($d0Og8N&1!ruE6(o0FaK1hn3tJd(BKxjd zI_LO;Kwp0^{)BaRdk6x79X+f<9jKp$#Hn$@k&DEkFE;QQ0xnZvq1@s0B(Gm#NsCpcm-r^WuZ8r??irOL?s zod93W>UEmCs)8sUiLNYtGLTt%4)-H^oM!bo8sk&rCAO6%3dok}7sv4#(`M)7sC%qI zd^N2*npvE+ptZ0Ng`tjDZIFfyvSNgx-PEDx`~=Btt|5VL=hP+ z43bc#&0;`A7=8uhA=91|0c8{8$(F?nqfQCMRj7Me%|<)m4>_4{nS4Axhx-|@$|Um85=QFCg%~#l3#|43uU=fe=+4>l1s0K zcw}CBF!D&sgtH`*8MFumzWdAa^!Fl@*+O6cu*pvK)xY*kQ7R+i)@Y+S`Y@k?zksnd zvO|?Se3d8EiKk|an!V~z$Qq6+w1Ka<*HoV9xckJ4compiler=C >memtype=cmm main ram compact >optimize=-Os diff --git a/Learn/Simple Libraries/TextDevices/libsimpletext/putStrWithNpcVals.c b/Learn/Simple Libraries/TextDevices/libsimpletext/putStrWithNpcVals.c new file mode 100644 index 00000000..78ad4d36 --- /dev/null +++ b/Learn/Simple Libraries/TextDevices/libsimpletext/putStrWithNpcVals.c @@ -0,0 +1,65 @@ +/* + * @file putStrWithNpCVals.c + * Function to print a string to the terminal with non printable characters + * displayed as decimal values in brackets. + * + * Copyright (c) 2018, Parallax Inc. + * Written by Andy Lindsay + */ + +#include "simpletext.h" + +int putStrWithNpcVals(const char *s) +{ + int size = strlen(s); + for(int n = 0; n <= size; n++) + { + if(s[n] <= '~' && s[n] >= ' ') + { + print("%c", s[n]); + } + else if(s[n] == 0) + { + print("[%d]", s[n]); + break; + } + else if(s[n] == '\n' || s[n] == '\r') + { + print("[%d]%c", s[n], s[n]); + } + else + { + print("[%d]", s[n]); + } + //pause(10); + } + //pause(10); + return size; +} + +/* ++-------------------------------------------------------------------- +| TERMS OF USE: MIT License ++-------------------------------------------------------------------- +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files +(the "Software"), to deal in the Software without restriction, +including without limitation the rights to use, copy, modify, merge, +publish, distribute, sublicense, and/or sell copies of the Software, +and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ++-------------------------------------------------------------------- +*/ + + diff --git a/Learn/Simple Libraries/TextDevices/libsimpletext/scanAfterStr.c b/Learn/Simple Libraries/TextDevices/libsimpletext/scanAfterStr.c new file mode 100644 index 00000000..8b137891 --- /dev/null +++ b/Learn/Simple Libraries/TextDevices/libsimpletext/scanAfterStr.c @@ -0,0 +1 @@ + diff --git a/Learn/Simple Libraries/TextDevices/libsimpletext/simpletext.h b/Learn/Simple Libraries/TextDevices/libsimpletext/simpletext.h index 0c7e5324..70ff3222 100644 --- a/Learn/Simple Libraries/TextDevices/libsimpletext/simpletext.h +++ b/Learn/Simple Libraries/TextDevices/libsimpletext/simpletext.h @@ -385,6 +385,27 @@ int sprint(char *buffer, const char *format, ...) __attribute__((format (printf, int sscan(const char *buffer, const char *fmt, ...) __attribute__((format (printf, 2, 3))); +/** + * @brief Store values represented by characters in a buffer in variable list + * using "..." args. + * + * @note See scan for format specifiers. + * + * @param *buffer Pointer to string with formatted values. + * + * @param *str String to find before scanning. Scanning starts after the last character. + * in this string. + * + * @param *fmt C printf comparable format string. + * + * @param ... Arguments where output will go and must be pointers. + * + * @returns Number of % specifiers successfully matched. + */ +int sscanAfterStr(char *buffer, char *str, char *fmt, ...) __attribute__((format (printf, 3, 4))); + + + /** * @name Print/Scan for Device Communication * @{ @@ -668,6 +689,18 @@ int putln(const char* str); int putLine(const char* str); +/** + * @brief Print string to the debug port, and display all non printable characters + * (NPCs) as decimal ASCII values in brackets. For example, delete would be [127]. + * This function also displays the null [0] terminator. + * + * @param *str Null terminated string to send. + */ +int putStrWithNpcVals(const char *s); + + + + /** * @} * diff --git a/Learn/Simple Libraries/TextDevices/libsimpletext/sscanAfterStr.c b/Learn/Simple Libraries/TextDevices/libsimpletext/sscanAfterStr.c new file mode 100644 index 00000000..f5ef7080 --- /dev/null +++ b/Learn/Simple Libraries/TextDevices/libsimpletext/sscanAfterStr.c @@ -0,0 +1,53 @@ +/* + * Super-simple text I/O for PropGCC, stripped of all stdio overhead. + * Copyright (c) 2012, Ted Stefanik. Concept inspired by: + * + * very simple printf, adapted from one written by me [Eric Smith] + * for the MiNT OS long ago + * placed in the public domain + * - Eric Smith + * Propeller specific adaptations + * Copyright (c) 2011 Parallax, Inc. + * Written by Eric R. Smith, Total Spectrum Software Inc. + * + * MIT licensed (see terms at end of file) + */ + +#include +#include "simpletext.h" + +//int sscanAfterStr(char *str, char *strAfter, char *fmt, ...) +int sscanAfterStr(char *buffer, char *str, char *fmt, ...){ + char *loc = strstr(buffer, str); + loc += strlen(str); + va_list args; + va_start(args, fmt); + int blocks = _doscanf(loc, fmt, args); + va_end(args); + return blocks; +} + +/* +-------------------------------------------------------------------- + * | TERMS OF USE: MIT License + * +-------------------------------------------------------------------- + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files + * (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, + * publish, distribute, sublicense, and/or sell copies of the Software, + * and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * +-------------------------------------------------------------------- + */ + diff --git a/version.txt b/version.txt index a6abb852..777e29a2 100644 --- a/version.txt +++ b/version.txt @@ -1 +1 @@ -v1.4.89 +v1.4.94 \ No newline at end of file