Skip to content

Commit

Permalink
Add scheduler examples
Browse files Browse the repository at this point in the history
  • Loading branch information
hubmartin authored and Pavel Hübner committed Nov 18, 2017
1 parent 92780ee commit c3845be
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 0 deletions.
11 changes: 11 additions & 0 deletions _examples/scheduler-advanced/README.md
@@ -0,0 +1,11 @@
# Scheduler advanced example

This example explains how to use scheduler to handle more tasks
The application_task() Task is created automatically by SDK.
If you need more tasks then call the bc_scheduler_register()

This example demonstrates scheduling the task to blink a LED. The right and more
efficient way would be use just the bc_led_set_mode(&led, BC_LED_MODE_BLINK) function
to set the blinking speed and the toggling will be handled in the background.

For more information please see http://sdk.bigclown.com/group__bc__scheduler.html
48 changes: 48 additions & 0 deletions _examples/scheduler-advanced/application.c
@@ -0,0 +1,48 @@
/*
This example explains how to use scheduler to handle more tasks
*/

#include <application.h>

// LED instance
bc_led_t led;

// To control the tasks we need to save task IDs
bc_scheduler_task_id_t id_task_turn_on;
bc_scheduler_task_id_t id_task_turn_off;

void task_turn_on(void *param)
{
// Convert param from void* type to bc_led_t
bc_led_t *param_led = (bc_led_t*)param;
bc_led_set_mode(param_led, BC_LED_MODE_ON);
// Schedule the start of the LED turn off task in 500 ms
bc_scheduler_plan_relative(id_task_turn_off, 500);
}

void task_turn_off(void *param)
{
bc_led_t *param_led = (bc_led_t*)param;
bc_led_set_mode(param_led, BC_LED_MODE_OFF);
}

// Setup the LED and register two tasks
void application_init(void)
{
bc_led_init(&led, BC_GPIO_LED, false, false);

// Register two tasks
// We pass the &led pointer as a parameter to the task functions
// Also register function returns the task ID which we use later.
// BC_TICK_INFINITY means that the task is not run automatically
id_task_turn_on = bc_scheduler_register(task_turn_on, &led, BC_TICK_INFINITY);
id_task_turn_off = bc_scheduler_register(task_turn_off, &led, BC_TICK_INFINITY);
}

void application_task(void)
{
// Start the task to turn LED on
bc_scheduler_plan_now(id_task_turn_on);
// Schedule calling this function again in 1000 ms
bc_scheduler_plan_current_relative(1000);
}
6 changes: 6 additions & 0 deletions _examples/scheduler-advanced/application.h
@@ -0,0 +1,6 @@
#ifndef _APPLICATION_H
#define _APPLICATION_H

#include <bcl.h>

#endif // _APPLICATION_H
11 changes: 11 additions & 0 deletions _examples/scheduler-basics/README.md
@@ -0,0 +1,11 @@
# Scheduler basic example

This example explains how the sheduler plans the tasks.
The application_task() Task is created automatically by SDK.
If you need more tasks then call the bc_scheduler_register()

This example demonstrates scheduling the task to blink a LED. The right and more
efficient way would be use just the bc_led_set_mode(&led, BC_LED_MODE_BLINK) function
to set the blinking speed and the toggling will be handled in the background.

For more information please see http://sdk.bigclown.com/group__bc__scheduler.html
35 changes: 35 additions & 0 deletions _examples/scheduler-basics/application.c
@@ -0,0 +1,35 @@
/*
This example explains how the sheduler plans the tasks.
The application_task() Task is created automatically by SDK.
If you need more tasks then call the bc_scheduler_register()
This example demonstrates scheduling the task to blink a LED. The right and more
efficient way would be use just the bc_led_set_mode(&led, BC_LED_MODE_BLINK) function
to set the blinking speed and the toggling will be handled in the background.
For more information please see http://sdk.bigclown.com/group__bc__scheduler.html
*/

#include <application.h>

// LED instance
bc_led_t led;

// Setup the LED
void application_init(void)
{
bc_led_init(&led, BC_GPIO_LED, false, false);
}

// application_task() is like the Arduino loop() but more power efficient.
// At the end of the function you have to specify when
// the function will be called next. Otherwise it won't be called again.
void application_task(void)
{
bc_led_set_mode(&led, BC_LED_MODE_TOGGLE);

// Shedule this function to be called 500 ms later
// during the 500 ms the MCU will sleep and conserve power
bc_scheduler_plan_current_relative(500);
}
6 changes: 6 additions & 0 deletions _examples/scheduler-basics/application.h
@@ -0,0 +1,6 @@
#ifndef _APPLICATION_H
#define _APPLICATION_H

#include <bcl.h>

#endif // _APPLICATION_H

0 comments on commit c3845be

Please sign in to comment.