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_NON_INVERTED 0
25#define BUTTON_INVERTED 1
26
27#define BUTTON_STATE_RELEASED 0
28#define BUTTON_STATE_PRESSED 1
29#define BUTTON_CHANGED 1
34class ButtonKilohertzPlugin : public Plugin{ //we'll set up to run on every kilohertz frame
35 public:
36 ButtonKilohertzPlugin();
37 void begin();
38 protected:
39 void run();
40};
42
43/**/
52class Button : public Plugin{
53 public:
66 void begin(uint8_t pin); //defaults to INPUT_PULLUP
76 uint8_t read(); //returns button state
81 uint8_t read_raw(); //returns the asynchronous raw state of the input pin
86 uint8_t has_changed(); //returns 1 if the button state has changed
91 void set_callback_on_toggle(void (*callback_function)());
96 void set_callback_on_press(void (*callback_function)());
101 void set_callback_on_release(void (*callback_function)());
106 void set_debounce_ms(uint16_t debounce_ms);
111 void set_mode(uint8_t button_mode); //standard, or toggle
115 void set_invert(); //inverts the button state
119 void clear_invert(); //clears the invert button mode
120
124
125 static Button* registered_buttons[MAX_NUM_BUTTONS]; //stores all registered buttons
126 static uint8_t num_registered_buttons;
127 static ButtonKilohertzPlugin kilohertz_plugin; // runs in the kilohertz frame
128 void begin(uint8_t pin, uint8_t mode);
129 void on_frame(); //gets called at each kilohertz frame
130 void enroll(RPC *rpc, const String& instance_name);
132
133 private:
134 uint8_t input_pin;
135 uint8_t invert = BUTTON_NON_INVERTED;
136 uint16_t debounce_period_ms = DIGITAL_IN_DEFAULT_DEBOUNCE_MS;
137 uint8_t button_mode = BUTTON_MODE_STANDARD;
138 volatile uint16_t debounce_blackout_remaining_ms = 0; //starts at debounce_period_ms, and counts down to zero
139 volatile uint8_t button_state = 0;
140 volatile uint8_t change_flag = 0; //set when the button state changes, unless a callback is provided
141 volatile uint8_t last_raw_pin_state = 0;
142 void (*callback_on_toggle)() = nullptr;
143 void (*callback_on_press)() = nullptr;
144 void (*callback_on_release)() = nullptr;
145};
146
147
148
149
150
151#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...
uint8_t read()
Initializes the Button with the specified pin and mode.
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 released.
void clear_invert()
Clears the invert flag, restoring normal button state logic.
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