-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLedButton.h
More file actions
48 lines (40 loc) · 1.24 KB
/
LedButton.h
File metadata and controls
48 lines (40 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#ifndef LedButton_h
#define LedButton_h
#include <Bounce2.h>
#include "util/Timer.h"
enum class LedMode { ON, OFF, BLINKING };
enum class ButtonState { NONE, PRESS, RELEASE };
struct LedButtonConfig {
int buttonPin;
int ledPin;
};
class LedButton {
public:
void setup(LedButtonConfig buttonConfig);
void setOnPressCallback(void (*onPress)(void*), void *objRef);
void setOnReleaseCallback(void (*onRelease)(void*), void *objRef);
void loop();
void setLedOn();
void setLedOff();
void setLedBlinking(int intervalMs);
unsigned long getPressedButtonDurationMs();
bool isPressed();
private:
ButtonState determineState();
void notifyObserver(ButtonState state);
void blinkLed();
void doSetLed(bool isOn);
void (*onPress)(void*) = NULL;
void (*onRelease)(void*) = NULL;
void *onPressObjRef = NULL;
void *onReleaseObjRef = NULL;
ButtonState buttonState = ButtonState::NONE;
LedButtonConfig ledButtonConfig;
int blinkIntervalMs;
Bounce button;
LedMode ledMode = LedMode::OFF;
Timer blinkLedTimer;
Timer pressedButtonTimer;
bool isLedOn = false;
};
#endif