From 4015e9e989530f8d77e5f1b1aabb766af765d38f Mon Sep 17 00:00:00 2001 From: cyberman54 Date: Sun, 6 Nov 2022 15:00:54 +0100 Subject: [PATCH] new button lib --- include/button.h | 12 ++++++--- include/cyclic.h | 1 + include/irqhandler.h | 8 ------ platformio_orig.ini | 2 +- platformio_orig_s3.ini | 2 +- src/button.cpp | 61 +++++++++++++++++++++++------------------- src/cyclic.cpp | 7 +++++ src/irqhandler.cpp | 16 +++-------- src/main.cpp | 3 ++- src/power.cpp | 2 ++ 10 files changed, 61 insertions(+), 53 deletions(-) diff --git a/include/button.h b/include/button.h index 92eeb3f4..afc42a43 100644 --- a/include/button.h +++ b/include/button.h @@ -1,7 +1,7 @@ #ifndef _BUTTON_H #define _BUTTON_H -#include +#include #include "irqhandler.h" #include "senddata.h" #include "display.h" @@ -11,7 +11,13 @@ #define BUTTON_ACTIVEHIGH 0 #endif -void button_init(int pin); -void readButton(); +#ifndef BUTTON_PULLUP +#define BUTTON_PULLUP 0 +#endif + +extern TaskHandle_t buttonLoopTask; + +void button_init(void); +void IRAM_ATTR readButton(void); #endif \ No newline at end of file diff --git a/include/cyclic.h b/include/cyclic.h index d53b10e2..d9a7db45 100644 --- a/include/cyclic.h +++ b/include/cyclic.h @@ -13,6 +13,7 @@ #include "reset.h" #include "led.h" #include "power.h" +#include "button.h" extern Ticker cyclicTimer; diff --git a/include/irqhandler.h b/include/irqhandler.h index eacbd35f..91f076c8 100644 --- a/include/irqhandler.h +++ b/include/irqhandler.h @@ -36,12 +36,4 @@ void IRAM_ATTR DisplayIRQ(); void IRAM_ATTR MatrixDisplayIRQ(); #endif -#ifdef HAS_BUTTON -void IRAM_ATTR ButtonIRQ(); -#endif - -#ifdef HAS_PMU -void IRAM_ATTR PMUIRQ(); -#endif - #endif \ No newline at end of file diff --git a/platformio_orig.ini b/platformio_orig.ini index f291f52c..87eeecf4 100644 --- a/platformio_orig.ini +++ b/platformio_orig.ini @@ -81,7 +81,7 @@ lib_deps_basic = https://github.com/SukkoPera/Arduino-Rokkit-Hash.git bblanchon/ArduinoJson @ ^6 makuna/RTC @ ^2.3.5 - spacehuhn/SimpleButton + mathertel/OneButton @ ^2.0.3 lewisxhe/XPowersLib @ ^0.1.4 256dpi/MQTT @ ^2.5.0 lib_deps_all = diff --git a/platformio_orig_s3.ini b/platformio_orig_s3.ini index c8bf4c65..30e98be7 100644 --- a/platformio_orig_s3.ini +++ b/platformio_orig_s3.ini @@ -29,7 +29,7 @@ lib_deps_all = https://github.com/cyberman54/libpax.git https://github.com/SukkoPera/Arduino-Rokkit-Hash.git bblanchon/ArduinoJson @ ^6 - spacehuhn/SimpleButton + mathertel/OneButton @ ^2.0.3 256dpi/MQTT @ ^2.5.0 ricmoo/QRCode @ ^0.0.1 build_flags_basic = diff --git a/src/button.cpp b/src/button.cpp index 8e78de6f..b929222b 100644 --- a/src/button.cpp +++ b/src/button.cpp @@ -3,42 +3,49 @@ #include "globals.h" #include "button.h" -using namespace simplebutton; - // Local logging tag static const char TAG[] = __FILE__; -static Button *b = NULL; +OneButton button(HAS_BUTTON, !BUTTON_ACTIVEHIGH, !!BUTTON_PULLUP); +TaskHandle_t buttonLoopTask; -void button_init(int pin) { -#ifdef BUTTON_PULLUP - b = new ButtonPullup(pin); -#else - b = new Button(pin, !BUTTON_ACTIVEHIGH); -#endif +void IRAM_ATTR readButton(void) { button.tick(); } - // attach events to the button - - b->setOnDoubleClicked([]() {}); - - b->setOnClicked([]() { +void singleClick(void) { #ifdef HAS_DISPLAY - dp_refresh(true); // switch to next display page + dp_refresh(true); // switch to next display page #endif #ifdef HAS_MATRIX_DISPLAY - refreshTheMatrixDisplay(true); // switch to next display page + refreshTheMatrixDisplay(true); // switch to next display page #endif - }); - - b->setOnHolding([]() { - payload.reset(); - payload.addButton(0x01); - SendPayload(BUTTONPORT); - }); - - // attach interrupt to the button - attachInterrupt(digitalPinToInterrupt(pin), ButtonIRQ, CHANGE); } -void readButton() { b->update(); } +void longPressStart(void) { + payload.reset(); + payload.addButton(0x01); + SendPayload(BUTTONPORT); +} + +void buttonLoop(void *parameter) { + while (1) { + doIRQ(BUTTON_IRQ); + delay(20); + } +} + +void button_init(void) { + ESP_LOGI(TAG, "Starting button Controller..."); + xTaskCreatePinnedToCore(buttonLoop, // task function + "buttonloop", // name of task + 2048, // stack size of task + (void *)1, // parameter of the task + 2, // priority of the task + &buttonLoopTask, // task handle + 1); // CPU core + + button.setPressTicks(1000); + button.attachClick(singleClick); + button.attachLongPressStart(longPressStart); +}; + #endif \ No newline at end of file diff --git a/src/cyclic.cpp b/src/cyclic.cpp index ef2f2d5c..3b384333 100644 --- a/src/cyclic.cpp +++ b/src/cyclic.cpp @@ -71,6 +71,13 @@ void doHousekeeping() { eTaskGetState(ledLoopTask)); #endif +#ifdef HAS_BUTTON + if (buttonLoopTask != NULL) + ESP_LOGD(TAG, "Buttonloop %d bytes left | Taskstate = %d", + uxTaskGetStackHighWaterMark(buttonLoopTask), + eTaskGetState(buttonLoopTask)); +#endif + // read battery voltage into global variable #if (defined BAT_MEASURE_ADC || defined HAS_PMU || defined HAS_IP5306) batt_level = read_battlevel(); diff --git a/src/irqhandler.cpp b/src/irqhandler.cpp index 81b24304..87f6c7bd 100644 --- a/src/irqhandler.cpp +++ b/src/irqhandler.cpp @@ -13,10 +13,10 @@ void irqHandler(void *pvParameters) { // task remains in blocked state until it is notified by an irq for (;;) { - xTaskNotifyWait(0x00, // Don't clear any bits on entry - ULONG_MAX, // Clear all bits on exit - &irqSource, // Receives the notification value - portMAX_DELAY); // wait forever + xTaskNotifyWait(0x00, // Don't clear any bits on entry + ULONG_MAX, // Clear all bits on exit + &irqSource, // Receives the notification value + portMAX_DELAY); // wait forever if (irqSource & UNMASK_IRQ) // interrupt handler to be enabled? irqSource &= ~MASK_IRQ; // then clear irq mask flag @@ -102,14 +102,6 @@ void IRAM_ATTR DisplayIRQ() { doIRQ(DISPLAY_IRQ); } void IRAM_ATTR MatrixDisplayIRQ() { doIRQ(MATRIX_DISPLAY_IRQ); } #endif -#ifdef HAS_BUTTON -void IRAM_ATTR ButtonIRQ() { doIRQ(BUTTON_IRQ); } -#endif - -#ifdef HAS_PMU -void IRAM_ATTR PMUIRQ() { doIRQ(PMU_IRQ); } -#endif - void mask_user_IRQ() { xTaskNotify(irqHandlerTask, MASK_IRQ, eSetBits); } void unmask_user_IRQ() { xTaskNotify(irqHandlerTask, UNMASK_IRQ, eSetBits); } \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index a1af483c..511b9244 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -28,6 +28,7 @@ licenses. Refer to LICENSE.txt file in repository for more details. Task Core Prio Purpose ------------------------------------------------------------------------------- ledloop* 1 1 blinks LEDs +buttonloop* 1 2 reads button spiloop# 0 2 reads/writes data on spi interface lmictask* 1 2 MCCI LMiC LORAWAN stack clockloop# 1 6 generates realtime telegrams for external clock @@ -468,7 +469,7 @@ void setup() { #else strcat_P(features, "PD"); #endif // BUTTON_PULLUP - button_init(HAS_BUTTON); + button_init(); #endif // HAS_BUTTON // only if we have a timesource we do timesync diff --git a/src/power.cpp b/src/power.cpp index 0e346cac..61f7e27d 100644 --- a/src/power.cpp +++ b/src/power.cpp @@ -25,6 +25,8 @@ static const adc_unit_t unit = ADC_UNIT_1; #ifdef HAS_PMU XPowersPMU pmu; +void IRAM_ATTR PMUIRQ() { doIRQ(PMU_IRQ); } + void AXP192_powerevent_IRQ(void) { pmu.getIrqStatus();