From 88525723b4529f435e7a66ad6401493a00c579dc Mon Sep 17 00:00:00 2001 From: mikee47 Date: Wed, 27 Mar 2024 15:52:27 +0000 Subject: [PATCH 01/13] Revise `HttpServer_AJAX` so that GPIO numbers are consistent with code Allow variable number of pins in list, web page updated dynamically --- samples/HttpServer_AJAX/app/application.cpp | 45 ++++++++++++-------- samples/HttpServer_AJAX/web/build/index.html | 3 +- samples/HttpServer_AJAX/web/dev/index.html | 3 +- 3 files changed, 30 insertions(+), 21 deletions(-) diff --git a/samples/HttpServer_AJAX/app/application.cpp b/samples/HttpServer_AJAX/app/application.cpp index ccd3595381..205c72e578 100644 --- a/samples/HttpServer_AJAX/app/application.cpp +++ b/samples/HttpServer_AJAX/app/application.cpp @@ -7,18 +7,26 @@ #define WIFI_PWD "PleaseEnterPass" #endif +namespace +{ HttpServer server; FtpServer ftp; -int inputs[] = {0, 2}; // Set input GPIO pins here -Vector namesInput; -const int countInputs = sizeof(inputs) / sizeof(inputs[0]); +// Set input GPIO pins here +const uint8_t inputs[] = {5, 2}; void onIndex(HttpRequest& request, HttpResponse& response) { TemplateFileStream* tmpl = new TemplateFileStream("index.html"); - //auto& vars = tmpl->variables(); - //vars["counter"] = String(counter); + auto& vars = tmpl->variables(); + String gpioList; + for(unsigned i = 0; i < ARRAY_SIZE(inputs); ++i) { + String s = F("GPIO{gpio} "); + s.replace("{id}", String(i)); + s.replace("{gpio}", String(inputs[i])); + gpioList += s; + } + vars["gpio_list"] = gpioList; response.sendNamedStream(tmpl); // this template object will be deleted automatically } @@ -40,8 +48,8 @@ void onAjaxInput(HttpRequest& request, HttpResponse& response) JsonObject json = stream->getRoot(); json["status"] = (bool)true; - String stringKey = "StringKey"; - String stringValue = "StringValue"; + String stringKey = F("StringKey"); + String stringValue = F("StringValue"); json[stringKey] = stringValue; @@ -51,8 +59,9 @@ void onAjaxInput(HttpRequest& request, HttpResponse& response) } JsonObject gpio = json.createNestedObject("gpio"); - for(int i = 0; i < countInputs; i++) - gpio[namesInput[i].c_str()] = digitalRead(inputs[i]); + for(unsigned i = 0; i < ARRAY_SIZE(inputs); ++i) { + gpio[String(i)] = digitalRead(inputs[i]); + } response.sendDataStream(stream, MIME_JSON); } @@ -64,7 +73,7 @@ void onAjaxFrequency(HttpRequest& request, HttpResponse& response) JsonObjectStream* stream = new JsonObjectStream(); JsonObject json = stream->getRoot(); - json["status"] = (bool)true; + json["status"] = true; json["value"] = (int)System.getCpuFrequency(); response.sendDataStream(stream, MIME_JSON); @@ -78,10 +87,11 @@ void startWebServer() server.paths.set("/ajax/frequency", onAjaxFrequency); server.paths.setDefault(onFile); - Serial.println(_F("\r\n" - "=== WEB SERVER STARTED ===")); - Serial.println(WifiStation.getIP()); - Serial.println(_F("==========================\r\n")); + Serial << endl + << _F("=== WEB SERVER STARTED ===") << endl + << WifiStation.getIP() << endl + << _F("==========================") << endl + << endl; } void startFTP() @@ -101,6 +111,8 @@ void gotIP(IpAddress ip, IpAddress netmask, IpAddress gateway) startWebServer(); } +} // namespace + void init() { spiffs_mount(); // Mount file system, in order to work with files @@ -112,9 +124,8 @@ void init() WifiStation.config(WIFI_SSID, WIFI_PWD); WifiAccessPoint.enable(false); - for(int i = 0; i < countInputs; i++) { - namesInput.add(String(inputs[i])); - pinMode(inputs[i], INPUT); + for(auto pin : inputs) { + pinMode(pin, INPUT); } // Run our method when station was connected to AP diff --git a/samples/HttpServer_AJAX/web/build/index.html b/samples/HttpServer_AJAX/web/build/index.html index 1fab1be6ed..cb23240030 100644 --- a/samples/HttpServer_AJAX/web/build/index.html +++ b/samples/HttpServer_AJAX/web/build/index.html @@ -71,8 +71,7 @@

Sming AJAX Example

Reading Input value

-

GPIO0 -

GPIO2 + {gpio_list}

diff --git a/samples/HttpServer_AJAX/web/dev/index.html b/samples/HttpServer_AJAX/web/dev/index.html index 1fab1be6ed..cb23240030 100644 --- a/samples/HttpServer_AJAX/web/dev/index.html +++ b/samples/HttpServer_AJAX/web/dev/index.html @@ -71,8 +71,7 @@

Sming AJAX Example

Reading Input value

-

GPIO0 -

GPIO2 + {gpio_list}

From 4bff8d70d8baf43f1ad333d12bd27f963072a7f0 Mon Sep 17 00:00:00 2001 From: mikee47 Date: Sat, 6 Jan 2024 10:43:54 +0000 Subject: [PATCH 02/13] Update Basic_Ssl keys, use Bearssl --- samples/Basic_Ssl/app/application.cpp | 10 ++++++---- samples/Basic_Ssl/component.mk | 2 +- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/samples/Basic_Ssl/app/application.cpp b/samples/Basic_Ssl/app/application.cpp index e0b9fae5eb..6ee2a57197 100644 --- a/samples/Basic_Ssl/app/application.cpp +++ b/samples/Basic_Ssl/app/application.cpp @@ -63,16 +63,18 @@ void grcSslInit(Ssl::Session& session, HttpRequest& request) // Use the Gibson Research fingerprints web page as an example. Unlike Google, these fingerprints change very infrequently. static const Ssl::Fingerprint::Cert::Sha1 sha1Fingerprint PROGMEM = { - 0xC3, 0xFB, 0x91, 0x85, 0xCC, 0x6B, 0x4C, 0x7D, 0xE7, 0x18, - 0xED, 0xD8, 0x00, 0xD2, 0x84, 0xE7, 0x6E, 0x97, 0x06, 0x07, + 0xA6, 0x8F, 0x8C, 0x47, 0x6B, 0xD0, 0xDE, 0x9E, 0x1D, 0x18, + 0x4A, 0x0A, 0x51, 0x4D, 0x90, 0x11, 0x31, 0x93, 0x40, 0x6D, + }; static const Ssl::Fingerprint::Cert::Sha256 certSha256Fingerprint PROGMEM = { - 0xC3, 0xFB, 0x91, 0x85, 0xCC, 0x6B, 0x4C, 0x7D, 0xE7, 0x18, - 0xED, 0xD8, 0x00, 0xD2, 0x84, 0xE7, 0x6E, 0x97, 0x06, 0x07, + 0xDA, 0x2C, 0xF7, 0x62, 0x09, 0x2C, 0x0A, 0x7B, 0x88, 0xA0, 0xDA, 0x65, 0xF9, 0x29, 0x45, 0x97, + 0xA6, 0xDB, 0xAA, 0x2C, 0x80, 0xFD, 0x75, 0x0A, 0xD9, 0xA0, 0x75, 0xEE, 0x64, 0xEE, 0x06, 0x68, }; static const Ssl::Fingerprint::Pki::Sha256 publicKeyFingerprint PROGMEM = { + // Out of date 0xad, 0xcc, 0x21, 0x92, 0x8e, 0x65, 0xc7, 0x54, 0xac, 0xac, 0xb8, 0x2f, 0x12, 0x95, 0x2e, 0x19, 0x7d, 0x15, 0x7e, 0x32, 0xbe, 0x90, 0x27, 0x43, 0xab, 0xfe, 0xf1, 0xf2, 0xf2, 0xe1, 0x9c, 0x35, }; diff --git a/samples/Basic_Ssl/component.mk b/samples/Basic_Ssl/component.mk index b994d8280f..59fa1e1767 100644 --- a/samples/Basic_Ssl/component.mk +++ b/samples/Basic_Ssl/component.mk @@ -4,4 +4,4 @@ COMPONENT_DEPENDS += malloc_count COMPONENT_CXXFLAGS += -DENABLE_MALLOC_COUNT=1 endif -ENABLE_SSL = 1 +ENABLE_SSL = Bearssl From 08033be413796cd68aee495adc1962cd2c00129e Mon Sep 17 00:00:00 2001 From: mikee47 Date: Wed, 27 Mar 2024 15:59:47 +0000 Subject: [PATCH 03/13] Initialise timers using templated methods where appropriate Includes static range check on value --- .../Bluetooth_Gamepad/app/application.cpp | 2 +- .../Bluetooth_Keyboard/app/application.cpp | 2 +- .../samples/generic/app/application.cpp | 2 +- Sming/Libraries/DS18S20/ds18s20.cpp | 6 ++--- .../samples/generic/app/application.cpp | 2 +- .../Bluetooth_Joycon/app/application.cpp | 2 +- .../Accelerometer_MMA7455/app/application.cpp | 2 +- samples/Basic_Blink/app/application.cpp | 2 +- samples/Basic_Capsense/app/application.cpp | 2 +- samples/Basic_HwPWM/app/application.cpp | 2 +- samples/Basic_NFC/app/application.cpp | 4 +-- samples/Basic_Neopixel/app/application.cpp | 2 +- samples/Basic_ScannerI2C/app/application.cpp | 2 +- samples/Basic_Serial/app/application.cpp | 2 +- .../Basic_WebSkeletonApp/app/application.cpp | 2 +- .../app/application.cpp | 2 +- samples/CanBus/app/application.cpp | 2 +- samples/Compass_HMC5883L/app/application.cpp | 2 +- samples/DFPlayerMini/app/application.cpp | 4 +-- .../HttpClient_ThingSpeak/app/application.cpp | 2 +- .../app/application.cpp | 2 +- samples/Humidity_AM2321/app/application.cpp | 2 +- samples/Humidity_DHT22/app/application.cpp | 2 +- samples/Humidity_SI7021/app/application.cpp | 4 +-- samples/IR_lib/app/application.cpp | 2 +- samples/LED_YeelightBulb/app/application.cpp | 2 +- samples/Light_BH1750/app/application.cpp | 2 +- samples/MeteoControl/app/application.cpp | 6 ++--- samples/MeteoControl_mqtt/app/bmp180.cpp | 2 +- samples/MeteoControl_mqtt/app/si7021.cpp | 2 +- .../MeteoControl_mqtt/include/configuration.h | 2 +- samples/MqttClient_Hello/app/application.cpp | 4 +-- samples/Network_Ping/app/application.cpp | 4 +-- samples/Nextion_Button/app/application.cpp | 2 +- .../PortExpander_MCP23S17/app/application.cpp | 2 +- samples/Radio_RCSwitch/app/application.cpp | 4 +-- samples/Radio_nRF24L01/app/application.cpp | 2 +- samples/Radio_si4432/app/application.cpp | 2 +- .../ScreenOLED_SSD1306/app/application.cpp | 4 +-- samples/ScreenTFT_ST7735/app/application.cpp | 26 +++++++++---------- .../TcpClient_NarodMon/app/application.cpp | 2 +- samples/Ultrasonic_HCSR04/app/application.cpp | 2 +- samples/Websocket_Client/app/application.cpp | 4 +-- 43 files changed, 67 insertions(+), 67 deletions(-) diff --git a/Sming/Libraries/BLEGamepad/samples/Bluetooth_Gamepad/app/application.cpp b/Sming/Libraries/BLEGamepad/samples/Bluetooth_Gamepad/app/application.cpp index 6fa4659ec5..84dc8a3719 100644 --- a/Sming/Libraries/BLEGamepad/samples/Bluetooth_Gamepad/app/application.cpp +++ b/Sming/Libraries/BLEGamepad/samples/Bluetooth_Gamepad/app/application.cpp @@ -40,5 +40,5 @@ void init() // Auto reporting is enabled by default. // Use bleGamepad.setAutoReport(false); to disable auto reporting, and then use bleGamepad.sendReport(); as needed - procTimer.initializeMs(500, loop).start(); + procTimer.initializeMs<500>(loop).start(); } diff --git a/Sming/Libraries/BLEKeyboard/samples/Bluetooth_Keyboard/app/application.cpp b/Sming/Libraries/BLEKeyboard/samples/Bluetooth_Keyboard/app/application.cpp index bc6d959e34..5ecbd571ba 100644 --- a/Sming/Libraries/BLEKeyboard/samples/Bluetooth_Keyboard/app/application.cpp +++ b/Sming/Libraries/BLEKeyboard/samples/Bluetooth_Keyboard/app/application.cpp @@ -44,5 +44,5 @@ void init() Serial.println("Starting BLE Keyboard sample!"); bleKeyboard.begin(); - procTimer.initializeMs(1000, loop).start(); + procTimer.initializeMs<1000>(loop).start(); } diff --git a/Sming/Libraries/CS5460/samples/generic/app/application.cpp b/Sming/Libraries/CS5460/samples/generic/app/application.cpp index ea0bec20a1..0349ba6653 100644 --- a/Sming/Libraries/CS5460/samples/generic/app/application.cpp +++ b/Sming/Libraries/CS5460/samples/generic/app/application.cpp @@ -26,5 +26,5 @@ void init() powerMeter.writeRegister(CONFIG_REGISTER, conf); powerMeter.startMultiConvert(); - printVoltageTimer.initializeMs(1000, printVoltage).start(); + printVoltageTimer.initializeMs<1000>(printVoltage).start(); } diff --git a/Sming/Libraries/DS18S20/ds18s20.cpp b/Sming/Libraries/DS18S20/ds18s20.cpp index 6c9d7d3b6e..f0d5ab4dbd 100644 --- a/Sming/Libraries/DS18S20/ds18s20.cpp +++ b/Sming/Libraries/DS18S20/ds18s20.cpp @@ -51,7 +51,7 @@ void DS18S20::StartMeasure() InProgress=true; ds->begin(); ds->reset_search(); - DelaysTimer.initializeMs(150, TimerDelegate(&DS18S20::DoSearch, this)).start(false); + DelaysTimer.initializeMs<150>(TimerDelegate(&DS18S20::DoSearch, this)).start(false); } } @@ -146,7 +146,7 @@ void DS18S20::StartReadNext() ds->select(addr); ds->write(STARTCONVO, 1); // start conversion, with parasite power on at the end - DelaysTimer.initializeMs(900, TimerDelegate(&DS18S20::DoMeasure, this)).start(false); + DelaysTimer.initializeMs<900>(TimerDelegate(&DS18S20::DoMeasure, this)).start(false); } else { @@ -210,7 +210,7 @@ void DS18S20::DoMeasure() debugx(" DBG: Temperature = %f Celsius, %f Fahrenheit",celsius[numberOfread],fahrenheit[numberOfread]); numberOfread++; - DelaysTimer.initializeMs(100, TimerDelegate(&DS18S20::StartReadNext, this)).start(false); + DelaysTimer.initializeMs<100>(TimerDelegate(&DS18S20::StartReadNext, this)).start(false); } diff --git a/Sming/Libraries/ModbusMaster/samples/generic/app/application.cpp b/Sming/Libraries/ModbusMaster/samples/generic/app/application.cpp index ddefd7a32b..05f9d14b6d 100644 --- a/Sming/Libraries/ModbusMaster/samples/generic/app/application.cpp +++ b/Sming/Libraries/ModbusMaster/samples/generic/app/application.cpp @@ -109,5 +109,5 @@ void init() mbMaster.logReceive(mbLogReceive); mbMaster.logTransmit(mbLogTransmit); - mbLoopTimer.initializeMs(1000, mbLoop).start(); + mbLoopTimer.initializeMs<1000>(mbLoop).start(); } diff --git a/Sming/Libraries/SwitchJoycon/samples/Bluetooth_Joycon/app/application.cpp b/Sming/Libraries/SwitchJoycon/samples/Bluetooth_Joycon/app/application.cpp index caa9ef8fe4..c9c38a6bf5 100644 --- a/Sming/Libraries/SwitchJoycon/samples/Bluetooth_Joycon/app/application.cpp +++ b/Sming/Libraries/SwitchJoycon/samples/Bluetooth_Joycon/app/application.cpp @@ -31,7 +31,7 @@ void onConnect(NimBLEServer& server) { Serial.println("Connected :) !"); - procTimer.initializeMs(500, loop).start(); + procTimer.initializeMs<500>(loop).start(); } void onDisconnect(NimBLEServer& server) diff --git a/samples/Accelerometer_MMA7455/app/application.cpp b/samples/Accelerometer_MMA7455/app/application.cpp index edff93d77a..257dedc9d2 100644 --- a/samples/Accelerometer_MMA7455/app/application.cpp +++ b/samples/Accelerometer_MMA7455/app/application.cpp @@ -29,5 +29,5 @@ void init() accel.initSensitivity(MMA_7455_2G_MODE); // Start reading loop - procTimer.initializeMs(300, readSensor).start(); + procTimer.initializeMs<300>(readSensor).start(); } diff --git a/samples/Basic_Blink/app/application.cpp b/samples/Basic_Blink/app/application.cpp index 65422d0701..6c20f2c84b 100644 --- a/samples/Basic_Blink/app/application.cpp +++ b/samples/Basic_Blink/app/application.cpp @@ -18,5 +18,5 @@ void blink() void init() { pinMode(LED_PIN, OUTPUT); - procTimer.initializeMs(1000, blink).start(); + procTimer.initializeMs<1000>(blink).start(); } diff --git a/samples/Basic_Capsense/app/application.cpp b/samples/Basic_Capsense/app/application.cpp index 8fcab1a935..242103f358 100644 --- a/samples/Basic_Capsense/app/application.cpp +++ b/samples/Basic_Capsense/app/application.cpp @@ -19,5 +19,5 @@ void capsense() void init() { - procTimer.initializeMs(100, capsense).start(); + procTimer.initializeMs<100>(capsense).start(); } diff --git a/samples/Basic_HwPWM/app/application.cpp b/samples/Basic_HwPWM/app/application.cpp index e04e7d050a..1d828406b7 100644 --- a/samples/Basic_HwPWM/app/application.cpp +++ b/samples/Basic_HwPWM/app/application.cpp @@ -69,5 +69,5 @@ void init() debugf("PWM output set on all 8 Pins. Kindly check..."); debugf("Now Pin 2 will go from 0 to VCC to 0 in cycles."); - procTimer.initializeMs(100, doPWM).start(); + procTimer.initializeMs<100>(doPWM).start(); } diff --git a/samples/Basic_NFC/app/application.cpp b/samples/Basic_NFC/app/application.cpp index 9d532ec4bd..29e11a58c9 100644 --- a/samples/Basic_NFC/app/application.cpp +++ b/samples/Basic_NFC/app/application.cpp @@ -58,12 +58,12 @@ void init() { Serial.begin(SERIAL_BAUD_RATE); // 115200 by default - procTimer.initializeMs(2000, sayHello).start(); + procTimer.initializeMs<2000>(sayHello).start(); //----- NFC MFRC522 mfrc522(SS_PIN, SS_PIN); SPI.begin(); mfrc522.PCD_Init(); // Init MFRC522 - //nfcScanTimer.initializeMs(50, scanNfc).startOnce(); + //nfcScanTimer.initializeMs<50>(scanNfc).startOnce(); } diff --git a/samples/Basic_Neopixel/app/application.cpp b/samples/Basic_Neopixel/app/application.cpp index 82a6a11f3f..5dac6ce468 100644 --- a/samples/Basic_Neopixel/app/application.cpp +++ b/samples/Basic_Neopixel/app/application.cpp @@ -173,5 +173,5 @@ void init() strip.begin(); //init port - StripDemoTimer.initializeMs(1000, StartDemo).start(); //start demo + StripDemoTimer.initializeMs<1000>(StartDemo).start(); //start demo } diff --git a/samples/Basic_ScannerI2C/app/application.cpp b/samples/Basic_ScannerI2C/app/application.cpp index 81ecd50d4f..b3082f5fbb 100644 --- a/samples/Basic_ScannerI2C/app/application.cpp +++ b/samples/Basic_ScannerI2C/app/application.cpp @@ -75,5 +75,5 @@ void init() //Wire.pins(14, 12); // SDA, SCL Wire.begin(); - procTimer.initializeMs(3000, scanBus).start(); + procTimer.initializeMs<3000>(scanBus).start(); } diff --git a/samples/Basic_Serial/app/application.cpp b/samples/Basic_Serial/app/application.cpp index 0faceff2a4..b5ff51674e 100644 --- a/samples/Basic_Serial/app/application.cpp +++ b/samples/Basic_Serial/app/application.cpp @@ -312,7 +312,7 @@ void init() debugf("(DEBUG) message printed on UART1"); // You should see the debug message in UART1 only. - procTimer.initializeMs(2000, sayHello).start(); + procTimer.initializeMs<2000>(sayHello).start(); testPrintf(); diff --git a/samples/Basic_WebSkeletonApp/app/application.cpp b/samples/Basic_WebSkeletonApp/app/application.cpp index ace2e6c8e6..90469993f0 100644 --- a/samples/Basic_WebSkeletonApp/app/application.cpp +++ b/samples/Basic_WebSkeletonApp/app/application.cpp @@ -57,5 +57,5 @@ void init() System.onReady(startWebServer); - counterTimer.initializeMs(1000, counterLoop).start(); + counterTimer.initializeMs<1000>(counterLoop).start(); } diff --git a/samples/Basic_WebSkeletonApp_LTS/app/application.cpp b/samples/Basic_WebSkeletonApp_LTS/app/application.cpp index 96b54664e4..6224c65df9 100644 --- a/samples/Basic_WebSkeletonApp_LTS/app/application.cpp +++ b/samples/Basic_WebSkeletonApp_LTS/app/application.cpp @@ -25,7 +25,7 @@ void init() System.onReady(startWebServer); - counterTimer.initializeMs(1000, counterLoop).start(); + counterTimer.initializeMs<1000>(counterLoop).start(); } void counterLoop() diff --git a/samples/CanBus/app/application.cpp b/samples/CanBus/app/application.cpp index b616aae340..0234267eae 100644 --- a/samples/CanBus/app/application.cpp +++ b/samples/CanBus/app/application.cpp @@ -55,5 +55,5 @@ void init() canBus0.sendMsgBuf(0x1000000, 1, 8, txBuf0); canBus1.sendMsgBuf(0x1000001, 1, 8, txBuf1); - loopTimer.initializeMs(1000, loop).start(); + loopTimer.initializeMs<1000>(loop).start(); } diff --git a/samples/Compass_HMC5883L/app/application.cpp b/samples/Compass_HMC5883L/app/application.cpp index 471dfebccf..b0a8ea9064 100644 --- a/samples/Compass_HMC5883L/app/application.cpp +++ b/samples/Compass_HMC5883L/app/application.cpp @@ -28,7 +28,7 @@ void init() } // Start reading loop - procTimer.initializeMs(100, readCompass).start(); + procTimer.initializeMs<100>(readCompass).start(); } void readCompass() diff --git a/samples/DFPlayerMini/app/application.cpp b/samples/DFPlayerMini/app/application.cpp index 4dcdea9666..9d2c9df707 100644 --- a/samples/DFPlayerMini/app/application.cpp +++ b/samples/DFPlayerMini/app/application.cpp @@ -24,7 +24,7 @@ void init() Serial.begin(9600); pinMode(GPIO_LED, OUTPUT); - timerLedBlink.initializeMs(100, blink).start(); + timerLedBlink.initializeMs<100>(blink).start(); while(!player.begin(Serial)) { delay(500); @@ -35,5 +35,5 @@ void init() player.volume(15); - timerDFPlayer.initializeMs(10000, nextSong).start(); + timerDFPlayer.initializeMs<10000>(nextSong).start(); } diff --git a/samples/HttpClient_ThingSpeak/app/application.cpp b/samples/HttpClient_ThingSpeak/app/application.cpp index ee6424f537..5169fd4aa2 100644 --- a/samples/HttpClient_ThingSpeak/app/application.cpp +++ b/samples/HttpClient_ThingSpeak/app/application.cpp @@ -62,7 +62,7 @@ void connectFail(const String& ssid, MacAddress bssid, WifiDisconnectReason reas void gotIP(IpAddress ip, IpAddress netmask, IpAddress gateway) { // Start send data loop - procTimer.initializeMs(25 * 1000, sendData).start(); // every 25 seconds + procTimer.initializeMs<25 * 1000>(sendData).start(); // every 25 seconds } void init() diff --git a/samples/HttpServer_ConfigNetwork/app/application.cpp b/samples/HttpServer_ConfigNetwork/app/application.cpp index b9bf939fce..c3649efd57 100644 --- a/samples/HttpServer_ConfigNetwork/app/application.cpp +++ b/samples/HttpServer_ConfigNetwork/app/application.cpp @@ -160,7 +160,7 @@ void onAjaxConnect(HttpRequest& request, HttpResponse& response) password = curPass; debugf("CONNECT TO: %s %s", network.c_str(), password.c_str()); json["connected"] = false; - connectionTimer.initializeMs(1200, makeConnection).startOnce(); + connectionTimer.initializeMs<1200>(makeConnection).startOnce(); } else { json["connected"] = WifiStation.isConnected(); debugf("Network already selected. Current status: %s", WifiStation.getConnectionStatusName().c_str()); diff --git a/samples/Humidity_AM2321/app/application.cpp b/samples/Humidity_AM2321/app/application.cpp index 0890214c5e..ca07d7267d 100644 --- a/samples/Humidity_AM2321/app/application.cpp +++ b/samples/Humidity_AM2321/app/application.cpp @@ -32,5 +32,5 @@ void init() am2321.begin(); // REQUIRED. Call it after choosing I2C pins. Serial.println(am2321.uid()); - procTimer.initializeMs(3000, read).start(); + procTimer.initializeMs<3000>(read).start(); } diff --git a/samples/Humidity_DHT22/app/application.cpp b/samples/Humidity_DHT22/app/application.cpp index 4f5f8e09ba..c46f85215d 100644 --- a/samples/Humidity_DHT22/app/application.cpp +++ b/samples/Humidity_DHT22/app/application.cpp @@ -15,7 +15,7 @@ void init() Serial.systemDebugOutput(true); // Allow debug output to serial dht.setup(WORK_PIN, DHTesp::DHT22); - readTemperatureProcTimer.initializeMs(5 * 1000, onTimer_readTemperatures).start(); // every so often. + readTemperatureProcTimer.initializeMs<5 * 1000>(onTimer_readTemperatures).start(); // every so often. Serial.println(_F("\r\n" "DHT improved lib")); diff --git a/samples/Humidity_SI7021/app/application.cpp b/samples/Humidity_SI7021/app/application.cpp index 8861e78543..462a993368 100644 --- a/samples/Humidity_SI7021/app/application.cpp +++ b/samples/Humidity_SI7021/app/application.cpp @@ -58,6 +58,6 @@ void init() Serial.print(_F("Start I2c")); Wire.pins(I2C_SDA, I2C_SCL); // SDA, SCL Wire.begin(); - procTimer_ht.initializeMs(10000, si_read_ht).start(); - procTimer_olt.initializeMs(15000, si_read_olt).start(); + procTimer_ht.initializeMs<10000>(si_read_ht).start(); + procTimer_olt.initializeMs<15000>(si_read_olt).start(); } diff --git a/samples/IR_lib/app/application.cpp b/samples/IR_lib/app/application.cpp index fa97eb7c7d..3049acc6c1 100644 --- a/samples/IR_lib/app/application.cpp +++ b/samples/IR_lib/app/application.cpp @@ -36,6 +36,6 @@ void init() Serial.begin(SERIAL_BAUD_RATE); // 115200 by default Serial.println("Setting up..."); irrecv.enableIRIn(); // Start the receiver - irTimer.initializeMs(200, receiveIR).start(); + irTimer.initializeMs<200>(receiveIR).start(); Serial.println("Ready..."); } diff --git a/samples/LED_YeelightBulb/app/application.cpp b/samples/LED_YeelightBulb/app/application.cpp index 734e681cfa..3c756b5e46 100644 --- a/samples/LED_YeelightBulb/app/application.cpp +++ b/samples/LED_YeelightBulb/app/application.cpp @@ -31,7 +31,7 @@ void gotIP(IpAddress ip, IpAddress netmask, IpAddress gateway) // Connection to Yeelight Bulb will be established on any first action: bulb.updateState(); // Read actual bulb state - procTimer.initializeMs(5000, blink).start(); + procTimer.initializeMs<5000>(blink).start(); } void init() diff --git a/samples/Light_BH1750/app/application.cpp b/samples/Light_BH1750/app/application.cpp index d55733ceb3..960a4270ab 100644 --- a/samples/Light_BH1750/app/application.cpp +++ b/samples/Light_BH1750/app/application.cpp @@ -36,5 +36,5 @@ void init() LightSensor.setMode(BH1750_Continuous_H_resolution_Mode); // Start reading loop - procTimer.initializeMs(300, readLight).start(); + procTimer.initializeMs<300>(readLight).start(); } diff --git a/samples/MeteoControl/app/application.cpp b/samples/MeteoControl/app/application.cpp index b320285b4d..7c335dbf3c 100644 --- a/samples/MeteoControl/app/application.cpp +++ b/samples/MeteoControl/app/application.cpp @@ -61,7 +61,7 @@ void init() WifiEvents.onStationDisconnect(connectFail); WifiEvents.onStationGotIP(gotIP); - procTimer.initializeMs(5000, process).start(); + procTimer.initializeMs<5000>(process).start(); process(); } @@ -103,7 +103,7 @@ void process() StrRH = String(h, 0); if(!displayTimer.isStarted()) - displayTimer.initializeMs(1000, showValues).start(); + displayTimer.initializeMs<1000>(showValues).start(); } void connectOk(const String& SSID, MacAddress bssid, uint8_t channel) @@ -201,5 +201,5 @@ void startWebClock() { lastClockUpdate = 0; clockRefresher.stop(); - clockRefresher.initializeMs(500, refreshClockTime).start(); + clockRefresher.initializeMs<500>(refreshClockTime).start(); } diff --git a/samples/MeteoControl_mqtt/app/bmp180.cpp b/samples/MeteoControl_mqtt/app/bmp180.cpp index de51ac26c7..d239500b88 100644 --- a/samples/MeteoControl_mqtt/app/bmp180.cpp +++ b/samples/MeteoControl_mqtt/app/bmp180.cpp @@ -52,7 +52,7 @@ void BMPinit() barometer.Initialize(); barometer.PrintCalibrationData(); - publishBMPTimer.initializeMs(TIMER * 3000, publishBMP).start(); // start publish BMP180 sensor data + publishBMPTimer.initializeMs(publishBMP).start(); // start publish BMP180 sensor data } #endif /* INCLUDE_BMP180_H_ */ diff --git a/samples/MeteoControl_mqtt/app/si7021.cpp b/samples/MeteoControl_mqtt/app/si7021.cpp index 8fb2c61fe6..9aaea31dab 100644 --- a/samples/MeteoControl_mqtt/app/si7021.cpp +++ b/samples/MeteoControl_mqtt/app/si7021.cpp @@ -42,7 +42,7 @@ void publishSI() void SIinit() { - publishSITimer.initializeMs(TIMER * 1000, publishSI).start(); // start publish SI sensor data + publishSITimer.initializeMs(publishSI).start(); // start publish SI sensor data } #endif /* INCLUDE_SI7021_H_ */ diff --git a/samples/MeteoControl_mqtt/include/configuration.h b/samples/MeteoControl_mqtt/include/configuration.h index ba02ccdb83..2d3e15e25d 100644 --- a/samples/MeteoControl_mqtt/include/configuration.h +++ b/samples/MeteoControl_mqtt/include/configuration.h @@ -31,7 +31,7 @@ #define SI_H "testing/status/SI7021/Humidity" #define VER_TOPIC "testing/firmware/version" -int TIMER = 20; // every N* seconds send to mqtt server +constexpr int TIMER = 20; // every N* seconds send to mqtt server // Forward declarations void startMqttClient(); diff --git a/samples/MqttClient_Hello/app/application.cpp b/samples/MqttClient_Hello/app/application.cpp index d2f8772610..01bbef0655 100644 --- a/samples/MqttClient_Hello/app/application.cpp +++ b/samples/MqttClient_Hello/app/application.cpp @@ -37,7 +37,7 @@ void checkMQTTDisconnect(TcpClient& client, bool flag) } // Restart connection attempt after few seconds - procTimer.initializeMs(2 * 1000, startMqttClient).start(); // every 2 seconds + procTimer.initializeMs<2 * 1000>(startMqttClient).start(); // every 2 seconds } int onMessageDelivered(MqttClient& client, mqtt_message_t* message) @@ -88,7 +88,7 @@ void startMqttClient() // Start publishing message now publishMessage(); // and schedule a timer to send messages every 5 seconds - procTimer.initializeMs(5 * 1000, publishMessage).start(); + procTimer.initializeMs<5 * 1000>(publishMessage).start(); return 0; }); diff --git a/samples/Network_Ping/app/application.cpp b/samples/Network_Ping/app/application.cpp index 12c0aa5a42..b1cabe877d 100644 --- a/samples/Network_Ping/app/application.cpp +++ b/samples/Network_Ping/app/application.cpp @@ -45,7 +45,7 @@ void onSent(void* arg, void* pdata) } debug_d("Scheduling another ping in %d seconds", PING_INTERVAL_SECONDS); - procTimer.initializeMs(PING_INTERVAL_SECONDS * 1000, pingTask).startOnce(); + procTimer.initializeMs(pingTask).startOnce(); } void onReceived(void* arg, void* pdata) @@ -79,7 +79,7 @@ void ping(uint32_t ip) void connectOk(IpAddress ip, IpAddress mask, IpAddress gateway) { debug_d("Scheduling initial ping in 1 second."); - procTimer.initializeMs(1000, pingTask).startOnce(); + procTimer.initializeMs<1000>(pingTask).startOnce(); } } // namespace diff --git a/samples/Nextion_Button/app/application.cpp b/samples/Nextion_Button/app/application.cpp index 6bddd18ef9..24023fa23c 100644 --- a/samples/Nextion_Button/app/application.cpp +++ b/samples/Nextion_Button/app/application.cpp @@ -54,5 +54,5 @@ void init() pinMode(GPIO_LED, OUTPUT); nexInit(); b0.attachPop(b0PopCallback, &b0); - timerNextion.initializeMs(100, loopNextion).start(); + timerNextion.initializeMs<100>(loopNextion).start(); } diff --git a/samples/PortExpander_MCP23S17/app/application.cpp b/samples/PortExpander_MCP23S17/app/application.cpp index 4095b02ea8..7e64f82501 100644 --- a/samples/PortExpander_MCP23S17/app/application.cpp +++ b/samples/PortExpander_MCP23S17/app/application.cpp @@ -35,7 +35,7 @@ void init() inputchip.inputInvert(0x0000); // Use word-write mode to invert the inputs so that logic 0 is read as HIGH outputchip.pinMode(0x0000); // Use word-write mode to Set all of the pins on outputchip to be outputs - procTimer.initializeMs(200, loop).start(); + procTimer.initializeMs<200>(loop).start(); } void loop() diff --git a/samples/Radio_RCSwitch/app/application.cpp b/samples/Radio_RCSwitch/app/application.cpp index 7ce74fc87c..db4d167d6d 100644 --- a/samples/Radio_RCSwitch/app/application.cpp +++ b/samples/Radio_RCSwitch/app/application.cpp @@ -41,6 +41,6 @@ void init() // Optional set protocol (default is 1, will work for most outlets) // mySwitch.setProtocol(2); - sendTimer.initializeMs(1000, sendRF).start(); - receiveTimer.initializeMs(20, receiveRF).start(); + sendTimer.initializeMs<1000>(sendRF).start(); + receiveTimer.initializeMs<20>(receiveRF).start(); } diff --git a/samples/Radio_nRF24L01/app/application.cpp b/samples/Radio_nRF24L01/app/application.cpp index 167154e8eb..1030ded391 100644 --- a/samples/Radio_nRF24L01/app/application.cpp +++ b/samples/Radio_nRF24L01/app/application.cpp @@ -107,6 +107,6 @@ void init() radio.printDetails(); Serial.println(_F("Initialization completed.")); - procTimer.initializeMs(10, loopListen).start(); + procTimer.initializeMs<10>(loopListen).start(); Serial.println(_F("Listening...")); } diff --git a/samples/Radio_si4432/app/application.cpp b/samples/Radio_si4432/app/application.cpp index 7a947f63eb..5e6ba09407 100644 --- a/samples/Radio_si4432/app/application.cpp +++ b/samples/Radio_si4432/app/application.cpp @@ -106,5 +106,5 @@ void init() lastPingTime = millis(); //start listen loop - procTimer.initializeMs(10, loopListen).start(); + procTimer.initializeMs<10>(loopListen).start(); } diff --git a/samples/ScreenOLED_SSD1306/app/application.cpp b/samples/ScreenOLED_SSD1306/app/application.cpp index d0b8d3b5f6..301f9b7972 100644 --- a/samples/ScreenOLED_SSD1306/app/application.cpp +++ b/samples/ScreenOLED_SSD1306/app/application.cpp @@ -57,7 +57,7 @@ void Demo1() display.fillCircle(display.width() / 2, display.height() / 2, 10, WHITE); display.display(); DemoTimer.stop(); - DemoTimer.initializeMs(2000, Demo2).start(); + DemoTimer.initializeMs<2000>(Demo2).start(); } void init() @@ -70,5 +70,5 @@ void init() // bool:reset set to TRUE or FALSE depending on your display display.begin(SSD1306_SWITCHCAPVCC, 0x3c, false); display.display(); - DemoTimer.initializeMs(2000, Demo1).start(); + DemoTimer.initializeMs<2000>(Demo1).start(); } diff --git a/samples/ScreenTFT_ST7735/app/application.cpp b/samples/ScreenTFT_ST7735/app/application.cpp index b4a2ed3701..bf7eb764b6 100644 --- a/samples/ScreenTFT_ST7735/app/application.cpp +++ b/samples/ScreenTFT_ST7735/app/application.cpp @@ -239,7 +239,7 @@ void screen12() startTime = millis(); bmpDraw(tft, "sming.bmp", 0, 0); debugf("screen12: bmpDraw %d ms", millis() - startTime); - // DemoScreenTimer.initializeMs(2000, screen13).start(false); + // DemoScreenTimer.initializeMs<2000>(screen13).start(false); } void screen11() @@ -247,7 +247,7 @@ void screen11() startTime = millis(); mediabuttons(); debugf("screen11: mediabuttons %d ms", millis() - startTime); - DemoScreenTimer.initializeMs(1000, screen12).start(false); + DemoScreenTimer.initializeMs<1000>(screen12).start(false); } void screen10() @@ -255,7 +255,7 @@ void screen10() startTime = millis(); testtriangles(); debugf("screen10: testtriangles %d ms", millis() - startTime); - DemoScreenTimer.initializeMs(1000, screen11).start(false); + DemoScreenTimer.initializeMs<1000>(screen11).start(false); } void screen9() @@ -263,7 +263,7 @@ void screen9() startTime = millis(); testroundrects(); debugf("screen9: testroundrects %d ms", millis() - startTime); - DemoScreenTimer.initializeMs(1000, screen10).start(false); + DemoScreenTimer.initializeMs<1000>(screen10).start(false); } void screen8() @@ -273,7 +273,7 @@ void screen8() testfillcircles(10, ST7735_BLUE); testdrawcircles(10, ST7735_WHITE); debugf("screen8: testfillcircles %d ms", millis() - startTime); - DemoScreenTimer.initializeMs(1000, screen9).start(false); + DemoScreenTimer.initializeMs<1000>(screen9).start(false); } void screen7() @@ -281,7 +281,7 @@ void screen7() startTime = millis(); testfillrects(ST7735_YELLOW, ST7735_MAGENTA); debugf("screen7: testfillrects %d ms", millis() - startTime); - DemoScreenTimer.initializeMs(1000, screen8).start(false); + DemoScreenTimer.initializeMs<1000>(screen8).start(false); } void screen6() @@ -289,7 +289,7 @@ void screen6() startTime = millis(); testdrawrects(ST7735_GREEN); debugf("screen6: testdrawrects %d ms", millis() - startTime); - DemoScreenTimer.initializeMs(1000, screen7).start(false); + DemoScreenTimer.initializeMs<1000>(screen7).start(false); } void screen5() @@ -298,7 +298,7 @@ void screen5() // optimized lines testfastlines(ST7735_RED, ST7735_BLUE); debugf("screen5: testfastlines %d ms", millis() - startTime); - DemoScreenTimer.initializeMs(1000, screen6).start(false); + DemoScreenTimer.initializeMs<1000>(screen6).start(false); } void screen4() @@ -307,7 +307,7 @@ void screen4() // line draw test testlines(ST7735_YELLOW); debugf("screen4: testlines %d ms", millis() - startTime); - DemoScreenTimer.initializeMs(1000, screen5).start(false); + DemoScreenTimer.initializeMs<1000>(screen5).start(false); } void screen3() @@ -315,7 +315,7 @@ void screen3() startTime = millis(); tftPrintTest2(); debugf("screen3: tftPrintTest2 %d ms", millis() - startTime); - DemoScreenTimer.initializeMs(1000, screen4).start(false); + DemoScreenTimer.initializeMs<1000>(screen4).start(false); } void screen2() @@ -323,7 +323,7 @@ void screen2() startTime = millis(); tftPrintTest1(); debugf("screen2: tftPrintTest1 %d ms", millis() - startTime); - DemoScreenTimer.initializeMs(1000, screen3).start(false); + DemoScreenTimer.initializeMs<1000>(screen3).start(false); } void screen1() @@ -337,7 +337,7 @@ void screen1() "hendrerit. Ut sed nisi lorem. In vestibulum purus a tortor imperdiet posuere. ", ST7735_WHITE); debugf("screen1: testdrawtext %d ms", millis() - startTime); - DemoScreenTimer.initializeMs(1000, screen2).start(false); + DemoScreenTimer.initializeMs<1000>(screen2).start(false); } void init() @@ -367,5 +367,5 @@ void init() debugf("Initialized in %d ms\n", startTime); - DemoScreenTimer.initializeMs(500, screen1).start(false); + DemoScreenTimer.initializeMs<500>(screen1).start(false); } diff --git a/samples/TcpClient_NarodMon/app/application.cpp b/samples/TcpClient_NarodMon/app/application.cpp index 063bdd03a6..bf2a5fa4de 100644 --- a/samples/TcpClient_NarodMon/app/application.cpp +++ b/samples/TcpClient_NarodMon/app/application.cpp @@ -123,7 +123,7 @@ void gotIP(IpAddress ip, IpAddress netmask, IpAddress gateway) { // Call the sendData function by timer, every 6 minutes // вызываем по таймеру функцию sendData - procTimer.initializeMs(SENDDATA_INTERVAL * 1000, sendData).start(); // каждые 6 минут + procTimer.initializeMs(sendData).start(); // каждые 6 минут // Send immediately on startup // ну и заодно сразу после запуска вызываем, чтобы не ждать 6 минут первый раз diff --git a/samples/Ultrasonic_HCSR04/app/application.cpp b/samples/Ultrasonic_HCSR04/app/application.cpp index 756e9e3c76..c807d33632 100644 --- a/samples/Ultrasonic_HCSR04/app/application.cpp +++ b/samples/Ultrasonic_HCSR04/app/application.cpp @@ -30,5 +30,5 @@ void init() { Serial.begin(SERIAL_BAUD_RATE); ultrasonic.begin(TRIG_PIN, ECHO_PIN); - procTimer.initializeMs(500, measure).start(); + procTimer.initializeMs<500>(measure).start(); } diff --git a/samples/Websocket_Client/app/application.cpp b/samples/Websocket_Client/app/application.cpp index d2f075b278..0a4442966c 100644 --- a/samples/Websocket_Client/app/application.cpp +++ b/samples/Websocket_Client/app/application.cpp @@ -44,7 +44,7 @@ void sendNewMessage(); void wsConnected(WebsocketConnection& wsConnection) { Serial << _F("Start sending messages every ") << MESSAGE_INTERVAL << _F(" second(s)...") << endl; - msgTimer.initializeMs(MESSAGE_INTERVAL * 1000, sendNewMessage); + msgTimer.initializeMs(sendNewMessage); msgTimer.startOnce(); } @@ -72,7 +72,7 @@ void restart() void wsDisconnected(WebsocketConnection& wsConnection) { Serial << _F("Restarting websocket client after ") << RESTART_PERIOD << _F(" seconds") << endl; - msgTimer.initializeMs(RESTART_PERIOD * 1000, restart); + msgTimer.initializeMs(restart); msgTimer.startOnce(); } From ebdaee5567a2559f3aa581abb6c5ad322fa001da Mon Sep 17 00:00:00 2001 From: mikee47 Date: Wed, 27 Mar 2024 16:08:41 +0000 Subject: [PATCH 04/13] Fix unused variable warning in `CommandProcessing` library --- .../CommandProcessing/samples/TelnetServer/app/application.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sming/Libraries/CommandProcessing/samples/TelnetServer/app/application.cpp b/Sming/Libraries/CommandProcessing/samples/TelnetServer/app/application.cpp index 8dc8e9be27..6d2862b5e4 100644 --- a/Sming/Libraries/CommandProcessing/samples/TelnetServer/app/application.cpp +++ b/Sming/Libraries/CommandProcessing/samples/TelnetServer/app/application.cpp @@ -34,7 +34,7 @@ bool processTelnetInput(TcpClient& client, char* data, int size) char c = *data++; if(skip) { --skip; - } else if(c == '\xff') { + } else if(c == TC_ESC) { skip = 2; } else { commandHandler.process(c); From 0206adbf300fba983c87e7c9e165af12e6fc084a Mon Sep 17 00:00:00 2001 From: mikee47 Date: Wed, 27 Mar 2024 16:11:45 +0000 Subject: [PATCH 05/13] Update jerryscript library --- Sming/Libraries/jerryscript | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sming/Libraries/jerryscript b/Sming/Libraries/jerryscript index ac5f4ed3cf..c8f24895a5 160000 --- a/Sming/Libraries/jerryscript +++ b/Sming/Libraries/jerryscript @@ -1 +1 @@ -Subproject commit ac5f4ed3cf68b4f5877db890fa02929a128498c3 +Subproject commit c8f24895a59ff64e9bba212946dcf2dad504b27a From ec6f68a95ff88bcc0e810deb19e9bc312c3cddfd Mon Sep 17 00:00:00 2001 From: mikee47 Date: Tue, 2 Apr 2024 11:33:15 +0100 Subject: [PATCH 06/13] Update Basic_DateTime sample Simplify code using LineBuffer Accept timestamp or HTTP date string --- samples/Basic_DateTime/app/application.cpp | 180 +++++++++++---------- samples/Basic_DateTime/component.mk | 2 - 2 files changed, 95 insertions(+), 87 deletions(-) diff --git a/samples/Basic_DateTime/app/application.cpp b/samples/Basic_DateTime/app/application.cpp index c941d5354e..2de8e3968d 100644 --- a/samples/Basic_DateTime/app/application.cpp +++ b/samples/Basic_DateTime/app/application.cpp @@ -5,107 +5,117 @@ Prints each type of DateTime::format option */ #include +#include -static time_t timestamp = 0; -static size_t tsLength = 0; - +namespace +{ DEFINE_FSTR_LOCAL(commandPrompt, "Enter Unix timestamp: "); -void showTime(time_t timestamp) +void showTime(DateTime dt) { - DateTime dt(timestamp); - //Non-time - Serial.println(dt.format("%%%% Percent sign: %%")); - Serial.println(dt.format("%%n New-line character: %n")); - Serial.println(dt.format("%%t Horizontal-tab character: >|%t|<")); - //Year - Serial.println(dt.format("%%Y Full year (YYYY): %Y")); - Serial.println(dt.format("%%C Year, first two digits (00-99)%: %C")); - Serial.println(dt.format("%%y Year, last two digits (00-99): %y")); - //Month - Serial.println(dt.format("%%B Full month name (e.g. June): %B")); - Serial.println(dt.format("%%b Abbreviated month name (e.g. Jun): %b")); - Serial.println(dt.format("%%h Abbreviated month name (e.g. Jun): %h")); - Serial.println(dt.format("%%m Month as a decimal number (01-12): %m")); - //Week - Serial.println(dt.format("%%U Week number with the first Sunday as the first day of week one (00-53): %U")); //NYI - Serial.println(dt.format("%%W Week number with the first Monday as the first day of week one (00-53): %W")); //NYI - Serial.println(dt.format("%%V ISO 8601 week number (01-53): %V")); //NYI - //Day - Serial.println(dt.format("%%j Day of the year (001-366): %j")); - Serial.println(dt.format("%%d Day of the month, zero-padded (01-31)%: %d")); - Serial.println(dt.format("%%e Day of the month, space-padded ( 1-31): %e")); - Serial.println(dt.format("%%A Full weekday name (e.g. Monday): %A")); - Serial.println(dt.format("%%a Abbreviated weekday name (e.g. Mon): %a")); - Serial.println(dt.format("%%w Weekday as a decimal number with Sunday as 0 (0-6): %w")); - Serial.println(dt.format("%%u ISO 8601 weekday as number with Monday as 1 (1-7): %u")); - //Hour - Serial.println(dt.format("%%p Meridiem (AM|PM): %p")); - Serial.println(dt.format("%%H Hour in 24h format (00-23): %H")); - Serial.println(dt.format("%%h Hour in 12h format (01-12): %I")); - //Minute - Serial.println(dt.format("%%M Minute (00-59): %M")); - //Second - Serial.println(dt.format("%%S Second (00-61): %S")); - //Formatted strings - Serial.println(dt.format("%%R 24-hour time (HH:MM): %R")); - Serial.println(dt.format("%%r 12-hour time (hh:MM:SS AM): %r")); - Serial.println(dt.format("%%c Locale date and time: %c")); - Serial.println(dt.format("%%D US short date (MM/DD/YY): %D")); - Serial.println(dt.format("%%F ISO 8601 date (YYYY-MM-DD): %F")); - Serial.println(dt.format("%%T ISO 8601 time (HH:MM:SS): %T")); - Serial.println(dt.format("%%x Locale date: %x")); - Serial.println(dt.format("%%X Locale time: %X")); - //HTTP date - Serial << "toHTTPDate: " << dt.toHTTPDate() << endl; + auto printFormat = [&dt](const String& fmt, const String& msg) -> void { + Serial << fmt << ' ' << msg << ": " << dt.format(fmt) << endl; + }; + + // Non-time + printFormat("%%", F("Percent sign")); + printFormat("%n", F("New-line character")); + printFormat("|<", F("Horizontal-tab character: >|")); + // Year + printFormat("%Y", F("Full year (YYYY)")); + printFormat("%C", F("Year, first two digits (00-99)")); + printFormat("%y", F("Year, last two digits (00-99)")); + // Month + printFormat("%B", F("Full month name (e.g. June)")); + printFormat("%b", F("Abbreviated month name (e.g. Jun)")); + printFormat("%h", F("Abbreviated month name (e.g. Jun)")); + printFormat("%m", F("Month as a decimal number (01-12)")); + // Week + printFormat("%U", F("Week number with the first Sunday as the first day of week one (00-53)")); + printFormat("%W", F("Week number with the first Monday as the first day of week one (00-53)")); + printFormat("%V", F("ISO 8601 week number (01-53)")); + // Day + printFormat("%j", F("Day of the year (001-366)")); + printFormat("%d", F("Day of the month, zero-padded (01-31)")); + printFormat("%e", F("Day of the month, space-padded ( 1-31)")); + printFormat("%A", F("Full weekday name (e.g. Monday)")); + printFormat("%a", F("Abbreviated weekday name (e.g. Mon)")); + printFormat("%w", F("Weekday as a decimal number with Sunday as 0 (0-6)")); + printFormat("%u", F("ISO 8601 weekday as number with Monday as 1 (1-7)")); + // Hour + printFormat("%p", F("Meridiem (AM|PM)")); + printFormat("%H", F("Hour in 24h format (00-23)")); + printFormat("%I", F("Hour in 12h format (01-12)")); + // Minute + printFormat("%M", F("Minute (00-59)")); + // Second + printFormat("%S", F("Second (00-61)")); + // Formatted strings + printFormat("%R", F("24-hour time (HH:MM)")); + printFormat("%r", F("12-hour time (hh:MM:SS AM)")); + printFormat("%c", F("Locale date and time")); + printFormat("%D", F("US short date (MM/DD/YY)")); + printFormat("%F", F("ISO 8601 date (YYYY-MM-DD)")); + printFormat("%T", F("ISO 8601 time (HH:MM:SS)")); + printFormat("%x", F("Locale date")); + printFormat("%X", F("Locale time")); + + auto print = [](const String& tag, const String& value) { Serial << tag << ": " << value << endl; }; + + // HTTP date + print(F("toHTTPDate"), dt.toHTTPDate()); DateTime dt2; dt2.fromHttpDate(dt.toHTTPDate()); - Serial << "fromHTTPDate: " << dt2.toHTTPDate() << endl; - Serial << "toFullDateTimeString: " << dt.toFullDateTimeString() << endl; - Serial << "toISO8601: " << dt.toISO8601() << endl; - Serial << "toShortDateString: " << dt.toShortDateString() << endl; - Serial << "toShortTimeString: " << dt.toShortTimeString() << endl; + print(F("fromHTTPDate"), dt2.toHTTPDate()); + print(F("toFullDateTimeString"), dt.toFullDateTimeString()); + print(F("toISO8601"), dt.toISO8601()); + print(F("toShortDateString"), dt.toShortDateString()); + print(F("toShortTimeString"), dt.toShortTimeString()); } void onRx(Stream& source, char arrivedChar, unsigned short availableCharsCount) { - switch(arrivedChar) { - case '\n': - Serial.println(); - Serial.println(); - Serial << _F("****Showing DateTime formatting options for Unix timestamp: ") << timestamp << endl; - showTime(timestamp); - Serial.print(commandPrompt); - timestamp = 0; - tsLength = 0; - break; - case '0' ... '9': - timestamp *= 10; - timestamp += arrivedChar - '0'; - ++tsLength; - Serial.print(arrivedChar); - break; - case '\b': - if(tsLength) { - Serial.print('\b'); - Serial.print(" "); - Serial.print('\b'); - --tsLength; - timestamp /= 10; + static LineBuffer<32> buffer; + + switch(buffer.process(source, Serial)) { + case buffer.Action::submit: { + if(!buffer) { + break; + } + String s(buffer); + buffer.clear(); + char* p; + time_t timestamp = strtoul(s.c_str(), &p, 0); + if(p == s.end()) { + Serial << endl + << _F("****Showing DateTime formatting options for Unix timestamp: ") << uint32_t(timestamp) << endl; + showTime(timestamp); + break; } + + DateTime dt; + if(dt.fromHttpDate(s)) { + Serial << endl << _F("****Showing DateTime formatting options for Http time: ") << s << endl; + showTime(dt); + break; + } + + Serial << endl << _F("Please enter a valid numeric timestamp, or HTTP time string!") << endl; break; - case 27: - timestamp = 0; - tsLength = 0; - Serial.print('\r'); - Serial.print(_F(" ")); - Serial.print('\r'); - Serial.print(commandPrompt); + } + + case buffer.Action::clear: break; + + default:; + return; } - m_puts("\r\n"); + + Serial.print(commandPrompt); } +} // namespace + void init() { Serial.begin(COM_SPEED_SERIAL); diff --git a/samples/Basic_DateTime/component.mk b/samples/Basic_DateTime/component.mk index 250bb9a77d..5c4d4e7499 100644 --- a/samples/Basic_DateTime/component.mk +++ b/samples/Basic_DateTime/component.mk @@ -1,3 +1 @@ -# Emulate UART 0 -ENABLE_HOST_UARTID := 0 DISABLE_NETWORK := 1 From f54282278b98577252b1c7b8f6d4f629c384f1a7 Mon Sep 17 00:00:00 2001 From: mikee47 Date: Tue, 2 Apr 2024 13:04:47 +0100 Subject: [PATCH 07/13] Rewrite Basic_Neopixel sample --- samples/Basic_Neopixel/app/application.cpp | 217 ++++++++++----------- 1 file changed, 104 insertions(+), 113 deletions(-) diff --git a/samples/Basic_Neopixel/app/application.cpp b/samples/Basic_Neopixel/app/application.cpp index 5dac6ce468..86f5a1b6b8 100644 --- a/samples/Basic_Neopixel/app/application.cpp +++ b/samples/Basic_Neopixel/app/application.cpp @@ -13,71 +13,96 @@ // How many NeoPixels are attached to the Esp8266? #define NUMPIXELS 16 -Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800); - -void StartDemo(void); - -Timer StripDemoTimer; -Timer ColorWipeTimer; -Timer TheaterChaseTimer; - -int StripDemoType = 0; -int StripColor = 0; -int StripNo = 0; -int ChaseCycle = 0; -int TheaterChaseQ = 0; +namespace +{ +Adafruit_NeoPixel strip(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800); + +void startDemo(); + +SimpleTimer timer; +uint32_t stripColor; +uint8_t stripDemoType; // Defined by demoData below +uint8_t chaseCycle; + +enum ColorMap { + CLR_BLACK = 0x000000, + CLR_RED = 0xff0000, + CLR_GREEN = 0x00ff00, + CLR_BLUE = 0x0000ff, + CLR_DIM_WHITE = 0x7f7f7f, + CLR_DIM_RED = 0x7f0000, + CLR_DIM_BLUE = 0x00007f, +}; + +struct DemoData { + uint32_t color; + uint16_t stepMilliseconds; + uint8_t chase; +}; + +const DemoData demoData[]{ + {CLR_BLACK, 50, 0}, // 0 + {CLR_RED, 50, 0}, // 1 + {CLR_GREEN, 100, 0}, // 2 + {CLR_BLUE, 150, 0}, // 3 + {CLR_BLACK, 50, 0}, // 4 + {CLR_DIM_WHITE, 50, 10}, // 5 + {CLR_DIM_RED, 60, 15}, // 6 + {CLR_DIM_BLUE, 70, 20}, // 7 +}; // // Fill the dots one after the other with a color +// Timer callback // - -void ColorWipe() +void colorWipe() { - if(StripNo < strip.numPixels()) { - strip.setPixelColor(StripNo, StripColor); - strip.show(); - StripNo++; - } else { - StripDemoType++; // next demo type - ColorWipeTimer.stop(); // stop this demo timer - StripDemoTimer.initializeMs(2000, StartDemo).start(true); // start next demo after 2 seconds + static uint16_t stripNumber; + + if(stripNumber >= strip.numPixels()) { + stripNumber = 0; + // start next demo after 2 seconds + stripDemoType++; + timer.initializeMs<2000>(startDemo).startOnce(); + return; } + + strip.setPixelColor(stripNumber, stripColor); + strip.show(); + ++stripNumber; } // -//Theatre-style crawling lights. -//Timer callback - -void TheaterChase() +// Theatre-style crawling lights +// Timer callback +// +void theatreChase() { - int i, b = 0; - if(ChaseCycle > 0) { - if(TheaterChaseQ == 0) - b = 3; - if(TheaterChaseQ == 1) - b = 0; - if(TheaterChaseQ == 2) - b = 1; - if(TheaterChaseQ == 3) - b = 2; - - for(i = 0; i < strip.numPixels(); i = i + 4) - strip.setPixelColor(i + b, 0); //turn prev every third pixel off - - for(i = 0; i < strip.numPixels(); i = i + 4) - strip.setPixelColor(i + TheaterChaseQ, StripColor); //turn every third pixel on - - strip.show(); - TheaterChaseQ++; - if(TheaterChaseQ > 3) { - TheaterChaseQ = 0; - ChaseCycle--; - } - } else { - // finish this demo - StripDemoType++; // next demo type - TheaterChaseTimer.stop(); // stop this demo dimer - StripDemoTimer.initializeMs(2000, StartDemo).start(true); // start another demo after 2 seconds + static uint8_t theatreChaseQ; + + if(chaseCycle <= 0) { + assert(theatreChaseQ == 0); + // start another demo after 2 seconds + stripDemoType++; + timer.initializeMs<2000>(startDemo).startOnce(); + return; + } + + const uint8_t offsetList[]{3, 0, 1, 2}; + int b = offsetList[theatreChaseQ]; + + // Turn every third pixel off + for(unsigned i = 0; i < strip.numPixels(); i += 4) { + strip.setPixelColor(i + b, 0); + strip.setPixelColor(i + theatreChaseQ, stripColor); + } + + strip.show(); + + ++theatreChaseQ; + if(theatreChaseQ > 3) { + theatreChaseQ = 0; + --chaseCycle; } } @@ -85,71 +110,38 @@ void TheaterChase() // Demo timer callback // -void StartDemo() +void startDemo() { - Serial << _F("NeoPixel Demo type: ") << StripDemoType << endl; - - StripDemoTimer.stop(); // next demo wait until this demo ends - - StripNo = 0; //start from led index 0 - TheaterChaseQ = 0; //another counter - - switch(StripDemoType) { // select demo type - case 0: - StripColor = strip.Color(0, 0, 0); // black - ColorWipeTimer.initializeMs(50, ColorWipe).start(true); // 50 ms step - break; - case 1: - StripColor = strip.Color(255, 0, 0); // Red - ColorWipeTimer.initializeMs(50, ColorWipe).start(true); // 50 ms step - break; - case 2: - StripColor = strip.Color(0, 255, 0); // Green - ColorWipeTimer.initializeMs(100, ColorWipe).start(true); // 100 ms step - break; - case 3: - StripColor = strip.Color(0, 0, 255); // Blue - ColorWipeTimer.initializeMs(150, ColorWipe).start(true); // 150 ms step - break; - case 4: - StripColor = strip.Color(0, 0, 0); // black - ColorWipeTimer.initializeMs(50, ColorWipe).start(true); // 50 ms step - break; - - case 5: - ChaseCycle = 10; //do 10 cycles of chasing - StripColor = strip.Color(127, 127, 127); // White - TheaterChaseTimer.initializeMs(50, TheaterChase).start(true); // 50 ms step - break; - case 6: - ChaseCycle = 15; //do 15 cycles of chasing - StripColor = strip.Color(127, 0, 0); // Red - TheaterChaseTimer.initializeMs(60, TheaterChase).start(true); // 60 ms step - break; - case 7: - ChaseCycle = 20; //do 20 cycles of chasing - StripColor = strip.Color(0, 0, 127); // Blue - TheaterChaseTimer.initializeMs(70, TheaterChase).start(true); // 70 ms step - break; - - default: - StripDemoType = 0; - StripDemoTimer.initializeMs(1000, StartDemo).start(true); //demo loop restart - break; + Serial << _F("NeoPixel Demo type: ") << stripDemoType << endl; + + if(stripDemoType >= ARRAY_SIZE(demoData)) { + // Demo loop restart + stripDemoType = 0; + timer.initializeMs<1000>(startDemo).start(); + return; } + + auto& data = demoData[stripDemoType]; + chaseCycle = data.chase; + stripColor = data.color; + timer.initializeMs(data.stepMilliseconds, chaseCycle ? theatreChase : colorWipe).start(); } -void got_IP(IpAddress ip, IpAddress netmask, IpAddress gateway) +#ifndef DISABLE_WIFI +void gotIp(IpAddress ip, IpAddress netmask, IpAddress gateway) { Serial << "IP: " << ip << endl; // You can put here other job like web,tcp etc. } // Will be called when WiFi station loses connection -void connect_Fail(const String& ssid, MacAddress bssid, WifiDisconnectReason reason) +void connectFail(const String& ssid, MacAddress bssid, WifiDisconnectReason reason) { Serial.println(_F("I'm NOT CONNECTED!")); } +#endif + +} // namespace void init() { @@ -165,13 +157,12 @@ void init() WifiStation.config(WIFI_SSID, WIFI_PWD); WifiStation.enable(true); WifiAccessPoint.enable(false); - WifiEvents.onStationDisconnect(connect_Fail); - WifiEvents.onStationGotIP(got_IP); + WifiEvents.onStationDisconnect(connectFail); + WifiEvents.onStationGotIP(gotIp); #endif - StripDemoType = 0; //demo index to be displayed - - strip.begin(); //init port + stripDemoType = 0; + strip.begin(); - StripDemoTimer.initializeMs<1000>(StartDemo).start(); //start demo + timer.initializeMs<1000>(startDemo).start(); } From 5929fd9ac4bc06207d710fa4fa0cec4fe7012af2 Mon Sep 17 00:00:00 2001 From: mikee47 Date: Tue, 2 Apr 2024 13:44:06 +0100 Subject: [PATCH 08/13] Fix Basic_Storage Didn't actually use RAM. --- .../Storage/src/include/Storage/SysMem.h | 2 ++ samples/Basic_Storage/app/application.cpp | 14 +++++++++++--- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/Sming/Components/Storage/src/include/Storage/SysMem.h b/Sming/Components/Storage/src/include/Storage/SysMem.h index d2d333f1d2..7b6f20361d 100644 --- a/Sming/Components/Storage/src/include/Storage/SysMem.h +++ b/Sming/Components/Storage/src/include/Storage/SysMem.h @@ -73,6 +73,8 @@ class SysMem : public Device class SysMemPartitionTable : public PartitionTable { public: + using PartitionTable::add; + /** * @brief Add partition entry for FlashString data access */ diff --git a/samples/Basic_Storage/app/application.cpp b/samples/Basic_Storage/app/application.cpp index 04a260862a..bb0e8e7920 100644 --- a/samples/Basic_Storage/app/application.cpp +++ b/samples/Basic_Storage/app/application.cpp @@ -3,6 +3,9 @@ #include #include +namespace +{ +// Use this source file as contents of our test partitions IMPORT_FSTR(FS_app, PROJECT_DIR "/app/application.cpp") void listSpiffsPartitions() @@ -19,8 +22,7 @@ void listSpiffsPartitions() Directory dir; if(dir.open()) { while(dir.next()) { - Serial.print(" "); - Serial.println(dir.stat().name); + Serial << " " << dir.stat().name << endl; } } Serial << dir.count() << _F(" files found") << endl << endl; @@ -58,6 +60,8 @@ void printPart(const String& partitionName) } } +} // namespace + void init() { Serial.begin(SERIAL_BAUD_RATE); @@ -88,7 +92,11 @@ void init() printPart(part); Serial.println(_F("** Reading SysMem device (RAM)")); - part = Storage::sysMem.editablePartitions().add(F("fs_app RAM"), FS_app, {Storage::Partition::Type::data, 100}); + const size_t bufferSize{4096}; + auto buffer = std::make_unique(bufferSize); + FS_app.readFlash(0, buffer.get(), bufferSize); + part = Storage::sysMem.editablePartitions().add(F("fs_app RAM"), {Storage::Partition::Type::data, 100}, + storage_size_t(buffer.get()), bufferSize); printPart(part); printPart(part); printPart(part); From 2c8e5f50891375e9bf4c22224325b37661912e07 Mon Sep 17 00:00:00 2001 From: mikee47 Date: Tue, 2 Apr 2024 15:57:34 +0100 Subject: [PATCH 09/13] Add Host `Print` stream support, update `Basic_Utility` sample --- .../hostlib/include/hostlib/Streams.h | 50 +++++++++++++++++++ samples/Basic_Utility/app/utility.cpp | 44 ++++++++-------- 2 files changed, 70 insertions(+), 24 deletions(-) create mode 100644 Sming/Arch/Host/Components/hostlib/include/hostlib/Streams.h diff --git a/Sming/Arch/Host/Components/hostlib/include/hostlib/Streams.h b/Sming/Arch/Host/Components/hostlib/include/hostlib/Streams.h new file mode 100644 index 0000000000..698fb25f05 --- /dev/null +++ b/Sming/Arch/Host/Components/hostlib/include/hostlib/Streams.h @@ -0,0 +1,50 @@ +/** + * Streams.h - Print support for host output + * + * Copyright 2024 mikee47 + * + * This file is part of the Sming Framework Project + * + * This library is free software: you can redistribute it and/or modify it under the terms of the + * GNU General Public License as published by the Free Software Foundation, version 3 or later. + * + * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; + * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with SHEM. + * If not, see . + * + ****/ + +#pragma once + +#include + +namespace Host +{ +class OutputStream : public Print +{ +public: + OutputStream(int fileno) : fileno(fileno) + { + } + + virtual size_t write(uint8_t c) override + { + return write(&c, 1); + } + + size_t write(const uint8_t* buffer, size_t size) override + { + return ::write(fileno, buffer, size); + } + +private: + int fileno; +}; + +OutputStream standardOutput(STDOUT_FILENO); +OutputStream standardError(STDERR_FILENO); + +}; // namespace Host diff --git a/samples/Basic_Utility/app/utility.cpp b/samples/Basic_Utility/app/utility.cpp index 58fac25f67..3ef86868de 100644 --- a/samples/Basic_Utility/app/utility.cpp +++ b/samples/Basic_Utility/app/utility.cpp @@ -1,5 +1,8 @@ #include #include +#include + +auto& output = Host::standardOutput; namespace { @@ -14,21 +17,20 @@ DEFINE_FSTR(testWebConstants, "testWebConstants") void testWebConstants() { - m_printf("%s\n", __FUNCTION__); + output << __FUNCTION__ << endl; const char* extensions[] = { "htm", "html", "txt", "js", "css", "xml", "json", "jpg", "gif", "png", "svg", "ico", "gzip", "zip", "git", "sh", "bin", "exe", }; - for(unsigned i = 0; i < ARRAY_SIZE(extensions); ++i) { - auto ext = extensions[i]; + for(auto ext : extensions) { String contentType = ContentType::fromFileExtension(ext); if(!contentType) { - contentType = "(NOT FOUND)"; + contentType = F("(NOT FOUND)"); } MimeType mimeType = ContentType::fromString(contentType); - m_printf(" %u %s: %s (#%u)\n", i, ext, contentType.c_str(), unsigned(mimeType)); + output << " " << ext << ": " << contentType << " (#" << unsigned(mimeType) << ')' << endl; } } @@ -36,44 +38,38 @@ void testWebConstants() void init() { - // Hook up debug output - Serial.begin(COM_SPEED_SERIAL); - Serial.systemDebugOutput(true); - /* * In a real utility we'd probably need to parse command-line arguments. * See Arch/Host/Components/hostlib/options.h for one way to do this. */ - m_printf("\n** Basic Host utility sample **\n"); + output << endl << "** Basic Host utility sample **" << endl; auto parameters = commandLine.getParameters(); if(parameters.count() == 0) { - m_printf("No command line parameters\n" - "Try doing this:\n" - " make run HOST_PARAMETERS='command=%s'\n", - String(Command::testWebConstants).c_str()); + output << "No command line parameters" << endl + << "Try doing this:" << endl + << " make run HOST_PARAMETERS='command=" << String(Command::testWebConstants) << "'" << endl; } else { - m_printf("Command-line parameters: %u\n", parameters.count()); - for(unsigned i = 0; i < parameters.count(); ++i) { - auto param = parameters[i]; - m_printf(" %u: text = '%s'\n", i, param.text); - m_printf(" name = '%s'\n", param.getName().c_str()); - m_printf(" value = '%s'\n", param.getValue().c_str()); + output << "Command-line parameters: " << parameters.count() << endl; + for(auto& param : parameters) { + output << param.text << endl; + output << " name = '" << param.getName() << "'" << endl; + output << " value = '" << param.getValue() << "'" << endl; } - m_putc('\n'); + output.println(); auto param = parameters.findIgnoreCase("command"); if(param) { if(Command::testWebConstants.equalsIgnoreCase(param.getValue())) { testWebConstants(); } else { - m_printf("Command '%s' not recognised. Try '%s'.\n", param.getValue().c_str(), - String(Command::testWebConstants).c_str()); + output << "Command '" << param.getValue() << "' not recognised. Try '" + << String(Command::testWebConstants) << "'." << endl; } } } - m_putc('\n'); + output.println(); System.restart(); } From 68cabd1afb17a6cc979029e7c177af32c626db2d Mon Sep 17 00:00:00 2001 From: mikee47 Date: Tue, 2 Apr 2024 16:17:22 +0100 Subject: [PATCH 10/13] Basic_WebSkeletonApp --- .../Basic_WebSkeletonApp/app/application.cpp | 30 ++++++++++++------- .../app/configuration.cpp | 28 ++++++++++------- .../Basic_WebSkeletonApp/app/webserver.cpp | 16 +++++----- .../include/configuration.h | 11 ++----- .../Basic_WebSkeletonApp/include/tytherm.h | 8 ----- .../Basic_WebSkeletonApp/include/webserver.h | 3 ++ 6 files changed, 51 insertions(+), 45 deletions(-) delete mode 100644 samples/Basic_WebSkeletonApp/include/tytherm.h create mode 100644 samples/Basic_WebSkeletonApp/include/webserver.h diff --git a/samples/Basic_WebSkeletonApp/app/application.cpp b/samples/Basic_WebSkeletonApp/app/application.cpp index 90469993f0..9175976292 100644 --- a/samples/Basic_WebSkeletonApp/app/application.cpp +++ b/samples/Basic_WebSkeletonApp/app/application.cpp @@ -1,29 +1,35 @@ -#include +#include +#include +#include -static Timer counterTimer; -unsigned long counter = 0; +// Global +unsigned long counter; -static void counterLoop() +namespace +{ +SimpleTimer counterTimer; + +void counterCallback() { counter++; } -static void STADisconnect(const String& ssid, MacAddress bssid, WifiDisconnectReason reason) +void STADisconnect(const String& ssid, MacAddress bssid, WifiDisconnectReason reason) { - debugf("DISCONNECT - SSID: %s, REASON: %s\n", ssid.c_str(), WifiEvents.getDisconnectReasonDesc(reason).c_str()); + Serial << _F("DISCONNECT - SSID: ") << ssid << _F(", REASON: ") << WifiEvents.getDisconnectReasonDesc(reason) + << endl; if(!WifiAccessPoint.isEnabled()) { - debugf("Starting OWN AP"); + Serial << _F("Starting OWN AP"); WifiStation.disconnect(); WifiAccessPoint.enable(true); WifiStation.connect(); } } -static void STAGotIP(IpAddress ip, IpAddress mask, IpAddress gateway) +void STAGotIP(IpAddress ip, IpAddress mask, IpAddress gateway) { - debugf("GOTIP - IP: %s, MASK: %s, GW: %s\n", ip.toString().c_str(), mask.toString().c_str(), - gateway.toString().c_str()); + Serial << _F("GOTIP - IP: ") << ip << _F(", MASK: ") << mask << _F(", GW: ") << gateway << endl; if(WifiAccessPoint.isEnabled()) { debugf("Shutdown OWN AP"); @@ -32,6 +38,8 @@ static void STAGotIP(IpAddress ip, IpAddress mask, IpAddress gateway) // Add commands to be executed after successfully connecting to AP and got IP from it } +} // namespace + void init() { Serial.begin(SERIAL_BAUD_RATE); // 115200 by default @@ -57,5 +65,5 @@ void init() System.onReady(startWebServer); - counterTimer.initializeMs<1000>(counterLoop).start(); + counterTimer.initializeMs<1000>(counterCallback).start(); } diff --git a/samples/Basic_WebSkeletonApp/app/configuration.cpp b/samples/Basic_WebSkeletonApp/app/configuration.cpp index a97aa3227a..6795908354 100644 --- a/samples/Basic_WebSkeletonApp/app/configuration.cpp +++ b/samples/Basic_WebSkeletonApp/app/configuration.cpp @@ -7,6 +7,14 @@ #define WIFI_PWD "PleaseEnterPass" #endif +namespace +{ +DEFINE_FSTR(KEY_NETWORK, "network") +DEFINE_FSTR(KEY_SSID, "StaSSID") +DEFINE_FSTR(KEY_PASS, "StaPassword") +DEFINE_FSTR(KEY_ENABLE, "StaEnable") +} // namespace + ThermConfig activeConfig; ThermConfig loadConfig() @@ -14,14 +22,14 @@ ThermConfig loadConfig() StaticJsonDocument doc; ThermConfig cfg; if(Json::loadFromFile(doc, THERM_CONFIG_FILE)) { - JsonObject network = doc["network"]; - cfg.StaSSID = network["StaSSID"].as(); - cfg.StaPassword = network["StaPassword"].as(); - cfg.StaEnable = network["StaEnable"]; + JsonObject network = doc[KEY_NETWORK]; + cfg.StaSSID = network[KEY_SSID].as(); + cfg.StaPassword = network[KEY_PASS].as(); + cfg.StaEnable = network[KEY_ENABLE]; } else { // Factory defaults if no config file present, or could not access it - cfg.StaSSID = WIFI_SSID; - cfg.StaPassword = WIFI_PWD; + cfg.StaSSID = F(WIFI_SSID); + cfg.StaPassword = F(WIFI_PWD); } return cfg; } @@ -30,10 +38,10 @@ void saveConfig(ThermConfig& cfg) { StaticJsonDocument doc; - JsonObject network = doc.createNestedObject("network"); - network["StaSSID"] = cfg.StaSSID; - network["StaPassword"] = cfg.StaPassword; - network["StaEnable"] = cfg.StaEnable; + JsonObject network = doc.createNestedObject(KEY_NETWORK); + network[KEY_SSID] = cfg.StaSSID; + network[KEY_PASS] = cfg.StaPassword; + network[KEY_ENABLE] = cfg.StaEnable; Json::saveToFile(doc, THERM_CONFIG_FILE, Json::Pretty); } diff --git a/samples/Basic_WebSkeletonApp/app/webserver.cpp b/samples/Basic_WebSkeletonApp/app/webserver.cpp index 5e19982205..228c45d4cb 100644 --- a/samples/Basic_WebSkeletonApp/app/webserver.cpp +++ b/samples/Basic_WebSkeletonApp/app/webserver.cpp @@ -1,8 +1,10 @@ -#include #include #include #include #include "DelayStream.h" +#include + +extern unsigned long counter; // Kind of heartbeat counter namespace { @@ -40,20 +42,20 @@ void sendFile(const String& fileName, HttpServerConnection& connection) auto response = connection.getResponse(); String compressed = fileName + ".gz"; - auto v = fileMap[compressed]; - if(v) { + auto content = fileMap[compressed]; + if(content) { response->headers[HTTP_HEADER_CONTENT_ENCODING] = _F("gzip"); } else { - v = fileMap[fileName]; - if(!v) { + content = fileMap[fileName]; + if(!content) { debug_w("File '%s' not found", fileName.c_str()); response->code = HTTP_STATUS_NOT_FOUND; return; } } - debug_i("found %s in fileMap", String(v.key()).c_str()); - auto stream = new FSTR::Stream(v.content()); + debug_i("found %s in fileMap", String(content.key()).c_str()); + auto stream = new FSTR::Stream(content.content()); response->sendDataStream(stream, ContentType::fromFullFileName(fileName)); // Use client caching for better performance. diff --git a/samples/Basic_WebSkeletonApp/include/configuration.h b/samples/Basic_WebSkeletonApp/include/configuration.h index 555e69c3a3..25de6c43d1 100644 --- a/samples/Basic_WebSkeletonApp/include/configuration.h +++ b/samples/Basic_WebSkeletonApp/include/configuration.h @@ -1,6 +1,6 @@ #pragma once -#include +#include const char THERM_CONFIG_FILE[] = ".therm.conf"; // leading point for security reasons :) @@ -8,16 +8,9 @@ const char THERM_CONFIG_FILE[] = ".therm.conf"; // leading point for security re const uint8_t ConfigJsonBufferSize = 200; struct ThermConfig { - ThermConfig() - { - StaEnable = 1; //Enable WIFI Client - } - String StaSSID; String StaPassword; - uint8_t StaEnable; - - // ThermControl settings + bool StaEnable{true}; // Enable WIFI Client }; ThermConfig loadConfig(); diff --git a/samples/Basic_WebSkeletonApp/include/tytherm.h b/samples/Basic_WebSkeletonApp/include/tytherm.h deleted file mode 100644 index 5239e3df42..0000000000 --- a/samples/Basic_WebSkeletonApp/include/tytherm.h +++ /dev/null @@ -1,8 +0,0 @@ -#pragma once - -#include "configuration.h" - -extern unsigned long counter; // Kind of heartbeat counter - -// Webserver -void startWebServer(); diff --git a/samples/Basic_WebSkeletonApp/include/webserver.h b/samples/Basic_WebSkeletonApp/include/webserver.h new file mode 100644 index 0000000000..0ea88af3fd --- /dev/null +++ b/samples/Basic_WebSkeletonApp/include/webserver.h @@ -0,0 +1,3 @@ +#pragma once + +void startWebServer(); From 0f9b562f6bf5bf79737033e076d905528bd777f6 Mon Sep 17 00:00:00 2001 From: mikee47 Date: Tue, 2 Apr 2024 20:33:09 +0100 Subject: [PATCH 11/13] Rewrite Display_TM1637 as state machine (coroutine) --- samples/Display_TM1637/app/application.cpp | 217 +++++++++++++-------- 1 file changed, 138 insertions(+), 79 deletions(-) diff --git a/samples/Display_TM1637/app/application.cpp b/samples/Display_TM1637/app/application.cpp index 011a5fd917..14ae2294c8 100644 --- a/samples/Display_TM1637/app/application.cpp +++ b/samples/Display_TM1637/app/application.cpp @@ -8,105 +8,164 @@ // The amount of time (in milliseconds) between tests #define TEST_DELAY 500 -const uint8_t SEG_DONE[] = { +namespace +{ +const uint8_t SEG_DONE[4]{ SEG_B | SEG_C | SEG_D | SEG_E | SEG_G, // d SEG_A | SEG_B | SEG_C | SEG_D | SEG_E | SEG_F, // O SEG_C | SEG_E | SEG_G, // n SEG_A | SEG_D | SEG_E | SEG_F | SEG_G // E }; +const uint8_t SEG_ALL_OFF[4]{}; +const uint8_t SEG_ALL_ON[4]{0xff, 0xff, 0xff, 0xff}; TM1637Display display(CLK, DIO); +SimpleTimer stateTimer; -void testDisplay() +bool testDisplay() { - int k; - uint8_t data[] = {0xff, 0xff, 0xff, 0xff}; - display.setBrightness(0x0f); - - // All segments on - display.setSegments(data); - delay(TEST_DELAY); - - // Selectively set different digits - data[0] = 0b01001001; - data[1] = display.encodeDigit(1); - data[2] = display.encodeDigit(2); - data[3] = display.encodeDigit(3); - - for(k = 3; k >= 0; k--) { - display.setSegments(data, 1, k); - delay(TEST_DELAY); + static uint8_t state; + static uint8_t subState; + +#define STATE(fmt, ...) debug_i("#%u.%u " fmt, state, subState, ##__VA_ARGS__); + + switch(state) { + case 0: { + // All segments on + STATE("All segments on"); + display.setBrightness(7); + display.setSegments(SEG_ALL_ON); + break; } - - display.setSegments(data + 2, 2, 2); - delay(TEST_DELAY); - - display.setSegments(data + 2, 2, 1); - delay(TEST_DELAY); - - display.setSegments(data + 1, 3, 1); - delay(TEST_DELAY); - - // Show decimal numbers with/without leading zeros - bool lz = false; - for(uint8_t z = 0; z < 2; z++) { - for(k = 0; k < 10000; k += k * 4 + 7) { - display.showNumberDec(k, lz); - delay(TEST_DELAY); + case 1: { + // Selectively set different digits + uint8_t pos = 3 - subState; + STATE("setSegments(data, 1, %u)", pos); + const uint8_t seg = 0b01001001; + display.setSegments(&seg, 1, pos); + if(subState++ < 3) { + return false; } - lz = true; + break; } - - // Show decimal number whose length is smaller than 4 - for(k = 0; k < 4; k++) { - data[k] = 0; + case 2: { + uint8_t data[]{ + display.encodeDigit(1), + display.encodeDigit(2), + display.encodeDigit(3), + }; + uint8_t length = (subState == 2) ? 3 : 2; + uint8_t pos = (subState == 0) ? 2 : 1; + STATE("setSegments([0x%02x, 0x%02x, 0x%02x], %u, %u)", data[0], data[1], data[2], length, pos); + display.setSegments(data, length, pos); + if(subState++ < 3) { + return false; + } + break; } - display.setSegments(data); - - // Run through all the dots - for(k = 0; k <= 4; k++) { - display.showNumberDecEx(0, (0x80 >> k), true); - delay(TEST_DELAY); + case 3: + case 4: { + // Show decimal numbers with/without leading zeros + static unsigned k; + if(subState == 0) { + k = 0; + ++subState; + } + bool showLeadingZeroes = (state == 4); + STATE("showNumberDec(%u, %u)", k, showLeadingZeroes); + display.showNumberDec(k, showLeadingZeroes); + k += k * 4 + 7; + if(k < 10000) { + return false; + } + break; } - - display.showNumberDec(153, false, 3, 1); - delay(TEST_DELAY); - display.showNumberDec(22, false, 2, 2); - delay(TEST_DELAY); - display.showNumberDec(0, true, 1, 3); - delay(TEST_DELAY); - display.showNumberDec(0, true, 1, 2); - delay(TEST_DELAY); - display.showNumberDec(0, true, 1, 1); - delay(TEST_DELAY); - display.showNumberDec(0, true, 1, 0); - delay(TEST_DELAY); - - // Brightness Test - for(k = 0; k < 4; k++) { - data[k] = 0xff; + case 5: { + // Run through all the dots + display.setSegments(SEG_ALL_OFF); + uint8_t dots = 0x80 >> subState; + STATE("showNumberDec(0, 0x%02x, true)", dots); + display.showNumberDecEx(0, dots, true); + if(subState++ < 4) { + return false; + } + break; } - for(k = 0; k < 7; k++) { - display.setBrightness(k); - display.setSegments(data); - delay(TEST_DELAY); + case 6: { + // Show decimal number whose length is smaller than 4 + struct NumberArgs { + uint8_t num; + bool leadingZero; + uint8_t length; + uint8_t pos; + }; + const NumberArgs arglist[]{ + {153, false, 3, 1}, // + {22, false, 2, 2}, // + {0, true, 1, 3}, // + {0, true, 1, 2}, // + {0, true, 1, 1}, // + {0, true, 1, 0}, // + }; + auto& args = arglist[subState]; + STATE("showNumberDec(%u, %u, %u, %u)", args.num, args.leadingZero, args.length, args.pos); + display.showNumberDec(args.num, args.leadingZero, args.length, args.pos); + if(subState++ < ARRAY_SIZE(arglist)) { + return false; + } + break; } - - // On/Off test - for(k = 0; k < 4; k++) { - display.setBrightness(7, false); // Turn off - display.setSegments(data); - delay(TEST_DELAY); - display.setBrightness(7, true); // Turn on - display.setSegments(data); - delay(TEST_DELAY); + case 7: { + // Brightness Test + uint8_t brightness = subState; + STATE("setBrightness(%u)", brightness); + display.setBrightness(brightness); + display.setSegments(SEG_ALL_ON); + if(++subState < 7) { + return false; + } + break; + } + case 8: { + // On/Off test + bool onOff = subState & 0x01; + debug_i("setBrightness(7, %u)", onOff); + display.setBrightness(7, onOff); + display.setSegments(SEG_ALL_ON); + if(++subState < 8) { + return false; + } + break; + } + case 9: + // Done! + debug_i("Done!"); + display.setSegments(SEG_DONE); + return true; } - // Done! - display.setSegments(SEG_DONE); + ++state; + subState = 0; + return false; } +} // namespace + void init() { - testDisplay(); + Serial.begin(SERIAL_BAUD_RATE); + Serial.systemDebugOutput(true); + + Serial << _F("Sample TM1637 application") << endl; + +#ifdef ARCH_HOST + setDigitalHooks(nullptr); +#endif + + stateTimer.initializeMs([]() { + if(!testDisplay()) { + stateTimer.startOnce(); + } + }); + stateTimer.startOnce(); } From 04c0331ad3bb8496a525e28b15de4192bdb3d01d Mon Sep 17 00:00:00 2001 From: mikee47 Date: Wed, 3 Apr 2024 13:47:09 +0100 Subject: [PATCH 12/13] Fix bad memory casts in si4432 library Also builds for other architectures than esp8266, so remove SOC restrictions. --- Sming/Libraries/si4432/component.mk | 1 - Sming/Libraries/si4432/si4432.cpp | 16 +++++++--------- 2 files changed, 7 insertions(+), 10 deletions(-) delete mode 100644 Sming/Libraries/si4432/component.mk diff --git a/Sming/Libraries/si4432/component.mk b/Sming/Libraries/si4432/component.mk deleted file mode 100644 index e243360e9c..0000000000 --- a/Sming/Libraries/si4432/component.mk +++ /dev/null @@ -1 +0,0 @@ -COMPONENT_SOC := esp8266 diff --git a/Sming/Libraries/si4432/si4432.cpp b/Sming/Libraries/si4432/si4432.cpp index 69499dfbad..27ea4c2118 100644 --- a/Sming/Libraries/si4432/si4432.cpp +++ b/Sming/Libraries/si4432/si4432.cpp @@ -354,16 +354,16 @@ void Si4432::BurstWrite(Registers startReg, const byte value[], uint8_t length) // _spi->enable(); _spi->beginTransaction(_spi->SPIDefaultSettings); delayMicroseconds(1); -// _spi->send(®Val, 1); - _spi->transfer(®Val, 1); + _spi->transfer(regVal); #if DEBUG_VERBOSE_SI4432 debugf("Writing: %x | %x ... %x (%d bytes)", (regVal != 0xFF ? (regVal) & 0x7F : 0x7F), value[0], value[length-1], length); #endif -// _spi->send(value, length); - _spi->transfer((uint8 *)value, length); + uint8_t buffer[length]; + memcpy(buffer, value, length); + _spi->transfer(buffer, length); // _spi->disable(); _spi->endTransaction(); @@ -376,13 +376,11 @@ void Si4432::BurstRead(Registers startReg, byte value[], uint8_t length) { // _spi->enable(); _spi->beginTransaction(_spi->SPIDefaultSettings); delayMicroseconds(1); -// _spi->send(®Val, 1); - _spi->transfer(®Val, 1); + _spi->transfer(regVal); // _spi->setMOSI(HIGH); /* Send 0xFF */ - _spi->transfer((uint8 *)0xFF, 1); -// _spi->recv(value, length); - _spi->transfer((uint8 *)value, length); + _spi->transfer(0xff); + _spi->transfer(value, length); #if DEBUG_VERBOSE_SI4432 debugf("Reading: %x | %x..%x (%d bytes)", (regVal != 0x7F ? (regVal) & 0x7F : 0x7F), From cef7be99cec58e8168063fa55afadf0988d38a7f Mon Sep 17 00:00:00 2001 From: mikee47 Date: Mon, 1 Apr 2024 21:24:28 +0100 Subject: [PATCH 13/13] General updates --- .../samples/generic/app/application.cpp | 23 +-- .../samples/generic/app/application.cpp | 47 +++--- .../Accel_Gyro_MPU6050/app/application.cpp | 3 +- .../Accelerometer_MMA7455/app/application.cpp | 4 +- samples/Basic_APA102/app/application.cpp | 18 ++- samples/Basic_AWS/app/application.cpp | 8 +- samples/Basic_Audio/app/application.cpp | 48 +++--- samples/Basic_Blink/app/application.cpp | 2 +- samples/Basic_Capsense/app/application.cpp | 15 +- samples/Basic_HwPWM/app/application.cpp | 46 ++++-- samples/Basic_HwPWM/component.mk | 1 + samples/Basic_IFS/app/application.cpp | 7 +- samples/Basic_NFC/app/application.cpp | 63 +++----- samples/Basic_ScannerI2C/app/application.cpp | 21 ++- .../app/SerialReadingDelegateDemo.cpp | 5 + samples/Basic_Serial/app/application.cpp | 8 +- samples/Basic_Servo/app/application.cpp | 18 ++- samples/Basic_SmartConfig/app/application.cpp | 23 ++- samples/Basic_Ssl/app/application.cpp | 54 ++++--- samples/Basic_WiFi/app/application.cpp | 4 + samples/CanBus/app/application.cpp | 12 +- samples/Compass_HMC5883L/app/application.cpp | 40 ++--- samples/DFPlayerMini/app/application.cpp | 37 +++-- .../DS3232RTC_NTP_Setter/app/application.cpp | 22 +-- samples/Distance_Vl53l0x/app/application.cpp | 9 +- samples/DnsCaptivePortal/app/application.cpp | 12 +- samples/Echo_Ssl/app/application.cpp | 10 +- samples/FtpServer_Files/app/application.cpp | 6 +- samples/Gesture_APDS-9960/app/application.cpp | 82 ++++++---- samples/HttpClient/app/application.cpp | 22 ++- .../HttpClient_Instapush/app/application.cpp | 30 ++-- .../HttpClient_ThingSpeak/app/application.cpp | 12 +- .../HttpServer_Bootstrap/app/application.cpp | 27 ++-- .../app/application.cpp | 40 ++--- .../include/AppSettings.h | 11 +- .../app/application.cpp | 15 +- .../HttpServer_Plugins/app/application.cpp | 14 +- .../HttpServer_WebSockets/app/CUserData.cpp | 24 +-- .../HttpServer_WebSockets/app/application.cpp | 62 ++++--- samples/HttpServer_WebSockets/component.mk | 7 +- .../HttpServer_WebSockets/include/CUserData.h | 13 +- samples/Humidity_AM2321/app/application.cpp | 6 +- samples/Humidity_BME280/app/application.cpp | 6 +- samples/Humidity_DHT22/app/application.cpp | 39 ++--- samples/Humidity_SI7021/app/application.cpp | 20 ++- samples/IR_lib/app/application.cpp | 14 +- samples/LED_WS2812/app/application.cpp | 26 +-- samples/LED_YeelightBulb/app/application.cpp | 10 +- samples/Light_BH1750/app/application.cpp | 14 +- .../LiquidCrystal_44780/app/application.cpp | 62 +++++-- samples/LiveDebug/app/application.cpp | 33 ++-- samples/MeteoControl/app/application.cpp | 146 +++++++++-------- samples/MeteoControl/app/configuration.cpp | 6 +- samples/MeteoControl/app/webserver.cpp | 63 ++++---- samples/MeteoControl/include/configuration.h | 11 +- samples/MeteoControl/include/webserver.h | 5 +- samples/MeteoControl_mqtt/app/application.cpp | 26 +-- samples/MeteoControl_mqtt/app/bmp180.cpp | 48 +++--- samples/MeteoControl_mqtt/app/si7021.cpp | 46 +++--- .../MeteoControl_mqtt/include/configuration.h | 18 +-- samples/MqttClient_Hello/app/application.cpp | 13 +- samples/Network_Ping/app/application.cpp | 2 +- samples/Nextion_Button/app/application.cpp | 19 ++- .../PortExpander_MCP23017/app/application.cpp | 17 +- .../PortExpander_MCP23S17/app/application.cpp | 25 +-- samples/Pressure_BMP180/app/application.cpp | 1 + samples/Radio_RCSwitch/app/application.cpp | 31 ++-- samples/Radio_nRF24L01/app/application.cpp | 6 +- samples/Radio_si4432/app/application.cpp | 73 ++++----- samples/SDCard/app/application.cpp | 16 +- samples/ScreenLCD_5110/app/application.cpp | 38 +++-- .../ScreenOLED_SSD1306/app/application.cpp | 15 +- .../ScreenTFT_ILI9163C/app/application.cpp | 50 +++--- .../app/application.cpp | 101 +++++++----- samples/ScreenTFT_ST7735/app/application.cpp | 153 ++++++++++-------- samples/ScreenTFT_ST7735/component.mk | 1 - samples/SmtpClient/app/application.cpp | 27 ++-- samples/SystemClock_NTP/app/application.cpp | 9 +- .../TcpClient_NarodMon/app/application.cpp | 16 +- .../Temperature_DS1820/app/application.cpp | 52 +++--- samples/UdpServer_Echo/app/application.cpp | 8 +- samples/UdpServer_mDNS/app/application.cpp | 54 +------ samples/Ultrasonic_HCSR04/app/application.cpp | 8 +- samples/WebcamServer/app/application.cpp | 6 +- samples/Websocket_Client/app/application.cpp | 2 +- samples/Wifi_Sniffer/app/application.cpp | 93 ++++++----- 86 files changed, 1323 insertions(+), 1037 deletions(-) diff --git a/Sming/Libraries/CS5460/samples/generic/app/application.cpp b/Sming/Libraries/CS5460/samples/generic/app/application.cpp index 0349ba6653..31e0983b93 100644 --- a/Sming/Libraries/CS5460/samples/generic/app/application.cpp +++ b/Sming/Libraries/CS5460/samples/generic/app/application.cpp @@ -1,28 +1,29 @@ #include #include +namespace +{ CS5460 powerMeter(PIN_NDEFINED, PIN_NDEFINED, PIN_NDEFINED, PIN_NDEFINED); - -Timer printVoltageTimer; +SimpleTimer printVoltageTimer; void printVoltage() { - debugf("Measured RMS voltage is: %f", powerMeter.getRMSVoltage()); + Serial << _F("Measured RMS voltage is: ") << powerMeter.getRMSVoltage() << endl; } +} // namespace + void init() { - Serial.begin(SERIAL_BAUD_RATE, SERIAL_8N1, - SERIAL_FULL); // 115200 by default, GPIO1,GPIO3, see Serial.swap(), HardwareSerial + Serial.begin(SERIAL_BAUD_RATE); Serial.systemDebugOutput(true); powerMeter.init(); - powerMeter.setCurrentGain(190.84); //0.25 / shunt (0.00131) - powerMeter.setVoltageGain(500); //0.25V (Veff max) * dividerGain - uint32_t conf = 0; - conf = powerMeter.readRegister(CONFIG_REGISTER); - conf |= ENABLE_VOLTAGE_HPF; - conf |= ENABLE_CURRENT_HPF; + powerMeter.setCurrentGain(190.84); // 0.25 / shunt (0.00131) + powerMeter.setVoltageGain(500); // 0.25V (Veff max) * dividerGain + + uint32_t conf = powerMeter.readRegister(CONFIG_REGISTER); + conf |= ENABLE_VOLTAGE_HPF | ENABLE_CURRENT_HPF; powerMeter.writeRegister(CONFIG_REGISTER, conf); powerMeter.startMultiConvert(); diff --git a/Sming/Libraries/ModbusMaster/samples/generic/app/application.cpp b/Sming/Libraries/ModbusMaster/samples/generic/app/application.cpp index 05f9d14b6d..2a3800a694 100644 --- a/Sming/Libraries/ModbusMaster/samples/generic/app/application.cpp +++ b/Sming/Libraries/ModbusMaster/samples/generic/app/application.cpp @@ -6,26 +6,28 @@ #define MB_SLAVE_ADDR 1 #define SLAVE_REG_ADDR 1 -Timer mbLoopTimer; - +namespace +{ +SimpleTimer mbLoopTimer; ModbusMaster mbMaster; HardwareSerial modbusComPort(UART_ID_0); HardwareSerial debugComPort(UART_ID_1); -uint16_t globalSeconds = 0; +uint16_t globalSeconds; void mbLoop() { globalSeconds++; - uint8_t numberOfRegistersToWrite = 1; - uint8_t bufferPosition = 0; + const uint8_t numberOfRegistersToWrite = 1; + const uint8_t bufferPosition = 0; mbMaster.begin(MB_SLAVE_ADDR, modbusComPort); mbMaster.setTransmitBuffer(bufferPosition, globalSeconds); mbMaster.writeMultipleRegisters(SLAVE_REG_ADDR, numberOfRegistersToWrite); - uint8_t nrOfRegistersToRead = 1; - uint8_t mbResult = mbMaster.readHoldingRegisters(SLAVE_REG_ADDR, nrOfRegistersToRead); //see also readInputRegisters + // see also readInputRegisters + const uint8_t nrOfRegistersToRead = 1; + uint8_t mbResult = mbMaster.readHoldingRegisters(SLAVE_REG_ADDR, nrOfRegistersToRead); if(mbResult == mbMaster.ku8MBSuccess) { /* @@ -34,9 +36,9 @@ void mbLoop() debugComPort.printf("Reg %d: %d\r\n", i, buffer[i]); } */ - debugf("Data from slave: %d", mbMaster.getResponseBuffer(0)); + debug_i("Data from slave: %d", mbMaster.getResponseBuffer(0)); } else { - debugf("Res err: %d", mbResult); + debug_i("Res err: %d", mbResult); } mbMaster.clearResponseBuffer(); @@ -57,37 +59,37 @@ void mbLogReceive(const uint8_t* adu, size_t aduSize, uint8_t status) if(status != mbMaster.ku8MBSuccess) { switch(status) { case mbMaster.ku8MBIllegalFunction: - debugf("MB Illegal Function"); + debug_i("MB Illegal Function"); break; case mbMaster.ku8MBIllegalDataAddress: - debugf("MB Illegal Address"); + debug_i("MB Illegal Address"); break; case mbMaster.ku8MBIllegalDataValue: - debugf("MB Illegal Data Value"); + debug_i("MB Illegal Data Value"); break; case mbMaster.ku8MBSlaveDeviceFailure: - debugf("MB Slave Device Failure"); + debug_i("MB Slave Device Failure"); break; case mbMaster.ku8MBInvalidSlaveID: - debugf("MB Invalid Slave ID"); + debug_i("MB Invalid Slave ID"); break; case mbMaster.ku8MBInvalidFunction: - debugf("MB Invalid function"); + debug_i("MB Invalid function"); break; case mbMaster.ku8MBResponseTimedOut: - debugf("MB Response Timeout"); + debug_i("MB Response Timeout"); break; case mbMaster.ku8MBInvalidCRC: - debugf("MB Invalid CRC"); + debug_i("MB Invalid CRC"); break; case mbMaster.ku8MBResponseTooLarge: - debugf("MB Response too large"); + debug_i("MB Response too large"); break; } - debugf("ADU Size: %d, status: %d ", aduSize, status); + debug_i("ADU Size: %d, status: %d ", aduSize, status); debug_hex(INFO, "RX ADU", adu, aduSize); } - debugf("\r\n"); + debug_i("\r\n"); } void mbLogTransmit(const uint8_t* adu, size_t aduSize) @@ -95,13 +97,14 @@ void mbLogTransmit(const uint8_t* adu, size_t aduSize) debug_hex(INFO, "TX ADU", adu, aduSize); } +} // namespace + void init() { pinMode(RS485_RE_PIN, OUTPUT); digitalWrite(RS485_RE_PIN, LOW); modbusComPort.begin(MODBUS_COM_SPEED, SERIAL_8N1, SERIAL_FULL); - debugComPort.begin(SERIAL_BAUD_RATE, SERIAL_8N1, - SERIAL_TX_ONLY); // 115200 by default, GPIO1,GPIO3, see Serial.swap(), HardwareSerial + debugComPort.begin(SERIAL_BAUD_RATE, SERIAL_8N1, SERIAL_TX_ONLY); debugComPort.systemDebugOutput(true); mbMaster.preTransmission(preTransmission); diff --git a/samples/Accel_Gyro_MPU6050/app/application.cpp b/samples/Accel_Gyro_MPU6050/app/application.cpp index 0190f99c03..f060d104f2 100644 --- a/samples/Accel_Gyro_MPU6050/app/application.cpp +++ b/samples/Accel_Gyro_MPU6050/app/application.cpp @@ -18,7 +18,8 @@ void init() Wire.begin(DEFAULT_SDA_PIN, DEFAULT_SCL_PIN); mpu.initialize(); - Serial.println(mpu.testConnection() ? "MPU6050 connection successful" : "MPU6050 connection failed"); + bool success = mpu.testConnection(); + Serial << _F("MPU6050 connection ") << (success ? _F("successful") : _F("failed")) << endl; mainLoopTimer.initializeMs(mainLoop).start(); } diff --git a/samples/Accelerometer_MMA7455/app/application.cpp b/samples/Accelerometer_MMA7455/app/application.cpp index 257dedc9d2..2a02f7b75d 100644 --- a/samples/Accelerometer_MMA7455/app/application.cpp +++ b/samples/Accelerometer_MMA7455/app/application.cpp @@ -3,11 +3,11 @@ // For more information read: https://code.google.com/p/mma-7455-arduino-library/ MMA_7455 accel; -Timer procTimer; +SimpleTimer procTimer; void readSensor() { - Serial.println("Reading.."); + Serial.println(_F("Reading..")); int8_t x = accel.readAxis('x'); int8_t y = accel.readAxis('y'); diff --git a/samples/Basic_APA102/app/application.cpp b/samples/Basic_APA102/app/application.cpp index 9297b41639..0558482bc3 100644 --- a/samples/Basic_APA102/app/application.cpp +++ b/samples/Basic_APA102/app/application.cpp @@ -23,7 +23,9 @@ #define SPI_CS 2 -Timer procTimer; +namespace +{ +SimpleTimer procTimer; // in this demo, the same ports for HW and SW SPI are used #ifdef _USE_SOFTSPI @@ -34,12 +36,12 @@ APA102 LED(NUM_LED); // APA102 constructor, call with number of LEDs //APA102 LED(NUM_LED, SPI); #endif -static SPISettings SPI_1MHZ = SPISettings(1000000, MSBFIRST, SPI_MODE3); -static SPISettings SPI_2MHZ = SPISettings(2000000, MSBFIRST, SPI_MODE3); +SPISettings SPI_1MHZ{1000000, MSBFIRST, SPI_MODE3}; +SPISettings SPI_2MHZ{2000000, MSBFIRST, SPI_MODE3}; /* color wheel function: * (simple) three 120° shifted colors -> color transitions r-g-b-r */ -static col_t colorWheel(uint16_t step, uint16_t numStep) +col_t colorWheel(uint16_t step, uint16_t numStep) { col_t col = {0}; col.br = 10; @@ -63,10 +65,10 @@ static col_t colorWheel(uint16_t step, uint16_t numStep) return col; } -static void updateLED() +void updateLED() { - static unsigned state = 0; - static unsigned cnt = 0; + static unsigned state; + static unsigned cnt; switch(state++) { case 0: @@ -113,6 +115,8 @@ static void updateLED() } } +} // namespace + void init() { Serial.begin(SERIAL_BAUD_RATE); diff --git a/samples/Basic_AWS/app/application.cpp b/samples/Basic_AWS/app/application.cpp index 03c637a0ab..7f2edde2f2 100644 --- a/samples/Basic_AWS/app/application.cpp +++ b/samples/Basic_AWS/app/application.cpp @@ -26,11 +26,11 @@ void startMqttClient() Url url; url.Scheme = URI_SCHEME_MQTT_SECURE; url.Host = awsEndpoint; - mqtt.connect(url, "Basic_AWS"); + mqtt.connect(url, F("Basic_AWS")); // Assign a disconnect callback function // mqtt.setCompleteDelegate(checkMQTTDisconnect); - mqtt.subscribe("thing/fish/test"); + mqtt.subscribe(F("thing/fish/test")); } // Publish our message @@ -41,8 +41,8 @@ void publishMessage() startMqttClient(); } - Serial.println("publish message"); - mqtt.publish("version", "ver.1.2"); + Serial.println(_F("publish message")); + mqtt.publish(F("version"), F("ver.1.2")); } // Callback for messages, arrived from MQTT server diff --git a/samples/Basic_Audio/app/application.cpp b/samples/Basic_Audio/app/application.cpp index 30c549a672..2d623b36a7 100644 --- a/samples/Basic_Audio/app/application.cpp +++ b/samples/Basic_Audio/app/application.cpp @@ -19,6 +19,8 @@ // If using an external DAC, set this to 0 - you'll probably need to change other settings as well #define ENABLE_DELTA_SIGMA +namespace +{ // Set this to 0 to output fixed values for easier checking on a scope or signal analyzer //#define GENERATE_FIXED_VALUES constexpr i2s_sample_t fixedSampleValue = {0xF55F0AA0}; @@ -30,17 +32,17 @@ constexpr unsigned targetSampleRate = 44100; constexpr float sineWaveFrequency = 440; // Measure time taken to fill the I2S DMA buffers -static Profiling::MicroTimes fillTime("Fill Time"); +Profiling::MicroTimes fillTime("Fill Time"); // Measure time taken between I2S callback (interrupt) and our task callback getting executed -static Profiling::MicroTimes callbackLatency("Callback latency"); +Profiling::MicroTimes callbackLatency("Callback latency"); // Report status periodically -static SimpleTimer statusTimer; -static constexpr unsigned statusIntervalMs = 5000; +SimpleTimer statusTimer; +constexpr unsigned statusIntervalMs = 5000; // One full sine-wave cycle -static struct { +struct SineWaveTable { std::unique_ptr samples; unsigned sampleCount = 0; unsigned readPos = 0; @@ -81,7 +83,24 @@ static struct { } return value; } -} sineWaveTable; +}; + +SineWaveTable sineWaveTable; + +#ifdef GENERATE_FIXED_VALUES + +void writeFixedValues() +{ + i2s_buffer_info_t info; + while(i2s_dma_write(&info, UINT_MAX)) { + memset(info.samples, 0, info.size); + // for(unsigned i = 0; i < info.size / sizeof(i2s_sample_t); ++i) { + // info.samples[i] = fixedSampleValue; + // } + } +} + +#else /* * Outputs a 172.266Hz sine wave (256 samples at 44100 samples/sec) @@ -105,16 +124,7 @@ void writeSine() } } -void writeFixedValues() -{ - i2s_buffer_info_t info; - while(i2s_dma_write(&info, UINT_MAX)) { - memset(info.samples, 0, info.size); - // for(unsigned i = 0; i < info.size / sizeof(i2s_sample_t); ++i) { - // info.samples[i] = fixedSampleValue; - // } - } -} +#endif // GENERATE_FIXED_VALUES void fillBuffers() { @@ -128,7 +138,7 @@ void fillBuffers() fillTime.update(); } -static void checkReceive() +void checkReceive() { unsigned total = 0; i2s_buffer_info_t info; @@ -154,7 +164,7 @@ void IRAM_ATTR i2sCallback(void* param, i2s_event_type_t event) } } -static void initialiseI2S() +void initialiseI2S() { i2s_config_t config; memset(&config, 0, sizeof(config)); @@ -231,6 +241,8 @@ static void initialiseI2S() i2s_start(); } +} // namespace + void init() { /* diff --git a/samples/Basic_Blink/app/application.cpp b/samples/Basic_Blink/app/application.cpp index 6c20f2c84b..1856c6e5c0 100644 --- a/samples/Basic_Blink/app/application.cpp +++ b/samples/Basic_Blink/app/application.cpp @@ -6,7 +6,7 @@ #define LED_PIN 2 // GPIO2 #endif -Timer procTimer; +SimpleTimer procTimer; bool state = true; void blink() diff --git a/samples/Basic_Capsense/app/application.cpp b/samples/Basic_Capsense/app/application.cpp index 242103f358..8d34b8a55a 100644 --- a/samples/Basic_Capsense/app/application.cpp +++ b/samples/Basic_Capsense/app/application.cpp @@ -7,16 +7,23 @@ // clock speed on the ESP means we need a higher charge current than arduino ?? // Further investigation required. -CapacitiveSensor cs_0_2 = CapacitiveSensor(0, 2); //Send pin 0, Receive Pin 2. +#define PIN_SEND 0 +#define PIN_RECEIVE 2 +#define SAMPLES_TO_READ 30 -Timer procTimer; +namespace +{ +CapacitiveSensor cs_0_2(PIN_SEND, PIN_RECEIVE); +SimpleTimer procTimer; void capsense() { - long total = cs_0_2.capacitiveSensor(30); //Read sensor with 30 samples - Serial << _F("Sense Value: ") << total << endl; // print sensor output + long total = cs_0_2.capacitiveSensor(SAMPLES_TO_READ); + Serial << _F("Sense Value: ") << total << endl; } +} // namespace + void init() { procTimer.initializeMs<100>(capsense).start(); diff --git a/samples/Basic_HwPWM/app/application.cpp b/samples/Basic_HwPWM/app/application.cpp index 1d828406b7..b1c29c4784 100644 --- a/samples/Basic_HwPWM/app/application.cpp +++ b/samples/Basic_HwPWM/app/application.cpp @@ -18,34 +18,46 @@ #include #include -uint8_t pins[8] = {4, 5, 0, 2, 15, 13, 12, 14}; // List of pins that you want to connect to pwm -HardwarePWM HW_pwm(pins, 8); +#define LED_PIN 2 -Timer procTimer; -int32 i = 0; -bool countUp = true; +namespace +{ +// List of pins that you want to connect to pwm +uint8_t pins[]{ + LED_PIN, 4, 5, 0, 15, 13, 12, 14, +}; +HardwarePWM HW_pwm(pins, ARRAY_SIZE(pins)); + +SimpleTimer procTimer; -int maxDuty = HW_pwm.getMaxDuty(); -int32 inc = maxDuty / 50; +const int maxDuty = HW_pwm.getMaxDuty(); void doPWM() { - if(countUp == true) { - i += inc; - if(i >= maxDuty) { - i = maxDuty; + static bool countUp = true; + static int duty; + + const int increment = maxDuty / 50; + + if(countUp) { + duty += increment; + if(duty >= maxDuty) { + duty = maxDuty; countUp = false; } } else { - i -= inc; - if(i <= 0) { - i = 0; + duty -= increment; + if(duty <= 0) { + duty = 0; countUp = true; } } - HW_pwm.analogWrite(2, i); + + HW_pwm.analogWrite(LED_PIN, duty); } +} // namespace + void init() { Serial.begin(SERIAL_BAUD_RATE); // 115200 by default @@ -67,7 +79,7 @@ void init() HW_pwm.analogWrite(12, 2 * maxDuty / 3); HW_pwm.analogWrite(14, maxDuty); - debugf("PWM output set on all 8 Pins. Kindly check..."); - debugf("Now Pin 2 will go from 0 to VCC to 0 in cycles."); + Serial.println(_F("PWM output set on all 8 Pins. Kindly check...\r\n" + "Now LED_PIN will go from 0 to VCC to 0 in cycles.")); procTimer.initializeMs<100>(doPWM).start(); } diff --git a/samples/Basic_HwPWM/component.mk b/samples/Basic_HwPWM/component.mk index ec78f78162..be6ff9c63a 100644 --- a/samples/Basic_HwPWM/component.mk +++ b/samples/Basic_HwPWM/component.mk @@ -1,4 +1,5 @@ COMPONENT_SOC := esp8266 +DISABLE_NETWORK := 1 # Uncomment the line below if you want to use Espressif's PWM library. #ENABLE_CUSTOM_PWM=0 diff --git a/samples/Basic_IFS/app/application.cpp b/samples/Basic_IFS/app/application.cpp index 5bdfc2787a..c3bd209ed3 100644 --- a/samples/Basic_IFS/app/application.cpp +++ b/samples/Basic_IFS/app/application.cpp @@ -54,6 +54,8 @@ IMPORT_FSTR(listing_json, PROJECT_DIR "/resource/listing.json") HttpServer server; FtpServer ftp; int requestCount; +Profiling::TaskStat taskStat(Serial); +SimpleTimer statTimer; /* * Handle any custom fields here @@ -465,9 +467,6 @@ void fstest() listAttributes(); } -Profiling::TaskStat taskStat(Serial); -Timer statTimer; - } // namespace void init() @@ -489,6 +488,6 @@ void init() WifiEvents.onStationGotIP(gotIP); - statTimer.initializeMs<2000>(InterruptCallback([]() { taskStat.update(); })); + statTimer.initializeMs<2000>([]() { taskStat.update(); }); statTimer.start(); } diff --git a/samples/Basic_NFC/app/application.cpp b/samples/Basic_NFC/app/application.cpp index 29e11a58c9..1ecaba08bc 100644 --- a/samples/Basic_NFC/app/application.cpp +++ b/samples/Basic_NFC/app/application.cpp @@ -1,69 +1,56 @@ #include #include -Timer procTimer; -static Timer nfcScanTimer; -int helloCounter = 0; +#define SS_PIN 4 // D2 + +namespace +{ +SimpleTimer procTimer; -void scanNfc(byte scanner); +MFRC522 mfrc522(SS_PIN, SS_PIN); -#define SS_PIN 4 // D2 +// List of pins where NFC devices may be connected +const uint8_t ss_pin[]{4, 15}; -MFRC522 mfrc522(SS_PIN, SS_PIN); // Create MFRC522 instance -byte ss_pin[] = {4, 15}; +void scanNfc(uint8_t scanner); void sayHello() { - for(int pinNdx = 0; pinNdx < 2; pinNdx++) { - byte pin = ss_pin[pinNdx]; + for(auto pin : ss_pin) { mfrc522.setControlPins(pin, pin); - mfrc522.PCD_Init(); // Init MFRC522 + mfrc522.PCD_Init(); scanNfc(pin); } } -//--------------------------------- -static void dump_byte_array(byte* buffer, byte bufferSize) -{ - String hexOut; - for(byte i = 0; i < bufferSize; i++) { - hexOut += String(buffer[i], HEX); - } - debugf("%s", hexOut.c_str()); -} -//--------------------------------- -void scanNfc(byte scanner) + +void scanNfc(uint8_t scannerPin) { if(!mfrc522.PICC_IsNewCardPresent()) { - debugf("Scanning nfc Scanner:%d \r\n", scanner); + Serial << _F("Scanning NFC pin #") << scannerPin << _F(" not present") << endl; return; } - if(!mfrc522.PICC_ReadCardSerial()) { // Select one of the cards - debugf("Selecting card failed..."); + // Select one of the cards + if(!mfrc522.PICC_ReadCardSerial()) { + Serial << _F("Selecting card #") << scannerPin << _F(" failed...") << endl; } else { // Show some details of the PICC (that is: the tag/card) - debugf("Card UID on scanner:%d:", scanner); - dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size); - debugf(); + Serial << _F("Card UID on scanner: ") << scannerPin << endl; + m_printHex("UID", mfrc522.uid.uidByte, mfrc522.uid.size); } + mfrc522.PICC_HaltA(); + // Stop encryption on PCD mfrc522.PCD_StopCrypto1(); - mfrc522.PCD_Init(); // Init MFRC522 - - //nfcScanTimer.restart(); + mfrc522.PCD_Init(); } +} // namespace + void init() { - Serial.begin(SERIAL_BAUD_RATE); // 115200 by default + Serial.begin(SERIAL_BAUD_RATE); procTimer.initializeMs<2000>(sayHello).start(); - - //----- NFC - MFRC522 mfrc522(SS_PIN, SS_PIN); - SPI.begin(); - mfrc522.PCD_Init(); // Init MFRC522 - - //nfcScanTimer.initializeMs<50>(scanNfc).startOnce(); } diff --git a/samples/Basic_ScannerI2C/app/application.cpp b/samples/Basic_ScannerI2C/app/application.cpp index b3082f5fbb..c155894ac7 100644 --- a/samples/Basic_ScannerI2C/app/application.cpp +++ b/samples/Basic_ScannerI2C/app/application.cpp @@ -29,22 +29,22 @@ #include -Timer procTimer; +namespace +{ +SimpleTimer procTimer; +} void scanBus() { - byte error, address; - int nDevices; - Serial.println("Scanning..."); - nDevices = 0; - for(address = 1; address < 127; address++) { + unsigned nDevices{0}; + for(uint8_t address = 1; address < 127; address++) { // The i2c_scanner uses the return value of // the Write.endTransmisstion to see if // a device did acknowledge to the address. Wire.beginTransmission(address); - error = Wire.endTransmission(); + auto error = Wire.endTransmission(); WDT.alive(); // Second option: notify Watch Dog what you are alive (feed it) @@ -55,11 +55,8 @@ void scanBus() Serial << _F("Unknown error at address 0x") << String(address, HEX, 2) << endl; } } - if(nDevices == 0) { - Serial.println("No I2C devices found\n"); - } else { - Serial.println("done\n"); - } + + Serial << nDevices << _F(" I2C devices found.") << endl << endl; } void init() diff --git a/samples/Basic_Serial/app/SerialReadingDelegateDemo.cpp b/samples/Basic_Serial/app/SerialReadingDelegateDemo.cpp index 4e9b346b9e..3e73508fac 100644 --- a/samples/Basic_Serial/app/SerialReadingDelegateDemo.cpp +++ b/samples/Basic_Serial/app/SerialReadingDelegateDemo.cpp @@ -50,6 +50,11 @@ void SerialReadingDelegateDemo::onData(Stream& stream, char arrivedChar, unsigne numCallback++; + /* + * A more functional serial command-line interface can be implemented using the `LineBuffer` class. + * See `Basic_DateTime` sample application for a demonstration. + */ + if(arrivedChar == '\n') // Lets show data! { serial->println(_F("")); diff --git a/samples/Basic_Serial/app/application.cpp b/samples/Basic_Serial/app/application.cpp index b5ff51674e..a33a47bf68 100644 --- a/samples/Basic_Serial/app/application.cpp +++ b/samples/Basic_Serial/app/application.cpp @@ -5,6 +5,8 @@ #include "SerialReadingDelegateDemo.h" #include "SerialTransmitDemo.h" +namespace +{ struct SerialPins { uint8_t tx; uint8_t rx; @@ -128,9 +130,9 @@ DEFINE_FSTR(testFlashData, "The following are the graphical (non-control) charac "\r\n" "END OF TEXT\r\n"); -Timer procTimer; +SimpleTimer procTimer; SerialReadingDelegateDemo delegateDemoClass; -int helloCounter = 0; +int helloCounter; /** * Serial1 uses UART1, TX pin is GPIO2. @@ -229,6 +231,8 @@ void handleCommand(const String& command) } } +} // namespace + void init() { /* diff --git a/samples/Basic_Servo/app/application.cpp b/samples/Basic_Servo/app/application.cpp index 970d6fbb1c..d3d18c1afa 100644 --- a/samples/Basic_Servo/app/application.cpp +++ b/samples/Basic_Servo/app/application.cpp @@ -7,6 +7,8 @@ #include #include +namespace +{ // Un-comment to use raw time values instead of degrees for servo positioning //#define UPDATE_RAW @@ -36,8 +38,6 @@ class MyServoChannel : public ServoChannel int degree = 0; }; -static MyServoChannel channels[4]; - void MyServoChannel::calcValue() { const char indent[] = " "; @@ -63,10 +63,10 @@ void MyServoChannel::calcValue() value = 0; // overflow and restart linear ramp } - Serial << " value = " << value; + Serial << _F(" value = ") << value; if(!setValue(value)) { - Serial << ": setValue(" << value << ") failed!"; + Serial << _F(": setValue(") << value << _F(") failed!"); } #else @@ -77,10 +77,10 @@ void MyServoChannel::calcValue() ++degree; } - Serial << " degree = " << degree; + Serial << _F(" degree = ") << degree; if(!setDegree(degree)) { - Serial << ": setDegree(" << degree << ") failed!"; + Serial << _F(": setDegree(") << degree << _F(") failed!"); } #endif @@ -102,6 +102,10 @@ void MyServoChannel::calcValue() Serial.println(); } +MyServoChannel channels[4]; + +} // namespace + void init() { Serial.begin(SERIAL_BAUD_RATE); @@ -109,7 +113,7 @@ void init() Serial.setTxWait(false); Serial.setTxBufferSize(1024); - Serial.println("Init Basic Servo Sample"); + Serial.println(_F("Init Basic Servo Sample")); System.setCpuFrequency(CpuCycleClockNormal::cpuFrequency()); #ifdef ARCH_HOST diff --git a/samples/Basic_SmartConfig/app/application.cpp b/samples/Basic_SmartConfig/app/application.cpp index 05f9eec4ca..7e0909b529 100644 --- a/samples/Basic_SmartConfig/app/application.cpp +++ b/samples/Basic_SmartConfig/app/application.cpp @@ -1,24 +1,30 @@ #include +namespace +{ bool smartConfigCallback(SmartConfigEvent event, const SmartConfigEventInfo& info) { switch(event) { case SCE_Wait: - debugf("SCE_Wait\n"); + Serial.println(_F("SCE_Wait")); break; + case SCE_FindChannel: - debugf("SCE_FindChannel\n"); + Serial.println(_F("SCE_FindChannel")); break; + case SCE_GettingSsid: - debugf("SCE_GettingSsid, type = %d\n", info.type); + Serial << _F("SCE_GettingSsid, type = ") << info.type << endl; break; + case SCE_Link: - debugf("SCE_Link\n"); + Serial.println(_F("SCE_Link")); WifiStation.config(info.ssid, info.password); WifiStation.connect(); break; + case SCE_LinkOver: - debugf("SCE_LinkOver\n"); + Serial.println(_F("SCE_LinkOver")); WifiStation.smartConfigStop(); break; } @@ -29,9 +35,11 @@ bool smartConfigCallback(SmartConfigEvent event, const SmartConfigEventInfo& inf void gotIP(IpAddress ip, IpAddress netmask, IpAddress gateway) { - Serial << "Connected: " << ip << endl; + Serial << _F("Connected: ") << ip << endl; } +} // namespace + void init() { Serial.begin(SERIAL_BAUD_RATE); // 115200 by default @@ -40,8 +48,9 @@ void init() WifiEvents.onStationGotIP(gotIP); WifiAccessPoint.enable(false); WifiStation.enable(true); + // automatic (acts as the sample callback above) - //WifiStation.smartConfigStart(SCT_EspTouch); + // WifiStation.smartConfigStart(SCT_EspTouch); // manual, use callback above WifiStation.smartConfigStart(SCT_EspTouch, smartConfigCallback); } diff --git a/samples/Basic_Ssl/app/application.cpp b/samples/Basic_Ssl/app/application.cpp index 6ee2a57197..d6c16c1b51 100644 --- a/samples/Basic_Ssl/app/application.cpp +++ b/samples/Basic_Ssl/app/application.cpp @@ -11,13 +11,35 @@ #define WIFI_PWD "PleaseEnterPass" #endif -Timer procTimer; +namespace +{ +// Use the Gibson Research fingerprints web page as an example. Unlike Google, these fingerprints change very infrequently. +DEFINE_FSTR(REQUEST_URL, "https://www.grc.com/fingerprints.htm") + +const Ssl::Fingerprint::Cert::Sha1 sha1Fingerprint PROGMEM = { + 0xA6, 0x8F, 0x8C, 0x47, 0x6B, 0xD0, 0xDE, 0x9E, 0x1D, 0x18, + 0x4A, 0x0A, 0x51, 0x4D, 0x90, 0x11, 0x31, 0x93, 0x40, 0x6D, + +}; + +const Ssl::Fingerprint::Cert::Sha256 certSha256Fingerprint PROGMEM = { + 0xDA, 0x2C, 0xF7, 0x62, 0x09, 0x2C, 0x0A, 0x7B, 0x88, 0xA0, 0xDA, 0x65, 0xF9, 0x29, 0x45, 0x97, + 0xA6, 0xDB, 0xAA, 0x2C, 0x80, 0xFD, 0x75, 0x0A, 0xD9, 0xA0, 0x75, 0xEE, 0x64, 0xEE, 0x06, 0x68, +}; + +const Ssl::Fingerprint::Pki::Sha256 publicKeyFingerprint PROGMEM = { + // Out of date + 0xad, 0xcc, 0x21, 0x92, 0x8e, 0x65, 0xc7, 0x54, 0xac, 0xac, 0xb8, 0x2f, 0x12, 0x95, 0x2e, 0x19, + 0x7d, 0x15, 0x7e, 0x32, 0xbe, 0x90, 0x27, 0x43, 0xab, 0xfe, 0xf1, 0xf2, 0xf2, 0xe1, 0x9c, 0x35, +}; + HttpClient downloadClient; OneShotFastMs connectTimer; +SimpleTimer heapTimer; void printHeap() { - Serial.println(_F("Heap statistics")); + Serial << _F("Heap statistics") << endl; Serial << _F(" Free bytes: ") << system_get_free_heap_size() << endl; #ifdef ENABLE_MALLOC_COUNT Serial << _F(" Used: ") << MallocCount::getCurrent() << endl; @@ -61,24 +83,6 @@ void grcSslInit(Ssl::Session& session, HttpRequest& request) { debug_i("Initialising SSL session for GRC"); - // Use the Gibson Research fingerprints web page as an example. Unlike Google, these fingerprints change very infrequently. - static const Ssl::Fingerprint::Cert::Sha1 sha1Fingerprint PROGMEM = { - 0xA6, 0x8F, 0x8C, 0x47, 0x6B, 0xD0, 0xDE, 0x9E, 0x1D, 0x18, - 0x4A, 0x0A, 0x51, 0x4D, 0x90, 0x11, 0x31, 0x93, 0x40, 0x6D, - - }; - - static const Ssl::Fingerprint::Cert::Sha256 certSha256Fingerprint PROGMEM = { - 0xDA, 0x2C, 0xF7, 0x62, 0x09, 0x2C, 0x0A, 0x7B, 0x88, 0xA0, 0xDA, 0x65, 0xF9, 0x29, 0x45, 0x97, - 0xA6, 0xDB, 0xAA, 0x2C, 0x80, 0xFD, 0x75, 0x0A, 0xD9, 0xA0, 0x75, 0xEE, 0x64, 0xEE, 0x06, 0x68, - }; - - static const Ssl::Fingerprint::Pki::Sha256 publicKeyFingerprint PROGMEM = { - // Out of date - 0xad, 0xcc, 0x21, 0x92, 0x8e, 0x65, 0xc7, 0x54, 0xac, 0xac, 0xb8, 0x2f, 0x12, 0x95, 0x2e, 0x19, - 0x7d, 0x15, 0x7e, 0x32, 0xbe, 0x90, 0x27, 0x43, 0xab, 0xfe, 0xf1, 0xf2, 0xf2, 0xe1, 0x9c, 0x35, - }; - // Trust certificate only if it matches the SHA1 fingerprint... session.validators.pin(sha1Fingerprint); @@ -97,12 +101,11 @@ void grcSslInit(Ssl::Session& session, HttpRequest& request) void gotIP(IpAddress ip, IpAddress netmask, IpAddress gateway) { - Serial.print(F("Connected. Got IP: ")); - Serial.println(ip); + Serial << _F("Connected. Got IP: ") << ip << endl; connectTimer.start(); - auto request = new HttpRequest(F("https://www.grc.com/fingerprints.htm")); + auto request = new HttpRequest(String(REQUEST_URL)); request->onSslInit(grcSslInit); request->onRequestComplete(onDownload); request->setResponseStream(new CounterStream); @@ -114,6 +117,8 @@ void connectFail(const String& ssid, MacAddress bssid, WifiDisconnectReason reas Serial.println(F("I'm NOT CONNECTED!")); } +} // namespace + void init() { Serial.begin(SERIAL_BAUD_RATE); @@ -124,8 +129,7 @@ void init() MallocCount::enableLogging(true); #endif - auto heapTimer = new Timer; - heapTimer->initializeMs<5000>(printHeap).start(); + heapTimer.initializeMs<5000>(printHeap).start(); // Setup the WIFI connection WifiStation.enable(true); diff --git a/samples/Basic_WiFi/app/application.cpp b/samples/Basic_WiFi/app/application.cpp index 8b1f26c94f..7de448f2ee 100644 --- a/samples/Basic_WiFi/app/application.cpp +++ b/samples/Basic_WiFi/app/application.cpp @@ -6,6 +6,8 @@ #define WIFI_PWD "PleaseEnterPass" #endif +namespace +{ // Will be called when WiFi station network scan was completed void listNetworks(bool succeeded, BssList& list) { @@ -60,6 +62,8 @@ void ready() } } +} // namespace + void init() { Serial.begin(SERIAL_BAUD_RATE); diff --git a/samples/CanBus/app/application.cpp b/samples/CanBus/app/application.cpp index 0234267eae..e36dbe58fd 100644 --- a/samples/CanBus/app/application.cpp +++ b/samples/CanBus/app/application.cpp @@ -3,9 +3,11 @@ #include #include -unsigned long rxId = 0; -uint8_t len = 0; -uint8_t rxBuf[8] = {0}; +namespace +{ +unsigned long rxId; +uint8_t len; +uint8_t rxBuf[8]; uint8_t txBuf0[] = {0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55}; uint8_t txBuf1[] = {0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA}; @@ -13,7 +15,7 @@ uint8_t txBuf1[] = {0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA}; MCP_CAN canBus0(10); // CAN0 interface using CS on digital pin 10 MCP_CAN canBus1(9); // CAN1 interface using CS on digital pin 9 -Timer loopTimer; +SimpleTimer loopTimer; SPISettings spiSettings{8000000, MSBFIRST, SPI_MODE3}; @@ -29,6 +31,8 @@ void loop() } } +} // namespace + void init() { Serial.begin(SERIAL_BAUD_RATE); diff --git a/samples/Compass_HMC5883L/app/application.cpp b/samples/Compass_HMC5883L/app/application.cpp index b0a8ea9064..ee674bb2d3 100644 --- a/samples/Compass_HMC5883L/app/application.cpp +++ b/samples/Compass_HMC5883L/app/application.cpp @@ -1,15 +1,34 @@ #include #include +namespace +{ // class default I2C address is 0x1E // specific I2C addresses may be passed as a parameter here // this device only supports one I2C address (0x1E) HMC5883L mag; int16_t mx, my, mz; -Timer procTimer; +SimpleTimer procTimer; + +void readCompass() +{ + // read raw heading measurements from device + mag.getHeading(&mx, &my, &mz); + + // To calculate heading in degrees. 0 degree indicates North + float heading = atan2(my, mx); + if(heading < 0) { + heading += 2 * PI; + } else if(heading > 2 * PI) { + heading -= 2 * PI; + } + + // display tab-separated gyro x/y/z values and heading + Serial << "mag:\t" << mx << '\t' << my << '\t' << mz << "\theading:\t" << heading * RAD_TO_DEG << endl; +} -void readCompass(); +} // namespace void init() { @@ -30,20 +49,3 @@ void init() // Start reading loop procTimer.initializeMs<100>(readCompass).start(); } - -void readCompass() -{ - // read raw heading measurements from device - mag.getHeading(&mx, &my, &mz); - - // To calculate heading in degrees. 0 degree indicates North - float heading = atan2(my, mx); - if(heading < 0) { - heading += 2 * PI; - } else if(heading > 2 * PI) { - heading -= 2 * PI; - } - - // display tab-separated gyro x/y/z values and heading - Serial << "mag:\t" << mx << '\t' << my << '\t' << mz << "\theading:\t" << heading * RAD_TO_DEG << endl; -} diff --git a/samples/DFPlayerMini/app/application.cpp b/samples/DFPlayerMini/app/application.cpp index 9d2c9df707..29580239b4 100644 --- a/samples/DFPlayerMini/app/application.cpp +++ b/samples/DFPlayerMini/app/application.cpp @@ -3,37 +3,40 @@ #define GPIO_LED 2 -Timer timerDFPlayer; -Timer timerLedBlink; -DFRobotDFPlayerMini player; -bool ledState = true; - -void blink() +namespace { - digitalWrite(GPIO_LED, ledState); - ledState = !ledState; -} +SimpleTimer timer; +DFRobotDFPlayerMini player; void nextSong() { player.next(); } -void init() +void checkReady() { - Serial.begin(9600); + static bool ledState; - pinMode(GPIO_LED, OUTPUT); - timerLedBlink.initializeMs<100>(blink).start(); + ledState = !ledState; + digitalWrite(GPIO_LED, ledState); - while(!player.begin(Serial)) { - delay(500); + if(!player.begin(Serial)) { + return; } - timerLedBlink.stop(); digitalWrite(GPIO_LED, 0); player.volume(15); - timerDFPlayer.initializeMs<10000>(nextSong).start(); + timer.initializeMs<10000>(nextSong).start(); +} + +} // namespace + +void init() +{ + Serial.begin(9600); + + pinMode(GPIO_LED, OUTPUT); + timer.initializeMs<250>(checkReady).start(); } diff --git a/samples/DS3232RTC_NTP_Setter/app/application.cpp b/samples/DS3232RTC_NTP_Setter/app/application.cpp index 802812dbfe..fba95fbf86 100644 --- a/samples/DS3232RTC_NTP_Setter/app/application.cpp +++ b/samples/DS3232RTC_NTP_Setter/app/application.cpp @@ -1,5 +1,4 @@ #include - #include #include @@ -9,39 +8,44 @@ #define WIFI_PWD "ENTER_YOUR_PASSWORD" #endif +namespace +{ // Change as required static constexpr uint8_t SDA_PIN{4}; static constexpr uint8_t SCL_PIN{5}; void onNtpReceive(NtpClient& client, time_t timestamp); -Timer printTimer; - NtpClient ntpClient(onNtpReceive); +SimpleTimer printTimer; void onPrintSystemTime() { DateTime rtcNow = DSRTC.get(); Serial.println(_F("Current time")); - Serial << _F(" System(LOCAL TZ): ") << SystemClock.getSystemTimeString() << endl; - Serial << _F(" UTC(UTC TZ): ") << SystemClock.getSystemTimeString(eTZ_UTC) << endl; - Serial << _F(" DSRTC(UTC TZ): ") << rtcNow.toFullDateTimeString() << endl; + Serial << _F(" System (LOCAL TZ): ") << SystemClock.getSystemTimeString() << endl; + Serial << _F(" UTC (UTC TZ): ") << SystemClock.getSystemTimeString(eTZ_UTC) << endl; + Serial << _F(" DSRTC (UTC TZ): ") << rtcNow.toFullDateTimeString() << endl; } void onNtpReceive(NtpClient& client, time_t timestamp) { - SystemClock.setTime(timestamp, eTZ_UTC); //System timezone is LOCAL so to set it from UTC we specify TZ - DSRTC.set(timestamp); //DSRTC timezone is UTC so we need TZ-correct DSRTC.get() + // Both System and DSRTC timezones are UTC + SystemClock.setTime(timestamp, eTZ_UTC); + DSRTC.set(timestamp); + // Display LOCAL time Serial << _F("Time synchronized: ") << SystemClock.getSystemTimeString() << endl; } void gotIP(IpAddress ip, IpAddress netmask, IpAddress gateway) { - debug_i("Got IP %s", ip.toString().c_str()); + Serial << _F("Got IP ") << ip << endl; ntpClient.requestTime(); } +} // namespace + void init() { Serial.begin(SERIAL_BAUD_RATE); diff --git a/samples/Distance_Vl53l0x/app/application.cpp b/samples/Distance_Vl53l0x/app/application.cpp index 3a5e97a96b..2a5e2251c0 100644 --- a/samples/Distance_Vl53l0x/app/application.cpp +++ b/samples/Distance_Vl53l0x/app/application.cpp @@ -1,15 +1,16 @@ #include #include -Adafruit_VL53L0X lox; - // GPIO - NodeMCU pins #define SDA 4 // D2 #define SCL 5 // D1 #define XSHUT 14 // D5 #define INT 12 // D6 -Timer loopTimer; +namespace +{ +Adafruit_VL53L0X lox; +SimpleTimer loopTimer; void loop() { @@ -25,6 +26,8 @@ void loop() } } +} // namespace + void init() { Serial.begin(SERIAL_BAUD_RATE); // 115200 by default diff --git a/samples/DnsCaptivePortal/app/application.cpp b/samples/DnsCaptivePortal/app/application.cpp index c06c947f1f..5e599afcc2 100644 --- a/samples/DnsCaptivePortal/app/application.cpp +++ b/samples/DnsCaptivePortal/app/application.cpp @@ -1,14 +1,16 @@ #include +#define DNS_PORT 53 + +namespace +{ DnsServer dnsServer; HttpServer server; -#define DNS_PORT 53 - void onIndex(HttpRequest& request, HttpResponse& response) { response.setContentType(MIME_HTML); - response.sendString("SMING captive portal"); + response.sendString(F("SMING captive portal")); } void startWebServer() @@ -25,6 +27,8 @@ void startServers() startWebServer(); } +} // namespace + void init() { Serial.begin(SERIAL_BAUD_RATE); // 115200 by default @@ -33,7 +37,7 @@ void init() // Start AP WifiStation.enable(false); WifiAccessPoint.enable(true); - WifiAccessPoint.config("DNSCaptive Portal", "", AUTH_OPEN); + WifiAccessPoint.config(F("DNSCaptive Portal"), nullptr, AUTH_OPEN); System.onReady(startServers); } diff --git a/samples/Echo_Ssl/app/application.cpp b/samples/Echo_Ssl/app/application.cpp index 348cede4e6..87f582a348 100644 --- a/samples/Echo_Ssl/app/application.cpp +++ b/samples/Echo_Ssl/app/application.cpp @@ -19,7 +19,9 @@ #define SERVER_IP "127.0.0.1" #endif -Timer procTimer; +namespace +{ +SimpleTimer procTimer; TcpClient* client; bool showMeta = true; @@ -53,12 +55,14 @@ void connectFail(const String& ssid, MacAddress bssid, WifiDisconnectReason reas void gotIP(IpAddress ip, IpAddress netmask, IpAddress gateway) { - debugf("IP: %s", ip.toString().c_str()); + Serial << _F("Got IP ") << ip << endl; client = new TcpClient(TcpClientDataDelegate(onReceive)); client->setSslInitHandler([](Ssl::Session& session) { session.options.verifyLater = true; }); client->connect(IpAddress(SERVER_IP), 4444, true); } +} // namespace + void init() { Serial.begin(SERIAL_BAUD_RATE); @@ -66,7 +70,7 @@ void init() // Setup the WIFI connection WifiStation.enable(true); - WifiStation.config(WIFI_SSID, WIFI_PWD); // Put your SSID and password here + WifiStation.config(WIFI_SSID, WIFI_PWD); // Run our method when station was connected to AP (or not connected) WifiEvents.onStationDisconnect(connectFail); diff --git a/samples/FtpServer_Files/app/application.cpp b/samples/FtpServer_Files/app/application.cpp index 0b659e560b..1071126279 100644 --- a/samples/FtpServer_Files/app/application.cpp +++ b/samples/FtpServer_Files/app/application.cpp @@ -6,11 +6,13 @@ #define WIFI_PWD "PleaseEnterPass" #endif +namespace +{ FtpServer ftp; void gotIP(IpAddress ip, IpAddress netmask, IpAddress gateway) { - Serial << "IP: " << ip << endl; + Serial << _F("Got IP ") << ip << endl; // Start FTP server ftp.listen(21); // Add user accounts @@ -26,6 +28,8 @@ void connectFail(const String& ssid, MacAddress bssid, WifiDisconnectReason reas Serial.println(_F("I'm NOT CONNECTED. Need help!!! :(")); } +} // namespace + void init() { Serial.begin(SERIAL_BAUD_RATE); // 115200 by default diff --git a/samples/Gesture_APDS-9960/app/application.cpp b/samples/Gesture_APDS-9960/app/application.cpp index 3018d2b7c0..6dbdd3ce12 100644 --- a/samples/Gesture_APDS-9960/app/application.cpp +++ b/samples/Gesture_APDS-9960/app/application.cpp @@ -2,48 +2,58 @@ #include -SparkFun_APDS9960 apds = SparkFun_APDS9960(); +namespace +{ +SparkFun_APDS9960 apds; // For I2C // Default I2C pins 0 and 2. Pin 4 - interrupt pin // -#define APDS9960_INT 4 // Needs to be an interrupt pin +#define APDS9960_INT 4 + +String dirToString(unsigned dir) +{ + switch(dir) { + case DIR_NONE: + return F("NONE"); + case DIR_LEFT: + return F("LEFT"); + case DIR_RIGHT: + return F("RIGHT"); + case DIR_UP: + return F("UP"); + case DIR_DOWN: + return F("DOWN"); + case DIR_NEAR: + return F("NEAR"); + case DIR_FAR: + return F("FAR"); + case DIR_ALL: + return F("ALL"); + default: + return nullptr; + } +} void handleGesture() { - if(apds.isGestureAvailable()) { - switch(apds.readGesture()) { - case DIR_UP: - Serial.println(_F("UP")); - break; - case DIR_DOWN: - Serial.println(_F("DOWN")); - break; - case DIR_LEFT: - Serial.println(_F("LEFT")); - break; - case DIR_RIGHT: - Serial.println(_F("RIGHT")); - break; - case DIR_NEAR: - Serial.println(_F("NEAR")); - break; - case DIR_FAR: - Serial.println(_F("FAR")); - break; - default: - Serial.println(_F("NONE")); - } + if(!apds.isGestureAvailable()) { + return; } + + auto dir = apds.readGesture(); + Serial.println(dirToString(dir)); } -void interruptRoutine() +void interruptDelegate() { detachInterrupt(APDS9960_INT); handleGesture(); - attachInterrupt(APDS9960_INT, InterruptDelegate(interruptRoutine), FALLING); + attachInterrupt(APDS9960_INT, InterruptDelegate(interruptDelegate), FALLING); } +} // namespace + void init() { Serial.begin(SERIAL_BAUD_RATE); // 115200 by default @@ -61,20 +71,22 @@ void init() "--------------------------------\r\n")); // Initialize APDS-9960 (configure I2C and initial values) - if(apds.init()) { - Serial.println(_F("APDS-9960 initialization complete")); - } else { + if(!apds.init()) { Serial.println(_F("Something went wrong during APDS-9960 init!")); + return; } + Serial.println(_F("APDS-9960 initialization complete")); + // Start running the APDS-9960 gesture sensor engine - if(apds.enableGestureSensor(true)) { - Serial.println(_F("Gesture sensor is now running")); - } else { + if(!apds.enableGestureSensor(true)) { Serial.println(_F("Something went wrong during gesture sensor init!")); + return; } + Serial.println(_F("Gesture sensor is now running")); + // Initialize interrupt service routine - pinMode(APDS9960_INT, (GPIO_INT_TYPE)GPIO_PIN_INTR_ANYEDGE); - attachInterrupt(APDS9960_INT, InterruptDelegate(interruptRoutine), FALLING); + pinMode(APDS9960_INT, GPIO_PIN_INTR_ANYEDGE); + attachInterrupt(APDS9960_INT, InterruptDelegate(interruptDelegate), FALLING); } diff --git a/samples/HttpClient/app/application.cpp b/samples/HttpClient/app/application.cpp index ed50d78ad8..ed64810044 100644 --- a/samples/HttpClient/app/application.cpp +++ b/samples/HttpClient/app/application.cpp @@ -7,19 +7,25 @@ #define WIFI_PWD "PleaseEnterPass" #endif +namespace +{ HttpClient httpClient; int onDownload(HttpConnection& connection, bool success) { - debugf("\n=========[ URL: %s ]============", connection.getRequest()->uri.toString().c_str()); - debugf("RemoteIP: %s", connection.getRemoteIp().toString().c_str()); - debugf("Got response code: %d", connection.getResponse()->code); - debugf("Success: %d", success); - if(connection.getRequest()->method != HTTP_HEAD) { + auto& request = *connection.getRequest(); + auto& response = *connection.getResponse(); + + Serial << endl << _F("=========[ URL: ") << request.uri << _F(" ]============") << endl; + Serial << _F("RemoteIP: ") << connection.getRemoteIp() << endl; + Serial << _F("Got response code: ") << response.code << " " << toString(response.code) << endl; + Serial << _F("Success: ") << success << " " << (success ? "OK" : "FAIL") << endl; + + if(request.method != HTTP_HEAD) { Serial.print(_F("Got content: ")); - auto stream = connection.getResponse()->stream; + auto stream = response.stream; if(stream == nullptr || stream->available() == 0) { - Serial.println("EMPTY!"); + Serial.println(_F("EMPTY!")); } else { Serial.copyFrom(stream); Serial.println(); @@ -146,6 +152,8 @@ void connectOk(IpAddress ip, IpAddress mask, IpAddress gateway) httpClient.send(putRequest); } +} // namespace + void init() { Serial.begin(SERIAL_BAUD_RATE); diff --git a/samples/HttpClient_Instapush/app/application.cpp b/samples/HttpClient_Instapush/app/application.cpp index 5575d65e95..12e1f22de0 100644 --- a/samples/HttpClient_Instapush/app/application.cpp +++ b/samples/HttpClient_Instapush/app/application.cpp @@ -1,6 +1,5 @@ #include -#include -#include +#include /*** Direct PUSH notifications on your mobile phone! * @@ -14,29 +13,27 @@ * 3. Update Application ID and Application Secret below: */ -#define APP_ID "55719abba4c48a802c881205" -#define APP_SECRET "5300adbe3f906938950fc0cdbc301986" - // If you want, you can define WiFi settings globally in Eclipse Environment Variables #ifndef WIFI_SSID #define WIFI_SSID "PleaseEnterSSID" // Put your SSID and password here #define WIFI_PWD "PleaseEnterPass" #endif -class InstapushTrackers : public HashMap +namespace { -}; +DEFINE_FSTR(APP_ID, "55719abba4c48a802c881205") +DEFINE_FSTR(APP_SECRET, "5300adbe3f906938950fc0cdbc301986") + +using InstapushTrackers = HashMap; class InstapushApplication : protected HttpClient { public: - InstapushApplication(String appId, String appSecret) + InstapushApplication(const String& appId, const String& appSecret) : app(appId), secret(appSecret) { - app = appId; - secret = appSecret; } - void notify(String event, InstapushTrackers& trackersInfo) + void notify(const String& event, InstapushTrackers& trackersInfo) { debugf("preparing request"); @@ -47,7 +44,8 @@ class InstapushApplication : protected HttpClient requestHeaders[F("x-instapush-appid")] = app; requestHeaders[F("x-instapush-appsecret")] = secret; - DynamicJsonDocument root(1024); + auto stream = new JsonObjectStream(1024); + auto root = stream->getRoot(); root["event"] = event; JsonObject trackers = root.createNestedObject("trackers"); for(auto info : trackersInfo) { @@ -55,8 +53,6 @@ class InstapushApplication : protected HttpClient trackers[info.key()] = info.value(); } - auto stream = new MemoryDataStream; - Json::serialize(root, stream); request->setBody(stream); request->onRequestComplete(RequestCompletedDelegate(&InstapushApplication::processed, this)); @@ -73,10 +69,10 @@ class InstapushApplication : protected HttpClient private: String app; String secret; - const char* url = "http://api.instapush.im/v1/post"; + DEFINE_FSTR_LOCAL(url, "http://api.instapush.im/v1/post"); }; -Timer procTimer; +SimpleTimer procTimer; InstapushApplication pusher(APP_ID, APP_SECRET); // Publish our message @@ -98,6 +94,8 @@ void gotIP(IpAddress ip, IpAddress netmask, IpAddress gateway) procTimer.initializeMs<10 * 1000>(publishMessage).start(); } +} // namespace + void init() { Serial.begin(SERIAL_BAUD_RATE); // 115200 by default diff --git a/samples/HttpClient_ThingSpeak/app/application.cpp b/samples/HttpClient_ThingSpeak/app/application.cpp index 5169fd4aa2..e11ca1f162 100644 --- a/samples/HttpClient_ThingSpeak/app/application.cpp +++ b/samples/HttpClient_ThingSpeak/app/application.cpp @@ -6,8 +6,10 @@ #define WIFI_PWD "PleaseEnterPass" #endif -Timer procTimer; -int sensorValue = 0; +namespace +{ +SimpleTimer procTimer; +int sensorValue; HttpClient thingSpeak; int onDataSent(HttpConnection& client, bool successful) @@ -40,8 +42,8 @@ void sendData() */ Url url; url.Scheme = URI_SCHEME_HTTP; - url.Host = "api.thingspeak.com"; - url.Path = "/update"; + url.Host = F("api.thingspeak.com"); + url.Path = F("/update"); url.Query["key"] = F("7XXUJWCWYTMXKN3L"); url.Query["field1"] = String(sensorValue); thingSpeak.downloadString(url, onDataSent); @@ -65,6 +67,8 @@ void gotIP(IpAddress ip, IpAddress netmask, IpAddress gateway) procTimer.initializeMs<25 * 1000>(sendData).start(); // every 25 seconds } +} // namespace + void init() { Serial.begin(SERIAL_BAUD_RATE); // 115200 by default diff --git a/samples/HttpServer_Bootstrap/app/application.cpp b/samples/HttpServer_Bootstrap/app/application.cpp index bf75ae9fba..cdc1fdbb99 100644 --- a/samples/HttpServer_Bootstrap/app/application.cpp +++ b/samples/HttpServer_Bootstrap/app/application.cpp @@ -8,8 +8,11 @@ #define LED_PIN 0 // GPIO number +namespace +{ HttpServer server; int counter = 0; +HttpClient downloadClient; void onIndex(HttpRequest& request, HttpResponse& response) { @@ -56,26 +59,24 @@ void startWebServer() server.paths.set("/hello", onHello); server.paths.setDefault(onFile); - Serial.println(_F("\r\n" - "=== WEB SERVER STARTED ===")); - Serial.println(WifiStation.getIP()); - Serial.println(_F("==========================\r\n")); + Serial << endl + << _F("=== WEB SERVER STARTED ===") << endl + << WifiStation.getIP() << endl + << _F("==========================") << endl + << endl; } -Timer downloadTimer; -HttpClient downloadClient; -int dowfid = 0; void downloadContentFiles() { downloadClient.downloadFile(F("http://simple.anakod.ru/templates/index.html")); downloadClient.downloadFile(F("http://simple.anakod.ru/templates/bootstrap.css.gz")); downloadClient.downloadFile(F("http://simple.anakod.ru/templates/jquery.js.gz"), - (RequestCompletedDelegate)([](HttpConnection& connection, bool success) -> int { + [](HttpConnection& connection, bool success) -> int { if(success) { startWebServer(); } return 0; - })); + }); } void gotIP(IpAddress ip, IpAddress netmask, IpAddress gateway) @@ -88,15 +89,17 @@ void gotIP(IpAddress ip, IpAddress netmask, IpAddress gateway) } } +} // namespace + void init() { + Serial.begin(SERIAL_BAUD_RATE); // 115200 by default + Serial.systemDebugOutput(true); // Enable debug output to serial + spiffs_mount(); // Mount file system, in order to work with files pinMode(LED_PIN, OUTPUT); - Serial.begin(SERIAL_BAUD_RATE); // 115200 by default - Serial.systemDebugOutput(true); // Enable debug output to serial - WifiStation.enable(true); WifiStation.config(WIFI_SSID, WIFI_PWD); WifiAccessPoint.enable(false); diff --git a/samples/HttpServer_ConfigNetwork/app/application.cpp b/samples/HttpServer_ConfigNetwork/app/application.cpp index c3649efd57..2301a5257e 100644 --- a/samples/HttpServer_ConfigNetwork/app/application.cpp +++ b/samples/HttpServer_ConfigNetwork/app/application.cpp @@ -5,18 +5,25 @@ namespace { +ApplicationSettingsStorage AppSettings; + HttpServer server; FtpServer ftp; HashMap networks; -String network, password; -Timer connectionTimer; +String network; +String password; +SimpleTimer connectionTimer; String lastModified; SimpleTimer scanTimer; +DEFINE_FSTR(DEFAULT_IP, "192.168.1.77") +DEFINE_FSTR(DEFAULT_NETMASK, "255.255.255.0") +DEFINE_FSTR(DEFAULT_GATEWAY, "192.168.1.1") + // Instead of using a SPIFFS file, here we demonstrate usage of imported Flash Strings IMPORT_FSTR_LOCAL(flashSettings, PROJECT_DIR "/web/build/settings.html") @@ -31,7 +38,7 @@ void onIndex(HttpRequest& request, HttpResponse& response) int onIpConfig(HttpServerConnection& connection, HttpRequest& request, HttpResponse& response) { if(request.method == HTTP_POST) { - debugf("Request coming from IP: %s", connection.getRemoteIp().toString().c_str()); + Serial << _F("Request coming from IP: ") << connection.getRemoteIp() << endl; // If desired you can also limit the access based on remote IP. Example below: // if(IpAddress("192.168.4.23") != connection.getRemoteIp()) { // return 1; // error @@ -41,7 +48,7 @@ int onIpConfig(HttpServerConnection& connection, HttpRequest& request, HttpRespo AppSettings.ip = request.getPostParameter("ip"); AppSettings.netmask = request.getPostParameter("netmask"); AppSettings.gateway = request.getPostParameter("gateway"); - debugf("Updating IP settings: %d", AppSettings.ip.isNull()); + Serial << _F("Updating IP settings: ") << AppSettings.ip.isNull() << endl; AppSettings.save(); } @@ -54,18 +61,13 @@ int onIpConfig(HttpServerConnection& connection, HttpRequest& request, HttpRespo auto& vars = tmpl->variables(); bool dhcp = WifiStation.isEnabledDHCP(); - vars["dhcpon"] = dhcp ? "checked='checked'" : ""; - vars["dhcpoff"] = !dhcp ? "checked='checked'" : ""; + vars["dhcpon"] = dhcp ? _F("checked='checked'") : ""; + vars["dhcpoff"] = !dhcp ? _F("checked='checked'") : ""; - if(!WifiStation.getIP().isNull()) { - vars["ip"] = WifiStation.getIP().toString(); - vars["netmask"] = WifiStation.getNetworkMask().toString(); - vars["gateway"] = WifiStation.getNetworkGateway().toString(); - } else { - vars["ip"] = "192.168.1.77"; - vars["netmask"] = "255.255.255.0"; - vars["gateway"] = "192.168.1.1"; - } + auto set = !WifiStation.getIP().isNull(); + vars["ip"] = set ? WifiStation.getIP().toString() : DEFAULT_IP; + vars["netmask"] = set ? WifiStation.getNetworkMask().toString() : DEFAULT_NETMASK; + vars["gateway"] = set ? WifiStation.getNetworkGateway().toString() : DEFAULT_GATEWAY; response.sendNamedStream(tmpl); // will be automatically deleted @@ -186,9 +188,9 @@ void startWebServer() server.listen(80); #endif server.paths.set("/", onIndex); - server.paths.set("/ipconfig", onIpConfig); - server.paths.set("/ajax/get-networks", onAjaxNetworkList); - server.paths.set("/ajax/connect", onAjaxConnect); + server.paths.set(F("/ipconfig"), onIpConfig); + server.paths.set(F("/ajax/get-networks"), onAjaxNetworkList); + server.paths.set(F("/ajax/connect"), onAjaxConnect); server.paths.setDefault(onFile); } @@ -196,7 +198,7 @@ void startFTP() { if(!fileExist("index.html")) fileSetContent("index.html", - "

Please connect to FTP and upload files from folder 'web/build' (details in code)

"); + F("

Please connect to FTP and upload files from folder 'web/build' (details in code)

")); // Start FTP server ftp.listen(21); diff --git a/samples/HttpServer_ConfigNetwork/include/AppSettings.h b/samples/HttpServer_ConfigNetwork/include/AppSettings.h index 66a32fcb89..0c97b586ef 100644 --- a/samples/HttpServer_ConfigNetwork/include/AppSettings.h +++ b/samples/HttpServer_ConfigNetwork/include/AppSettings.h @@ -5,11 +5,10 @@ * Author: Anakod */ -#include -#include +#pragma once -#ifndef INCLUDE_APPSETTINGS_H_ -#define INCLUDE_APPSETTINGS_H_ +#include +#include #define APP_SETTINGS_FILE ".settings.conf" // leading point for security reasons :) @@ -61,7 +60,3 @@ struct ApplicationSettingsStorage { return fileExist(APP_SETTINGS_FILE); } }; - -static ApplicationSettingsStorage AppSettings; - -#endif /* INCLUDE_APPSETTINGS_H_ */ diff --git a/samples/HttpServer_FirmwareUpload/app/application.cpp b/samples/HttpServer_FirmwareUpload/app/application.cpp index 2dbfe83325..586f3251e8 100644 --- a/samples/HttpServer_FirmwareUpload/app/application.cpp +++ b/samples/HttpServer_FirmwareUpload/app/application.cpp @@ -2,8 +2,15 @@ #include #include #include -#include +// If you want, you can define WiFi settings globally in Eclipse Environment Variables +#ifndef WIFI_SSID +#define WIFI_SSID "PleaseEnterSSID" // Put your SSID and password here +#define WIFI_PWD "PleaseEnterPass" +#endif + +namespace +{ HttpServer server; void onFile(HttpRequest& request, HttpResponse& response) @@ -83,6 +90,8 @@ void startWebServer() server.paths.setDefault(onFile); } +} // namespace + void init() { Serial.begin(SERIAL_BAUD_RATE); // 115200 by default @@ -91,8 +100,8 @@ void init() spiffs_mount(); // Mount file system, in order to work with files WifiStation.enable(true); - //WifiStation.config(WIFI_SSID, WIFI_PWD); - //WifiStation.enableDHCP(true); + WifiStation.config(WIFI_SSID, WIFI_PWD); + // WifiStation.enableDHCP(true); // Run WEB server on system ready System.onReady(startWebServer); diff --git a/samples/HttpServer_Plugins/app/application.cpp b/samples/HttpServer_Plugins/app/application.cpp index 04af70bb83..cc1689288d 100644 --- a/samples/HttpServer_Plugins/app/application.cpp +++ b/samples/HttpServer_Plugins/app/application.cpp @@ -55,8 +55,8 @@ void startWebServer() * * make sure to replace the IP address with the IP address of your HttpServer */ - server->paths.set("/ip-n-auth", echoContentBody, - new ResourceIpAuth(IpAddress("192.168.13.0"), IpAddress("255.255.255.0")), pluginBasicAuth); + server->paths.set("/ip-n-auth", echoContentBody, new ResourceIpAuth(F("192.168.13.0"), F("255.255.255.0")), + pluginBasicAuth); /* * This content coming to this resource is modified on the fly @@ -70,16 +70,18 @@ void startWebServer() */ server->paths.set("/test", echoContentBody, new ContentDecoder()); - Serial.println(F("\r\n=== WEB SERVER STARTED ===")); - Serial.println(WifiStation.getIP()); - Serial.println(F("==============================\r\n")); + Serial << endl + << _F("=== WEB SERVER STARTED ===") << endl + << WifiStation.getIP() << endl + << _F("==============================") << endl + << endl; } // Will be called when WiFi station becomes fully operational void gotIP(IpAddress ip, IpAddress netmask, IpAddress gateway) { startWebServer(); - debug_i("free heap = %u", system_get_free_heap_size()); + Serial << _F("Free heap = ") << system_get_free_heap_size() << endl; } } // namespace diff --git a/samples/HttpServer_WebSockets/app/CUserData.cpp b/samples/HttpServer_WebSockets/app/CUserData.cpp index f12e893988..ea949da3be 100644 --- a/samples/HttpServer_WebSockets/app/CUserData.cpp +++ b/samples/HttpServer_WebSockets/app/CUserData.cpp @@ -1,7 +1,5 @@ #include "CUserData.h" -//Simplified container modelling a user session - void CUserData::addSession(WebsocketConnection& connection) { activeWebSockets.addElement(&connection); @@ -10,27 +8,17 @@ void CUserData::addSession(WebsocketConnection& connection) void CUserData::removeSession(WebsocketConnection& connection) { - int i = activeWebSockets.indexOf(&connection); - if(i < 0) { - return; + if(activeWebSockets.removeElement(&connection)) { + connection.setUserData(nullptr); + Serial.println(F("Removed user session")); } - - activeWebSockets[i]->setUserData(nullptr); - activeWebSockets.remove(i); - Serial.println(F("Removed user session")); } void CUserData::printMessage(WebsocketConnection& connection, const String& msg) { - unsigned i = 0; - for(; i < activeWebSockets.count(); i++) { - if(connection == *(activeWebSockets[i])) { - break; - } - } - - if(i < activeWebSockets.count()) { - Serial << _F("Received msg on connection ") << i << ": " << msg; + int i = activeWebSockets.indexOf(&connection); + if(i >= 0) { + Serial << _F("Received msg on connection ") << i << ": " << msg << endl; } } diff --git a/samples/HttpServer_WebSockets/app/application.cpp b/samples/HttpServer_WebSockets/app/application.cpp index cd98bd93fc..2d0a059c4c 100644 --- a/samples/HttpServer_WebSockets/app/application.cpp +++ b/samples/HttpServer_WebSockets/app/application.cpp @@ -2,7 +2,7 @@ #include #include "CUserData.h" -#if ENABLE_CMD_HANDLER +#ifdef ENABLE_CMD_HANDLER #include CommandProcessing::Handler commandHandler; #endif @@ -13,10 +13,12 @@ CommandProcessing::Handler commandHandler; #define WIFI_PWD "PleaseEnterPass" #endif +namespace +{ HttpServer server; -unsigned totalActiveSockets = 0; +unsigned totalActiveSockets; -CUserData userGeorge("George", "I like SMING"); +CUserData userGeorge; void onIndex(HttpRequest& request, HttpResponse& response) { @@ -38,6 +40,14 @@ void onFile(HttpRequest& request, HttpResponse& response) } } +void shutdownServer() +{ + // Don't shutdown immediately, wait a bit to allow messages to propagate + auto timer = new AutoDeleteTimer; + timer->initializeMs<1000>([&]() { server.shutdown(); }); + timer->startOnce(); +} + void wsConnected(WebsocketConnection& socket) { totalActiveSockets++; @@ -58,37 +68,29 @@ void wsMessageReceived(WebsocketConnection& socket, const String& message) if(message == _F("shutdown")) { String message(F("The server is shutting down...")); socket.broadcast(message); - - // Don't shutdown immediately, wait a bit to allow messages to propagate - auto timer = new SimpleTimer; - timer->initializeMs<1000>( - [](void* timer) { - delete static_cast(timer); - server.shutdown(); - }, - timer); - timer->startOnce(); + shutdownServer(); return; } String response = F("Echo: ") + message; socket.sendString(response); - //Normally you would use dynamic cast but just be careful not to convert to wrong object type! - auto user = reinterpret_cast(socket.getUserData()); + // Normally you would use dynamic cast but just be careful not to convert to wrong object type! + auto user = static_cast(socket.getUserData()); if(user != nullptr) { user->printMessage(socket, message); } } -#if ENABLE_CMD_HANDLER +#ifdef ENABLE_CMD_HANDLER void wsCommandReceived(WebsocketConnection& socket, const String& message) { + debug_i("%s(%s)", __FUNCTION__, message.c_str()); String response = commandHandler.processNow(message.c_str(), message.length()); socket.sendString(response); - //Normally you would use dynamic cast but just be careful not to convert to wrong object type! - auto user = reinterpret_cast(socket.getUserData()); + // Normally you would use dynamic cast but just be careful not to convert to wrong object type! + auto user = static_cast(socket.getUserData()); if(user != nullptr) { user->printMessage(socket, message); } @@ -96,15 +98,7 @@ void wsCommandReceived(WebsocketConnection& socket, const String& message) void processShutdownCommand(String commandLine, ReadWriteStream& commandOutput) { - // Don't shutdown immediately, wait a bit to allow messages to propagate - auto timer = new SimpleTimer; - timer->initializeMs<1000>( - [](void* timer) { - delete static_cast(timer); - server.shutdown(); - }, - timer); - timer->startOnce(); + shutdownServer(); } #endif @@ -117,8 +111,8 @@ void wsDisconnected(WebsocketConnection& socket) { totalActiveSockets--; - //Normally you would use dynamic cast but just be careful not to convert to wrong object type! - auto user = reinterpret_cast(socket.getUserData()); + // Normally you would use dynamic cast but just be careful not to convert to wrong object type! + auto user = static_cast(socket.getUserData()); if(user != nullptr) { user->removeSession(socket); } @@ -138,7 +132,7 @@ void startWebServer() auto wsResource = new WebsocketResource(); wsResource->setConnectionHandler(wsConnected); wsResource->setMessageHandler(wsMessageReceived); -#if ENABLE_CMD_HANDLER +#ifdef ENABLE_CMD_HANDLER wsResource->setMessageHandler(wsCommandReceived); #endif @@ -159,21 +153,25 @@ void gotIP(IpAddress ip, IpAddress netmask, IpAddress gateway) startWebServer(); } +} // namespace + void init() { spiffs_mount(); // Mount file system, in order to work with files -#if ENABLE_CMD_HANDLER +#ifdef ENABLE_CMD_HANDLER commandHandler.registerSystemCommands(); commandHandler.registerCommand( {CMDP_STRINGS("shutdown", "Shutdown Server Command", "Application"), processShutdownCommand}); #endif + userGeorge = CUserData{F("George"), F("I like SMING")}; + Serial.begin(SERIAL_BAUD_RATE); // 115200 by default Serial.systemDebugOutput(true); // Enable debug output to serial WifiStation.enable(true); - WifiStation.config(WIFI_SSID, WIFI_PWD); + WifiStation.config(F(WIFI_SSID), F(WIFI_PWD)); WifiAccessPoint.enable(false); // Run our method when station was connected to AP diff --git a/samples/HttpServer_WebSockets/component.mk b/samples/HttpServer_WebSockets/component.mk index 25bbd74f26..4c7fcfebf6 100644 --- a/samples/HttpServer_WebSockets/component.mk +++ b/samples/HttpServer_WebSockets/component.mk @@ -3,8 +3,7 @@ HWCONFIG := spiffs-2m COMPONENT_VARS += ENABLE_CMD_HANDLER ENABLE_CMD_HANDLER ?= 1 -ifeq ($(ENABLE_CMD_HANDLER), 1) - COMPONENT_DEPENDS += CommandProcessing +ifeq ($(ENABLE_CMD_HANDLER),1) +COMPONENT_DEPENDS += CommandProcessing +APP_CFLAGS += -DENABLE_CMD_HANDLER=1 endif - -APP_CFLAGS += -DENABLE_CMD_HANDLER=$(ENABLE_CMD_HANDLER) \ No newline at end of file diff --git a/samples/HttpServer_WebSockets/include/CUserData.h b/samples/HttpServer_WebSockets/include/CUserData.h index 74e5d7024a..bf93d68c2e 100644 --- a/samples/HttpServer_WebSockets/include/CUserData.h +++ b/samples/HttpServer_WebSockets/include/CUserData.h @@ -1,13 +1,16 @@ -#ifndef C_USER_DATA_H_SAMPLE -#define C_USER_DATA_H_SAMPLE +#pragma once #include -//Simplified container modelling a user session +// Simplified container modelling a user session class CUserData { public: - CUserData(const char* uName, const char* uData) : userName(uName), userData(uData) + CUserData() + { + } + + CUserData(const String& uName, const String& uData) : userName(uName), userData(uData) { } @@ -26,5 +29,3 @@ class CUserData String userData; Vector activeWebSockets; }; - -#endif /*C_USER_DATA_H_SAMPLE*/ diff --git a/samples/Humidity_AM2321/app/application.cpp b/samples/Humidity_AM2321/app/application.cpp index ca07d7267d..5b01a7f96b 100644 --- a/samples/Humidity_AM2321/app/application.cpp +++ b/samples/Humidity_AM2321/app/application.cpp @@ -1,9 +1,11 @@ #include #include "Libraries/AM2321/AM2321.h" +namespace +{ AM2321 am2321; -Timer procTimer; +SimpleTimer procTimer; bool state = true; // You can change I2C pins here: @@ -15,6 +17,8 @@ void read() Serial << am2321.read() << ',' << am2321.temperature / 10.0 << ',' << am2321.humidity / 10.0 << endl; } +} // namespace + void init() { Serial.begin(SERIAL_BAUD_RATE); // 115200 by default diff --git a/samples/Humidity_BME280/app/application.cpp b/samples/Humidity_BME280/app/application.cpp index 395d5a62a0..0f8b38eb41 100644 --- a/samples/Humidity_BME280/app/application.cpp +++ b/samples/Humidity_BME280/app/application.cpp @@ -24,8 +24,10 @@ #define SEALEVELPRESSURE_HPA (1013.25) +namespace +{ Adafruit_BME280 bme; -Timer procTimer; +SimpleTimer procTimer; // Will use default pins for selected architecture. You can override values here // #define SDA 4 @@ -40,6 +42,8 @@ void printValues() Serial.println(); } +} // namespace + void init() { Serial.begin(SERIAL_BAUD_RATE); // 115200 by default diff --git a/samples/Humidity_DHT22/app/application.cpp b/samples/Humidity_DHT22/app/application.cpp index c46f85215d..8849ad545c 100644 --- a/samples/Humidity_DHT22/app/application.cpp +++ b/samples/Humidity_DHT22/app/application.cpp @@ -4,24 +4,10 @@ //#define WORK_PIN 14 // GPIO14 #define WORK_PIN 2 -DHTesp dht; - -Timer readTemperatureProcTimer; -void onTimer_readTemperatures(); - -void init() +namespace { - Serial.begin(SERIAL_BAUD_RATE); // 115200 by default - Serial.systemDebugOutput(true); // Allow debug output to serial - - dht.setup(WORK_PIN, DHTesp::DHT22); - readTemperatureProcTimer.initializeMs<5 * 1000>(onTimer_readTemperatures).start(); // every so often. - - Serial.println(_F("\r\n" - "DHT improved lib")); - Serial << _F("TickCount=") << RTC.getRtcNanoseconds() / 1000000 - << _F("; Need to wait 1 second for the sensor to boot up") << endl; -} +DHTesp dht; +SimpleTimer readTemperatureProcTimer; void onTimer_readTemperatures() { @@ -107,10 +93,25 @@ void onTimer_readTemperatures() Serial.print(_F("Cold And Dry")); break; default: - Serial.print(_F("Unknown:")); - Serial.print(cf); + Serial << _F("Unknown:") << cf; break; } Serial.println(')'); } + +} // namespace + +void init() +{ + Serial.begin(SERIAL_BAUD_RATE); // 115200 by default + Serial.systemDebugOutput(true); // Allow debug output to serial + + dht.setup(WORK_PIN, DHTesp::DHT22); + readTemperatureProcTimer.initializeMs<5 * 1000>(onTimer_readTemperatures).start(); // every so often. + + Serial << endl + << _F("DHT improved lib") << endl + << _F("TickCount=") << RTC.getRtcNanoseconds() / 1000000 + << _F("; Need to wait 1 second for the sensor to boot up") << endl; +} diff --git a/samples/Humidity_SI7021/app/application.cpp b/samples/Humidity_SI7021/app/application.cpp index 462a993368..fe189dfdd4 100644 --- a/samples/Humidity_SI7021/app/application.cpp +++ b/samples/Humidity_SI7021/app/application.cpp @@ -2,13 +2,15 @@ #include #include -SI7021 hydrometer; -Timer procTimer_ht; -Timer procTimer_olt; - #define I2C_SCL 5 // SCL #define I2C_SDA 4 // SCA +namespace +{ +SI7021 hydrometer; +SimpleTimer procTimer_ht; +SimpleTimer procTimer_olt; + //LN10 - 2.30258509299404568402 double getDewPoint(unsigned int humidity, int temperature) { @@ -21,7 +23,9 @@ void si_read_ht() { if(!hydrometer.begin()) { Serial.println(_F("Could not connect to SI7021.")); + return; } + Serial.println(_F("Start reading Humidity and Temperature")); si7021_env env_data = hydrometer.getHumidityAndTemperature(); @@ -39,7 +43,9 @@ void si_read_olt() { if(!hydrometer.begin()) { Serial.println(_F("Could not connect to SI7021.")); + return; } + Serial.println(_F("Start reading Temperature")); si7021_olt olt_data = hydrometer.getTemperatureOlt(); @@ -51,13 +57,17 @@ void si_read_olt() } } +} // namespace + void init() { Serial.begin(SERIAL_BAUD_RATE); // 115200 by default Serial.systemDebugOutput(true); // Allow debug output to serial - Serial.print(_F("Start I2c")); + + Serial.println(_F("Start I2c")); Wire.pins(I2C_SDA, I2C_SCL); // SDA, SCL Wire.begin(); + procTimer_ht.initializeMs<10000>(si_read_ht).start(); procTimer_olt.initializeMs<15000>(si_read_olt).start(); } diff --git a/samples/IR_lib/app/application.cpp b/samples/IR_lib/app/application.cpp index 3049acc6c1..1f8ba9c3f5 100644 --- a/samples/IR_lib/app/application.cpp +++ b/samples/IR_lib/app/application.cpp @@ -10,13 +10,14 @@ #define IR_RECV_PIN 12 // GPIO12 #define IR_SEND_PIN 5 // GPIO5 -Timer irTimer; +namespace +{ +SimpleTimer irTimer; IRrecv irrecv(IR_RECV_PIN); IRsend irsend(IR_SEND_PIN); void receiveIR() { - irTimer.stop(); decode_results dresults; dresults.decode_type = UNUSED; if(irrecv.decode(&dresults)) { @@ -28,14 +29,19 @@ void receiveIR() Serial.println("Send IR Code"); irsend.send(dresults.decode_type, dresults.value, dresults.bits); } - irTimer.start(); + irTimer.startOnce(); } +} // namespace + void init() { Serial.begin(SERIAL_BAUD_RATE); // 115200 by default + Serial.println("Setting up..."); + irrecv.enableIRIn(); // Start the receiver - irTimer.initializeMs<200>(receiveIR).start(); + irTimer.initializeMs<200>(receiveIR).startOnce(); + Serial.println("Ready..."); } diff --git a/samples/LED_WS2812/app/application.cpp b/samples/LED_WS2812/app/application.cpp index 7e39358bad..bff8a7e489 100644 --- a/samples/LED_WS2812/app/application.cpp +++ b/samples/LED_WS2812/app/application.cpp @@ -2,18 +2,26 @@ #define LED_PIN 2 // GPIO2 -void init() +namespace { - while(true) { - char buffer1[] = "\x40\x00\x00\x00\x40\x00\x00\x00\x40"; - ws2812_writergb(LED_PIN, buffer1, sizeof(buffer1)); - os_delay_us(500000); - - //We need to feed WDT. - WDT.alive(); +SimpleTimer procTimer; +bool state; +void update() +{ + if(state) { char buffer2[] = "\x00\x40\x40\x40\x00\x40\x40\x40\x00"; ws2812_writergb(LED_PIN, buffer2, sizeof(buffer2)); - os_delay_us(500000); + } else { + char buffer1[] = "\x40\x00\x00\x00\x40\x00\x00\x00\x40"; + ws2812_writergb(LED_PIN, buffer1, sizeof(buffer1)); } + state = !state; +} + +} // namespace + +void init() +{ + procTimer.initializeMs<500>(update).start(); } diff --git a/samples/LED_YeelightBulb/app/application.cpp b/samples/LED_YeelightBulb/app/application.cpp index 3c756b5e46..384d0e1d12 100644 --- a/samples/LED_YeelightBulb/app/application.cpp +++ b/samples/LED_YeelightBulb/app/application.cpp @@ -7,11 +7,13 @@ #define WIFI_PWD "PleaseEnterPass" #endif +namespace +{ // Enter your bulb IP here: YeelightBulb bulb(IpAddress("192.168.1.100")); -Timer procTimer; -bool state = false; +SimpleTimer procTimer; +bool state; void blink() { @@ -27,13 +29,15 @@ void blink() // Will be called when WiFi station was connected to AP void gotIP(IpAddress ip, IpAddress netmask, IpAddress gateway) { - debugf("I'm CONNECTED"); + Serial << _F("I'm CONNECTED, IP ") << ip << endl; // Connection to Yeelight Bulb will be established on any first action: bulb.updateState(); // Read actual bulb state procTimer.initializeMs<5000>(blink).start(); } +} // namespace + void init() { Serial.begin(SERIAL_BAUD_RATE); // 115200 by default diff --git a/samples/Light_BH1750/app/application.cpp b/samples/Light_BH1750/app/application.cpp index 960a4270ab..790e8de670 100644 --- a/samples/Light_BH1750/app/application.cpp +++ b/samples/Light_BH1750/app/application.cpp @@ -1,6 +1,8 @@ #include #include +namespace +{ /* Set the Address pin state to change I2C address: BH1750FVI_ADDRESS_LOW "0x23" - usually by default @@ -8,7 +10,7 @@ BH1750FVI_ADDRESS_HIGH "0x5C" */ BH1750FVI LightSensor(BH1750FVI_ADDRESS_LOW); -Timer procTimer; +SimpleTimer procTimer; void readLight() { @@ -19,15 +21,19 @@ void readLight() Serial.println(" lux"); } +} // namespace + void init() { Serial.begin(SERIAL_BAUD_RATE); // 115200 by default Serial.systemDebugOutput(false); // Disable debug output to serial - if(LightSensor.begin() == 0) - Serial.println(_F("LightSensor initialized")); - else + if(LightSensor.begin() != 0) { Serial.println(_F("LightSensor not available. May be wrong I2C address?")); + return; + } + + Serial.println(_F("LightSensor initialized")); /* Set the Working Mode for this sensor diff --git a/samples/LiquidCrystal_44780/app/application.cpp b/samples/LiquidCrystal_44780/app/application.cpp index 7bf4477424..5462634d21 100644 --- a/samples/LiquidCrystal_44780/app/application.cpp +++ b/samples/LiquidCrystal_44780/app/application.cpp @@ -9,8 +9,54 @@ // For more information visit useful wiki page: http://arduino-info.wikispaces.com/LCD-Blue-I2C #define I2C_LCD_ADDR 0x27 + LiquidCrystal_I2C lcd(I2C_LCD_ADDR, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); +SimpleTimer flashTimer; + +//-------- Write characters on the display ------------------ +void writeDisplay() +{ + debug_d("%s", __FUNCTION__); + + // NOTE: Cursor Position: (CHAR, LINE) start at 0 + lcd.setCursor(0, 0); + lcd.print(_F("SMING: Let's do")); + lcd.setCursor(0, 1); + lcd.print(_F("smart things!")); +} + +// ------- Quick 3 blinks of backlight ------------- +void flashBacklight() +{ + static unsigned state; + + if(state == 6) { + debug_d("%s DONE", __FUNCTION__); + lcd.backlight(); // finish with backlight on + writeDisplay(); + return; + } + + if(state == 0) { + debug_d("%s INIT", __FUNCTION__); + flashTimer.setCallback(flashBacklight); + } + + if(state & 0x01) { + debug_d("%s OFF", __FUNCTION__); + lcd.noBacklight(); + flashTimer.setIntervalMs<250>(); + } else { + debug_d("%s ON", __FUNCTION__); + lcd.backlight(); + flashTimer.setIntervalMs<150>(); + } + + ++state; + flashTimer.startOnce(); +} + void init() { Serial.begin(SERIAL_BAUD_RATE); // 115200 by default @@ -20,19 +66,5 @@ void init() lcd.begin(16, 2); // initialize the lcd for 16 chars 2 lines, turn on backlight - // ------- Quick 3 blinks of backlight ------------- - for(int i = 0; i < 3; i++) { - lcd.backlight(); - delay(150); - lcd.noBacklight(); - delay(250); - } - lcd.backlight(); // finish with backlight on - - //-------- Write characters on the display ------------------ - // NOTE: Cursor Position: (CHAR, LINE) start at 0 - lcd.setCursor(0, 0); - lcd.print(_F("SMING: Let's do")); - lcd.setCursor(0, 1); - lcd.print(_F("smart things!")); + flashBacklight(); } diff --git a/samples/LiveDebug/app/application.cpp b/samples/LiveDebug/app/application.cpp index 0adca0af28..6606f63742 100644 --- a/samples/LiveDebug/app/application.cpp +++ b/samples/LiveDebug/app/application.cpp @@ -8,6 +8,8 @@ #define LED_PIN 2 // Note: LED is attached to UART1 TX output +namespace +{ // Max length of debug command const unsigned MAX_COMMAND_LENGTH = 64; @@ -49,18 +51,15 @@ Timer procTimer; #define CALLBACK_ATTR GDB_IRAM_ATTR #endif -// See blink() -bool ledState = true; - // A simple log file stored on the host -static GdbFileStream logFile; +GdbFileStream logFile; #define LOG_FILENAME "testlog.txt" // Handles messages from SDK -static OsMessageInterceptor osMessageInterceptor; +OsMessageInterceptor osMessageInterceptor; // Supports `consoleOff` command to prevent re-enabling when debugger is attached -bool consoleOffRequested = false; +bool consoleOffRequested; // IFS::Gdb::FileSystem gdbfs; @@ -75,8 +74,10 @@ void readConsole(); */ void CALLBACK_ATTR blink() { - digitalWrite(LED_PIN, ledState); + static bool ledState; + ledState = !ledState; + digitalWrite(LED_PIN, ledState); } void showPrompt() @@ -418,7 +419,7 @@ COMMAND_HANDLER(write0) * @param msg * @retval bool true if we want to report this */ -static bool __noinline parseOsMessage(OsMessage& msg) +bool __noinline parseOsMessage(OsMessage& msg) { m_printf(_F("[OS] %s\r\n"), msg.getBuffer()); if(msg.startsWith(_F("E:M "))) { @@ -436,7 +437,7 @@ static bool __noinline parseOsMessage(OsMessage& msg) * @brief Called when the OS outputs a debug message using os_printf, etc. * @param msg The message */ -static void onOsMessage(OsMessage& msg) +void onOsMessage(OsMessage& msg) { // Note: We do the check in a separate function to avoid messing up the stack pointer if(parseOsMessage(msg)) { @@ -622,6 +623,14 @@ void readConsole() })); } +void printTimerDetails() +{ + Serial << procTimer << ", maxTicks = " << procTimer.maxTicks() + << ", maxTime = " << procTimer.micros().ticksToTime(procTimer.maxTicks()).value() << endl; +} + +} // namespace + extern "C" void gdb_on_attach(bool attached) { debug_i("GdbAttach(%d)", attached); @@ -646,12 +655,6 @@ extern "C" void gdb_on_attach(bool attached) } } -static void printTimerDetails() -{ - Serial << procTimer << ", maxTicks = " << procTimer.maxTicks() - << ", maxTime = " << procTimer.micros().ticksToTime(procTimer.maxTicks()).value() << endl; -} - void GDB_IRAM_ATTR init() { Serial.begin(SERIAL_BAUD_RATE); diff --git a/samples/MeteoControl/app/application.cpp b/samples/MeteoControl/app/application.cpp index 7c335dbf3c..9509a4a92a 100644 --- a/samples/MeteoControl/app/application.cpp +++ b/samples/MeteoControl/app/application.cpp @@ -10,6 +10,11 @@ #include "special_chars.h" #include "webserver.h" +// Sensors string values +String StrT, StrRH, StrTime; + +namespace +{ DHTesp dht; // For more information visit useful wiki page: http://arduino-info.wikispaces.com/LCD-Blue-I2C @@ -17,53 +22,9 @@ DHTesp dht; #define I2C_LCD_ADDR 0x27 LiquidCrystal_I2C lcd(I2C_LCD_ADDR, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); -Timer procTimer; -Timer displayTimer; +SimpleTimer procTimer; +SimpleTimer displayTimer; bool state = true; -// Sensors string values -String StrT, StrRH, StrTime; - -void process(); -void connectOk(const String& SSID, MacAddress bssid, uint8_t channel); -void connectFail(const String& ssid, MacAddress bssid, WifiDisconnectReason reason); -void gotIP(IpAddress ip, IpAddress netmask, IpAddress gateway); - -void init() -{ - spiffs_mount(); // Mount file system, in order to work with files - - Serial.begin(SERIAL_BAUD_RATE); // 115200 by default - Serial.systemDebugOutput(false); // Debug output to serial - - ActiveConfig = loadConfig(); - - // Select control line - pinMode(CONTROL_PIN, OUTPUT); - - // DHT sensor start - dht.setup(DHT_PIN, DHTesp::DHT11); - - lcd.begin(16, 2); - lcd.backlight(); - lcd.createChar(1, icon_termometer); - lcd.createChar(2, icon_water); - lcd.createChar(3, celsius); - lcd.createChar(4, icon_retarrow); - lcd.createChar(5, icon_clock); - lcd.createChar(6, icon_cross); - lcd.createChar(7, icon_check); - - WifiStation.config(ActiveConfig.NetworkSSID, ActiveConfig.NetworkPassword); - WifiStation.enable(true); - WifiAccessPoint.enable(false); - - WifiEvents.onStationConnect(connectOk); - WifiEvents.onStationDisconnect(connectFail); - WifiEvents.onStationGotIP(gotIP); - - procTimer.initializeMs<5000>(process).start(); - process(); -} void showValues() { @@ -93,17 +54,19 @@ void process() float t = dht.getTemperature() + ActiveConfig.AddT; float h = dht.getHumidity() + ActiveConfig.AddRH; - if(ActiveConfig.Trigger == eTT_Temperature) + if(ActiveConfig.Trigger == eTT_Temperature) { state = t < ActiveConfig.RangeMin || t > ActiveConfig.RangeMax; - else if(ActiveConfig.Trigger == eTT_Humidity) + } else if(ActiveConfig.Trigger == eTT_Humidity) { state = h < ActiveConfig.RangeMin || h > ActiveConfig.RangeMax; + } digitalWrite(CONTROL_PIN, state); StrT = String(t, 0); StrRH = String(h, 0); - if(!displayTimer.isStarted()) + if(!displayTimer.isStarted()) { displayTimer.initializeMs<1000>(showValues).start(); + } } void connectOk(const String& SSID, MacAddress bssid, uint8_t channel) @@ -115,8 +78,7 @@ void connectOk(const String& SSID, MacAddress bssid, uint8_t channel) void gotIP(IpAddress ip, IpAddress netmask, IpAddress gateway) { lcd.clear(); - lcd.print("\7 "); - lcd.print(ip); + lcd << "\7 " << ip; // Restart main screen output procTimer.restart(); displayTimer.stop(); @@ -124,10 +86,11 @@ void gotIP(IpAddress ip, IpAddress netmask, IpAddress gateway) startWebClock(); // At first run we will download web server content if(!fileExist("index.html") || !fileExist("config.html") || !fileExist("api.html") || - !fileExist("bootstrap.css.gz") || !fileExist("jquery.js.gz")) + !fileExist("bootstrap.css.gz") || !fileExist("jquery.js.gz")) { downloadContentFiles(); - else + } else { startWebServer(); + } } void connectFail(const String& ssid, MacAddress bssid, WifiDisconnectReason reason) @@ -152,26 +115,31 @@ void connectFail(const String& ssid, MacAddress bssid, WifiDisconnectReason reas } ////// WEB Clock ////// -Timer clockRefresher; + +SimpleTimer clockRefresher; HttpClient clockWebClient; -uint32_t lastClockUpdate = 0; +uint32_t lastClockUpdate; DateTime clockValue; const int clockUpdateIntervalMs = 10 * 60 * 1000; // Update web clock every 10 minutes int onClockUpdating(HttpConnection& client, bool successful) { + auto& response = *client.getResponse(); + if(!successful) { - debugf("CLOCK UPDATE FAILED %d (code: %d)", successful, client.getResponse()->code); + Serial << _F("CLOCK UPDATE FAILED, ") << response.code << " " << toString(response.code) << endl; lastClockUpdate = 0; return -1; } // Extract date header from response - clockValue = client.getResponse()->headers.getServerDate(); - if(clockValue.isNull()) - clockValue = client.getResponse()->headers.getLastModifiedDate(); - if(!clockValue.isNull()) + clockValue = response.headers.getServerDate(); + if(clockValue.isNull()) { + clockValue = response.headers.getLastModifiedDate(); + } + if(!clockValue.isNull()) { clockValue.addMilliseconds(ActiveConfig.AddTZ * 1000 * 60 * 60); + } return 0; } @@ -179,27 +147,65 @@ int onClockUpdating(HttpConnection& client, bool successful) void refreshClockTime() { uint32_t nowClock = millis(); - if(nowClock < lastClockUpdate) + if(nowClock < lastClockUpdate) { lastClockUpdate = 0; // Prevent overflow, restart + } if((lastClockUpdate == 0 || nowClock - lastClockUpdate > clockUpdateIntervalMs)) { clockWebClient.downloadString("google.com", onClockUpdating); lastClockUpdate = nowClock; - } else if(!clockValue.isNull()) + } else if(!clockValue.isNull()) { clockValue.addMilliseconds(clockRefresher.getIntervalMs()); + } - if(!clockValue.isNull()) { - StrTime = clockValue.toShortDateString() + " " + clockValue.toShortTimeString(false); - - if((millis() % 2000) > 1000) - StrTime.setCharAt(13, ' '); - else - StrTime.setCharAt(13, ':'); + if(clockValue.isNull()) { + return; } + + StrTime = clockValue.toShortDateString() + " " + clockValue.toShortTimeString(false); + StrTime.setCharAt(13, ((nowClock % 2000) > 1000) ? ' ' : ':'); } +} // namespace + void startWebClock() { lastClockUpdate = 0; - clockRefresher.stop(); clockRefresher.initializeMs<500>(refreshClockTime).start(); } + +void init() +{ + Serial.begin(SERIAL_BAUD_RATE); // 115200 by default + Serial.systemDebugOutput(false); // Debug output to serial + + spiffs_mount(); // Mount file system, in order to work with files + + ActiveConfig = loadConfig(); + + // Select control line + pinMode(CONTROL_PIN, OUTPUT); + + // DHT sensor start + dht.setup(DHT_PIN, DHTesp::DHT11); + + lcd.begin(16, 2); + lcd.backlight(); + lcd.createChar(1, icon_termometer); + lcd.createChar(2, icon_water); + lcd.createChar(3, celsius); + lcd.createChar(4, icon_retarrow); + lcd.createChar(5, icon_clock); + lcd.createChar(6, icon_cross); + lcd.createChar(7, icon_check); + + WifiStation.config(ActiveConfig.NetworkSSID, ActiveConfig.NetworkPassword); + WifiStation.enable(true); + WifiAccessPoint.enable(false); + + WifiEvents.onStationConnect(connectOk); + WifiEvents.onStationDisconnect(connectFail); + WifiEvents.onStationGotIP(gotIP); + + procTimer.initializeMs<5000>(process).start(); + process(); +} diff --git a/samples/MeteoControl/app/configuration.cpp b/samples/MeteoControl/app/configuration.cpp index 603130b664..fb844d0977 100644 --- a/samples/MeteoControl/app/configuration.cpp +++ b/samples/MeteoControl/app/configuration.cpp @@ -1,7 +1,5 @@ #include "configuration.h" -#include - MeteoConfig ActiveConfig; MeteoConfig loadConfig() @@ -23,8 +21,8 @@ MeteoConfig loadConfig() cfg.RangeMin = trigger["min"]; cfg.RangeMax = trigger["max"]; } else { - cfg.NetworkSSID = WIFI_SSID; - cfg.NetworkPassword = WIFI_PWD; + cfg.NetworkSSID = F(WIFI_SSID); + cfg.NetworkPassword = F(WIFI_PWD); } return cfg; } diff --git a/samples/MeteoControl/app/webserver.cpp b/samples/MeteoControl/app/webserver.cpp index bc1cc12adb..530dda51da 100644 --- a/samples/MeteoControl/app/webserver.cpp +++ b/samples/MeteoControl/app/webserver.cpp @@ -1,11 +1,16 @@ #include - #include "configuration.h" +#include "webserver.h" -bool serverStarted = false; -HttpServer server; extern String StrT, StrRH; // Sensors string values +namespace +{ +HttpServer server; +HttpClient downloadClient; +bool serverStarted = false; +int dowfid = 0; + void onIndex(HttpRequest& request, HttpResponse& response) { TemplateFileStream* tmpl = new TemplateFileStream("index.html"); @@ -92,23 +97,44 @@ void onApiSensors(HttpRequest& request, HttpResponse& response) void onApiOutput(HttpRequest& request, HttpResponse& response) { int val = request.getQueryParameter("control", "-1").toInt(); - if(val == 0 || val == 1) + if(val == 0 || val == 1) { digitalWrite(CONTROL_PIN, val == 1); - else + } else { val = -1; + } JsonObjectStream* stream = new JsonObjectStream(); JsonObject json = stream->getRoot(); - json["status"] = val != -1; - if(val == -1) + json["status"] = val >= 0; + if(val < 0) json["error"] = "Wrong control parameter value, please use: ?control=0|1"; response.sendDataStream(stream, MIME_JSON); } +} // namespace + +void downloadContentFiles() +{ + debugf("DownloadContentFiles"); + + downloadClient.downloadFile(F("http://simple.anakod.ru/templates/MeteoControl/MeteoControl.html"), "index.html"); + downloadClient.downloadFile(F("http://simple.anakod.ru/templates/MeteoControl/MeteoConfig.html"), "config.html"); + downloadClient.downloadFile(F("http://simple.anakod.ru/templates/MeteoControl/MeteoAPI.html"), "api.html"); + downloadClient.downloadFile(F("http://simple.anakod.ru/templates/bootstrap.css.gz")); + downloadClient.downloadFile(F("http://simple.anakod.ru/templates/jquery.js.gz"), + [](HttpConnection& connection, bool success) -> int { + if(success) { + startWebServer(); + } + return 0; + }); +} + void startWebServer() { - if(serverStarted) + if(serverStarted) { return; + } server.listen(80); server.paths.set("/", onIndex); @@ -124,24 +150,3 @@ void startWebServer() if(WifiAccessPoint.isEnabled()) debugf("AP: %s", WifiAccessPoint.getIP().toString().c_str()); } - -/// FileSystem Initialization /// - -HttpClient downloadClient; -int dowfid = 0; -void downloadContentFiles() -{ - debugf("DownloadContentFiles"); - - downloadClient.downloadFile("http://simple.anakod.ru/templates/MeteoControl/MeteoControl.html", "index.html"); - downloadClient.downloadFile("http://simple.anakod.ru/templates/MeteoControl/MeteoConfig.html", "config.html"); - downloadClient.downloadFile("http://simple.anakod.ru/templates/MeteoControl/MeteoAPI.html", "api.html"); - downloadClient.downloadFile("http://simple.anakod.ru/templates/bootstrap.css.gz"); - downloadClient.downloadFile("http://simple.anakod.ru/templates/jquery.js.gz", - (RequestCompletedDelegate)([](HttpConnection& connection, bool success) -> int { - if(success) { - startWebServer(); - } - return 0; - })); -} diff --git a/samples/MeteoControl/include/configuration.h b/samples/MeteoControl/include/configuration.h index d39a298f39..24b490c888 100644 --- a/samples/MeteoControl/include/configuration.h +++ b/samples/MeteoControl/include/configuration.h @@ -1,5 +1,4 @@ -#ifndef INCLUDE_CONFIGURATION_H_ -#define INCLUDE_CONFIGURATION_H_ +#pragma once #include #include @@ -20,7 +19,11 @@ #define METEO_CONFIG_FILE ".meteo.conf" // leading point for security reasons :) -enum TriggerType { eTT_None = 0, eTT_Temperature, eTT_Humidity }; +enum TriggerType { + eTT_None = 0, + eTT_Temperature, + eTT_Humidity, +}; struct MeteoConfig { MeteoConfig() @@ -50,5 +53,3 @@ void saveConfig(MeteoConfig& cfg); extern void startWebClock(); extern MeteoConfig ActiveConfig; - -#endif /* INCLUDE_CONFIGURATION_H_ */ diff --git a/samples/MeteoControl/include/webserver.h b/samples/MeteoControl/include/webserver.h index 9ce09aa046..54f6987fc9 100644 --- a/samples/MeteoControl/include/webserver.h +++ b/samples/MeteoControl/include/webserver.h @@ -1,7 +1,4 @@ -#ifndef INCLUDE_WEBSERVER_H_ -#define INCLUDE_WEBSERVER_H_ +#pragma once void startWebServer(); void downloadContentFiles(); - -#endif /* INCLUDE_WEBSERVER_H_ */ diff --git a/samples/MeteoControl_mqtt/app/application.cpp b/samples/MeteoControl_mqtt/app/application.cpp index 9fb95ca902..5851177202 100644 --- a/samples/MeteoControl_mqtt/app/application.cpp +++ b/samples/MeteoControl_mqtt/app/application.cpp @@ -1,13 +1,14 @@ #include #include - #include "configuration.h" // application configuration -#include "bmp180.cpp" // bmp180 configuration -#include "si7021.cpp" // htu21d configuration +extern void BMPinit(); +extern void SIinit(); -Timer publishTimer; +MqttClient mqtt; +namespace +{ // Publish our message void publishMessage() // uncomment timer in connectOk() if need publishMessage() loop { @@ -26,7 +27,15 @@ int onMessageReceived(MqttClient& client, mqtt_message_t* message) return 0; } -// Run MQTT client +void gotIP(IpAddress ip, IpAddress netmask, IpAddress gateway) +{ + Serial << _F("Connected: ") << ip << endl; + startMqttClient(); + publishMessage(); // run once publishMessage +} + +} // namespace + void startMqttClient() { Url url(URI_SCHEME_MQTT, F(LOG), F(PASS), F(MQTT_SERVER), MQTT_PORT); @@ -35,13 +44,6 @@ void startMqttClient() mqtt.subscribe(SUB_TOPIC); } -void gotIP(IpAddress ip, IpAddress netmask, IpAddress gateway) -{ - Serial << _F("Connected: ") << ip << endl; - startMqttClient(); - publishMessage(); // run once publishMessage -} - void init() { Serial.begin(SERIAL_BAUD_RATE); // 115200 by default diff --git a/samples/MeteoControl_mqtt/app/bmp180.cpp b/samples/MeteoControl_mqtt/app/bmp180.cpp index d239500b88..024b8c1696 100644 --- a/samples/MeteoControl_mqtt/app/bmp180.cpp +++ b/samples/MeteoControl_mqtt/app/bmp180.cpp @@ -1,6 +1,3 @@ -#ifndef INCLUDE_BMP180_H_ -#define INCLUDE_BMP180_H_ - #include #include @@ -9,9 +6,10 @@ // GPIO0 SCL // GPIO2 SDA +namespace +{ BMP180 barometer; - -Timer publishBMPTimer; +SimpleTimer publishBMPTimer; void publishBMP() { @@ -22,27 +20,29 @@ void publishBMP() Serial.println(_F("Start reading BMP180 sensor")); if(!barometer.EnsureConnected()) { Serial.println(_F("Could not connect to BMP180")); - } else { - // Retrieve the current pressure in Pascals - long currentPressure = barometer.GetPressure(); - // convert pressure to mmHg - float BMPPress = currentPressure / 133.322; - - // Print out the Pressure - Serial << _F("Pressure: ") << BMPPress << _F(" mmHg") << endl; - mqtt.publish(BMP_P, String(BMPPress)); - - // Retrieve the current temperature in degrees celsius - float BMPTemp = barometer.GetTemperature(); - - // Print out the Temperature - Serial << _F("Temperature: ") << BMPTemp << " °C" << endl; - mqtt.publish(BMP_T, String(BMPTemp)); - Serial.println(_F("BMP180 sensor read and transmitted to server\r\n" - "********************************************")); + return; } + // Retrieve the current pressure in Pascals + long currentPressure = barometer.GetPressure(); + // convert pressure to mmHg + float BMPPress = currentPressure / 133.322; + + // Print out the Pressure + Serial << _F("Pressure: ") << BMPPress << _F(" mmHg") << endl; + mqtt.publish(BMP_P, String(BMPPress)); + + // Retrieve the current temperature in degrees celsius + float BMPTemp = barometer.GetTemperature(); + + // Print out the Temperature + Serial << _F("Temperature: ") << BMPTemp << " °C" << endl; + mqtt.publish(BMP_T, String(BMPTemp)); + Serial.println(_F("BMP180 sensor read and transmitted to server\r\n" + "********************************************")); } +} // namespace + void BMPinit() { // When we have connected, we reset the device to ensure a clean start. @@ -54,5 +54,3 @@ void BMPinit() publishBMPTimer.initializeMs(publishBMP).start(); // start publish BMP180 sensor data } - -#endif /* INCLUDE_BMP180_H_ */ diff --git a/samples/MeteoControl_mqtt/app/si7021.cpp b/samples/MeteoControl_mqtt/app/si7021.cpp index 9aaea31dab..7eb1000f20 100644 --- a/samples/MeteoControl_mqtt/app/si7021.cpp +++ b/samples/MeteoControl_mqtt/app/si7021.cpp @@ -1,14 +1,12 @@ -#ifndef INCLUDE_SI7021_H_ -#define INCLUDE_SI7021_H_ - #include #include #include "configuration.h" +namespace +{ SI7021 hydrometer; - -Timer publishSITimer; +SimpleTimer publishSITimer; void publishSI() { @@ -21,28 +19,30 @@ void publishSI() if(!hydrometer.begin()) { Serial.println(_F("Could not connect to SI7021")); - } else { - si7021_env data = hydrometer.getHumidityAndTemperature(); - if(data.error_crc == 1) { - Serial.println(_F("\tCRC ERROR")); - } else { - float SIhum = data.humidityPercent; - // Print out the humidity - Serial << _F("Humidity: ") << SIhum << '%' << endl; - mqtt.publish(SI_H, String(SIhum)); - float SITemp = data.temperature; - // Print out the Temperature - Serial << _F("Temperature: ") << SITemp / 100 << " °C" << endl; - mqtt.publish(SI_T, String(SITemp / 100)); - Serial.println(_F("SI sensor read and transmitted to server\r\n" - "*********************************************")); - } + return; + } + + si7021_env data = hydrometer.getHumidityAndTemperature(); + if(data.error_crc == 1) { + Serial.println(_F("\tCRC ERROR")); + return; } + + float SIhum = data.humidityPercent; + // Print out the humidity + Serial << _F("Humidity: ") << SIhum << '%' << endl; + mqtt.publish(SI_H, String(SIhum)); + float SITemp = data.temperature; + // Print out the Temperature + Serial << _F("Temperature: ") << SITemp / 100 << " °C" << endl; + mqtt.publish(SI_T, String(SITemp / 100)); + Serial.println(_F("SI sensor read and transmitted to server\r\n" + "*********************************************")); } +} // namespace + void SIinit() { publishSITimer.initializeMs(publishSI).start(); // start publish SI sensor data } - -#endif /* INCLUDE_SI7021_H_ */ diff --git a/samples/MeteoControl_mqtt/include/configuration.h b/samples/MeteoControl_mqtt/include/configuration.h index 2d3e15e25d..a1d3cbf9ea 100644 --- a/samples/MeteoControl_mqtt/include/configuration.h +++ b/samples/MeteoControl_mqtt/include/configuration.h @@ -1,7 +1,6 @@ -#ifndef INCLUDE_CONFIGURATION_H_ -#define INCLUDE_CONFIGURATION_H_ +#pragma once -#include +#include //////////////////////////// Wi-Fi config /////////////////////////////////////// #ifndef WIFI_SSID @@ -33,11 +32,12 @@ constexpr int TIMER = 20; // every N* seconds send to mqtt server -// Forward declarations -void startMqttClient(); - -MqttClient mqtt; +enum TriggerType { + eTT_None = 0, + eTT_Temperature, + eTT_Humidity, +}; -enum TriggerType { eTT_None = 0, eTT_Temperature, eTT_Humidity }; +void startMqttClient(); -#endif /* INCLUDE_CONFIGURATION_H_ */ +extern MqttClient mqtt; diff --git a/samples/MqttClient_Hello/app/application.cpp b/samples/MqttClient_Hello/app/application.cpp index 01bbef0655..dc51bb8980 100644 --- a/samples/MqttClient_Hello/app/application.cpp +++ b/samples/MqttClient_Hello/app/application.cpp @@ -7,10 +7,12 @@ #define WIFI_PWD "PleaseEnterPass" #endif +namespace +{ // For testing purposes, try a few different URL formats -#define MQTT_URL1 "mqtt://test.mosquitto.org:1883" -#define MQTT_URL2 "mqtts://test.mosquitto.org:8883" // (Need ENABLE_SSL) -#define MQTT_URL3 "mqtt://frank:fiddle@192.168.100.107:1883" +DEFINE_FSTR(MQTT_URL1, "mqtt://test.mosquitto.org:1883") +DEFINE_FSTR(MQTT_URL2, "mqtts://test.mosquitto.org:8883") // (Need ENABLE_SSL) +DEFINE_FSTR(MQTT_URL3, "mqtt://frank:fiddle@192.168.100.107:1883") #ifdef ENABLE_SSL #include @@ -24,8 +26,7 @@ void startMqttClient(); MqttClient mqtt; - -Timer procTimer; +SimpleTimer procTimer; // Check for MQTT Disconnection void checkMQTTDisconnect(TcpClient& client, bool flag) @@ -118,6 +119,8 @@ void onConnected(IpAddress ip, IpAddress netmask, IpAddress gateway) startMqttClient(); } +} // namespace + void init() { Serial.begin(SERIAL_BAUD_RATE); // 115200 by default diff --git a/samples/Network_Ping/app/application.cpp b/samples/Network_Ping/app/application.cpp index b1cabe877d..360291aeee 100644 --- a/samples/Network_Ping/app/application.cpp +++ b/samples/Network_Ping/app/application.cpp @@ -18,7 +18,7 @@ constexpr uint8_t MAX_FAILED_ATTEMTPS = 5; constexpr uint8_t PING_INTERVAL_SECONDS = 10; constexpr uint8_t RESTART_DELAY_SECONDS = 2; -Timer procTimer; +SimpleTimer procTimer; void ping(uint32_t ip); diff --git a/samples/Nextion_Button/app/application.cpp b/samples/Nextion_Button/app/application.cpp index 24023fa23c..75662f56cd 100644 --- a/samples/Nextion_Button/app/application.cpp +++ b/samples/Nextion_Button/app/application.cpp @@ -1,9 +1,6 @@ #include #include -#include -#include - #define GPIO_LED 2 // See this example in action: https://youtu.be/lHk6fqDBHyI @@ -22,22 +19,22 @@ // Note: I always unplugged the ESP8266 from USB (connecting with computer) // while fiddling with the connections between ESP8266 and Nextion display. -NexButton b0 = NexButton(0, 1, "b0"); -NexText t0 = NexText(0, 2, "g0"); - +namespace +{ +NexButton b0(0, 1, "b0"); +NexText t0(0, 2, "g0"); NexTouch* nex_listen_list[] = {&b0, NULL}; - -Timer timerNextion; +SimpleTimer timerNextion; void loopNextion() { nexLoop(nex_listen_list); } -bool ledState = true; - void b0PopCallback(void* ptr) { + static bool ledState = true; + digitalWrite(GPIO_LED, ledState); // state == false => on // state == true => off @@ -49,6 +46,8 @@ void b0PopCallback(void* ptr) ledState = !ledState; } +} // namespace + void init() { pinMode(GPIO_LED, OUTPUT); diff --git a/samples/PortExpander_MCP23017/app/application.cpp b/samples/PortExpander_MCP23017/app/application.cpp index 8b00bc081e..0e8953d236 100644 --- a/samples/PortExpander_MCP23017/app/application.cpp +++ b/samples/PortExpander_MCP23017/app/application.cpp @@ -1,19 +1,24 @@ #include #include +namespace +{ MCP23017 mcp; -volatile bool awakenByInterrupt = false; +volatile bool awakenByInterrupt; byte mcpPinA = 0; byte interruptPin = 15; -void interruptCallback() +void interruptDelegate() { awakenByInterrupt = true; - Serial.println("Interrupt Called"); - while(!(mcp.digitalRead(mcpPinA))) - ; + Serial.println(_F("Interrupt Called")); + while(!mcp.digitalRead(mcpPinA)) { + // + } } +} // namespace + void init() { Serial.begin(COM_SPEED_SERIAL); @@ -36,5 +41,5 @@ void init() mcp.pinMode(mcpPinA, INPUT); mcp.pullUp(mcpPinA, HIGH); mcp.setupInterruptPin(mcpPinA, FALLING); - attachInterrupt(interruptPin, InterruptDelegate(interruptCallback), FALLING); + attachInterrupt(interruptPin, InterruptDelegate(interruptDelegate), FALLING); } diff --git a/samples/PortExpander_MCP23S17/app/application.cpp b/samples/PortExpander_MCP23S17/app/application.cpp index 7e64f82501..9126294024 100644 --- a/samples/PortExpander_MCP23S17/app/application.cpp +++ b/samples/PortExpander_MCP23S17/app/application.cpp @@ -1,21 +1,30 @@ #include -#include -#include #include +namespace +{ // // Instantiate an object called "inputchip" on an MCP23S17 device at address 1 = 0b00000001 and CS pin = GPIO16 MCP inputchip(1, 16); // Instantiate an object called "outputchip" on an MCP23S17 device at address 0 = 0b00000010 and CS pin = GPIO16 MCP outputchip(0, 16); -void loop(); +SimpleTimer procTimer; + +void loop() +{ + int value; // declare an integer to hold the value temporarily. + value = inputchip.digitalRead(); // read the input chip in word-mode, storing the result in "value" + outputchip.digitalWrite(value); // write the output chip in word-mode, using our variable "value" as the argument + // outputchip.digitalWrite(inputchip.digitalRead()); // this one line replaces the three above, and is more efficient +} -Timer procTimer; +} // namespace void init() { Serial.begin(SERIAL_BAUD_RATE); // 115200 by default Serial.systemDebugOutput(false); // Allow debug output to serial + Serial.println(_F("<-= Sming start =->")); // Set higher CPU freq & disable wifi sleep @@ -37,11 +46,3 @@ void init() procTimer.initializeMs<200>(loop).start(); } - -void loop() -{ - int value; // declare an integer to hold the value temporarily. - value = inputchip.digitalRead(); // read the input chip in word-mode, storing the result in "value" - outputchip.digitalWrite(value); // write the output chip in word-mode, using our variable "value" as the argument - // outputchip.digitalWrite(inputchip.digitalRead()); // this one line replaces the three above, and is more efficient -} diff --git a/samples/Pressure_BMP180/app/application.cpp b/samples/Pressure_BMP180/app/application.cpp index db9d44a384..b097b74ec0 100644 --- a/samples/Pressure_BMP180/app/application.cpp +++ b/samples/Pressure_BMP180/app/application.cpp @@ -2,6 +2,7 @@ #include BMP180 barometer; + void init() { Serial.begin(SERIAL_BAUD_RATE); // 115200 by default diff --git a/samples/Radio_RCSwitch/app/application.cpp b/samples/Radio_RCSwitch/app/application.cpp index db4d167d6d..0a2e997680 100644 --- a/samples/Radio_RCSwitch/app/application.cpp +++ b/samples/Radio_RCSwitch/app/application.cpp @@ -3,10 +3,11 @@ #define LED_PIN 2 // GPIO2 -Timer sendTimer; -Timer receiveTimer; - -RCSwitch mySwitch = RCSwitch(); +namespace +{ +SimpleTimer sendTimer; +SimpleTimer receiveTimer; +RCSwitch mySwitch; void sendRF() { @@ -16,18 +17,22 @@ void sendRF() void receiveRF() { - if(mySwitch.available()) { - if(mySwitch.getReceivedValue() == 0) { - Serial.print(_F("Unknown encoding")); - } else { - Serial << _F("Received ") << mySwitch.getReceivedValue() << " / " << mySwitch.getReceivedBitlength() - << " bit, Protocol: " << mySwitch.getReceivedProtocol() << endl; - } - - mySwitch.resetAvailable(); + if(mySwitch.available() == 0) { + return; + } + + if(mySwitch.getReceivedValue() == 0) { + Serial.println(_F("Unknown encoding")); + } else { + Serial << _F("Received ") << mySwitch.getReceivedValue() << " / " << mySwitch.getReceivedBitlength() + << " bit, Protocol: " << mySwitch.getReceivedProtocol() << endl; } + + mySwitch.resetAvailable(); } +} // namespace + void init() { Serial.begin(SERIAL_BAUD_RATE); // 115200 by default diff --git a/samples/Radio_nRF24L01/app/application.cpp b/samples/Radio_nRF24L01/app/application.cpp index 1030ded391..246e768bff 100644 --- a/samples/Radio_nRF24L01/app/application.cpp +++ b/samples/Radio_nRF24L01/app/application.cpp @@ -5,6 +5,8 @@ // Use this server with standard RF24 client example "pingpair" // https://github.com/maniacbug/RF24/tree/master/examples/pingpair +namespace +{ /* **** NRF24L01 pins connection: **** * VCC 3.3v @@ -27,7 +29,7 @@ RF24 radio(RF24_CE_PIN, RF24_CSN_PIN); // Radio pipe addresses for the 2 nodes to communicate. const uint64_t pipes[2] = {0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL}; -Timer procTimer; +SimpleTimer procTimer; void loopListen() { @@ -65,6 +67,8 @@ void loopListen() } } +} // namespace + void init() { Serial.begin(SERIAL_BAUD_RATE); // 115200 by default diff --git a/samples/Radio_si4432/app/application.cpp b/samples/Radio_si4432/app/application.cpp index 5e6ba09407..32cb28d277 100644 --- a/samples/Radio_si4432/app/application.cpp +++ b/samples/Radio_si4432/app/application.cpp @@ -18,27 +18,29 @@ Link: http://www.electrodragon.com/w/SI4432_433M-Wireless_Transceiver_Module_%28 #define PIN_RADIO_CK 15 /* Serial Clock */ #define PIN_RADIO_SS 13 /* Slave Select */ -Timer procTimer; -Si4432* radio = nullptr; -SPISoft* pRadioSPI = nullptr; +namespace +{ +SimpleTimer procTimer; +SPISoft radioSPI(PIN_RADIO_DO, PIN_RADIO_DI, PIN_RADIO_CK, PIN_RADIO_SS); +Si4432 radio(&radioSPI); #define PING_PERIOD_MS 2000 #define PING_WAIT_PONG_MS 100 -unsigned long lastPingTime; + +PeriodicFastMs pingTimer; void loopListen() { - const byte* ack = (const byte*)"OK"; //{ 0x01, 0x3, 0x11, 0x13 }; - const byte* ping = (const byte*)"PING"; + const char* ack = "OK"; //{ 0x01, 0x3, 0x11, 0x13 }; + const char* ping = "PING"; byte payLoad[64] = {0}; byte len = 0; //1. Ping from time to time, and wait for incoming response - if(millis() - lastPingTime > PING_PERIOD_MS) { - lastPingTime = millis(); - + if(pingTimer.expired()) { Serial.print(_F("Ping -> ")); - if(!radio->sendPacket(strlen((const char*)ping), ping, true, PING_WAIT_PONG_MS, &len, payLoad)) { + if(!radio.sendPacket(strlen(ping), reinterpret_cast(ping), true, PING_WAIT_PONG_MS, &len, + payLoad)) { Serial.println(" ERR!"); } else { Serial.println(_F(" SENT!")); @@ -49,62 +51,55 @@ void loopListen() } //2. Listen for any other incoming packet - bool pkg = radio->isPacketReceived(); + bool pkg = radio.isPacketReceived(); if(pkg) { - radio->getPacketReceived(&len, payLoad); + radio.getPacketReceived(&len, payLoad); Serial << _F("ASYNC RX (") << len << "): "; Serial.write(payLoad, len); Serial.println(); Serial.print(_F("Response -> ")); - if(!radio->sendPacket(strlen((const char*)ack), ack)) { + if(!radio.sendPacket(strlen(ack), reinterpret_cast(ack))) { Serial.println(_F("ERR!")); } else { Serial.println(_F("SENT!")); } - radio->startListening(); // restart the listening. + radio.startListening(); // restart the listening. } } +} // namespace + void init() { +#ifdef ARCH_HOST + setDigitalHooks(nullptr); +#endif + Serial.begin(SERIAL_BAUD_RATE); // 115200 by default Serial.systemDebugOutput(true); //Allow debug output to serial - Serial.println(_F("\nRadio si4432 example - !!! see code for HW setup !!! \n")); - - pRadioSPI = new SPISoft(PIN_RADIO_DO, PIN_RADIO_DI, PIN_RADIO_CK, PIN_RADIO_SS); - - if(pRadioSPI != nullptr) { - radio = new Si4432(pRadioSPI); - } - - if(radio == nullptr) { - Serial.println(_F("Error: Not enough heap.")); - return; - } - - delay(100); + Serial.println(_F("\r\nRadio si4432 example - !!! see code for HW setup !!!\r\n")); - //initialise radio with default settings - radio->init(); + // initialise radio with default settings + radio.init(); - //explicitly set baudrate and channel - radio->setBaudRateFast(eBaud_38k4); - radio->setChannel(0); + // explicitly set baudrate and channel + radio.setBaudRateFast(eBaud_38k4); + radio.setChannel(0); - //dump the register configuration to console - radio->readAll(); + // dump the register configuration to console + radio.readAll(); - //start listening for incoming packets + // start listening for incoming packets Serial.println("Listening..."); - radio->startListening(); + radio.startListening(); - lastPingTime = millis(); + pingTimer.reset(PING_PERIOD_MS); - //start listen loop + // start listen loop procTimer.initializeMs<10>(loopListen).start(); } diff --git a/samples/SDCard/app/application.cpp b/samples/SDCard/app/application.cpp index 7d2eee5be9..a7979af387 100644 --- a/samples/SDCard/app/application.cpp +++ b/samples/SDCard/app/application.cpp @@ -26,6 +26,8 @@ Descr: SDCard/FAT file usage and write benchmark. /* Sets the max frequency of SPI (init is done at a lower speed than the main communication) */ #define SPI_FREQ_LIMIT 2000000 +namespace +{ void writeToFile(const String& filename, uint32_t totalBytes, uint32_t bytesPerRound) { char* buf = new char[totalBytes]; @@ -201,8 +203,16 @@ bool speedTest(unsigned num) }; static const Test tests[] PROGMEM{ - {1024, 1}, {1024, 64}, {1024, 128}, {1024, 512}, {1024, 1024}, - {4096, 1024}, {8192, 512}, {8192, 1024}, {8192, 8192}, + {1024, 1}, // + {1024, 64}, // + {1024, 128}, // + {1024, 512}, // + {1024, 1024}, // + {4096, 1024}, // + {8192, 512}, // + {8192, 1024}, // + {8192, 8192}, // + }; if(num >= ARRAY_SIZE(tests)) { @@ -263,6 +273,8 @@ void runTest(uint32_t state = 0) System.queueCallback(runTest, state + 1); } +} // namespace + void init() { Serial.begin(SERIAL_BAUD_RATE); // 115200 by default diff --git a/samples/ScreenLCD_5110/app/application.cpp b/samples/ScreenLCD_5110/app/application.cpp index 2fffa50e1d..945dea1448 100644 --- a/samples/ScreenLCD_5110/app/application.cpp +++ b/samples/ScreenLCD_5110/app/application.cpp @@ -1,32 +1,48 @@ #include #include +namespace +{ // GPIO13/D7 - Serial clock out (SCLK) // GPIO12/D6 - Serial data out (DIN) // GPIO14/D5 - Data/Command select (D/C) // GPIO5/D1 - LCD chip select (CS) // GPIO4/D2 - LCD reset (RST) -Adafruit_PCD8544 display = Adafruit_PCD8544(13, 12, 14, 5, 4); +Adafruit_PCD8544 display(13, 12, 14, 5, 4); +SimpleTimer timer; void displayTest() { display.begin(); display.setContrast(10); display.display(); // show splashscreen - delay(2000); - display.clearDisplay(); // no changes will be visible until display() is called - display.setRotation(4); // rotate 90 degrees counter clockwise, can also use values of 2 and 3 to go further. - display.setTextSize(1); - display.setTextColor(BLACK); - display.setCursor(0, 0); - display.println("Sming"); - display.setTextSize(2); - display.println("Example"); - display.display(); + debug_d("Show splash..."); + + timer.initializeMs<2000>([]() { + debug_d("Update screen."); + display.clearDisplay(); // no changes will be visible until display() is called + display.setRotation(4); // rotate 90 degrees counter clockwise, can also use values of 2 and 3 to go further. + display.setTextSize(1); + display.setTextColor(BLACK); + display.setCursor(0, 0); + display.println("Sming"); + display.setTextSize(2); + display.println("Example"); + display.display(); + }); + timer.startOnce(); } +} // namespace + void init() { +#ifdef ARCH_HOST + setDigitalHooks(nullptr); +#endif + Serial.begin(SERIAL_BAUD_RATE); + Serial.systemDebugOutput(true); + displayTest(); } diff --git a/samples/ScreenOLED_SSD1306/app/application.cpp b/samples/ScreenOLED_SSD1306/app/application.cpp index 301f9b7972..df67dadd1b 100644 --- a/samples/ScreenOLED_SSD1306/app/application.cpp +++ b/samples/ScreenOLED_SSD1306/app/application.cpp @@ -18,6 +18,8 @@ #define SH1106_128_64 // larger one */ +namespace +{ // For spi oled module // Adafruit_SSD1306 display(0, 16, 2); @@ -25,7 +27,7 @@ // Default I2C pins 0 (SCL) and 2 (SDA). Pin 4 - optional reset Adafruit_SSD1306 display(-1); // reset Pin required but later ignored if set to False -Timer DemoTimer; +SimpleTimer demoTimer; void Demo2() { @@ -45,7 +47,6 @@ void Demo2() display.setTextSize(3); display.print("IoT"); display.display(); - DemoTimer.stop(); // Finish demo } void Demo1() @@ -56,10 +57,13 @@ void Demo1() // draw a circle, 10 pixel radius display.fillCircle(display.width() / 2, display.height() / 2, 10, WHITE); display.display(); - DemoTimer.stop(); - DemoTimer.initializeMs<2000>(Demo2).start(); + + demoTimer.setCallback(Demo2); + demoTimer.startOnce(); } +} // namespace + void init() { Serial.begin(SERIAL_BAUD_RATE); // 115200 by default @@ -70,5 +74,6 @@ void init() // bool:reset set to TRUE or FALSE depending on your display display.begin(SSD1306_SWITCHCAPVCC, 0x3c, false); display.display(); - DemoTimer.initializeMs<2000>(Demo1).start(); + + demoTimer.initializeMs<2000>(Demo1).startOnce(); } diff --git a/samples/ScreenTFT_ILI9163C/app/application.cpp b/samples/ScreenTFT_ILI9163C/app/application.cpp index 8994109c9c..3d1611a989 100644 --- a/samples/ScreenTFT_ILI9163C/app/application.cpp +++ b/samples/ScreenTFT_ILI9163C/app/application.cpp @@ -1,6 +1,8 @@ #include #include +namespace +{ /* * LED (BACKLIGHT) 3.3v * SCK (SCLK) GPIO14 @@ -12,33 +14,41 @@ * VCC (VCC) 3.3v */ TFT_ILI9163C tft(2, 0); +SimpleTimer timer; -void init() +void demo() { - Serial.begin(SERIAL_BAUD_RATE); // 115200 by default - Serial.systemDebugOutput(true); // Allow debug output to serial - Serial.println(_F("Display start")); tft.begin(); tft.setRotation(2); // try yourself tft.fillScreen(); tft.fillRect(10, 20, 100, 120, YELLOW); - delay(1000); - // text display tests - tft.fillScreen(); - tft.setTextSize(1); - tft.setTextColor(GREEN); - tft.setCursor(0, 0); - tft.println(_F("Sming Framework")); - tft.setTextColor(BLACK, WHITE); // 'inverted' text - tft.setCursor(104, 7); - tft.println("v1.0"); - tft.setTextColor(WHITE); - tft.println(_F("Let's do smart things")); - tft.setTextSize(3); - tft.setTextColor(BLUE); - tft.print("IoT"); + timer.initializeMs<1000>([]() { + // text display tests + tft.fillScreen(); + tft.setTextSize(1); + tft.setTextColor(GREEN); + tft.setCursor(0, 0); + tft.println(_F("Sming Framework")); + tft.setTextColor(BLACK, WHITE); // 'inverted' text + tft.setCursor(104, 7); + tft.println("v1.0"); + tft.setTextColor(WHITE); + tft.println(_F("Let's do smart things")); + tft.setTextSize(3); + tft.setTextColor(BLUE); + tft.print("IoT"); + }); + timer.startOnce(); +} + +} // namespace + +void init() +{ + Serial.begin(SERIAL_BAUD_RATE); // 115200 by default + Serial.systemDebugOutput(true); // Allow debug output to serial - delay(2000); + demo(); } diff --git a/samples/ScreenTFT_ILI9340-ILI9341/app/application.cpp b/samples/ScreenTFT_ILI9340-ILI9341/app/application.cpp index 2b0ec9b1d3..5aba476570 100644 --- a/samples/ScreenTFT_ILI9340-ILI9341/app/application.cpp +++ b/samples/ScreenTFT_ILI9340-ILI9341/app/application.cpp @@ -8,36 +8,41 @@ #define WIFI_PWD "PleaseEnterPass" #endif +namespace +{ // See library for pinout Adafruit_ILI9341 tft; -Timer guiTimer; - -int r = 0; - -int ara = 4, yerara = 15; -int u1 = 100; -int u2 = 320 - (u1 + ara); -int s1 = 0; -int s2 = (u1 + ara); -int p1 = 50; - -int g = 28; -int y = 90; -int satir = 6; - -String lists[] = {"a", "b", "c", "d", "e", "f"}; +SimpleTimer guiTimer; void basicBMP() { + debug_d("%s", __FUNCTION__); + tft.fillScreen(ILI9341_BLACK); // Clear display tft.setRotation(tft.getRotation() + 1); // Inc rotation 90 degrees - for(uint8_t i = 0; i < 4; i++) // Draw 4 parrots - bmpDraw(tft, "sming.bmp", tft.width() / 4 * i, tft.height() / 4 * i); + for(uint8_t i = 0; i < 4; i++) { // Draw 4 parrots + bmpDraw(tft, "sming.bmp", i * tft.width() / 4, i * tft.height() / 4); + } } void basicGui() { + debug_d("%s", __FUNCTION__); + + static int r; + + const int ara = 4; + const int yerara = 15; + const int u1 = 100; + const int u2 = 320 - (u1 + ara); + const int s1 = 0; + const int s2 = (u1 + ara); + + const int g = 28; + + int p1 = 50; + tft.setTextSize(1); tft.setRotation(1); @@ -48,12 +53,13 @@ void basicGui() tft.println("Sming"); tft.setTextSize(2); tft.fillRect((u1 * 2) + ara, 0, 318 - (u1 * 2), 48, ILI9341_RED); - for(int a = 0; a < satir; a++) { + for(auto a : {'a', 'b', 'c', 'd', 'e', 'f'}) { + debug_d("%c: %u", a, r); tft.setTextColor(ILI9341_GREEN); tft.fillRect(s1, p1, u1, g, ILI9341_DARKCYAN); tft.setCursor(s1 + yerara, p1 + 6); tft.setTextColor(ILI9341_WHITE); - tft.println(lists[a]); + tft.println(a); tft.fillRect(s2, p1, u2, g, ILI9341_DARKCYAN); tft.setCursor(s2 + yerara, p1 + 6); tft.println(r); @@ -61,24 +67,16 @@ void basicGui() } p1 = 50; r++; - guiTimer.initializeMs<1000>(basicBMP).start(false); -} -void init() -{ - Serial.begin(SERIAL_BAUD_RATE); // 115200 by default - Serial.systemDebugOutput(true); // Allow debug output to serial - -#ifndef DISABLE_WIFI - //WifiStation.config(WIFI_SSID, WIFI_PWD); - WifiStation.enable(false); - WifiAccessPoint.enable(false); -#endif + if(r >= 5) { + guiTimer.setCallback(basicBMP); + } - spiffs_mount(); - Serial.println(_F("FileSystem mounted.")); + guiTimer.startOnce(); +} - // delay(2000); +void demo() +{ Serial.println(_F("Display start")); // text display tests @@ -99,8 +97,33 @@ void init() tft.println(_F("ili9340-40C-41 ")); tft.setCursor(60, 125); tft.println(_F("M.Bozkurt")); - delay(2000); - tft.fillScreen(0); - guiTimer.initializeMs<1000>(basicGui).start(false); - //runTest(); + + guiTimer.initializeMs<2000>([]() { + tft.fillScreen(0); + guiTimer.initializeMs<1000>(basicGui).startOnce(); + }); + guiTimer.startOnce(); +} + +} // namespace + +void init() +{ +#ifdef ARCH_HOST + setDigitalHooks(nullptr); +#endif + + Serial.begin(SERIAL_BAUD_RATE); // 115200 by default + Serial.systemDebugOutput(true); // Allow debug output to serial + +#ifndef DISABLE_WIFI + //WifiStation.config(WIFI_SSID, WIFI_PWD); + WifiStation.enable(false); + WifiAccessPoint.enable(false); +#endif + + spiffs_mount(); + Serial.println(_F("FileSystem mounted.")); + + demo(); } diff --git a/samples/ScreenTFT_ST7735/app/application.cpp b/samples/ScreenTFT_ST7735/app/application.cpp index bf7eb764b6..b4a3b7bbe7 100644 --- a/samples/ScreenTFT_ST7735/app/application.cpp +++ b/samples/ScreenTFT_ST7735/app/application.cpp @@ -18,14 +18,12 @@ #define TFT_DC 0 #define TFT_CS 2 -//Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_MOSI, TFT_SCLK, TFT_RST); -Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST); - -Timer DemoScreenTimer; -float p = 3.1415926; -uint32_t startTime; +namespace +{ +//Adafruit_ST7735 tft(TFT_CS, TFT_DC, TFT_MOSI, TFT_SCLK, TFT_RST); +Adafruit_ST7735 tft(TFT_CS, TFT_DC, TFT_RST); -void testdrawtext(const char text[], uint16_t color) +void testdrawtext(const String& text, uint16_t color) { tft.setCursor(0, 0); tft.setTextColor(color); @@ -61,7 +59,7 @@ void tftPrintTest2() tft.println("Hello Sming!"); tft.setTextSize(1); tft.setTextColor(ST7735_GREEN); - tft.print(p, 6); + tft.print(PI, 6); tft.println(" Want pi?"); tft.println(" "); tft.print(8675309, HEX); // print 8,675,309 out in HEX! @@ -226,122 +224,155 @@ void mediabuttons() void screen13() { - startTime = millis(); - debugf("screen13: bmpDraw rotaton %d ms", millis() - startTime); tft.fillScreen(ST7735_BLACK); // Clear display tft.setRotation(tft.getRotation() + 1); // Inc rotation 90 degrees - for(uint8_t i = 0; i < 4; i++) // Draw 4 parrots + for(uint8_t i = 0; i < 4; i++) { // Draw 4 parrots bmpDraw(tft, "sming.bmp", tft.width() / 4 * i, tft.height() / 4 * i); + } } void screen12() { - startTime = millis(); bmpDraw(tft, "sming.bmp", 0, 0); - debugf("screen12: bmpDraw %d ms", millis() - startTime); - // DemoScreenTimer.initializeMs<2000>(screen13).start(false); } void screen11() { - startTime = millis(); mediabuttons(); - debugf("screen11: mediabuttons %d ms", millis() - startTime); - DemoScreenTimer.initializeMs<1000>(screen12).start(false); } void screen10() { - startTime = millis(); testtriangles(); - debugf("screen10: testtriangles %d ms", millis() - startTime); - DemoScreenTimer.initializeMs<1000>(screen11).start(false); } void screen9() { - startTime = millis(); testroundrects(); - debugf("screen9: testroundrects %d ms", millis() - startTime); - DemoScreenTimer.initializeMs<1000>(screen10).start(false); } void screen8() { - startTime = millis(); tft.fillScreen(ST7735_BLACK); testfillcircles(10, ST7735_BLUE); testdrawcircles(10, ST7735_WHITE); - debugf("screen8: testfillcircles %d ms", millis() - startTime); - DemoScreenTimer.initializeMs<1000>(screen9).start(false); } void screen7() { - startTime = millis(); testfillrects(ST7735_YELLOW, ST7735_MAGENTA); - debugf("screen7: testfillrects %d ms", millis() - startTime); - DemoScreenTimer.initializeMs<1000>(screen8).start(false); } void screen6() { - startTime = millis(); testdrawrects(ST7735_GREEN); - debugf("screen6: testdrawrects %d ms", millis() - startTime); - DemoScreenTimer.initializeMs<1000>(screen7).start(false); } void screen5() { - startTime = millis(); // optimized lines testfastlines(ST7735_RED, ST7735_BLUE); - debugf("screen5: testfastlines %d ms", millis() - startTime); - DemoScreenTimer.initializeMs<1000>(screen6).start(false); } void screen4() { - startTime = millis(); // line draw test testlines(ST7735_YELLOW); - debugf("screen4: testlines %d ms", millis() - startTime); - DemoScreenTimer.initializeMs<1000>(screen5).start(false); } void screen3() { - startTime = millis(); tftPrintTest2(); - debugf("screen3: tftPrintTest2 %d ms", millis() - startTime); - DemoScreenTimer.initializeMs<1000>(screen4).start(false); } void screen2() { - startTime = millis(); tftPrintTest1(); - debugf("screen2: tftPrintTest1 %d ms", millis() - startTime); - DemoScreenTimer.initializeMs<1000>(screen3).start(false); } void screen1() { - startTime = millis(); // large block of text tft.fillScreen(ST7735_BLACK); - testdrawtext("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur adipiscing ante sed nibh " - "tincidunt feugiat. Maecenas enim massa, fringilla sed malesuada et, malesuada sit amet turpis. Sed " - "porttitor neque ut ante pretium vitae malesuada nunc bibendum. Nullam aliquet ultrices massa eu " - "hendrerit. Ut sed nisi lorem. In vestibulum purus a tortor imperdiet posuere. ", - ST7735_WHITE); - debugf("screen1: testdrawtext %d ms", millis() - startTime); - DemoScreenTimer.initializeMs<1000>(screen2).start(false); + DEFINE_FSTR_LOCAL( + largeTextBlock, + "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur adipiscing ante sed nibh " + "tincidunt feugiat. Maecenas enim massa, fringilla sed malesuada et, malesuada sit amet turpis. Sed " + "porttitor neque ut ante pretium vitae malesuada nunc bibendum. Nullam aliquet ultrices massa eu " + "hendrerit. Ut sed nisi lorem. In vestibulum purus a tortor imperdiet posuere. ") + testdrawtext(largeTextBlock, ST7735_WHITE); } +void initialise() +{ + // Use this initializer if you're using a 1.8" TFT + // tft.initR(INITR_BLACKTAB); // initialize a ST7735S chip, black tab + + // Use this initializer (uncomment) if you're using a 1.44" TFT + tft.initR(INITR_144GREENTAB); // initialize a ST7735S chip, black tab + + tft.fillScreen(ST7735_BLACK); +} + +#define SCREEN_FUNCTION_MAP(XX) \ + XX(initialise, "Initialise") \ + XX(screen1, "testdrawtext") \ + XX(screen2, "tftPrintTest1") \ + XX(screen3, "tftPrintTest2") \ + XX(screen4, "testlines") \ + XX(screen5, "testfastlines") \ + XX(screen6, "testdrawrects") \ + XX(screen7, "testfillrects") \ + XX(screen8, "testfillcircles") \ + XX(screen9, "testroundrects") \ + XX(screen10, "testtriangles") \ + XX(screen11, "mediabuttons") \ + XX(screen12, "bmpDraw") \ + XX(screen13, "bmpDraw rotation") + +#define XX(function, title) DEFINE_FSTR(function##_title, #function ": " title) +SCREEN_FUNCTION_MAP(XX) +#undef XX + +struct DemoScreen { + InterruptCallback function; + const FlashString& title; +}; + +const DemoScreen screenList[] PROGMEM{ +#define XX(name, title) {name, name##_title}, + SCREEN_FUNCTION_MAP(XX) +#undef XX +}; + +SimpleTimer demoScreenTimer; +auto screen = std::begin(screenList); + +void nextScreen() +{ + OneShotFastMs elapseTimer; + screen->function(); + auto elapsed = elapseTimer.elapsedTime(); + + Serial << screen->title << " in " << elapsed << "ms" << endl; + + ++screen; + if(screen != std::end(screenList)) { + demoScreenTimer.startOnce(); + return; + } + + Serial.println(_F("All screens displayed")); +} + +} // namespace + void init() { +#ifdef ARCH_HOST + setDigitalHooks(nullptr); +#endif + spiffs_mount(); // Mount file system, in order to work with files Serial.begin(SERIAL_BAUD_RATE); // 115200 by default @@ -353,19 +384,7 @@ void init() WifiAccessPoint.enable(false); #endif - debugf("Display start"); - startTime = millis(); - - // Use this initializer if you're using a 1.8" TFT - // tft.initR(INITR_BLACKTAB); // initialize a ST7735S chip, black tab - - // Use this initializer (uncomment) if you're using a 1.44" TFT - tft.initR(INITR_144GREENTAB); // initialize a ST7735S chip, black tab - - tft.fillScreen(ST7735_BLACK); - startTime = millis() - startTime; - - debugf("Initialized in %d ms\n", startTime); - - DemoScreenTimer.initializeMs<500>(screen1).start(false); + Serial.println(_F("Initialise display")); + demoScreenTimer.initializeMs<500>(nextScreen); + nextScreen(); } diff --git a/samples/ScreenTFT_ST7735/component.mk b/samples/ScreenTFT_ST7735/component.mk index 17c7646fff..41fb0dc894 100644 --- a/samples/ScreenTFT_ST7735/component.mk +++ b/samples/ScreenTFT_ST7735/component.mk @@ -1,4 +1,3 @@ -COMPONENT_SOC := esp* HWCONFIG := spiffs-2m ARDUINO_LIBRARIES := Adafruit_ST7735 DISABLE_NETWORK := 1 diff --git a/samples/SmtpClient/app/application.cpp b/samples/SmtpClient/app/application.cpp index 9890635d3a..1b6083cc6c 100644 --- a/samples/SmtpClient/app/application.cpp +++ b/samples/SmtpClient/app/application.cpp @@ -7,12 +7,14 @@ #define WIFI_PWD "PleaseEnterPass" #endif +namespace +{ // Make sure to change those to your desired values -#define MAIL_FROM "admin@sming.com" -#define MAIL_TO "slav@attachix.com" -#define SMTP_USERNAME nullptr -#define SMTP_PASSWORD nullptr -#define SMTP_HOST "attachix.com" +DEFINE_FSTR(MAIL_FROM, "admin@sming.com") +DEFINE_FSTR(MAIL_TO, "slav@attachix.com") +DEFINE_FSTR(SMTP_USERNAME, "") +DEFINE_FSTR(SMTP_PASSWORD, "") +DEFINE_FSTR(SMTP_HOST, "attachix.com") #define SMTP_PORT 0 // Use default port #define SMTP_USE_SSL false @@ -31,11 +33,11 @@ int onMailSent(SmtpClient& client, int code, char* status) MailMessage* mail = client.getCurrentMessage(); // TODO: The status line contains the unique ID that was given to this email - debugf("Mail sent to '%s'. Status: %s", mail->to.c_str(), status); + Serial << _F("Mail sent to '") << mail->to << _F("'. Status: ") << status << endl; // And if there are no more pending emails then you can disconnect from the server if(client.countPending() == 0) { - debugf("No more mails to send. Quitting..."); + Serial.println(_F("No more mails to send. Quitting...")); client.quit(); } @@ -50,7 +52,7 @@ void onConnected(IpAddress ip, IpAddress mask, IpAddress gateway) Url dsn(SMTP_USE_SSL ? URI_SCHEME_SMTP_SECURE : URI_SCHEME_SMTP, SMTP_USERNAME, SMTP_PASSWORD, SMTP_HOST, SMTP_PORT); - debugf("Connecting to SMTP server using: %s", String(dsn).c_str()); + Serial << _F("Connecting to SMTP server using: ") << dsn << endl; client.connect(dsn); @@ -58,8 +60,9 @@ void onConnected(IpAddress ip, IpAddress mask, IpAddress gateway) mail->from = MAIL_FROM; mail->to = MAIL_TO; mail->subject = "Greetings from Sming"; - String body = F("Hello.\r\n.\r\n" - "This is test email from Sming " + String body = F("Hello.\r\n." + "\r\n" + "This is test email from Sming \r\n" "It contains attachment, Ümlauts, кирилица + etc"); // Note: Body can be quite large so use move semantics to avoid additional heap allocation mail->setBody(std::move(body)); @@ -71,11 +74,13 @@ void onConnected(IpAddress ip, IpAddress mask, IpAddress gateway) client.send(mail); } +} // namespace + void init() { Serial.begin(SERIAL_BAUD_RATE); Serial.systemDebugOutput(true); - Serial.println("Sming: SmtpClient example!"); + Serial.println(_F("Sming: SmtpClient example!")); spiffs_mount(); diff --git a/samples/SystemClock_NTP/app/application.cpp b/samples/SystemClock_NTP/app/application.cpp index 5129bb6f22..ecd3121802 100644 --- a/samples/SystemClock_NTP/app/application.cpp +++ b/samples/SystemClock_NTP/app/application.cpp @@ -7,9 +7,12 @@ #define WIFI_PWD "PleaseEnterPass" #endif +namespace +{ +#pragma GCC diagnostic ignored "-Wunused-function" void onNtpReceive(NtpClient& client, time_t timestamp); -Timer printTimer; +SimpleTimer printTimer; // Option 1 // Use this option if you want to have full control of NtpTime client @@ -62,7 +65,7 @@ void onNtpReceive(NtpClient& client, time_t timestamp) // Will be called when WiFi station timeout was reached void connectFail(const String& ssid, MacAddress bssid, WifiDisconnectReason reason) { - debugf("I'm NOT CONNECTED!"); + Serial.println(_F("I'm NOT CONNECTED!")); } void gotIP(IpAddress ip, IpAddress netmask, IpAddress gateway) @@ -88,6 +91,8 @@ void gotIP(IpAddress ip, IpAddress netmask, IpAddress gateway) } } +} // namespace + // Will be called when WiFi hardware and software initialization was finished // And system initialization was completed diff --git a/samples/TcpClient_NarodMon/app/application.cpp b/samples/TcpClient_NarodMon/app/application.cpp index bf2a5fa4de..cdc8c21c47 100644 --- a/samples/TcpClient_NarodMon/app/application.cpp +++ b/samples/TcpClient_NarodMon/app/application.cpp @@ -13,7 +13,9 @@ #define WIFI_PWD "PleaseEnterPass" #endif -#define NARODM_HOST "narodmon.ru" +namespace +{ +DEFINE_FSTR(NARODM_HOST, "narodmon.ru") #define NARODM_PORT 8283 // Time between command packets, in seconds @@ -21,7 +23,7 @@ const unsigned SENDDATA_INTERVAL = 6 * 60; // Таймер для периодического вызова отправки данных // Timer for a periodic call to send data -Timer procTimer; +SimpleTimer procTimer; // Переменная для хранения mac-адреса // Variable for storing MAC address @@ -103,20 +105,18 @@ void sendData() // Когда удачно подключились к роутеру void connectOk(const String& SSID, MacAddress bssid, uint8_t channel) { - // debug msg - debugf("I'm CONNECTED to WiFi"); - // Get the MAC address of our ESP // получаем MAC-адрес нашей ESP и помещаем в переменную mac mac = WifiStation.getMacAddress(); - debugf("mac: %s", mac.toString().c_str()); + + Serial << _F("I'm CONNECTED to WiFi, MAC: ") << mac << endl; } void connectFail(const String& ssid, MacAddress bssid, WifiDisconnectReason reason) { // Display a message on failed connection // Если подключение к роутеру не удалось, выводим сообщение - debugf("I'm NOT CONNECTED!"); + Serial.println(_F("I'm NOT CONNECTED!")); } void gotIP(IpAddress ip, IpAddress netmask, IpAddress gateway) @@ -130,6 +130,8 @@ void gotIP(IpAddress ip, IpAddress netmask, IpAddress gateway) sendData(); } +} // namespace + void init() { // Configure and enable output in UART for debug diff --git a/samples/Temperature_DS1820/app/application.cpp b/samples/Temperature_DS1820/app/application.cpp index 75d695c349..3c4277df35 100644 --- a/samples/Temperature_DS1820/app/application.cpp +++ b/samples/Temperature_DS1820/app/application.cpp @@ -3,8 +3,10 @@ #define I2C_PIN 4 +namespace +{ DS18S20 ReadTemp; -Timer procTimer; +SimpleTimer procTimer; //********************************************************** // DS18S20 example, reading @@ -22,31 +24,35 @@ Timer procTimer; //*********************************************************** void readData() { - if(!ReadTemp.MeasureStatus()) // the last measurement completed - { - auto sensorCount = ReadTemp.GetSensorsCount(); - if(sensorCount > 0) { - Serial.println(_F("******************************************")); - } - Serial.println(_F(" Reading temperature DEMO")); - // prints for all sensors - for(unsigned a = 0; a < sensorCount; a++) { - Serial << " T" << a + 1 << " = "; - if(ReadTemp.IsValidTemperature(a)) { - Serial << ReadTemp.GetCelsius(a) << _F(" Celsius, (") << ReadTemp.GetFahrenheit(a) << _F(" Fahrenheit)") - << endl; - } else { - Serial.println(_F("Temperature not valid")); - } - - Serial << _F(" ' << endl; - } - Serial.println(_F("******************************************")); - ReadTemp.StartMeasure(); // next measure, result after 1.2 seconds * number of sensors - } else + if(ReadTemp.MeasureStatus()) { Serial.println(_F("No valid Measure so far! wait please")); + return; + } + + // the last measurement completed + auto sensorCount = ReadTemp.GetSensorsCount(); + if(sensorCount > 0) { + Serial.println(_F("******************************************")); + } + Serial.println(_F(" Reading temperature DEMO")); + // prints for all sensors + for(unsigned a = 0; a < sensorCount; a++) { + Serial << " T" << a + 1 << " = "; + if(ReadTemp.IsValidTemperature(a)) { + Serial << ReadTemp.GetCelsius(a) << _F(" Celsius, (") << ReadTemp.GetFahrenheit(a) << _F(" Fahrenheit)") + << endl; + } else { + Serial.println(_F("Temperature not valid")); + } + + Serial << _F(" ' << endl; + } + Serial.println(_F("******************************************")); + ReadTemp.StartMeasure(); // next measure, result after 1.2 seconds * number of sensors } +} // namespace + void init() { Serial.begin(SERIAL_BAUD_RATE); diff --git a/samples/UdpServer_Echo/app/application.cpp b/samples/UdpServer_Echo/app/application.cpp index 52cf2503ec..ab90275c8f 100644 --- a/samples/UdpServer_Echo/app/application.cpp +++ b/samples/UdpServer_Echo/app/application.cpp @@ -6,7 +6,9 @@ #define WIFI_PWD "PleaseEnterPass" #endif -void onReceive(UdpConnection& connection, char* data, int size, IpAddress remoteIP, uint16_t remotePort); // Declaration +namespace +{ +void onReceive(UdpConnection& connection, char* data, int size, IpAddress remoteIP, uint16_t remotePort); // UDP server const uint16_t EchoPort = 1234; @@ -20,7 +22,7 @@ void onReceive(UdpConnection& connection, char* data, int size, IpAddress remote Serial << ">\t" << data; // Send echo to remote sender - String text = String("echo: ") + data; + String text = F("echo: ") + data; udp.sendStringTo(remoteIP, remotePort, text); } @@ -34,6 +36,8 @@ void gotIP(IpAddress ip, IpAddress gateway, IpAddress netmask) Serial.println(_F("==========================\r\n")); } +} // namespace + void init() { Serial.begin(SERIAL_BAUD_RATE); // 115200 by default diff --git a/samples/UdpServer_mDNS/app/application.cpp b/samples/UdpServer_mDNS/app/application.cpp index f9497b0c2c..83c60187cb 100644 --- a/samples/UdpServer_mDNS/app/application.cpp +++ b/samples/UdpServer_mDNS/app/application.cpp @@ -11,8 +11,8 @@ // Our device will be reachable via "sming.local" DEFINE_FSTR_LOCAL(hostName, "sming"); -HttpServer server; - +namespace +{ class MyHttpService : public mDNS::Service { public: @@ -28,37 +28,9 @@ class MyHttpService : public mDNS::Service } }; -static mDNS::Responder responder; -static MyHttpService myHttpService; - -void speedTest(mDNS::Question* question) -{ - using namespace mDNS; - - OneShotFastUs timer; - unsigned count{0}; - for(unsigned i = 0; i < 10000; ++i) { - if(question->getName() == fstrServicesLocal) { - ++count; - } - if(question->getName().equalsIgnoreCase("Haggis basher")) { - ++count; - } - } - debug_i("(question == fstrServicesLocal): %u, %s", count, timer.elapsedTime().toString().c_str()); - - timer.start(); - count = 0; - for(unsigned i = 0; i < 10000; ++i) { - if(String(question->getName()).equalsIgnoreCase(fstrServicesLocal)) { - ++count; - } - if(String(question->getName()).equalsIgnoreCase("Haggis basher")) { - ++count; - } - } - debug_i("(question == fstrServicesLocal): %u, %s", count, timer.elapsedTime().toString().c_str()); -} +HttpServer server; +mDNS::Responder responder; +MyHttpService myHttpService; void test() { @@ -97,8 +69,6 @@ void test() Query query; auto question = query.addQuestion(fstrServicesLocal, ResourceType::PTR); - // speedTest(question); - checkLike(question, _F("_services._dns-sd._udp.local"), true); checkLike(question, _F("_dns-sd._udp.local"), true); checkLike(question, _F("_udp.local"), true); @@ -141,18 +111,6 @@ void onIndex(HttpRequest& request, HttpResponse& response) response.sendFile("index.html"); } -void onFile(HttpRequest& request, HttpResponse& response) -{ - String file = request.uri.getRelativePath(); - - if(file[0] == '.') { - response.code = HTTP_STATUS_FORBIDDEN; - } else { - response.setCache(86400, true); // It's important to use cache for better performance. - response.sendFile(file); - } -} - void startWebServer() { server.listen(80); @@ -180,6 +138,8 @@ void gotIP(IpAddress ip, IpAddress netmask, IpAddress gateway) startmDNS(); // Start mDNS "Advertise" of your hostname "test.local" for this example } +} // namespace + void init() { Serial.begin(SERIAL_BAUD_RATE); // 115200 by default diff --git a/samples/Ultrasonic_HCSR04/app/application.cpp b/samples/Ultrasonic_HCSR04/app/application.cpp index c807d33632..bd2ee80f43 100644 --- a/samples/Ultrasonic_HCSR04/app/application.cpp +++ b/samples/Ultrasonic_HCSR04/app/application.cpp @@ -14,8 +14,10 @@ #define TRIG_PIN 2 #define ECHO_PIN 5 -Timer procTimer; -Ultrasonic ultrasonic = Ultrasonic(); +namespace +{ +SimpleTimer procTimer; +Ultrasonic ultrasonic; void measure() { @@ -26,6 +28,8 @@ void measure() Serial.println(dist); } +} // namespace + void init() { Serial.begin(SERIAL_BAUD_RATE); diff --git a/samples/WebcamServer/app/application.cpp b/samples/WebcamServer/app/application.cpp index 84a1320747..320d808fdb 100644 --- a/samples/WebcamServer/app/application.cpp +++ b/samples/WebcamServer/app/application.cpp @@ -10,8 +10,10 @@ #define WIFI_PWD "PleaseEnterPass" #endif +namespace +{ HttpServer server; -FakeCamera* camera = nullptr; +FakeCamera* camera; /* * default http handler to check if server is up and running @@ -82,6 +84,8 @@ void startWebServer() server.paths.setDefault(onFile); } +} // namespace + void init() { Serial.begin(SERIAL_BAUD_RATE); // Enable serial diff --git a/samples/Websocket_Client/app/application.cpp b/samples/Websocket_Client/app/application.cpp index 0a4442966c..6e6b591b8a 100644 --- a/samples/Websocket_Client/app/application.cpp +++ b/samples/Websocket_Client/app/application.cpp @@ -23,7 +23,7 @@ namespace { WebsocketClient wsClient; -Timer msgTimer; +SimpleTimer msgTimer; // Number of messages to send const unsigned MESSAGES_TO_SEND = 5; diff --git a/samples/Wifi_Sniffer/app/application.cpp b/samples/Wifi_Sniffer/app/application.cpp index 14d6076349..d5c71d2fd0 100644 --- a/samples/Wifi_Sniffer/app/application.cpp +++ b/samples/Wifi_Sniffer/app/application.cpp @@ -3,8 +3,10 @@ #include "Platform/WifiSniffer.h" #include "Data/HexString.h" -static BeaconInfoList knownAPs; ///< List of known APs -static ClientInfoList knownClients; ///< List of known CLIENTs +namespace +{ +BeaconInfoList knownAPs; ///< List of known APs +ClientInfoList knownClients; ///< List of known CLIENTs const unsigned scanTimeoutMs = 2000; ///< End scan on channel if no new devices found within this time @@ -16,7 +18,7 @@ SimpleTimer timer; * Replace these with ?. * Return a String of exactly 32 characters. */ -static String makeSsidString(const uint8_t* ssid, size_t len) +String makeSsidString(const uint8_t* ssid, size_t len) { String s; s.pad(32); @@ -25,7 +27,7 @@ static String makeSsidString(const uint8_t* ssid, size_t len) return s; } -static void printBeacon(const BeaconInfo& beacon) +void printBeacon(const BeaconInfo& beacon) { if(beacon.err != 0) { Serial << _F("BEACON ERR: (") << beacon.err << ')' << endl; @@ -37,58 +39,68 @@ static void printBeacon(const BeaconInfo& beacon) Serial << makeHexString(beacon.ssid, 32) << " " << beacon.ssid_len << endl; } -static void printClient(const ClientInfo& client) +void printClient(const ClientInfo& client) { if(client.err != 0) { Serial << _F("CLIENT ERR: (") << client.err << ')' << endl; - } else { - Serial << _F("DEVICE: ") << client.station << _F(" ==> "); - - int i = knownAPs.indexOf(client.bssid); - if(i < 0) { - Serial << _F("Unknown/Malformed packet, BSSID = ") << client.bssid << endl; - } else { - auto& ap = knownAPs[i]; - String ssid = makeSsidString(ap.ssid, ap.ssid_len); - Serial << '[' << ssid << ']' << " " << client.ap << " " << String(ap.channel).padLeft(3) << " " - << String(client.rssi).padLeft(4) << endl; - } + return; } + + Serial << _F("DEVICE: ") << client.station << _F(" ==> "); + + int i = knownAPs.indexOf(client.bssid); + if(i < 0) { + Serial << _F("Unknown/Malformed packet, BSSID = ") << client.bssid << endl; + return; + } + + auto& ap = knownAPs[i]; + String ssid = makeSsidString(ap.ssid, ap.ssid_len); + Serial << '[' << ssid << ']' << " " << client.ap << " " << String(ap.channel).padLeft(3) << " " + << String(client.rssi).padLeft(4) << endl; } -static void printSummary() +void printSummary() { - Serial.println("\r\n" - "-------------------------------------------------------------------------------------\r\n"); + DEFINE_FSTR_LOCAL(SEPARATOR, + "\r\n" + "-------------------------------------------------------------------------------------\r\n") + + Serial.println(SEPARATOR); for(auto& client : knownClients) { printClient(client); } for(auto& ap : knownAPs) { printBeacon(ap); } - Serial.println("\r\n" - "-------------------------------------------------------------------------------------\r\n"); + Serial.println(SEPARATOR); } -static void onBeacon(const BeaconInfo& beacon) +void onBeacon(const BeaconInfo& beacon) { - if(knownAPs.indexOf(beacon.bssid) < 0) { - knownAPs.add(beacon); - printBeacon(beacon); - timer.restart(); + if(knownAPs.indexOf(beacon.bssid) >= 0) { + // Already listed + return; } + + knownAPs.add(beacon); + printBeacon(beacon); + timer.restart(); } -static void onClient(const ClientInfo& client) +void onClient(const ClientInfo& client) { - if(knownClients.indexOf(client.station) < 0) { - knownClients.add(client); - printClient(client); - timer.restart(); + if(knownClients.indexOf(client.station) >= 0) { + // Allready listed + return; } + + knownClients.add(client); + printClient(client); + timer.restart(); } -static void scanChannel(void* param) +void scanChannel(void* param) { auto channel = reinterpret_cast(param); if(channel <= 15) { @@ -97,21 +109,22 @@ static void scanChannel(void* param) sniffer.setChannel(channel); timer.initializeMs(scanChannel, reinterpret_cast(channel + 1)); timer.startOnce(); - } else { - // Stop sniffing and display final scan results - sniffer.end(); - printSummary(); + return; } + + // Stop sniffing and display final scan results + sniffer.end(); + printSummary(); } +} // namespace + void init() { Serial.begin(SERIAL_BAUD_RATE); // 115200 by default Serial.systemDebugOutput(true); // Debug output to serial - Serial << _F("\r\n\r\n" - "SDK version:") - << system_get_sdk_version() << endl; + Serial << endl << endl << _F("SDK version:") << system_get_sdk_version() << endl; Serial.println(_F("ESP8266 mini-sniff by Ray Burnette http://www.hackster.io/rayburne/projects")); Serial.println( _F("Type: /---------MAC---------/-----WiFi Access Point SSID-----/ /------MAC------/ Chnl RSSI"));