From f1629b7327e752271df45a2c62a496433218396d Mon Sep 17 00:00:00 2001 From: Maksym Date: Sun, 8 Dec 2024 19:10:39 +0100 Subject: [PATCH] added limit to maximum value of button added basic LED support --- platformio.ini | 3 ++- src/main.cpp | 40 ++++++++++++++++++++++++++++++++++------ 2 files changed, 36 insertions(+), 7 deletions(-) diff --git a/platformio.ini b/platformio.ini index 8123751..d966ae7 100644 --- a/platformio.ini +++ b/platformio.ini @@ -13,4 +13,5 @@ platform = raspberrypi board = adafruit_kb2040 framework = arduino upload_protocol = picotool -monitor_speed = 115200 \ No newline at end of file +monitor_speed = 115200 +lib_deps = fastled/FastLED@^3.9.4 diff --git a/src/main.cpp b/src/main.cpp index 5d37e91..cab84d4 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2,10 +2,14 @@ #include #include #include +#include #define PORT 2 #define PORT2 3 #define THRESHOLD 10 +#define LEDPORT 5 + +CRGB leds[2]; class HallButton { @@ -15,14 +19,18 @@ public: uint64_t firstPulseLength[3]; // value of button int64_t value; uint64_t _median; + int64_t maxValue; + bool isFirstClick = true; bool isDataReadyToRead = false; bool isButtonPressed = false; uint64_t nextTogglePoint = 0; // value when to toggle state of button + size_t ledId; - HallButton(uint8_t gpioNum, char buttonOnKeyboard) + HallButton(uint8_t gpioNum, char buttonOnKeyboard, size_t ledId) { this->gpioNum = gpioNum; this->buttonOnKeyboard = buttonOnKeyboard; + this->ledId = ledId; } void HandleISR(bool gpioState, uint64_t currentTime) @@ -45,13 +53,19 @@ public: return; _median = median(firstPulseLength[0], firstPulseLength[1], firstPulseLength[2]); value = filtered((float)_median); - if (nextTogglePoint == 0) - nextTogglePoint = value + THRESHOLD; + if (value > maxValue) + if (isFirstClick) + maxValue = value; + else + value = maxValue; + if (isButtonPressed) { if (value < nextTogglePoint) { Release(); + if (isFirstClick) + isFirstClick = false; } if ((int64_t)value - (int64_t)nextTogglePoint > THRESHOLD) nextTogglePoint = value - THRESHOLD; @@ -94,6 +108,7 @@ private: return; Keyboard.press(buttonOnKeyboard); isButtonPressed = true; + LEDTick(isButtonPressed, ledId); } void Release() { @@ -101,10 +116,16 @@ private: return; Keyboard.release(buttonOnKeyboard); isButtonPressed = false; + LEDTick(isButtonPressed, ledId); + } + static void LEDTick(bool status, size_t ledId) + { + leds[ledId] = status ? CRGB::Red : CRGB::Black; + FastLED.show(); } }; -HallButton btn[2] = {HallButton(PORT, 't'), HallButton(PORT2, 'y')}; +HallButton btn[2] = {HallButton(PORT, 't', 1), HallButton(PORT2, 'y', 0)}; void isr(uint gpio, uint32_t events) { @@ -133,12 +154,19 @@ void setup() gpio_set_irq_enabled_with_callback(PORT2, GPIO_IRQ_EDGE_RISE | GPIO_IRQ_EDGE_FALL, true, isr); // Serial.begin(9600); Keyboard.begin(); + FastLED.addLeds(leds, 2); } void printDebugInfo() { - Serial.printf("%llu,%llu,%llu,%llu,%i,", btn[0].firstPulseLength[btn[0].counter], btn[0]._median, btn[0].value, btn[0].nextTogglePoint, btn[0].isButtonPressed); - Serial.printf("%llu,%llu,%llu,%llu,%i\n", btn[1].firstPulseLength[btn[1].counter], btn[1]._median, btn[1].value, btn[1].nextTogglePoint, btn[1].isButtonPressed); + static uint32_t lastTime = 0; + uint32_t time = millis(); + if (time - lastTime >= 1) + { + Serial.printf("%d,%d,%llu,%llu,%i,", btn[0].isFirstClick, btn[0].maxValue, btn[0].value, btn[0].nextTogglePoint, btn[0].isButtonPressed); + Serial.printf("%d,%d,%llu,%llu,%i\n", btn[1].isFirstClick, btn[1].maxValue, btn[1].value, btn[1].nextTogglePoint, btn[1].isButtonPressed); + lastTime = time; + } } void loop() {