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)
{
(void) 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;
}
// 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_e
List of return code.
@ APP_SCHEDULER_RES_OK
#define APP_SCHEDULER_STOP_TASK
Value to return from task to remove it.
app_scheduler_res_e App_Scheduler_addTask_execTime(task_cb_f cb, uint32_t delay_ms, uint32_t exec_time_us)
Add a task.
app_scheduler_res_e App_Scheduler_cancelTask(task_cb_f cb)
Cancel a task.
#define APP_SCHEDULER_SCHEDULE_ASAP
Value to return from task or as initial time to be executed ASAP.
Board-independent button functions.
button_event_e
Different events for a button.
Definition button.h:21
@ BUTTON_PRESSED
Definition button.h:22
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.
#define LOG_INIT()
Definition debug_log.h:66
#define LVL_INFO
Definition debug_log.h:83
#define LOG(level, fmt,...)
Print a log message if its severity is lower or equal to DEBUG_LOG_MAX_LEVEL.
Definition debug_log.h:173
Board-independent LED functions.
led_res_e Led_toggle(uint8_t led_id)
Toggle the given LED.
__STATIC_INLINE app_res_e configureNodeFromBuildParameters()
Wrapper on top of configureNode to get parameters from build system and hardcoded values from chip (f...
@ APP_RES_OK
Definition wms_app.h:204
List of global functions, passed to App_entrypoint()
Definition wms_app.h:158