peripheral/button/main.c

/*******************************************************************************
* # License
* Copyright 2019 Silicon Laboratories Inc. www.silabs.com
*******************************************************************************
*
* The licensor of this software is Silicon Laboratories Inc. Your use of this
* software is governed by the terms of Silicon Labs Master Software License
* Agreement (MSLA) available at
* www.silabs.com/about-us/legal/master-software-license-agreement. This
* software is distributed to you in Source Code format and is governed by the
* sections of the MSLA applicable to Source Code.
*
******************************************************************************/
/* Documentation for this app is available online.
* See https://docs.silabs.com/gecko-os/4/standard/latest/sdk/examples/peripheral/button
*/
#include "gos.h"
#define BUTTON_DEBOUNCE_TIME_MS (50) // ms
#define BUTTON_CLICK_TIME_MS (500) // ms
#define BUTTON_PRESS_TIME_MS (1000) // ms
#define APPLICATION_START_LINE "\r\nButton example starting ..."
/*************************************************************************************************/
void gos_app_init(void)
{
const gos_button_config_t config =
{
.active_level = PLATFORM_BUTTON_ACTIVE_STATE,
.debounce = BUTTON_DEBOUNCE_TIME_MS,
.click_time = BUTTON_CLICK_TIME_MS,
.press_time = BUTTON_PRESS_TIME_MS,
.event_handler.press = button_pressed_event_handler,
.event_handler.click = button_clicked_event_handler,
.event_handler.toggle = button_toggled_event_handler,
.execution_context = GOS_BUTTON_CONTEXT_DEFAULT
};
gos_button_init(PLATFORM_BUTTON1, &config, (void*)1);
gos_button_init(PLATFORM_BUTTON2, &config, (void*)2);
GOS_LOG(APPLICATION_START_LINE);
GOS_LOG("Hold button 1 or 2 for more than %dms to invoke 'press' event", BUTTON_PRESS_TIME_MS);
GOS_LOG("Press button 1 or 2 for less than %dms to invoke 'click' event", BUTTON_CLICK_TIME_MS);
}
/*************************************************************************************************/
static void button_clicked_event_handler(void *arg)
{
uint32_t button_number = (uint32_t)arg;
GOS_LOG("Button%d: click", button_number);
}
/*************************************************************************************************/
static void button_pressed_event_handler(void *arg)
{
uint32_t button_number = (uint32_t)arg;
GOS_LOG("Button%d: pressed", button_number);
}
/*************************************************************************************************/
static void button_toggled_event_handler(void *arg)
{
uint32_t button_number = (uint32_t)arg;
GOS_LOG("Button%d: toggled", button_number);
}