From 72f45b028db89be0f59bdcdc74d55405b8a97a03 Mon Sep 17 00:00:00 2001 From: gjelsoe <36234524+gjelsoe@users.noreply.github.com> Date: Wed, 13 Nov 2024 16:53:02 +0100 Subject: [PATCH] Added BUTTON_PIN --- src/ButtonThread.cpp | 31 ++++++++++++++++++++++++++----- src/ButtonThread.h | 2 +- userPrefs.h | 5 +++++ 3 files changed, 32 insertions(+), 6 deletions(-) diff --git a/src/ButtonThread.cpp b/src/ButtonThread.cpp index 0ea1309cc..7522fa776 100644 --- a/src/ButtonThread.cpp +++ b/src/ButtonThread.cpp @@ -1,3 +1,4 @@ +#include "../userPrefs.h" #include "ButtonThread.h" #include "configuration.h" #if !MESHTASTIC_EXCLUDE_GPS @@ -26,12 +27,12 @@ using namespace concurrency; ButtonThread *buttonThread; // Declared extern in header volatile ButtonThread::ButtonEventType ButtonThread::btnEvent = ButtonThread::BUTTON_EVENT_NONE; -#if defined(BUTTON_PIN) || defined(ARCH_PORTDUINO) +#if defined(BUTTON_PIN) || defined(ARCH_PORTDUINO) || defined(USERPREFS_BUTTON_PIN) OneButton ButtonThread::userButton; // Get reference to static member #endif ButtonThread::ButtonThread() : OSThread("Button") { -#if defined(BUTTON_PIN) || defined(ARCH_PORTDUINO) +#if defined(BUTTON_PIN) || defined(ARCH_PORTDUINO) || defined(USERPREFS_BUTTON_PIN) #if defined(ARCH_PORTDUINO) if (settingsMap.count(user) != 0 && settingsMap[user] != RADIOLIB_NC) { @@ -39,7 +40,12 @@ ButtonThread::ButtonThread() : OSThread("Button") LOG_DEBUG("Use GPIO%02d for button", settingsMap[user]); } #elif defined(BUTTON_PIN) +#if !defined(USERPREFS_BUTTON_PIN) int pin = config.device.button_gpio ? config.device.button_gpio : BUTTON_PIN; // Resolved button pin +#endif +#ifdef USERPREFS_BUTTON_PIN + int pin = config.device.button_gpio ? config.device.button_gpio : USERPREFS_BUTTON_PIN; // Resolved button pin +#endif #if defined(HELTEC_CAPSULE_SENSOR_V3) this->userButton = OneButton(pin, false, false); #elif defined(BUTTON_ACTIVE_LOW) @@ -59,7 +65,7 @@ ButtonThread::ButtonThread() : OSThread("Button") #endif #endif -#if defined(BUTTON_PIN) || defined(ARCH_PORTDUINO) +#if defined(BUTTON_PIN) || defined(ARCH_PORTDUINO) || defined(USERPREFS_BUTTON_PIN) userButton.attachClick(userButtonPressed); userButton.setClickMs(BUTTON_CLICK_MS); userButton.setPressMs(BUTTON_LONGPRESS_MS); @@ -102,7 +108,7 @@ int32_t ButtonThread::runOnce() // If the button is pressed we suppress CPU sleep until release canSleep = true; // Assume we should not keep the board awake -#if defined(BUTTON_PIN) +#if defined(BUTTON_PIN) || defined(USERPREFS_BUTTON_PIN) userButton.tick(); canSleep &= userButton.isIdle(); #elif defined(ARCH_PORTDUINO) @@ -130,7 +136,12 @@ int32_t ButtonThread::runOnce() return 50; } #ifdef BUTTON_PIN +#if !defined(USERPREFS_BUTTON_PIN) if (((config.device.button_gpio ? config.device.button_gpio : BUTTON_PIN) != +#endif +#if defined(USERPREFS_BUTTON_PIN) + if (((config.device.button_gpio ? config.device.button_gpio : USERPREFS_BUTTON_PIN) != +#endif moduleConfig.canned_message.inputbroker_pin_press) || !(moduleConfig.canned_message.updown1_enabled || moduleConfig.canned_message.rotary1_enabled) || !moduleConfig.canned_message.enabled) { @@ -244,7 +255,12 @@ void ButtonThread::attachButtonInterrupts() #elif defined(BUTTON_PIN) // Interrupt for user button, during normal use. Improves responsiveness. attachInterrupt( +#if !defined(USERPREFS_BUTTON_PIN) config.device.button_gpio ? config.device.button_gpio : BUTTON_PIN, +#endif +#if defined(USERPREFS_BUTTON_PIN) + config.device.button_gpio ? config.device.button_gpio : USERPREFS_BUTTON_PIN, +#endif []() { ButtonThread::userButton.tick(); runASAP = true; @@ -273,8 +289,13 @@ void ButtonThread::detachButtonInterrupts() if (settingsMap.count(user) != 0 && settingsMap[user] != RADIOLIB_NC) detachInterrupt(settingsMap[user]); #elif defined(BUTTON_PIN) +#if !defined(USERPREFS_BUTTON_PIN) detachInterrupt(config.device.button_gpio ? config.device.button_gpio : BUTTON_PIN); #endif +#if defined(USERPREFS_BUTTON_PIN) + detachInterrupt(config.device.button_gpio ? config.device.button_gpio : USERPREFS_BUTTON_PIN); +#endif +#endif #ifdef BUTTON_PIN_ALT detachInterrupt(BUTTON_PIN_ALT); @@ -315,7 +336,7 @@ void ButtonThread::userButtonMultiPressed(void *callerThread) // Non-static method, runs during callback. Grabs info while still valid void ButtonThread::storeClickCount() { -#ifdef BUTTON_PIN +#if defined(BUTTON_PIN) || defined(USERPREFS_BUTTON_PIN) multipressClickCount = userButton.getNumberClicks(); #endif } diff --git a/src/ButtonThread.h b/src/ButtonThread.h index 9cd7b3dac..a01a1718f 100644 --- a/src/ButtonThread.h +++ b/src/ButtonThread.h @@ -38,7 +38,7 @@ class ButtonThread : public concurrency::OSThread void storeClickCount(); private: -#if defined(BUTTON_PIN) || defined(ARCH_PORTDUINO) +#if defined(BUTTON_PIN) || defined(ARCH_PORTDUINO) || defined(USERPREFS_BUTTON_PIN) static OneButton userButton; // Static - accessed from an interrupt #endif #ifdef BUTTON_PIN_ALT diff --git a/userPrefs.h b/userPrefs.h index a58c4ce26..ff97f0a8d 100644 --- a/userPrefs.h +++ b/userPrefs.h @@ -90,4 +90,9 @@ static unsigned char USERPREFS_ADMIN_KEY[] = {0xcd, 0xc0, 0xb4, 0x3c, 0x53, 0x24 */ //#define USERPREFS_FIXED_BLUETOOTH 121212 +/* + * Will overwrite BUTTON_PIN if set + */ +//#define USERPREFS_BUTTON_PIN 36 + #endif \ No newline at end of file