nimphea/hid/switch

Source   Edit  

Momentary/Latching Switch with Debouncing

Generic class for handling momentary/latching switches with debouncing. Inspired by Mutable Instruments Switch classes.

Features:

  • Debounced button input
  • Rising/falling edge detection
  • Press duration tracking
  • Toggle or momentary modes
  • Normal or inverted polarity

Usage:

import nimphea/hid/switch

var button: Switch
button.init(D0())  # Simple momentary button on pin D0

while true:
  button.debounce()
  
  if button.risingEdge():
    echo "Button pressed!"
  
  if button.pressed():
    echo "Button held for ", button.timeHeldMs(), " ms"
  
  delayMs(1)

Types

GpioPull {.importcpp: "daisy::GPIO::Pull", size: 4.} = enum
  PULL_NOPULL,              ## No pull resistor
  PULL_UP,                  ## Pull-up resistor (default)
  PULL_DOWN                  ## Pull-down resistor
GPIO pull-up/pull-down configuration Source   Edit  
Switch {.importcpp: "daisy::Switch", header: "hid/switch.h".} = object
Debounced switch/button handler Source   Edit  
SwitchPolarity {.importcpp: "daisy::Switch::Polarity", size: 4.} = enum
  POLARITY_NORMAL,          ## HIGH = pressed
  POLARITY_INVERTED          ## LOW = pressed (default, common with pull-ups)
Switch polarity (which state is "pressed") Source   Edit  
SwitchType {.importcpp: "daisy::Switch::Type", size: 4.} = enum
  TYPE_TOGGLE,              ## Toggle/latching switch
  TYPE_MOMENTARY             ## Momentary pushbutton (default)
Switch behavior type Source   Edit  

Procs

proc Debounce(this: var Switch) {.importcpp: "#.Debounce()",
                                  header: "hid/switch.h", ...raises: [], tags: [],
                                  forbids: [].}
Source   Edit  
proc debounce(sw: var Switch) {....raises: [], tags: [], forbids: [].}

Update switch state with debouncing

Call this regularly (e.g., every 1ms) to update the switch state. Must be called for edge detection and timing to work correctly.

Example:

while true:
  button.debounce()
  # Check button state...
  delayMs(1)

Source   Edit  
proc fallingEdge(sw: Switch): bool {....raises: [], tags: [], forbids: [].}

Check if button was just released

Returns: true for one cycle when button transitions from pressed to released

Example:

if button.fallingEdge():
  echo "Button released!"

Source   Edit  
proc FallingEdge(this: Switch): bool {.importcpp: "#.FallingEdge()",
                                       header: "hid/switch.h", ...raises: [],
                                       tags: [], forbids: [].}
Source   Edit  
proc Init(this: var Switch; pin: Pin; update_rate: cfloat = 0.0) {.
    importcpp: "#.Init(@)", header: "hid/switch.h", ...raises: [], tags: [],
    forbids: [].}
Source   Edit  
proc Init(this: var Switch; pin: Pin; update_rate: cfloat; t: SwitchType;
          pol: SwitchPolarity; pu: GpioPull) {.importcpp: "#.Init(@)",
    header: "hid/switch.h", ...raises: [], tags: [], forbids: [].}
Source   Edit  
proc init(sw: var Switch; pin: Pin) {....raises: [], tags: [], forbids: [].}

Initialize switch with default settings (momentary, inverted, pull-up)

Parameters:

  • pin - GPIO pin for the switch

Example:

var button: Switch
button.init(D0())

Source   Edit  
proc init(sw: var Switch; pin: Pin; switchType: SwitchType;
          polarity: SwitchPolarity; pull: GpioPull) {....raises: [], tags: [],
    forbids: [].}

Initialize switch with full configuration

Parameters:

  • pin - GPIO pin for the switch
  • switchType - TYPE_MOMENTARY or TYPE_TOGGLE
  • polarity - POLARITY_NORMAL or POLARITY_INVERTED
  • pull - PULL_NOPULL, PULL_UP, or PULL_DOWN

Example:

var button: Switch
button.init(D0(), TYPE_MOMENTARY, POLARITY_INVERTED, PULL_UP)

Source   Edit  
proc pressed(sw: Switch): bool {....raises: [], tags: [], forbids: [].}

Check if button is currently held down

Returns: true while button is pressed (after debouncing)

Example:

if button.pressed():
  echo "Button is down"

Source   Edit  
proc Pressed(this: Switch): bool {.importcpp: "#.Pressed()",
                                   header: "hid/switch.h", ...raises: [], tags: [],
                                   forbids: [].}
Source   Edit  
proc rawState(sw: var Switch): bool {....raises: [], tags: [], forbids: [].}

Read raw button state without debouncing

Returns: true if button is physically pressed (without debounce)

Note: Usually you want pressed() instead

Source   Edit  
proc RawState(this: var Switch): bool {.importcpp: "#.RawState()",
                                        header: "hid/switch.h", ...raises: [],
                                        tags: [], forbids: [].}
Source   Edit  
proc RisingEdge(this: Switch): bool {.importcpp: "#.RisingEdge()",
                                      header: "hid/switch.h", ...raises: [],
                                      tags: [], forbids: [].}
Source   Edit  
proc risingEdge(sw: Switch): bool {....raises: [], tags: [], forbids: [].}

Check if button was just pressed

Returns: true for one cycle when button transitions from released to pressed

Example:

if button.risingEdge():
  echo "Button pressed!"

Source   Edit  
proc TimeHeldMs(this: Switch): cfloat {.importcpp: "#.TimeHeldMs()",
                                        header: "hid/switch.h", ...raises: [],
                                        tags: [], forbids: [].}
Source   Edit  
proc timeHeldMs(sw: Switch): float {....raises: [], tags: [], forbids: [].}

Get how long the button has been held

Returns: Time in milliseconds that button has been pressed (0 if not pressed)

Example:

if button.pressed():
  echo "Held for ", button.timeHeldMs(), " ms"

Source   Edit