From 88d0be6ece2193038fd151ff9388314bd1291536 Mon Sep 17 00:00:00 2001 From: jandolina Date: Tue, 8 Aug 2023 18:27:37 -0700 Subject: [PATCH 1/2] Sync up with fork --- platformio.ini | 3 +- .../platformio_override.ini | 17 ++ .../usermod_v2_background.h | 36 +++ .../usermod_v2_tdisplay.h | 250 ++++++++++++++++++ wled00/const.h | 3 + wled00/fcn_declare.h | 8 + wled00/um_manager.cpp | 5 + wled00/usermods_list.cpp | 16 ++ wled00/wled.cpp | 29 ++ wled00/wled.h | 14 + 10 files changed, 380 insertions(+), 1 deletion(-) create mode 100644 usermods/usermod_v2_background/platformio_override.ini create mode 100644 usermods/usermod_v2_background/usermod_v2_background.h create mode 100644 usermods/usermod_v2_background/usermod_v2_tdisplay.h diff --git a/platformio.ini b/platformio.ini index 9321eea04f..42792753a1 100644 --- a/platformio.ini +++ b/platformio.ini @@ -11,7 +11,7 @@ # CI binaries ; default_envs = nodemcuv2, esp8266_2m, esp01_1m_full, esp32dev, esp32_eth # ESP32 variant builds are temporarily excluded from CI due to toolchain issues on the GitHub Actions Linux environment -default_envs = nodemcuv2, esp8266_2m, esp01_1m_full, esp32dev, esp32_eth, lolin_s2_mini, esp32c3dev, esp32s3dev_8MB, esp32s3dev_8MB_PSRAM_opi +; default_envs = nodemcuv2, esp8266_2m, esp01_1m_full, esp32dev, esp32_eth, lolin_s2_mini, esp32c3dev, esp32s3dev_8MB, esp32s3dev_8MB_PSRAM_opi # Release binaries ; default_envs = nodemcuv2, esp8266_2m, esp01_1m_full, esp32dev, esp32_eth, lolin_s2_mini, esp32c3dev, esp32s3dev_8MB @@ -42,6 +42,7 @@ default_envs = nodemcuv2, esp8266_2m, esp01_1m_full, esp32dev, esp32_eth, lolin_ ; default_envs = esp32s2_saola ; default_envs = esp32c3dev ; default_envs = lolin_s2_mini +default_envs = esp32dev_multi src_dir = ./wled00 data_dir = ./wled00/data diff --git a/usermods/usermod_v2_background/platformio_override.ini b/usermods/usermod_v2_background/platformio_override.ini new file mode 100644 index 0000000000..874ae3d745 --- /dev/null +++ b/usermods/usermod_v2_background/platformio_override.ini @@ -0,0 +1,17 @@ +[env:esp32dev_multi] +extends = env:esp32dev +upload_speed = 460800 +monitor_speed = 115200 +build_flags = ${env:esp32dev.build_flags} + -D WLED_ENABLE_BACKGROUND + -D USERMOD_BACKGROUND_V2 + -D USERMOD_BACKGROUND_TDISPLAY ; For TTGO T-Display + ; -D TFT_BRIGHTNESS = 100 ; Optional + ; -D TFT_TIMEOUT = 10000 ; Optional + +lib_deps = + ${esp32.lib_deps} + TFT_eSPI ; For TTGO T-Display + +; board_build.partitions = tools/WLED_ESP32_4MB_MIN_SPIFFS.csv + diff --git a/usermods/usermod_v2_background/usermod_v2_background.h b/usermods/usermod_v2_background/usermod_v2_background.h new file mode 100644 index 0000000000..a1ca8c660d --- /dev/null +++ b/usermods/usermod_v2_background/usermod_v2_background.h @@ -0,0 +1,36 @@ +#pragma once + +#include "wled.h" + +class UsermodBackgroundDemo : public Usermod { + private: + int backgroundIndex = 0; + int mainIndex = 0; + + public: + void setup() { + Serial.println("Setup - UsermodBackgroundDemo"); + } + + void doBackgroundWork(){ + backgroundIndex++; + Serial.println("B: " + String(backgroundIndex)); + } + + void backgroundLoop(){ + EVERY_N_MILLISECONDS(5000){ doBackgroundWork(); } + } + + void doMainWork(){ + mainIndex++; + Serial.println("M: " + String(mainIndex)); + } + + void loop() { + EVERY_N_MILLISECONDS(1000) { doMainWork(); } + } + + uint16_t getId() { + return USERMOD_ID_BACKGROUND; + } +}; \ No newline at end of file diff --git a/usermods/usermod_v2_background/usermod_v2_tdisplay.h b/usermods/usermod_v2_background/usermod_v2_tdisplay.h new file mode 100644 index 0000000000..d4b97faa53 --- /dev/null +++ b/usermods/usermod_v2_background/usermod_v2_tdisplay.h @@ -0,0 +1,250 @@ +/* +REQUIRED SETUP + +TFT_eSPI Library Adjustments (board selection) +You need to modify a file in the 'TFT_eSPI' library to select the correct board. +Locate the 'User_Setup_Select.h' file can be found in the '/.pio/libdeps/YOUR-BOARD/TFT_eSPI_ID1559' folder. + +Modify the 'User_Setup_Select.h' +Comment out the following line +//#include // Default setup is root library folder + +Uncomment the following line +#include // Setup file for ESP32 and TTGO T-Display ST7789V SPI bus TFT +*/ + +#pragma once + +#include "wled.h" +#include +#include +#include "WiFi.h" +#include + +#define PRESET_NAME_BUFFER_SIZE 25 + +#define TFT_MOSI 19 +#define TFT_SCLK 18 +#define TFT_CS 5 +#define TFT_DC 16 +#define TFT_RST 23 +#define TFT_BL 4 // Display backlight control pin +#define ADC_EN 14 // Used for enabling battery voltage measurements - not used in this program +#define TFT_CH 6 + +TFT_eSPI tft = TFT_eSPI(135, 240); // Invoke custom library + +class UsermodBackgroundTDisplay : public Usermod { + + private: + #ifdef TFT_BRIGHTNESS + uint16_t tftBrightness = TFT_BRIGHTNESS; + #else + // 0=OFF; 255=MAX + uint16_t tftBrightness = 50; + #endif + + #ifdef TFT_TIMEOUT + uint16_t tftTimeout = TFT_TIMEOUT; + #else + uint16_t tftTimeout = 10000; + #endif + + bool initDone = false; + bool needRedraw = true; + + // TTGO T-Display + String knownSsid = ""; + IPAddress knownIp; + uint8_t knownBrightness = 0; + uint8_t knownMode = 0; + uint8_t knownPalette = 0; + uint8_t tftcharwidth = 19; // Number of chars that fit on screen with text size set to 2 + unsigned long tftNextTimeout = 0; + + // strings to reduce flash memory usage (used more than twice) + static const char _strTag[]; + static const char _strBrightness[]; + static const char _strName[]; + static const char _strTimeout[]; + + public: + + void setBrightness(uint32_t newBrightness) { + ledcWrite(TFT_CH, newBrightness); // 0-15, 0-255 (with 8 bit resolution); 0=totally dark;255=totally shiny + } + + void initDisplay(int rotation=3) { + pinMode(TFT_BL, OUTPUT); + ledcSetup(TFT_CH, 5000, 8); // 0-15, 5000, 8 + ledcAttachPin(TFT_BL, TFT_CH); // TFT_BL, 0 - 15 + + tft.init(); + tft.setRotation(rotation); //Rotation here is set up for the text to be readable with the port on the left. Use 1 to flip. + tft.fillScreen(TFT_BLACK); + tft.setTextSize(2); + tft.setTextColor(TFT_WHITE); + tft.setCursor(1, 10); + tft.setTextDatum(MC_DATUM); + tft.setTextSize(2); + tft.print("Init..."); + } + + void updateDisplay(){ + // Check if values which are shown on display changed from the last time. + if (((apActive) ? String(apSSID) : WiFi.SSID()) != knownSsid) { + needRedraw = true; + } else if (knownIp != (apActive ? IPAddress(4, 3, 2, 1) : WiFi.localIP())) { + needRedraw = true; + } else if (knownBrightness != bri) { + needRedraw = true; + } else if (knownMode != strip.getMainSegment().mode) { + needRedraw = true; + } else if (knownPalette != strip.getMainSegment().palette) { + needRedraw = true; + } + + if(tftNextTimeout < millis()) + setBrightness(0); + + if (!needRedraw) return; + + knownSsid = WiFi.SSID(); + knownIp = apActive ? IPAddress(4, 3, 2, 1) : WiFi.localIP(); + knownBrightness = bri; + knownMode = strip.getMainSegment().mode; + knownPalette = strip.getMainSegment().palette; + + tft.fillScreen(TFT_BLACK); + tft.setTextSize(2); + tft.setTextColor(TFT_YELLOW); + + // First row + tft.setCursor(1, 1); + tft.print(apSSID); + //tft.print(knownSsid.substring(0, tftcharwidth > 1 ? tftcharwidth - 1 : 0)); + //if (knownSsid.length() > tftcharwidth) + // tft.print("~"); + + // Second row + tft.setTextSize(2); + tft.setCursor(1, 24); + tft.setTextColor(TFT_GREENYELLOW); + + if (apActive) { + tft.print("AP IP: "); + tft.print(knownIp); + tft.setCursor(1,46); + tft.print("AP Pass:"); + tft.print(apPass); + } + else { + tft.print("IP: "); + tft.print(knownIp); + tft.setCursor(1,46); + tft.setTextColor(TFT_GREEN); + tft.print("Bright: "); + tft.print(((float(bri)/255)*100)); + tft.print("%"); + } + + // Third row + tft.setCursor(1, 68); + tft.setTextColor(TFT_SKYBLUE); + char lineBuffer[tftcharwidth+1]; + extractModeName(knownMode, JSON_mode_names, lineBuffer, tftcharwidth); + tft.print(lineBuffer); + + // Fourth row + tft.setCursor(1, 90); + tft.setTextColor(TFT_BLUE); + tft.print(strip.currentMilliamps); + tft.print("mA est."); + + // Fifth row + tft.setCursor(1, 112); + tft.setTextColor(TFT_VIOLET); + extractModeName(knownPalette, JSON_palette_names, lineBuffer, tftcharwidth); + tft.print(lineBuffer); + + + needRedraw = false; + tftNextTimeout = tftTimeout + millis(); + setBrightness(tftBrightness); + } + + void setup() { + Serial.println("Setup - UsermodBackgroundTDisplay"); + + initDisplay(); + setBrightness(tftBrightness); + needRedraw = true; + initDone = true; + } + + // gets called every time WiFi is (re-)connected. Initialize own network interfaces here + void connected() { + needRedraw = true; + } + + void doWork(){ + if(needRedraw) + updateDisplay(); + + EVERY_N_MILLISECONDS(5000){ updateDisplay(); } + } + + void backgroundLoop(){ + // Calling doWork on the background thread + // Only call it from one thread + doWork(); + } + + void loop(){ + // Calling doWork on the main thread + // Only call it from one thread + // doWork(); + } + + // fired upon WLED state change + void onStateChange(uint8_t mode) { + needRedraw = true; + } + + // Add JSON entries that go to cfg.json + void addToConfig(JsonObject &root) + { + JsonObject top = root.createNestedObject(FPSTR(_strTag)); + top[FPSTR(_strBrightness)] = tftBrightness; + top[FPSTR(_strTimeout)] = tftTimeout; + } + + // Read JSON entries that go to cfg.json + bool readFromConfig(JsonObject &root) + { + JsonObject top = root[FPSTR(_strTag)]; + + if (top.isNull()) { + DEBUG_PRINTLN(FPSTR(_strTag)); + return false; + } + + bool configComplete = !top.isNull(); + configComplete &= getJsonValue(top[FPSTR(_strBrightness)], tftBrightness); + configComplete &= getJsonValue(top[FPSTR(_strTimeout)], tftTimeout); + + return configComplete; + } + + /* + * getId() allows you to optionally give your V2 usermod an unique ID. + */ + uint16_t getId() { + return USERMOD_ID_TDISPLAY; + } +}; + +// strings to reduce flash memory usage (used more than twice) +const char UsermodBackgroundTDisplay::_strTag[] PROGMEM = "tdesp32"; +const char UsermodBackgroundTDisplay::_strBrightness[] PROGMEM = "TFT Brightness"; +const char UsermodBackgroundTDisplay::_strTimeout[] PROGMEM = "TFT Timeout"; \ No newline at end of file diff --git a/wled00/const.h b/wled00/const.h index aa256ebe0e..bac1e17d8a 100644 --- a/wled00/const.h +++ b/wled00/const.h @@ -150,6 +150,9 @@ #define USERMOD_ID_KLIPPER 40 // Usermod Klipper percentage #define USERMOD_ID_WIREGUARD 41 //Usermod "wireguard.h" +#define USERMOD_ID_BACKGROUND 50 //Usermod "usermod_v2_background.h" +#define USERMOD_ID_TDISPLAY 51 //Usermod "usermod_v2_tdisplay.h" + //Access point behavior #define AP_BEHAVIOR_BOOT_NO_CONN 0 //Open AP when no connection after boot #define AP_BEHAVIOR_NO_CONN 1 //Open when no connection (either after boot or if connection is lost) diff --git a/wled00/fcn_declare.h b/wled00/fcn_declare.h index c67fdbf387..88dd244aaa 100644 --- a/wled00/fcn_declare.h +++ b/wled00/fcn_declare.h @@ -296,6 +296,10 @@ class Usermod { virtual void onUpdateBegin(bool) {} // fired prior to and after unsuccessful firmware update virtual void onStateChange(uint8_t mode) {} // fired upon WLED state change virtual uint16_t getId() {return USERMOD_ID_UNSPECIFIED;} + + #if defined(WLED_ENABLE_BACKGROUND) + virtual void backgroundLoop() {} + #endif }; class UsermodManager { @@ -323,6 +327,10 @@ class UsermodManager { bool add(Usermod* um); Usermod* lookup(uint16_t mod_id); byte getModCount() {return numMods;}; + + #if defined(WLED_ENABLE_BACKGROUND) + void backgroundLoop(); + #endif }; //usermods_list.cpp diff --git a/wled00/um_manager.cpp b/wled00/um_manager.cpp index 2e176bc30a..20a7ca4ee0 100644 --- a/wled00/um_manager.cpp +++ b/wled00/um_manager.cpp @@ -9,6 +9,11 @@ void UsermodManager::connected() { for (byte i = 0; i < numMods; i++) um void UsermodManager::loop() { for (byte i = 0; i < numMods; i++) ums[i]->loop(); } void UsermodManager::handleOverlayDraw() { for (byte i = 0; i < numMods; i++) ums[i]->handleOverlayDraw(); } void UsermodManager::appendConfigData() { for (byte i = 0; i < numMods; i++) ums[i]->appendConfigData(); } + +#if defined(WLED_ENABLE_BACKGROUND) +void UsermodManager::backgroundLoop() { for (byte i = 0; i < numMods; i++) ums[i]->backgroundLoop(); } +#endif + bool UsermodManager::handleButton(uint8_t b) { bool overrideIO = false; for (byte i = 0; i < numMods; i++) { diff --git a/wled00/usermods_list.cpp b/wled00/usermods_list.cpp index c099658a5e..e132f793df 100644 --- a/wled00/usermods_list.cpp +++ b/wled00/usermods_list.cpp @@ -10,6 +10,13 @@ * \/ \/ \/ */ //#include "../usermods/EXAMPLE_v2/usermod_v2_example.h" +#ifdef USERMOD_BACKGROUND_V2 + #include "../usermods/usermod_v2_background/usermod_v2_background.h" +#endif + +#ifdef USERMOD_BACKGROUND_TDISPLAY + #include "../usermods/usermod_v2_background/usermod_v2_tdisplay.h" +#endif #ifdef USERMOD_BATTERY #include "../usermods/Battery/usermod_v2_Battery.h" @@ -202,6 +209,15 @@ void registerUsermods() * \/ \/ \/ */ //usermods.add(new MyExampleUsermod()); + #ifdef USERMOD_BACKGROUND_V2 + usermods.add(new UsermodBackgroundDemo()); + #endif + + #ifdef USERMOD_BACKGROUND_TDISPLAY + #include + usermods.add(new UsermodBackgroundTDisplay()); + #endif + #ifdef USERMOD_BATTERY usermods.add(new UsermodBattery()); #endif diff --git a/wled00/wled.cpp b/wled00/wled.cpp index 1c1ee97679..1345b09bc4 100644 --- a/wled00/wled.cpp +++ b/wled00/wled.cpp @@ -285,6 +285,31 @@ DEBUG_PRINTLN(F("Watchdog: disabled")); #endif } +#if defined(WLED_ENABLE_BACKGROUND) +void WLED::backgroundTask(void *parameter) +{ + // WLED *instance = static_cast(parameter); + + for (;;) + { + // instance->usermods.backgroundLoop(); + usermods.backgroundLoop(); + EVERY_N_MILLISECONDS(150){ delay(1); } + } +} + +void WLED::backgroundSetup(){ + xTaskCreatePinnedToCore( + backgroundTask, /* Task function. */ + "BGTask", /* name of task. */ + backgroundStackSize, /* Stack size of task */ + this, /* parameter of the task */ + 1, /* priority of the task */ + &BackgroundTaskHandle, /* Task handle to keep track of created task */ + xPortGetCoreID() == 0 ? 1 : 0); /* pin task to other core */ +} +#endif + void WLED::setup() { #if defined(ARDUINO_ARCH_ESP32) && defined(WLED_DISABLE_BROWNOUT_DET) @@ -496,6 +521,10 @@ void WLED::setup() #if defined(ARDUINO_ARCH_ESP32) && defined(WLED_DISABLE_BROWNOUT_DET) WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 1); //enable brownout detector #endif + + #if defined(WLED_ENABLE_BACKGROUND) + backgroundSetup(); + #endif } void WLED::beginStrip() diff --git a/wled00/wled.h b/wled00/wled.h index 789a073096..165d51b20a 100644 --- a/wled00/wled.h +++ b/wled00/wled.h @@ -832,6 +832,15 @@ WLED_GLOBAL volatile uint8_t jsonBufferLock _INIT(0); class WLED { public: + #if defined(WLED_ENABLE_BACKGROUND) + TaskHandle_t BackgroundTaskHandle; + #if defined(BACKGROUND_STACK_SIZE) + uint16_t backgroundStackSize = BACKGROUND_STACK_SIZE; + #else + uint16_t backgroundStackSize = 10000; + #endif + #endif + WLED(); static WLED& instance() { @@ -854,5 +863,10 @@ class WLED { void handleStatusLED(); void enableWatchdog(); void disableWatchdog(); + + #if defined(WLED_ENABLE_BACKGROUND) + static void backgroundTask(void *parameter); + void backgroundSetup(); + #endif }; #endif // WLED_H From 7aebb7855f776fffa182068741e86dbda3906f8c Mon Sep 17 00:00:00 2001 From: jandolina Date: Tue, 8 Aug 2023 18:30:08 -0700 Subject: [PATCH 2/2] Commenting out the ttgo display --- usermods/usermod_v2_background/platformio_override.ini | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/usermods/usermod_v2_background/platformio_override.ini b/usermods/usermod_v2_background/platformio_override.ini index 874ae3d745..ec29eeeab7 100644 --- a/usermods/usermod_v2_background/platformio_override.ini +++ b/usermods/usermod_v2_background/platformio_override.ini @@ -5,13 +5,13 @@ monitor_speed = 115200 build_flags = ${env:esp32dev.build_flags} -D WLED_ENABLE_BACKGROUND -D USERMOD_BACKGROUND_V2 - -D USERMOD_BACKGROUND_TDISPLAY ; For TTGO T-Display + ; -D USERMOD_BACKGROUND_TDISPLAY ; For TTGO T-Display ; -D TFT_BRIGHTNESS = 100 ; Optional ; -D TFT_TIMEOUT = 10000 ; Optional lib_deps = ${esp32.lib_deps} - TFT_eSPI ; For TTGO T-Display +; TFT_eSPI ; For TTGO T-Display ; board_build.partitions = tools/WLED_ESP32_4MB_MIN_SPIFFS.csv