Wirepas SDK
scheduler/app.c
/* Copyright 2018 Wirepas Ltd. All Rights Reserved.
*
* See file LICENSE.txt for full license details.
*
*/
/*
* \file app.c
* \brief This file is an example on how to use the application
* scheduler. It contains several examples:
* - Single shot task (mainly for interrupt deferred work)
* - Periodic task (with different periods)
* - Simple state machine
*/
#include <stdlib.h>
#include "api.h"
#include "led.h"
#include "button.h"
#include "app_scheduler.h"
#define DEBUG_LOG_MODULE_NAME "SCHED_EX"
#define DEBUG_LOG_MAX_LEVEL LVL_INFO
#include "debug_log.h"
/*
* \brief This task is a periodic task running ~every 50ms
* Toggle the led 0
*/
static uint32_t periodic_task_50ms()
{
return 50;
}
/*
* \brief This task is a periodic task running ~every 500ms
* Toggle the led 1
*/
static uint32_t periodic_task_500ms()
{
return 500;
}
/*
* \brief This task is a single shot task
* Toggle the led 2, scheduled from button
* interrupt context
*/
static uint32_t single_shot_task()
{
}
static uint32_t steps[] = {200, 200, 200, 200, 200, 200,
500, 500, 500, 500, 500, 500,
200, 200, 200, 200, 200, 200};
static uint32_t state_machine_task()
{
static uint8_t current_step = 0;
uint32_t next;
next = steps[current_step++];
if (current_step >= sizeof(steps) / sizeof(steps[0]))
{
// Reset the state machien
current_step = 0;
// Wait 2s before starting the state machine again
next = 2000;
}
return next;
}
/*
* \brief Button handler that will trigger a led toggle one time
*/
static void button_handler(uint8_t button_id, button_event_e event)
{
(void) button_id;
(void) event;
}
/*
* \brief Button handler that will trigger the start stop of the state machine
*/
static void button_handler_state_machine(uint8_t button_id, button_event_e event)
{
static bool started = false;
(void) button_id;
(void) event;
if (!started)
{
res = App_Scheduler_addTask_execTime(state_machine_task,
10);
started = (res == APP_SCHEDULER_RES_OK);
}
else
{
res = App_Scheduler_cancelTask(state_machine_task);
started = !(res == APP_SCHEDULER_RES_OK);
}
}
void App_init(const app_global_functions_t * functions)
{
// Basic configuration of the node with a unique node address
{
// Could not configure the node
// It should not happen except if one of the config value is invalid
return;
}
// Initialize peripherals
// Launch two periodic task with different period
// Register to button 0 event that will triger a single shot task
// Will work only if target board support buttons
// Register to button 1 event that will start/stop a state machine
// Will work only if target board support buttons
Button_register_for_event(1, BUTTON_PRESSED, button_handler_state_machine);
// Start the stack
lib_state->startStack();
LOG(LVL_INFO, "Scheduler example app started");
}
APP_SCHEDULER_RES_OK
@ APP_SCHEDULER_RES_OK
Definition: app_scheduler.h:50
LVL_INFO
#define LVL_INFO
Definition: debug_log.h:83
APP_SCHEDULER_STOP_TASK
#define APP_SCHEDULER_STOP_TASK
Value to return from task to remove it.
Definition: app_scheduler.h:37
button.h
Board-independent button functions.
node_configuration.h
App_Scheduler_addTask_execTime
app_scheduler_res_e App_Scheduler_addTask_execTime(task_cb_f cb, uint32_t delay_ms, uint32_t exec_time_us)
Add a task.
Led_toggle
led_res_e Led_toggle(uint8_t led_id)
Toggle the given LED.
app_scheduler_res_e
app_scheduler_res_e
List of return code.
Definition: app_scheduler.h:47
APP_RES_OK
@ APP_RES_OK
Definition: wms_app.h:204
app_scheduler.h
app_global_functions_t
List of global functions, passed to App_entrypoint()
Definition: wms_app.h:157
debug_log.h
Button_register_for_event
button_res_e Button_register_for_event(uint8_t button_id, button_event_e event, on_button_event_cb cb)
Register for a button event.
led.h
Board-independent LED functions.
configureNodeFromBuildParameters
__STATIC_INLINE app_res_e configureNodeFromBuildParameters()
Wrapper on top of configureNode to get parameters from build system and hardcoded values from chip (f...
Definition: node_configuration.h:233
App_Scheduler_cancelTask
app_scheduler_res_e App_Scheduler_cancelTask(task_cb_f cb)
Cancel a task.
Led_init
void Led_init(void)
Initialize Led module.
LOG_INIT
#define LOG_INIT()
Definition: debug_log.h:66
LOG
#define LOG(level, fmt,...)
Print a log message if its severity is lower or equal to DEBUG_LOG_MAX_LEVEL.
Definition: debug_log.h:172
Button_init
void Button_init(void)
Initialize Button module.
BUTTON_PRESSED
@ BUTTON_PRESSED
Definition: button.h:18
APP_SCHEDULER_SCHEDULE_ASAP
#define APP_SCHEDULER_SCHEDULE_ASAP
Value to return from task or as initial time to be executed ASAP.
Definition: app_scheduler.h:42
api.h
button_event_e
button_event_e
Different events for a button.
Definition: button.h:17