Simple round-robin scheduler for mbed
Overview
This library provides simple round-robin scheduling based on the mbed-os.
Why would you want to use this?
- Tasks are not run from within interrupt sub-routines (as they are using Tickers)
- Ordering/priority of tasks is deterministic (rate monotonic)
It's not rocket science, just a handy wrapper around the RTOS functions.
Example
#define BASE_RATE 0.1f #define TASK1_MULTIPLIER 1 #define TASK2_MULTIPLIER 2 .... RoundRobin::instance()->SetBaseRate( BASE_RATE ); RoundRobin::instance()->addTask( TASK1_MULTIPLIER, lcdRefresh ); RoundRobin::instance()->addTask( TASK2_MULTIPLIER, watchButtons );
Diff: RoundRobin.hpp
- Revision:
- 0:a8c603b939a7
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/RoundRobin.hpp Sat Jul 29 11:53:34 2017 +0000 @@ -0,0 +1,69 @@ +/* @brief Round Robin scheduler for mbed + + @author John Bailey + + @copyright Copyright 2017 John Bailey + + @section LICENSE + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +#if !defined ROUNDROBIN_HPP +#define ROUNDROBIN_HPP + +#include "rtos.h" +#include <map> + +class RoundRobin +{ + protected: + EventQueue eventQueue; + Thread* thread; + Ticker ticker; + static RoundRobin *p_instance; + + RoundRobin(); + static void EventTrigger( void ); + + class TaskEntry + { + void (*fn)( void ); + unsigned counter; + + public: + TaskEntry( void (*p_fn)( void ) ) : fn(p_fn),counter(0) {} + void tick( void ) { counter++; } + void triggerIfNeeded( unsigned multiplier ) + { + if( multiplier == counter ) + { + fn(); + counter = 0; + } + } + }; + + static std::multimap<unsigned,TaskEntry> p_taskList; + + public: + virtual ~RoundRobin(); + + static RoundRobin* instance( void ); + + static bool addTask( unsigned p_multiplier, void (*p_fn)(void) ); + + void SetBaseRate( const float p_rate ); +}; + +#endif