Button subscription
Use the button module to subscribe to button events.
Code
toit
import lightbug.devices as devices
import lightbug.messages as messages
main:
device := devices.I2C --background=false
device.buttons.subscribe --timeout=null --callback=(:: |button-data/messages.ButtonPress|
button-name := messages.ButtonPress.button-id-from-int button-data.button-id
print "Button pressed: $button-name (ID: $button-data.button-id) for $button-data.duration ms"
)Input
Press the right button a few times.
Result
This example only logs button presses to the console.
Console
Button pressed: Right Down (ID: 2) for 686 ms
Button pressed: Right Down (ID: 2) for 105 ms
Button pressed: Right Down (ID: 2) for 127 ms
Button pressed: Right Down (ID: 2) for 121 ms
Button pressed: Right Down (ID: 2) for 145 ms
Button pressed: Right Down (ID: 2) for 107 ms
Button pressed: Right Down (ID: 2) for 1113 ms
Button pressed: Right Down (ID: 2) for 2277 ms