nimphea/hid/switch3

Source   Edit  

3-Position Switch

Simple 3-position switch (toggle switch or encoder switch positions).

Positions:

  • CENTER (0)
  • UP/LEFT (1)
  • DOWN/RIGHT (2)

Usage:

import nimphea/hid/switch3

var sw: Switch3
sw.init(D0, D1)  # Two pins for 3 positions

# Read position
let pos = sw.read()
case pos
of SWITCH3_POS_CENTER:
  echo "Center"
of SWITCH3_POS_UP:
  echo "Up/Left"
of SWITCH3_POS_DOWN:
  echo "Down/Right"
else:
  discard

Types

Switch3 {.importcpp: "daisy::Switch3", header: "hid/switch3.h".} = object
3-position switch handler Source   Edit  

Consts

SWITCH3_POS_CENTER = 0
Center position Source   Edit  
SWITCH3_POS_DOWN = 2
Down position (same as RIGHT) Source   Edit  
SWITCH3_POS_LEFT = 1
Left position (same as UP) Source   Edit  
SWITCH3_POS_RIGHT = 2
Right position (same as DOWN) Source   Edit  
SWITCH3_POS_UP = 1
Up position (same as LEFT) Source   Edit  

Procs

proc init(this: var Switch3; pina, pinb: Pin) {.importcpp: "#.Init(#, #)",
    ...raises: [], tags: [], forbids: [].}

Initialize 3-position switch

Parameters:

  • pina - First pin
  • pinb - Second pin

Note: Both pins use internal pull-up resistors

Example:

var sw: Switch3
sw.init(D0, D1)

Source   Edit  
proc read(this: var Switch3): cint {.importcpp: "#.Read()", ...raises: [],
                                     tags: [], forbids: [].}

Read current switch position

Returns:

  • SWITCH3_POS_CENTER (0) - Center/neutral position
  • SWITCH3_POS_UP/LEFT (1) - Up or left position
  • SWITCH3_POS_DOWN/RIGHT (2) - Down or right position

Example:

let pos = sw.read()
if pos == SWITCH3_POS_UP:
  echo "Switch is up!"

Source   Edit