How to: Push Button

You can control your device in many ways - you can add switches, sensors, Wi-Fi connection, etc. But the most simple way is to use button(s) integrated in Core module.

Requirements

Integrated Buttons

Core module comes with one button that can be used when there is no other module on top of the Core module, otherwise it is pretty hard to reach.

This tutorial shows how to work with integrated button, but it can be used for your own buttons or switches.

Recognizable Button Events

TWR_BUTTON_EVENT_PRESS and TWR_BUTTON_EVENT_RELEASE are pretty straightforward - the first one stands for pressing the button and the second one for releasing the button.

TWR_BUTTON_EVENT_CLICK event is recognized, when button is pressed and held for period of time shorter than defined (can be defined by twr_button_set_click_timeout)

TWR_BUTTON_EVENT_HOLD event is recognized, when button is pressed and held for period of time longer than defined (can be defined by twr_button_set_hold_timeout)

Example

First, an instance of button is needed. You can achieve this by adding this line: twr_button_t button;

Button is initiated by function twr_button_init

  • *self is an address to the instantiated button

  • gpio_channel is GPIO channel number - defined as enum, more in SDK

  • gpio_pull stands for GPIO pull up/down settings

  • idle_state - state of a GPIO pin, when button is not pressed

In our example, we call init function this way:

twr_button_init(&button, TWR_GPIO_BUTTON, TWR_GPIO_PULL_DOWN, 0);

Then we need to define event handler - what function to call when something happens with the button.

twr_button_set_event_handler(&button, button_event_handler, NULL);

In other words: when button is triggered, call function called ‘button_event_handler’ with no additional parameters

In the button_event_handler function we mainly compare the event parameter with callback events defined in twr_button_event_t.

In this example we will use _HOLD and _PRESS events for better understanding. Programmed Core module will work by these rules:

  • when button is held for 1.5 seconds (or more), LED starts to blink fast

  • when button is pressed (simple short press), LED goes off

To set the 1.5 second interval we can use twr_button_set_hold_time function (inside application_init()) twr_button_set_hold_time(&button, 1500);

Everything else is defined in the button_event_handler function.

 1#include <application.h>
 2
 3twr_led_t led;
 4twr_button_t button;
 5
 6void button_event_handler(twr_button_t *self, twr_button_event_t event, void *event_param)
 7{
 8    (void) self;
 9    (void) event_param;
10
11    if (event == TWR_BUTTON_EVENT_PRESS)
12    {
13        twr_led_set_mode(&led, TWR_LED_MODE_OFF);
14    } else if (event == TWR_BUTTON_EVENT_HOLD ) {
15        twr_led_set_mode(&led,  TWR_LED_MODE_BLINK_FAST);
16    }
17}
18
19void application_init(void)
20{
21    // Initialize LED
22    twr_led_init(&led, TWR_GPIO_LED, false, false);
23    twr_led_set_mode(&led, TWR_LED_MODE_OFF);
24
25    // Initialize button
26    twr_button_init(&button, TWR_GPIO_BUTTON, TWR_GPIO_PULL_DOWN,0);
27    twr_button_set_event_handler(&button, button_event_handler, NULL);
28
29    // Set HOLD time
30    twr_button_set_hold_time(&button, 1500);
31}

Full ready-to-flash code for this example can be found on Github.