diff --git a/boards/orangeclock.json b/boards/orangeclock.json index c852a39..7ea7754 100644 --- a/boards/orangeclock.json +++ b/boards/orangeclock.json @@ -11,7 +11,7 @@ "-DARDUINO_ORANGECLOCK", "-DARDUINO_ESP32S3_DEV", "-DIS_ORANGECLOCK", - "-DARDUINO_USB_MODE=1", + "-DARDUINO_USB_MODE=0", "-DARDUINO_RUNNING_CORE=1", "-DARDUINO_EVENT_RUNNING_CORE=1", "-DARDUINO_USB_CDC_ON_BOOT=1" diff --git a/src/config.cpp b/src/config.cpp index d676389..46c1e7b 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -6,6 +6,9 @@ const char *ntpServer = "pool.ntp.org"; // const long gmtOffset_sec = 0; const int daylightOffset_sec = 3600; TaskHandle_t OTAHandle = NULL; +SemaphoreHandle_t xButtonSemaphore = NULL; +const TickType_t debounceDelay = pdMS_TO_TICKS(500); +TickType_t lastButtonPressTime = 0; #define STA_SSID "" #define STA_PASS "" @@ -92,7 +95,7 @@ void setupWifi() WiFiManager wm; - #ifndef ARDUINO_ORANGECLOCK +#ifndef ARDUINO_ORANGECLOCK // Touch pin 14 to reset if (touchRead(14) > 9000) { @@ -109,7 +112,7 @@ void setupWifi() wm.resetSettings(); } } - #endif +#endif String softAP_SSID = String("OrangeBTClock"); @@ -264,6 +267,42 @@ void OTAUpdateTask(void *pvParameters) } } +void HandleButtonTask(void *pvParameters) +{ + for (;;) + { + if (xSemaphoreTake(xButtonSemaphore, portMAX_DELAY) == pdTRUE) + { + TickType_t currentTime = xTaskGetTickCount(); + if ((currentTime - lastButtonPressTime) >= debounceDelay) + { + lastButtonPressTime = currentTime; + + Serial.println("Button Pressed"); + + leds[0] = CRGB::SkyBlue; + leds[1] = CRGB::Black; + + FastLED.show(); + + vTaskDelay(100); + + leds[0] = CRGB::Black; + leds[1] = CRGB::DarkOrange; + + FastLED.show(); + + vTaskDelay(100); + + leds[0] = CRGB::Black; + leds[1] = CRGB::Black; + + FastLED.show(); + } + } + } +} + char getCurrencyIcon() { char ret; @@ -286,4 +325,23 @@ char getCurrencyIcon() } return ret; +} + +void IRAM_ATTR onButtonPress() +{ + xSemaphoreGiveFromISR(xButtonSemaphore, NULL); +} + +void setupButtonISR() +{ + xButtonSemaphore = xSemaphoreCreateBinary(); + + xTaskCreatePinnedToCore( + HandleButtonTask, // Task function + "Button Task", // Task name + 2048, // Stack size (bytes) + NULL, // Task parameters + 1, // Priority (1 is default) + NULL, // Task handle + 0); // Core to run the task (0 or 1) } \ No newline at end of file diff --git a/src/config.hpp b/src/config.hpp index c4936e4..fc4f7ca 100644 --- a/src/config.hpp +++ b/src/config.hpp @@ -21,4 +21,6 @@ void wakeModemSleep(); void setModemSleep(); bool inPowerSaveMode(); -char getCurrencyIcon(); \ No newline at end of file +char getCurrencyIcon(); +void IRAM_ATTR onButtonPress(); +void setupButtonISR(); \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 7cb1674..ce78854 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -8,7 +8,6 @@ #include "config.hpp" #include "webserver.hpp" #include -#include #define uS_TO_S_FACTOR 1000000 /* Conversion factor for micro seconds to seconds */ #define TIME_TO_SLEEP 5 /* Time ESP32 will go to sleep (in seconds) */ @@ -35,25 +34,31 @@ uint currentPrice = 0; String currentBlock = ""; String currentFees = ""; -#define NUM_LEDS 2 CRGB leds[NUM_LEDS]; void setup() { // setCpuFrequencyMhz(40); Serial.begin(115200); - - //#ifndef IS_ORANGECLOCK + delay(2500); + Serial.println("Hello"); + #ifndef IS_ORANGECLOCK pinMode(LED_BUILTIN, OUTPUT); digitalWrite(LED_BUILTIN, HIGH); - //#else - FastLED.addLeds(leds, NUM_LEDS); - leds[0] = CRGB::DarkOrange; + #else + + pinMode(BUTTON_PIN, INPUT_PULLUP); + attachInterrupt(BUTTON_PIN, onButtonPress, FALLING); + + FastLED.addLeds(leds, NUM_LEDS); + leds[0] = CRGB::GreenYellow; leds[1] = CRGB::OrangeRed; FastLED.show(); - //#endif + + setupButtonISR(); + #endif setupPreferences(); setupDisplay(); @@ -102,24 +107,24 @@ void loop() // - IPAddress res; - uint result = WiFi.hostByName("mempool.space", res); + // IPAddress res; + // uint result = WiFi.hostByName("mempool.space", res); - if (result >= 0) - { - Serial.print("SUCCESS!"); - Serial.println(res.toString()); - } - else - { - WiFi.reconnect(); + // if (result >= 0) + // { + // Serial.print("SUCCESS!"); + // Serial.println(res.toString()); + // } + // else + // { + // WiFi.reconnect(); - while (WiFi.status() != WL_CONNECTED) - { - Serial.print('.'); - delay(1000); - } - } + // while (WiFi.status() != WL_CONNECTED) + // { + // Serial.print('.'); + // delay(1000); + // } + // } for (uint i = 1; i <= 3; i++) { diff --git a/src/shared.cpp b/src/shared.cpp index 2547a42..a7dd264 100644 --- a/src/shared.cpp +++ b/src/shared.cpp @@ -1,2 +1,3 @@ #include "shared.hpp" +volatile bool buttonPressed = false; diff --git a/src/shared.hpp b/src/shared.hpp index ec281c6..4321542 100644 --- a/src/shared.hpp +++ b/src/shared.hpp @@ -7,6 +7,9 @@ #include #include "utils.hpp" #include "fonts/fonts.hpp" +#include +#include +#include #ifdef VERSION_EPD_2_13 #define EPD_CLASS GxEPD2_213_B74 @@ -101,3 +104,5 @@ extern char currentIcon1; extern char currentIcon2; extern char currentIcon3; +extern CRGB leds[NUM_LEDS]; +extern volatile bool buttonPressed; \ No newline at end of file