2025-08-08 20:01:31 +10:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <Arduino.h>
|
|
|
|
|
|
|
|
|
|
#define BUTTON_EVENT_NONE 0
|
|
|
|
|
#define BUTTON_EVENT_CLICK 1
|
|
|
|
|
#define BUTTON_EVENT_LONG_PRESS 2
|
|
|
|
|
|
|
|
|
|
class MomentaryButton {
|
|
|
|
|
int8_t _pin;
|
|
|
|
|
int8_t prev, cancel;
|
2025-08-12 10:01:35 +10:00
|
|
|
bool _reverse, _pull;
|
2025-08-08 20:01:31 +10:00
|
|
|
int _long_millis;
|
2025-09-01 17:11:55 +10:00
|
|
|
int _threshold; // analog mode
|
2025-08-08 20:01:31 +10:00
|
|
|
unsigned long down_at;
|
|
|
|
|
|
|
|
|
|
bool isPressed(int level) const;
|
|
|
|
|
|
|
|
|
|
public:
|
2025-08-12 10:01:35 +10:00
|
|
|
MomentaryButton(int8_t pin, int long_press_mills=0, bool reverse=false, bool pulldownup=false);
|
2025-09-01 17:11:55 +10:00
|
|
|
MomentaryButton(int8_t pin, int long_press_mills, int analog_threshold);
|
2025-08-12 10:01:35 +10:00
|
|
|
void begin();
|
2025-08-08 20:01:31 +10:00
|
|
|
int check(bool repeat_click=false); // returns one of BUTTON_EVENT_*
|
|
|
|
|
void cancelClick(); // suppress next BUTTON_EVENT_CLICK (if already in DOWN state)
|
|
|
|
|
uint8_t getPin() { return _pin; }
|
|
|
|
|
bool isPressed() const;
|
|
|
|
|
};
|