Stepdance Software Library
Loading...
Searching...
No Matches
Button Class Reference

Button class for handling digital input buttons. More...

#include <digital_in.hpp>

Inheritance diagram for Button:

Public Member Functions

 Button ()
 Default constructor for Button. Does not initialize board hardware. Call begin() to set up the button pin.
void begin (uint8_t pin)
 Reads the debounced state of the button.
uint8_t read ()
 Initializes the Button with the specified pin and mode.
uint8_t read_raw ()
 Reads the raw state of the input pin without debouncing.
uint8_t has_changed ()
 Checks if the button state has changed since the last read.
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.
void set_callback_on_release (void(*callback_function)())
 Sets a callback function to be called when the button is released.
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 period to avoid false triggering due to mechanical noise.
void set_mode (uint8_t button_mode)
 Sets the button mode to either standard or toggle. Toggle mode triggers the button state only on presses. Standard will trigger callbacks on both press and release.
void set_invert ()
 Inverts the button state logic. When inverted, a pressed button reads as released and vice versa. Not sure why you want this, but it's here if you need it.
void clear_invert ()
 Clears the invert flag, restoring normal button state logic.

Detailed Description

Button class for handling digital input buttons.

The Button class provides an interface for reading and managing digital input buttons. It supports debouncing, state change detection, and callback functions for button events such as press, release, and toggle. Buttons can be configured in standard or toggle mode, and their state can be inverted if needed. Here's an example of how to instantiate and configure a Button:

#define module_driver
#include "stepdance.hpp"
Button button_d1;
Button button_d2;
void setup() {
button_d1.begin(IO_D1); //initialize button on physical input port IO_D1
button_d1.set_callback_on_release(&onButtonD1Release); //set callback function for button D1 release event
button_d2.begin(IO_D2); //initialize button on physical input port IO_D2
button_d2.set_mode(BUTTON_MODE_TOGGLE); //set button D2 to toggle mode
button_d2.set_callback_on_toggle(&onButtonD2Toggle); //set callback function for button D2 press event
dance_start();
}
void onButtonD1Release() {
// Code to execute when button D1 is released
Serial.println("Button D1 Released");
}
void onButtonD2Toggle() {
// Code to execute when button D2 is toggled
Serial.println("Button D2 Toggled");
}
void loop(){
dance_loop();
}

Member Function Documentation

◆ begin()

void Button::begin ( uint8_t pin)

Reads the debounced state of the button.

Returns
uint8_t Returns BUTTON_STATE_PRESSED or BUTTON_STATE_RELEASED.

Initializes the Button with the specified pin and mode.

Parameters
pinThe digital pin number where the button is connected (e.g. )

◆ has_changed()

uint8_t Button::has_changed ( )

Checks if the button state has changed since the last read.

Returns
uint8_t Returns 1 if the button state has changed, otherwise returns 0.

◆ read()

uint8_t Button::read ( )

Initializes the Button with the specified pin and mode.

Parameters
pinThe digital pin number where the button is connected (e.g. )
modeThe pin mode, e.g. INPUT, INPUT_PULLUP, etc.

Reads the state of the button.

Returns
uint8_t Returns BUTTON_STATE_PRESSED or BUTTON_STATE_RELEASED.

◆ read_raw()

uint8_t Button::read_raw ( )

Reads the raw state of the input pin without debouncing.

Returns
uint8_t Returns the raw pin state.

◆ set_callback_on_press()

void Button::set_callback_on_press ( void(* callback_function )())

Sets a callback function to be called when the button is pressed.

Parameters
callback_functionPointer to the callback function.

◆ set_callback_on_release()

void Button::set_callback_on_release ( void(* callback_function )())

Sets a callback function to be called when the button is released.

Parameters
callback_functionPointer to the callback function.

◆ set_callback_on_toggle()

void Button::set_callback_on_toggle ( void(* callback_function )())

Sets a callback function to be called when the button state toggles-either pressed or released.

Parameters
callback_functionPointer to the callback function.

◆ set_debounce_ms()

void Button::set_debounce_ms ( uint16_t debounce_ms)

Sets the debounce period for the button. Debouncing refers to checking the button state over a short period to avoid false triggering due to mechanical noise.

Parameters
debounce_msDebounce period in milliseconds.

◆ set_mode()

void Button::set_mode ( uint8_t button_mode)

Sets the button mode to either standard or toggle. Toggle mode triggers the button state only on presses. Standard will trigger callbacks on both press and release.

Parameters
button_modeBUTTON_MODE_STANDARD or BUTTON_MODE_TOGGLE.

The documentation for this class was generated from the following file: