2024-02-12 22:44:21 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "OneButton.h"
|
2022-05-07 10:31:21 +00:00
|
|
|
#include "concurrency/OSThread.h"
|
|
|
|
#include "configuration.h"
|
2022-02-28 21:19:38 +00:00
|
|
|
|
2024-05-09 13:14:58 +00:00
|
|
|
#ifndef BUTTON_CLICK_MS
|
|
|
|
#define BUTTON_CLICK_MS 250
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef BUTTON_LONGPRESS_MS
|
|
|
|
#define BUTTON_LONGPRESS_MS 5000
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef BUTTON_TOUCH_MS
|
2024-07-31 11:56:06 +00:00
|
|
|
#define BUTTON_TOUCH_MS 400
|
2024-05-09 13:14:58 +00:00
|
|
|
#endif
|
|
|
|
|
2023-01-19 03:13:31 +00:00
|
|
|
class ButtonThread : public concurrency::OSThread
|
|
|
|
{
|
2024-02-12 22:44:21 +00:00
|
|
|
public:
|
2024-05-09 13:14:58 +00:00
|
|
|
static const uint32_t c_holdOffTime = 30000; // hold off 30s after boot
|
2024-02-12 22:44:21 +00:00
|
|
|
|
|
|
|
enum ButtonEventType {
|
|
|
|
BUTTON_EVENT_NONE,
|
|
|
|
BUTTON_EVENT_PRESSED,
|
|
|
|
BUTTON_EVENT_DOUBLE_PRESSED,
|
|
|
|
BUTTON_EVENT_MULTI_PRESSED,
|
|
|
|
BUTTON_EVENT_LONG_PRESSED,
|
|
|
|
BUTTON_EVENT_LONG_RELEASED,
|
2024-04-06 13:04:26 +00:00
|
|
|
BUTTON_EVENT_TOUCH_LONG_PRESSED,
|
2024-02-12 22:44:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
ButtonThread();
|
|
|
|
int32_t runOnce() override;
|
2024-04-11 12:02:50 +00:00
|
|
|
void attachButtonInterrupts();
|
|
|
|
void detachButtonInterrupts();
|
2024-04-06 13:04:26 +00:00
|
|
|
void storeClickCount();
|
2024-02-12 22:44:21 +00:00
|
|
|
|
|
|
|
private:
|
2024-04-11 12:02:50 +00:00
|
|
|
#if defined(BUTTON_PIN) || defined(ARCH_PORTDUINO)
|
|
|
|
static OneButton userButton; // Static - accessed from an interrupt
|
2022-02-28 21:19:38 +00:00
|
|
|
#endif
|
|
|
|
#ifdef BUTTON_PIN_ALT
|
2023-01-19 03:13:31 +00:00
|
|
|
OneButton userButtonAlt;
|
2022-02-28 21:19:38 +00:00
|
|
|
#endif
|
|
|
|
#ifdef BUTTON_PIN_TOUCH
|
2023-01-19 03:13:31 +00:00
|
|
|
OneButton userButtonTouch;
|
2023-11-26 21:08:20 +00:00
|
|
|
#endif
|
2022-02-28 21:19:38 +00:00
|
|
|
|
2024-02-12 22:44:21 +00:00
|
|
|
// set during IRQ
|
|
|
|
static volatile ButtonEventType btnEvent;
|
2022-02-28 21:19:38 +00:00
|
|
|
|
2024-04-06 13:04:26 +00:00
|
|
|
// Store click count during callback, for later use
|
|
|
|
volatile int multipressClickCount = 0;
|
|
|
|
|
2024-02-12 22:44:21 +00:00
|
|
|
static void wakeOnIrq(int irq, int mode);
|
2022-02-28 21:19:38 +00:00
|
|
|
|
2024-02-12 22:44:21 +00:00
|
|
|
// IRQ callbacks
|
|
|
|
static void userButtonPressed() { btnEvent = BUTTON_EVENT_PRESSED; }
|
|
|
|
static void userButtonDoublePressed() { btnEvent = BUTTON_EVENT_DOUBLE_PRESSED; }
|
2024-04-06 13:04:26 +00:00
|
|
|
static void userButtonMultiPressed(void *callerThread); // Retrieve click count from non-static Onebutton while still valid
|
2024-02-12 22:44:21 +00:00
|
|
|
static void userButtonPressedLongStart();
|
|
|
|
static void userButtonPressedLongStop();
|
2024-04-06 13:04:26 +00:00
|
|
|
static void touchPressedLongStart() { btnEvent = BUTTON_EVENT_TOUCH_LONG_PRESSED; }
|
2023-01-19 03:13:31 +00:00
|
|
|
};
|
2024-04-11 12:02:50 +00:00
|
|
|
|
|
|
|
extern ButtonThread *buttonThread;
|