Skip to content

daisy::Switch

Module: LIBDAISY / HUMAN_INTERFACE / CONTROLS

More...

#include <switch.h>

Public Types

Name
enum Type
enum Polarity
enum Pull

Public Functions

Name
Switch()
~Switch()
void Init(dsy_gpio_pin pin, float update_rate, Type t, Polarity pol, Pull pu)
void Init(dsy_gpio_pin pin, float update_rate =0.f)
void Debounce()
bool RisingEdge() const
bool FallingEdge() const
bool Pressed() const
bool RawState()
float TimeHeldMs() const
void SetUpdateRate(float update_rate)

Detailed Description

class daisy::Switch;

Author: Stephen Hensley

Date: December 2019

Generic Class for handling momentary/latching switches

Inspired/influenced by Mutable Instruments (pichenettes) Switch classes

Public Types Documentation

enum Type

Enumerator Value Description
TYPE_TOGGLE &
TYPE_MOMENTARY &

Specifies the expected behavior of the switch

enum Polarity

Enumerator Value Description
POLARITY_NORMAL &
POLARITY_INVERTED &

Specifies whether the pressed is HIGH or LOW.

enum Pull

Enumerator Value Description
PULL_UP &
PULL_DOWN &
PULL_NONE &

Specifies whether to use built-in Pull Up/Down resistors to hold button at a given state when not engaged.

Public Functions Documentation

function Switch

inline Switch()

function ~Switch

inline ~Switch()

function Init

void Init(
    dsy_gpio_pin pin,
    float update_rate,
    Type t,
    Polarity pol,
    Pull pu
)

Parameters:

  • pin port/pin object to tell the switch which hardware pin to use.
  • update_rate Does nothing. Backwards compatibility until next breaking update.
  • t switch type – Default: TYPE_MOMENTARY
  • pol switch polarity – Default: POLARITY_INVERTED
  • pu switch pull up/down – Default: PULL_UP

Initializes the switch object with a given port/pin combo.


function Init

void Init(
    dsy_gpio_pin pin,
    float update_rate =0.f
)

Parameters:

  • pin port/pin object to tell the switch which hardware pin to use.
  • update_rate Left for backwards compatibility until next breaking change.

Simplified Init.


function Debounce

void Debounce()

Called at update_rate to debounce and handle timing for the switch. In order for events not to be missed, its important that the Edge/Pressed checks be made at the same rate as the debounce function is being called.


function RisingEdge

inline bool RisingEdge() const

Return: true if a button was just pressed.


function FallingEdge

inline bool FallingEdge() const

Return: true if the button was just released


function Pressed

inline bool Pressed() const

Return: true if the button is held down (or if the toggle is on)


function RawState

inline bool RawState()

Return: true if the button is held down, without debouncing


function TimeHeldMs

inline float TimeHeldMs() const

Return: the time in milliseconds that the button has been held (or toggle has been on)


function SetUpdateRate

inline void SetUpdateRate(
    float update_rate
)

Parameters:

  • update_rate Doesn't do anything

Left for backwards compatability until next breaking change

---

Updated on 2024-01-03 at 19:41:01 +0000