Stepdance Software Library
Loading...
Searching...
No Matches
digital_in.hpp
1#include <stdint.h>
2#include "core.hpp"
3/*
4Digital Input Module of the StepDance Control System
5
6This module is responsible for providing an interface to digital inputs, e.g. as buttons.
7
8[More Details to be Added]
9
10A part of the Mixing Metaphors Project
11(c) 2025 Ilan Moyer, Jennifer Jacobs, Devon Frost, Emilie Yu
12
13*/
14
15#ifndef digital_in_h //prevent importing twice
16#define digital_in_h
17
18#define DIGITAL_IN_DEFAULT_DEBOUNCE_MS 50
19#define MAX_NUM_BUTTONS 20
20
21// Button Mode
22#define BUTTON_MODE_STANDARD 0 // button state reflects pin state, modulo debounce and invert flag
23#define BUTTON_MODE_TOGGLE 1 // button state toggles each button press
24#define BUTTON_MODE_TOGGLE_THREE_STATE 2 // button state toggles to three distinct states
25#define BUTTON_MODE_MULTIPRESS 3 // button state activates different states on single, double, triple press
26
27#define BUTTON_NON_INVERTED 0
28#define BUTTON_INVERTED 1
29
30#define BUTTON_STATE_RELEASED 0
31#define BUTTON_STATE_PRESSED 1
32#define BUTTON_CHANGED 1
37class ButtonKilohertzPlugin : public Plugin{ //we'll set up to run on every kilohertz frame
38 public:
39 ButtonKilohertzPlugin();
40 void begin();
41 protected:
42 void run();
43};
45
46/**/
55class Button : public Plugin{
56 public:
69 void begin(uint8_t pin); //defaults to INPUT_PULLUP
79 uint8_t read(); //returns button state
84 uint8_t read_raw(); //returns the asynchronous raw state of the input pin
89 uint8_t has_changed(); //returns 1 if the button state has changed
94 void set_callback_on_toggle(void (*callback_function)());
99 void set_callback_on_press(void (*callback_function)());
104 void set_callback_on_first_press(void (*callback_function)());
109 void set_callback_on_second_press(void (*callback_function)());
114 void set_callback_on_third_press(void (*callback_function)());
119 void set_callback_on_doublepress(void (*callback_function)());
124 void set_callback_on_triplepress(void (*callback_function)());
129 void set_callback_on_release(void (*callback_function)());
134 void set_debounce_ms(uint16_t debounce_ms);
139 void set_mode(uint8_t button_mode); //standard, or toggle
143 void set_invert(); //inverts the button state
147 void clear_invert(); //clears the invert button mode
148
152
153 static Button* registered_buttons[MAX_NUM_BUTTONS]; //stores all registered buttons
154 static uint8_t num_registered_buttons;
155 static ButtonKilohertzPlugin kilohertz_plugin; // runs in the kilohertz frame
156 void begin(uint8_t pin, uint8_t mode);
157 void on_frame(); //gets called at each kilohertz frame
158 void enroll(RPC *rpc, const String& instance_name);
160
161 private:
162 uint8_t input_pin;
163 uint8_t invert = BUTTON_NON_INVERTED;
164 uint16_t debounce_period_ms = DIGITAL_IN_DEFAULT_DEBOUNCE_MS;
165 uint16_t current_ms = 0; //started when button is pressed, stores time between each press.
166 uint8_t button_mode = BUTTON_MODE_STANDARD;
167 volatile uint16_t debounce_blackout_remaining_ms = 0; //starts at debounce_period_ms, and counts down to zero
168 volatile uint8_t button_state = 0;
169 volatile uint8_t change_flag = 0; //set when the button state changes, unless a callback is provided
170 volatile uint8_t press_num = 0; //used to track number of presses for multi-press mode
171 volatile uint8_t last_raw_pin_state = 0;
172 void (*callback_on_toggle)() = nullptr;
173 void (*callback_on_press)() = nullptr;
174 void (*callback_on_release)() = nullptr;
175 void (*callback_on_first_press)() = nullptr;
176 void (*callback_on_second_press)() = nullptr;
177 void (*callback_on_third_press)() = nullptr;
178 void (*callback_on_doublepress)() = nullptr;
179 void (*callback_on_triplepress)() = nullptr;
180};
181
182
183
184
185
186#endif //digital_in_h
uint8_t read_raw()
Reads the raw state of the input pin without debouncing.
void set_callback_on_toggle(void(*callback_function)())
Sets a callback function to be called when the button state toggles-either pressed or released.
void set_callback_on_press(void(*callback_function)())
Sets a callback function to be called when the button is pressed.
Button()
Default constructor for Button. Does not initialize board hardware. Call begin() to set up the button...
void set_mode(uint8_t button_mode)
Sets the button mode to either standard or toggle. Toggle mode triggers the button state only on pres...
void set_callback_on_triplepress(void(*callback_function)())
Sets a callback function to be called when the button is pressed twice within a window.
uint8_t read()
Initializes the Button with the specified pin and mode.
void set_callback_on_first_press(void(*callback_function)())
Sets a callback function to be called when the button is released.
void set_invert()
Inverts the button state logic. When inverted, a pressed button reads as released and vice versa....
uint8_t has_changed()
Checks if the button state has changed since the last read.
void set_callback_on_release(void(*callback_function)())
Sets a callback function to be called when the button is pressed three times within a window.
void set_callback_on_second_press(void(*callback_function)())
Sets a callback function to be called when the button is pressed a first time.
void clear_invert()
Clears the invert flag, restoring normal button state logic.
void set_callback_on_third_press(void(*callback_function)())
Sets a callback function to be called when the button is pressed a second time.
void set_callback_on_doublepress(void(*callback_function)())
Sets a callback function to be called when the button is pressed a third time.
void begin(uint8_t pin)
Reads the debounced state of the button.
void set_debounce_ms(uint16_t debounce_ms)
Sets the debounce period for the button. Debouncing refers to checking the button state over a short ...
RPC class for handling remote procedure calls over serial streams.
Definition rpc.hpp:35