From 15d2700ca4dc118c8e1feca5d3fa02dda3eb46f6 Mon Sep 17 00:00:00 2001 From: mikee47 Date: Mon, 1 Apr 2024 21:24:28 +0100 Subject: [PATCH] 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_DateTime/app/application.cpp | 3 +- samples/Basic_DateTime/component.mk | 2 - 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 ++++++----- 88 files changed, 1325 insertions(+), 1040 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_DateTime/app/application.cpp b/samples/Basic_DateTime/app/application.cpp index d14405fdb6..2de8e3968d 100644 --- a/samples/Basic_DateTime/app/application.cpp +++ b/samples/Basic_DateTime/app/application.cpp @@ -87,7 +87,8 @@ void onRx(Stream& source, char arrivedChar, unsigned short availableCharsCount) 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; + Serial << endl + << _F("****Showing DateTime formatting options for Unix timestamp: ") << uint32_t(timestamp) << endl; showTime(timestamp); break; } 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 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"));