mupplet-core GPIO Switch class
The Switch class allows to integrate switches and buttons in muwerk by either polling GPIOs or by using interrupts. Optional automatic debouncing is provided for both modes, but mostly important for interrupt-mode.
In mode falling or rising, only a 'trigger' event is generated, there is no on or off state. This is useful to implement push-button events.
The mupplet can automatically generate short- and long-press events and provide timing information about length of button presses.
Other options include flip-flop mode and monostable impulses.
Additionally, a binary_sensor mode is available that simply reports the state of the input pin. Note however that there is a simpler implementation of BinarySensor available in the mupplet-sensor project, which is recommended for most use-cases.
The physical hardware of a switch can be overriden by a software setLogicalState, a switch stays in override-mode until the next state change of the physical hardware is detected.
Messages
Messages sent by the switch mupplet:
topic | message body | comment |
<mupplet-name>/switch/state | on , off or trigger | switch state, usually on or off . In modes falling and rising only trigger messages are sent on rising or falling signal. |
<mupplet-name>/switch/debounce | <time-in-ms> | reply to <mupplet-name>/switch/debounce/get , switch debounce time in ms [0..1000]ms. |
<custom-topic> | on , off or trigger | If a custom-topic is given during switch init, an addtional message is publish on switch state changes with that topic, The message is identical to ../switch/state', usually on or off . In modes falling and rising only trigger . |
<mupplet-name>/switch/shortpress | trigger | Switch is in duration mode, and button is pressed for less than <shortpress_ms> (default 3000ms). |
<mupplet-name>/switch/longpress | trigger | Switch is in duration mode, and button is pressed for less than <longpress_ms> (default 30000ms), yet longer than shortpress. |
<mupplet-name>/switch/verylongtpress | trigger | Switch is in duration mode, and button is pressed for longer than <longpress_ms> (default 30000ms). |
<mupplet-name>/switch/duration | <ms> | Switch is in duration mode, message contains the duration in ms the switch was pressed. |
<mupplet-name>/switch/counter | <count> | If counter was activated (see switch/counter/start msg below, or activateCounter() ), the number of times the switch as been switch logcial on. |
Message received by the switch mupplet:
topic | message body | comment |
<mupplet-name>/switch/set | on , off , true , false , toggle | Override switch setting. When setting the switch state via message, the hardware port remains overridden until the hardware changes state (e.g. button is physically pressed). Sending a switch/set message puts the switch in override-mode: e.g. when sending switch/set on , the state of the button is signalled on , even so the physical button might be off. Next time the physical button is pressed (or changes state), override mode is stopped, and the state of the actual physical button is published again. |
<mupplet-name>/switch/mode/set | default , rising , falling , flipflop , binary_sensor , timer <time-in-ms> , duration [shortpress_ms[,longpress_ms]] | Mode default sends on when a button is pushed, off on release. falling and rising send trigger on corresponding signal change. flipflop changes the state of the logical switch on each change from button on to off. timer keeps the switch on for the specified duration (ms). duration mode sends messages switch/shortpress , if button was pressed for less than <shortpress_ms> (default 3000ms), switch/longpress if pressed less than <longpress_ms> , and switch/verylongpress for longer presses. |
<mupplet-name>/switch/debounce/set | <time-in-ms> | String encoded switch debounce time in ms, [0..1000]ms. Default is 20ms. This is especially need, when switch is created in interrupt mode (see comment in example). |
<mupplet-name>/switch/state/get | | Sends current switch state (logical). |
<mupplet-name>/binary_sensor/state/get | | Sends current binary_sensor state, for Mode::BinarySensor only, (logical). |
<mupplet-name>/switch/counter/get | | Send current number of counts, if counter is active, otherwise NaN . |
<mupplet-name>/switch/counter/start | | Start counter, counter messages will be sent, count is reset to 0. All counters are off by default. |
<mupplet-name>/switch/counter/stop | | Stop counting |
More information: Switch application notes