lib Modulove
Library for building custom scripts for Modulove modules.
Loading...
Searching...
No Matches
button.h
Go to the documentation of this file.
1
13#ifndef BUTTON_H
14#define BUTTON_H
15
16#include <Arduino.h>
17
18namespace modulove {
19
20const uint8_t DEBOUNCE_MS = 10;
21
22class Button {
23 public:
26 CHANGE_UNCHANGED,
27 CHANGE_PRESSED,
28 CHANGE_RELEASED,
29 };
30
31 Button() {}
32 ~Button() {}
33
39 void Init(uint8_t pin) {
40 pinMode(pin, INPUT_PULLUP);
41 pin_ = pin;
42 }
43
50 void Init(uint8_t pin, uint8_t led_pin) {
51 pinMode(led_pin, OUTPUT);
52 led_pin_ = led_pin;
53#define LED_PIN_DEFINED
54 Init(pin);
55 }
56
61 void Process() {
62 old_read_ = read_;
63 read_ = digitalRead(pin_);
64
65 bool debounced = (millis() - last_press_) > DEBOUNCE_MS;
66 bool pressed = read_ == 0 && old_read_ == 1 && debounced;
67 bool released = read_ == 1 && old_read_ == 0 && debounced;
68 // Update variables for next loop
69 last_press_ = (pressed || released) ? millis(): last_press_;
70
71 // Determine current clock input state.
72 change_ = CHANGE_UNCHANGED;
73 if (pressed) {
74 change_ = CHANGE_PRESSED;
75 on_ = true;
76 } else if (released) {
77 change_ = CHANGE_RELEASED;
78 on_ = false;
79 }
80 #ifdef LED_PIN_DEFINED
81 // Change LED state
82 digitalWrite(led_pin_, on_);
83 #endif
84 }
85
91 inline ButtonChange Change() { return change_; }
92
99 inline bool On() { return on_; }
100
101 private:
102 uint8_t pin_;
103 uint8_t led_pin_;
104 uint8_t read_;
105 uint8_t old_read_;
106 unsigned long last_press_;
107 ButtonChange change_;
108 bool on_;
109};
110
111} // namespace modulove
112
113#endif
Definition button.h:22
ButtonChange Change()
Get the state change for the button.
Definition button.h:91
void Init(uint8_t pin)
Initializes a CV Input object.
Definition button.h:39
void Process()
Read the state of the cv input.
Definition button.h:61
bool On()
Current cv state represented as a bool.
Definition button.h:99
void Init(uint8_t pin, uint8_t led_pin)
Initializes an Button paired with an LED.
Definition button.h:50
ButtonChange
Enum constants for active change in button state.
Definition button.h:25