Close

Code Overview 3: Buttons/Switches

A project log for HEXABITZ - Modular Electronics for REAL

A new kind of electronic prototyping!

HexabitzHexabitz 04/03/2018 at 03:070 Comments

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:

Once a button has been defined, it will be saved to 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)

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

Note: You can clear a latching event using resetButtonEvent() API which takes the port (i.e., button) number and event type.

CLI Support

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.

Discussions