From cc2269299258d6eb59f598063b2f0fd2a2af0f26 Mon Sep 17 00:00:00 2001 From: Djuri Baars Date: Sun, 25 Jun 2023 15:06:29 +0200 Subject: [PATCH] Implemented LED brightness setting, started I2C slave code, made reset pin consistent with wiring for DOIT DevKit. --- data/src/css/style.scss | 2 ++ data/src/index.html | 6 +++++ data/src/js/script.ts | 1 + src/lib/functions.cpp | 59 ++++++++++++++++++++++++++++++++--------- src/lib/functions.hpp | 3 +++ src/lib/webserver.cpp | 12 ++++++++- src/main.cpp | 39 ++++++++++++++++----------- src/tasks/epd.cpp | 2 +- 8 files changed, 94 insertions(+), 30 deletions(-) diff --git a/data/src/css/style.scss b/data/src/css/style.scss index eed2930..29fc807 100644 --- a/data/src/css/style.scss +++ b/data/src/css/style.scss @@ -4,6 +4,8 @@ @import "../node_modules/bootstrap/scss/variables"; @import "../node_modules/bootstrap/scss/variables-dark"; +$form-range-track-bg: #fff; + @import "../node_modules/bootstrap/scss/mixins"; @import "../node_modules/bootstrap/scss/maps"; @import "../node_modules/bootstrap/scss/utilities"; diff --git a/data/src/index.html b/data/src/index.html index b6ac987..7feae8f 100644 --- a/data/src/index.html +++ b/data/src/index.html @@ -133,6 +133,12 @@ +
+ +
+ +
+
diff --git a/data/src/js/script.ts b/data/src/js/script.ts index 7768ebc..24ce8f9 100644 --- a/data/src/js/script.ts +++ b/data/src/js/script.ts @@ -51,6 +51,7 @@ fetch('/api/settings', { } document.getElementById('timePerScreen').value = jsonData.timerSeconds / 60; + document.getElementById('ledBrightness').value = jsonData.ledBrightness; var source = document.getElementById("screens-template").innerHTML; var template = Handlebars.compile(source); diff --git a/src/lib/functions.cpp b/src/lib/functions.cpp index d029ad7..de72f1f 100644 --- a/src/lib/functions.cpp +++ b/src/lib/functions.cpp @@ -7,7 +7,7 @@ std::map screenNameMap; #ifndef NO_MCP Adafruit_MCP23X17 mcp; -const int MCP_INT_PIN = 8; +const int MCP_INT_PIN = 8; #endif bool timerRunning = true; @@ -51,17 +51,13 @@ void setupComponents() else { Serial.println("MCP23017 ok"); - pinMode(MCP_INT_PIN, INPUT); + pinMode(MCP_INT_PIN, INPUT); mcp.setupInterrupts(true, false, LOW); } #endif #ifdef WITH_RGB_LED pixels.begin(); - pixels.setPixelColor(0, pixels.Color(255, 0, 0)); - pixels.setPixelColor(1, pixels.Color(0, 255, 0)); - pixels.setPixelColor(2, pixels.Color(0, 0, 255)); - pixels.setPixelColor(3, pixels.Color(255, 255, 255)); pixels.show(); #endif @@ -114,13 +110,21 @@ void setupPreferences() bgColor = preferences.getUInt("bgColor", DEFAULT_BG_COLOR); preferences.getBool("ledFlashOnUpd", false); - screenNameMap = {{SCREEN_BLOCK_HEIGHT, "Block Height"}, {SCREEN_MSCW_TIME, "Sats per dollar"}, {SCREEN_BTC_TICKER, "Ticker"}, {SCREEN_TIME, "Time"}, {SCREEN_HALVING_COUNTDOWN, "Halving countdown"}}; +#ifdef WITH_RGB_LED + pixels.setBrightness(preferences.getUInt("ledBrightness", 128)); + pixels.setPixelColor(0, pixels.Color(255, 0, 0)); + pixels.setPixelColor(1, pixels.Color(0, 255, 0)); + pixels.setPixelColor(2, pixels.Color(0, 0, 255)); + pixels.setPixelColor(3, pixels.Color(255, 255, 255)); + pixels.show(); +#endif + for (int i = 0; i < screenNameMap.size(); i++) { String key = "screen" + String(i) + "Visible"; @@ -154,7 +158,8 @@ void handleScreenTasks(uint screen) switch (currentScreen) { case SCREEN_BLOCK_HEIGHT: - if (blockNotifyTaskHandle) { + if (blockNotifyTaskHandle) + { vTaskResume(blockNotifyTaskHandle); } break; @@ -171,7 +176,8 @@ void handleScreenTasks(uint screen) vTaskResume(getPriceTaskHandle); break; case SCREEN_TIME: - if (minuteTaskHandle) { + if (minuteTaskHandle) + { TimeScreen::onActivate(); vTaskResume(minuteTaskHandle); } @@ -212,8 +218,9 @@ void timebasedChangeTask(void *parameter) } } -int modulo(int x,int N) { - return (x % N + N) %N; +int modulo(int x, int N) +{ + return (x % N + N) % N; } void nextScreen() @@ -236,7 +243,7 @@ void previousScreen() while (!preferences.getBool(key.c_str(), true)) { - newCurrentScreen = modulo(newCurrentScreen - 1, screenCount); + newCurrentScreen = modulo(newCurrentScreen - 1, screenCount); key = "screen" + String(newCurrentScreen) + "Visible"; } setCurrentScreen(newCurrentScreen); @@ -252,4 +259,32 @@ void setLights(int r, int g, int b) pixels.show(); #endif +} + +void setupI2C() +{ + bool slaveMode = preferences.getBool("I2CSlaveMode", false); + + if (slaveMode) + { + Serial.println("I2C Slave Mode enabled"); + Wire.onReceive(onI2CReceive); + Wire.begin((uint8_t)I2C_DEV_ADDR); + } +} + +void onI2CReceive(int len) +{ + Serial.printf("onReceive[%d]: ", len); + while (Wire.available()) + { + Serial.write(Wire.read()); + } + Serial.println(); +} + +void onI2CRequest() +{ + Wire.print("I2C Packets."); + Serial.println("onRequest"); } \ No newline at end of file diff --git a/src/lib/functions.hpp b/src/lib/functions.hpp index ceb5848..40fb8f6 100644 --- a/src/lib/functions.hpp +++ b/src/lib/functions.hpp @@ -16,6 +16,9 @@ void setupWifi(); void synchronizeTime(); void setupPreferences(); void setLights(int r, int g, int b); +void setupI2C(); +void onI2CReceive(int len); +void onI2CRequest(); uint getCurrentScreen(); void setCurrentScreen(uint screen); diff --git a/src/lib/webserver.cpp b/src/lib/webserver.cpp index 2fd4fe2..d9c6215 100644 --- a/src/lib/webserver.cpp +++ b/src/lib/webserver.cpp @@ -153,7 +153,7 @@ void onApiSettingsGet(AsyncWebServerRequest *request) root["epdColors"] = 3; #endif root["ledFlashOnUpdate"] = preferences.getBool("ledFlashOnUpd", false); - + root["ledBrightness"] = preferences.getUInt("ledBrightness", 128); JsonArray screens = root.createNestedArray("screens"); for (int i = 0; i < screenNameMap.size(); i++) @@ -208,6 +208,16 @@ void onApiSettingsPost(AsyncWebServerRequest *request) settingsChanged = true; } + if (request->hasParam("ledBrightness", true)) + { + AsyncWebParameter *ledBrightness = request->getParam("ledBrightness", true); + + preferences.putUInt("ledBrightness", ledBrightness->value().toInt()); + Serial.print("Setting brightness to "); + Serial.println(ledBrightness->value().c_str()); + settingsChanged = true; + } + for (int i = 0; i < screenNameMap.size(); i++) { String key = "screen[" + String(i) + "]"; diff --git a/src/main.cpp b/src/main.cpp index 3b9e505..30f123d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3,7 +3,7 @@ #include #include #include -//#include +// #include #include #ifdef CONFIG_BT_ENABLED @@ -54,30 +54,37 @@ void setup() #endif setupWifi(); - synchronizeTime(); - setupWebserver(); + bool slaveMode = preferences.getBool("I2CSlaveMode", false); - TimeScreen::init(); - BlockHeightScreen::init(); - TickerScreen::init(); - SatsPerDollarScreen::init(); + if (!slaveMode) + { + synchronizeTime(); + setupWebserver(); + + TimeScreen::init(); + BlockHeightScreen::init(); + TickerScreen::init(); + SatsPerDollarScreen::init(); #ifdef WITH_BUTTONS - setupButtonTask(); + setupButtonTask(); #endif #ifdef WITH_RGB_LED - setLights(0,0,0); - setupLedHandlerTask(); + setLights(0, 0, 0); + setupLedHandlerTask(); #endif - registerNewMinuteCallback(TimeScreen::onNewMinute); - registerNewBlockCallback(BlockHeightScreen::onNewBlock); - registerNewBlockCallback(HalvingCountdownScreen::onNewBlock); - registerNewPriceCallback(TickerScreen::onPriceUpdate); - registerNewPriceCallback(SatsPerDollarScreen::onPriceUpdate); + registerNewMinuteCallback(TimeScreen::onNewMinute); + registerNewBlockCallback(BlockHeightScreen::onNewBlock); + registerNewBlockCallback(HalvingCountdownScreen::onNewBlock); + registerNewPriceCallback(TickerScreen::onPriceUpdate); + registerNewPriceCallback(SatsPerDollarScreen::onPriceUpdate); - setupDisplays(); + setupDisplays(); + } else { + setupI2C(); + } } void loop() diff --git a/src/tasks/epd.cpp b/src/tasks/epd.cpp index ce8e8be..0483f32 100644 --- a/src/tasks/epd.cpp +++ b/src/tasks/epd.cpp @@ -41,7 +41,7 @@ const int EPD_BUSY[7] = {36, 39, 34, 35, 32, 33, 25}; const int EPD_RESET_MPD[7] = {8, 9, 10, 11, 12, 13, 14}; const int EPD_DC = 22; -const int RST_PIN = 12; +const int RST_PIN = 2; #endif #ifdef IS_BW