You can connect any mechanical button or switch to any of the array ports using the following API:
BOS_Status AddPortButton(uint8_t buttonType, uint8_t port)
Where buttonType can be:
- MOMENTARY_NO: A momentary tactile (push button) switch – naturally open.
- MOMENTARY_NC: A momentary tactile (push button) switch – naturally closed.
- ONOFF_NO: A toggle (ON/OFF) switch – naturally open.
- ONOFF_NC: A toggle (ON/OFF) switch – naturally closed.
Once a button has been defined, it will be saved to emulated EEPROM and configured automatically each time on startup. You can also use the following API to remove a button and delete its definition from EEPROM:
BOS_Status RemovePortButton(uint8_t port)
Current button status can be accessed via button[i].state where i is port number. (You can refer to a button connected at port P1 with B1 and so forth.) A button status could be one of the following states:
OFF ON OPEN CLOSED CLICKED DBL_CLICKED PRESSED RELEASED PRESSED_FOR_X1_SEC PRESSED_FOR_X2_SEC PRESSED_FOR_X3_SEC RELEASED_FOR_Y1_SEC RELEASED_FOR_Y2_SEC RELEASED_FOR_Y3_SEC
Once a button/switch is defined, you can configure it to trigger certain events using this API:
BOS_Status SetButtonEvents(uint8_t port, uint8_t clicked, uint8_t dbl_clicked, uint8_t pressed_x1sec, uint8_t pressed_x2sec, uint8_t pressed_x3sec, uint8_t released_y1sec, uint8_t released_y2sec, uint8_t released_y3sec, uint8_t mode)
- port: array port where the button is attached (P1 .. Px or B1 .. Bx).
- clicked: Single click event (1: Enable, 0: Disable)
- dbl_clicked: Double click event (1: Enable, 0: Disable)
- pressed_X1sec, pressed_X2sec, pressed_X3sec: Press time for events X1, X2 and X3 in seconds. Use 0 to disable the event. This is a latching event.
- released_Y1sec, released_Y2sec, released_Y3sec: Release time for events Y1, Y2 and Y3 in seconds. Use 0 to disable the event. This is a latching event.
- mode: BUTTON_EVENT_MODE_CLEAR to clear events marked with 0, BUTTON_EVENT_MODE_OR to OR events marked with 1 with existing events.
These events can be linked to user callbacks to execute some tasks. Add the following callbacks to your code in main.c to make use of button events:
void buttonClickedCallback(uint8_t port) { } void buttonDblClickedCallback(uint8_t port) { } void buttonPressedForXCallback(uint8_t port, uint8_t eventType) { } void buttonReleasedForYCallback(uint8_t port, uint8_t eventType) { }
Where eventType is:
PRESSED_FOR_X1_SEC PRESSED_FOR_X2_SEC PRESSED_FOR_X3_SEC RELEASED_FOR_Y1_SEC RELEASED_FOR_Y2_SEC RELEASED_FOR_Y3_SEC
CLI Support
Port buttons/switches functionality is available through the CLI as well. You can use the conditional if … end if Command Snippet to link a button event to another command as well. Check the Command Snippets article for more details.