diff --git a/include/Uhr.h b/include/Uhr.h index fa87f528..797ee05f 100644 --- a/include/Uhr.h +++ b/include/Uhr.h @@ -151,6 +151,11 @@ struct GLOBAL { bool bootShowWifi; bool bootShowIP; + uint8_t LEDpin; + uint8_t powerButton; + uint8_t modeButton; + uint8_t speedButton; + Birthday birthday[MAX_BIRTHDAY_COUNT]; }; GLOBAL G = {}; @@ -254,6 +259,7 @@ enum CommandWords { COMMAND_SET_AUTO_BRIGHT = 102, COMMAND_SET_LAYOUT_VARIANT = 103, COMMAND_SET_MQTT_HA_DISCOVERY = 104, + COMMAND_SET_GPIO = 105, COMMAND_SPEED = 152, diff --git a/include/clockWork.h b/include/clockWork.h index 9264870e..6fa8a2de 100644 --- a/include/clockWork.h +++ b/include/clockWork.h @@ -8,11 +8,17 @@ class ClockWork { uint32_t previousMillis = 0; uint32_t lux = 0; + // Variable to store the time when the button was pressed + unsigned long buttonPressStart = 0; + // State of the button + bool buttonPressed = false; + private: //------------------------------------------------------------------------------ // Helper Functions //------------------------------------------------------------------------------ void loopAutoBrightLogic(); + void loopGPIOinput(); uint32_t num32BitWithOnesAccordingToColumns(); bool isRomanLanguage(); diff --git a/include/clockWork.hpp b/include/clockWork.hpp index 35164984..78b7c6e0 100644 --- a/include/clockWork.hpp +++ b/include/clockWork.hpp @@ -104,6 +104,86 @@ void ClockWork::loopAutoBrightLogic() { //------------------------------------------------------------------------------ +void ClockWork::loopGPIOinput() { + // Read the power button state + int powerButtonState = digitalRead(G.powerButton); + + // Button is pressed + if (powerButtonState == HIGH && !buttonPressed) { + buttonPressed = true; // Button is now pressed + // Set off if brightness is set, set on if brightness is 0 + if (G.color[0].B > 0) { + for (uint8_t i = 0; i < 3; i++) { + G.color[i].B = 0; + } + } else { + for (uint8_t i = 0; i < 3; i++) { + G.color[i].B = 1; + } + } + } + + // Read the mode button state + int modeButtonState = digitalRead(G.modeButton); + + // Check if the button is pressed + if (modeButtonState == LOW && !buttonPressed) { + buttonPressed = true; // Button is now pressed + buttonPressStart = millis(); // Record the start time of the press + } + + // Check if the button is released + if (modeButtonState == HIGH && buttonPressed) { + buttonPressed = false; // Button is released + unsigned long pressDuration = millis() - buttonPressStart; + + if (pressDuration < 2000) { // Short press duration threshold (2000 ms) + // set next mode in range 1 - 8 + G.prog = (G.prog % 8) + 1; + } else { + // set transition in range 0 - 12 + G.transitionType = (G.transitionType + 1) % 13; + } + } + + // Read the speed button state + int speedButtonState = digitalRead(G.speedButton); + + // Check if the button is pressed + if (speedButtonState == LOW && !buttonPressed) { + buttonPressed = true; // Button is now pressed + buttonPressStart = millis(); // Record the start time of the press + } + + // Check if the button is released + if (speedButtonState == HIGH && buttonPressed) { + buttonPressed = false; // Button is released + unsigned long pressDuration = millis() - buttonPressStart; + + if (pressDuration < 2000) { // Short press duration threshold (2000 ms) + // Set brightness in range 10% - 100% with step 10% + if (G.color[0].B == 1) { + G.color[0].B = 0.1; + } else if (G.color[0].B >= 0.9) { + G.color[0].B = 1; + } else { + G.color[0].B += 0.1; + } + } else { + // Set hue in range 0 - 360 with step 30 + if (G.color[0].H == 360) { + G.color[0].B = 0; + } else if (G.color[0].B >= 330) { + G.color[0].B = 360; + } else { + G.color[0].B += 30; + } + } + } +} + +//------------------------------------------------------------------------------ + iUhrType *ClockWork::getPointer(uint8_t type) { switch (type) { case Ger10x11: @@ -185,11 +265,13 @@ void ClockWork::initLedStrip(uint8_t num) { } if (strip_RGBW == NULL) { #ifdef ESP8266 - strip_RGBW = new NeoPixelBus(500); + strip_RGBW = + new NeoPixelBus( + 500, G.LEDpin); #elif defined(ESP32) - pinMode(LED_PIN, OUTPUT); + pinMode(G.LEDpin, OUTPUT); strip_RGBW = - new NeoPixelBus(500, LED_PIN); + new NeoPixelBus(500, G.LEDpin); #endif strip_RGBW->Begin(); } @@ -201,11 +283,13 @@ void ClockWork::initLedStrip(uint8_t num) { } if (strip_RGB == NULL) { #ifdef ESP8266 - strip_RGB = new NeoPixelBus(500); + strip_RGB = + new NeoPixelBus(500, G.LEDpin); #elif defined(ESP32) - pinMode(LED_PIN, OUTPUT); + pinMode(G.LEDpin, OUTPUT); strip_RGB = new NeoPixelBus( - 500, LED_PIN); + 500, G.LEDpin); #endif strip_RGB->Begin(); } @@ -1416,6 +1500,23 @@ void ClockWork::loop(struct tm &tm) { break; } + case COMMAND_SET_GPIO: { + led.clear(); + + // Print new types + Serial.printf("Clock LED pin: GPIO%u\n", G.LEDpin); + Serial.printf("Clock power button: GPIO%u\n", G.powerButton); + Serial.printf("Clock mode button: GPIO%u\n", G.modeButton); + Serial.printf("Clock speed button: GPIO%u\n", G.speedButton); + + eeprom::write(); + initLedStrip(G.Colortype); + + clearClockByProgInit(); + parametersChanged = true; + break; + } + case COMMAND_SET_COLORTYPE: { // G.param1 sets new Colortype Serial.printf("LED Colortype: %u\n", G.param1); diff --git a/include/config.h b/include/config.h index 062f236a..bccac926 100644 --- a/include/config.h +++ b/include/config.h @@ -7,6 +7,9 @@ // PIN Configuration //-------------------------------------------------------------------------- #define LED_PIN 3 // Use direct pin number +#define POWER_BUTTON 2 // Use direct pin number +#define MODE_BUTTON 13 // Use direct pin number +#define SPEED_BUTTON 14 // Use direct pin number #define SDA_PIN_ESP32 21 // Use direct pin number #define SCL_PIN_ESP32 22 // Use direct pin number diff --git a/include/webPageAdapter.h b/include/webPageAdapter.h index 80a6d078..20e8b92c 100644 --- a/include/webPageAdapter.h +++ b/include/webPageAdapter.h @@ -459,6 +459,18 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t *payload, //------------------------------------------------------------------------------ + case COMMAND_SET_GPIO: { + G.progInit = true; + + G.LEDpin = split(payload, 3); + G.powerButton = split(payload, 6); + G.modeButton = split(payload, 9); + G.speedButton = split(payload, 12); + break; + } + + //------------------------------------------------------------------------------ + case COMMAND_SET_BIRTHDAYS: { for (uint8_t i = 0; i < MAX_BIRTHDAY_COUNT; i++) { diff --git a/src/Wortuhr.cpp b/src/Wortuhr.cpp index c42ef107..1a16ecbc 100644 --- a/src/Wortuhr.cpp +++ b/src/Wortuhr.cpp @@ -36,8 +36,8 @@ iUhrType *usedUhrType = nullptr; #include "NeoMultiFeature.hpp" #ifdef ESP8266 -NeoPixelBus *strip_RGB = NULL; -NeoPixelBus *strip_RGBW = NULL; +NeoPixelBus *strip_RGB = NULL; +NeoPixelBus *strip_RGBW = NULL; #elif defined(ESP32) NeoPixelBus *strip_RGBW = NULL; NeoPixelBus *strip_RGB = NULL; @@ -159,6 +159,9 @@ void setup() { //------------------------------------- #if GENERAL_VERBOSE Serial.begin(115200); + pinMode(G.powerButton, INPUT_PULLUP); + pinMode(G.modeButton, INPUT_PULLUP); + pinMode(G.speedButton, INPUT_PULLUP); Serial.println(""); Serial.println("--------------------------------------"); Serial.println("Begin Setup"); @@ -272,6 +275,11 @@ void setup() { G.transitionColorize = 0; G.transitionDemo = false; + G.LEDpin = LED_PIN; + G.powerButton = POWER_BUTTON; + G.modeButton = MODE_BUTTON; + G.speedButton = SPEED_BUTTON; + for (uint8_t i = 0; i < MAX_BIRTHDAY_COUNT; i++) { G.birthday[i].day = 1; G.birthday[i].month = 1; diff --git a/webpage/index.html b/webpage/index.html index 34a1bb33..69089631 100644 --- a/webpage/index.html +++ b/webpage/index.html @@ -542,6 +542,86 @@

+
+

+
+
+
+ + +
+
+ +
+
+ +
+
+ +
+
+ +
+
+
+
+

diff --git a/webpage/language/de.js b/webpage/language/de.js index 672ad21a..23dc19ef 100644 --- a/webpage/language/de.js +++ b/webpage/language/de.js @@ -219,6 +219,30 @@ let TRANSLATION_DE_DE = { "label": "Verbindungsstatus", "connect": "Verbinden" }, + "hardware-config": { + "h2": "Hardware Einstellung", + "help": "Hier kann der verwendete Pin für den LED-Streifen eingestellt werden.\n " + + "Hinweis: Es muss die GPIO-Nummer verwendet werden, nicht die aufgedruckte Zahl auf dem Board!\n " + + "Möglicherweise muss die Wortuhr neu gestartet werden.", + "led-pin": "LED pin", + "power-button": "Power Button", + "mode-button": "Mode Button", + "speed-button": "Speed Button", + "GPIO2": "GPIO2", + "GPIO3": "GPIO3", + "GPIO4": "GPIO4", + "GPIO5": "GPIO5", + "GPIO6": "GPIO6", + "GPIO7": "GPIO7", + "GPIO8": "GPIO8", + "GPIO9": "GPIO9", + "GPIO10": "GPIO10", + "GPIO13": "GPIO13", + "GPIO14": "GPIO14", + "GPIO20": "GPIO20", + "GPIO21": "GPIO21", + "save": "Einstellung speichern" + }, "led-type": { "h2": "LED-Typ", "help": "Hier können Sie den Typ des verwendeten LED-Streifens (WS2812 oder SK6812) einstellen.\n" + diff --git a/webpage/language/en.js b/webpage/language/en.js index 386cebe7..39375da8 100644 --- a/webpage/language/en.js +++ b/webpage/language/en.js @@ -219,6 +219,30 @@ let TRANSLATION_EN_US = { "label": "Connection Status", "connect": "Connect" }, + "hardware-config": { + "h2": "Hardware configuration", + "help": "Here you can set the pin used for the LED strip.\n " + + "Note: You need to set the GPIO pin, not the pin printed on your board!\n " + + "It may be neccessary to restart your Wordclock.", + "led-pin": "LED pin", + "power-button": "Power Button", + "mode-button": "Mode Button", + "speed-button": "Speed Button", + "GPIO2": "GPIO2", + "GPIO3": "GPIO3", + "GPIO4": "GPIO4", + "GPIO5": "GPIO5", + "GPIO6": "GPIO6", + "GPIO7": "GPIO7", + "GPIO8": "GPIO8", + "GPIO9": "GPIO9", + "GPIO10": "GPIO10", + "GPIO13": "GPIO13", + "GPIO14": "GPIO14", + "GPIO20": "GPIO20", + "GPIO21": "GPIO21", + "save": "Save Setting" + }, "led-type": { "h2": "LED Type", "help": "Here you can set the type of LED strip used (WS2812 or SK6812).\n " + diff --git a/webpage/script.js b/webpage/script.js index cfbc72d3..0542ffc1 100644 --- a/webpage/script.js +++ b/webpage/script.js @@ -96,6 +96,10 @@ var transitionDuration = 1; var transitionSpeed = 30; var transitionColorize = 1; var transitionDemo = false; +var ledpin = 3; +var powerbutton = 2; +var modebutton = 13; +var speedbutton = 14; // operation modes var COMMAND_MODE_WORD_CLOCK = 1; @@ -151,6 +155,7 @@ var COMMAND_SET_BOOT = 101; var COMMAND_SET_AUTO_BRIGHT = 102; var COMMAND_SET_LAYOUT_VARIANT = 103; var COMMAND_SET_MQTT_HA_DISCOVERY = 104; +var COMMAND_SET_GPIO = 105; var COMMAND_SPEED = 152; @@ -241,6 +246,10 @@ function initConfigValues() { transitionSpeed = 30; transitionColorize = 1; transitionDemo = false; + ledpin = 3; + powerbutton = 2; + modebutton = 13; + speedbutton = 14; } /* eslint-disable no-console */ @@ -394,8 +403,28 @@ function initWebsocket() { $("#front-layout").set("value", data.UhrtypeDef); $("#buildtype").set("value", data.buildtype); $("#whitetype").set("value", data.wType); + $("#ledpin").set("value", data.ledpin); + $("#powerbutton").set("value", data.powerbutton); + $("#modebutton").set("value", data.modebutton); + $("#speedbutton").set("value", data.speedbutton); $("#colortype").set("value", data.colortype); + removeSpecificOption("ledpin", data.powerbutton, true); + removeSpecificOption("ledpin", data.modebutton, true); + removeSpecificOption("ledpin", data.speedbutton, true); + + removeSpecificOption("powerbutton", data.ledpin, true); + removeSpecificOption("powerbutton", data.modebutton, true); + removeSpecificOption("powerbutton", data.speedbutton, true); + + removeSpecificOption("modebutton", data.ledpin, true); + removeSpecificOption("modebutton", data.powerbutton, true); + removeSpecificOption("modebutton", data.speedbutton, true); + + removeSpecificOption("speedbutton", data.ledpin, true); + removeSpecificOption("speedbutton", data.powerbutton, true); + removeSpecificOption("speedbutton", data.modebutton, true); + document.getElementById("boot-show-led-blink").checked = data.bootLedBlink; document.getElementById("boot-show-led-sweep").checked = data.bootLedSweep; document.getElementById("boot-show-wifi").checked = data.bootShowWifi; @@ -444,6 +473,10 @@ function initWebsocket() { hsb[1][2] = data.hsb12; effectBri = data.effectBri; effectSpeed = data.effectSpeed; + ledpin = data.ledpin; + powerbutton = data.powerbutton; + modebutton = data.modebutton; + speedbutton = data.speedbutton; colortype = data.colortype; hasHappyBirthday = data.hasHappyBirthday; @@ -933,6 +966,15 @@ $.ready(function() { sendCmd(COMMAND_REQUEST_CONFIG_VALUES); debugMessage("FrontLayout" + debugMessageReconfigured); }); + $("#hardware-config-save-button").on("click", function() { + ledpin = $("#ledpin").get("value"); + powerbutton = $("#powerbutton").get("value"); + modebutton = $("#modebutton").get("value"); + speedbutton = $("#speedbutton").get("value"); + + sendCmd(COMMAND_SET_GPIO, nstr(ledpin) + nstr(powerbutton) + nstr(modebutton) + nstr(speedbutton)); + debugMessage("hardware-config" + debugMessageReconfigured); + }); $("#colortype-button").on("click", function() { colortype = $("#colortype").get("value");