User Tools

Site Tools


mosh:element_library:button

Button

This page describes the configuration options available for the MOSH button element, including how to define its properties and map them to virtual wires.

The button element reads a digital GPIO pin and reports press, release, and advanced input events such as long press, double press, and short-long combinations.

Creating a Button element

#include <mosh_core.h>
#include <elements/button.h>
 
MOSH_Core MOSH_Core;
 
const uint8_t pin = 4
MOSH_Element_Button* myButton = nullptr;
 
void setup() {
  myButton.monitor(pin);
  MOSH_Core.registerElement(myButton);
}

Parameter Table:

Option TypeDefaultDescription
active_low bool falseWhether active state is low (true for inverted logic)
debounce_delay uint8_t 50 Amount of delay (in milliseconds) used for debouncing
id integer Unique Element ID
long_press_delay uint16_t750 Amount of delay (in milliseconds) for determining a long press
name string Friendly label
pin uint8_t GPIO pin number connected to the button
press_delay uint16_t250 Amount of delay (in milliseconds) for determining a short press
pullup bool true Enable internal pull-up resistor
stream_on_press bool falseSends a stream of PRESS notifications when the button is active
stream_on_press_frequint16_t50 The amount of time between PRESS notifications when stream_on_press is true
virtual_wire uint8_t ID of the virtual wire is assigned
constant_on bool false

Property Status Messages:

enum class MOSH_Element_Button_Status
NameBitDescription
RESET0x00Reserved for future use.
STATE_ON0x01Sent when the button is placed in the “pressed” position.
STATE_OFF0x02Sent when the button is released
PRESS0x03Sent when the button is pressed and released within the press_delay
DOUBLE_PRESS0x04Sent when the button is pressed and release twice within the press_delay
LONG_PRESS0x05Sent after the button is released after being pressed for long_press_delay
SHORT_LONG_PRESS0x06Sent after a combination of a PRESS and LONG_PRESS
USER_DEF10xFCSent with button_element.send_userdef_1()
USER_DEF20xFDSent with button_element.send_userdef_2()
USER_DEF30xFESent with button_element.send_userdef_3()
USER_DEF40xFFSent with button_element.send_userdef_4()

Static config:

None

Setup Methods:

Methods intended to be called during setup or initialization. These configure how the element behaves and are typically not changed during runtime.

MethodDirectionRequiredDefaultParametersDescription
setActiveLow(bool)WritetrueboolButton is pressed when pulled low
setDebounce(uint16_t)Write10uint16_tSet the debounce time in milliseconds
setLongPressDelay(uint16_t)Write750uint16_tSet the long-press delay in milliseconds
setName(string)WritenullstringSet a friendly name for the element
setPin(uint8_t)Writenulluint8_tThe pin the button is wired to
setPressDelay(uint16_t)Write250uint16_tSet the press delay in milliseconds
setPullup(bool)WritetrueboolUse the internal pullup
setStreamOnPress(bool)WritefalseboolButton will send stream of PRESS events while active
setStreamOnPressFreq(uint16_t)Writenulluint16_tSets how frequently a PRESS event is sent during a press stream

Runtime Methods:

MethodDirectionParametersreturnsDescription
buttonIsPressed()ReadboolReturns true if button is pressed, false if not
getFriendlyName()ReadstringReturns the friendly name of the element
getLastPropertyChange()Readuint32_tget the amount of time (in millies) since the last state change)
getProperty(name)ReadfriendlyName, state, event, lastChangePropertyValueReturns the current value of a property
getID()Readuint8_tReturns the element's unique ID
getName()ReadStringReturns the element's friendly name.
send_userdef_1()User calledvoidUser defined action
send_userdef_2()User calledvoidUser defined action
send_userdef_3()User calledvoidUser defined action
send_userdef_4()User calledvoidUser defined action
/var/www/wiki.moshnetworks.com/data/pages/mosh/element_library/button.txt · Last modified: by mosh

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki