JavaScript-like button and switch callback binding for Arduino
Installation 1
From the KeySwitch repo, select Clone or download > Download ZIP
, or use the direct download link.
In the Arduino IDE, navigate to Sketch > Include Library > Add .ZIP Library
. At the top of the drop down list, select the option to Add .ZIP Library
.
Return to the Sketch > Include Library
menu. You should now see the library at the bottom of the drop-down menu. It is ready to be used in your sketch. The zip file will have been expanded in the libraries folder in your Arduino sketches directory.
Two code examples are provided:
Demonstrates setting simple button bindings using pre-defined void->void functions.
Demonstrates use of C++ lamabdas for cleaner callback handling.
Create a 2-Key macro keypad that copies and pastes. Note: Given example uses the TrinketKeyboard library for the ATtiny85 (Digispark, etc), but other keyboard libraries can be used as well.
Re-implement of Adafruit NeoPixel butoncycler example.
Bind digital pin state changes to function callbacks.
Name | Type | Description |
---|---|---|
pin |
uin8t_t |
Pin number or alias to bind class |
invert |
bool |
Invert the button state FALSE Pin is passive low (Pulled down) and high when pressed.TRUE Pin is passive high (Pulled up) and low when pressed |
Returns: A new KeySwitch
class instance.
Bind to the digital IO pin and begin listening to the switch.
Poll IO state for changes. Triggers callbacks if bound.
Is the button currently down? See: isPressed
Returns: bool
TRUE
if the button is down.
Is the button currently up?
Returns: bool
TRUE
if the button is up.
Is the button currently pressed?
Returns: bool
TRUE
if the button is pressed.
Is the button currently held?
Returns: bool
TRUE
if the button has been held for longer than the configured timeout.
Bind a callback that triggers once, every time the button is pressed.
Name | Type | Description |
---|---|---|
callback |
void * |
Function to invoke when button is pressed |
Bind a callback that triggers once, every time the button is released.
Name | Type | Description |
---|---|---|
callback |
void * |
Function to invoke when button is released |
Bind a callback that triggers continuously as long as the button is pressed.
Name | Type | Description |
---|---|---|
callback |
void * |
Function to invoke when the button is pressed |
Bind a callback that triggers once, every time the button is held longer than hold_time
.
Name | Type | Description |
---|---|---|
callback |
void * |
Function to invoke when button is held. |
hold_time |
uint16_t |
How long must the button be held before invoking the callback. |
2019 Ian McLinden under the GNU General Public License v3.0. Use at your own risk, author retains no liability for misuse.
1) Installation instructions provided in part from the official Installing Additional Arduino Libraries guide.
2) Library format based in part on the Arduino Libraries documentation, and on the Arduino Test Library Implementation