TinyRobotics
Loading...
Searching...
No Matches
Scheduler.h
1#pragma once
2#include <cstdint>
3#ifdef ARDUINO
4#include "Arduino.h"
5#else
6#include <chrono>
7#endif
8
9namespace tinyrobotics {
10
11/**
12 * @class Scheduler
13 * @ingroup control
14 * @brief Simple periodic task scheduler for embedded and Arduino environments.
15 *
16 * This class provides a lightweight, non-blocking scheduler for executing a
17 * callback function at a fixed interval (in milliseconds). It is designed for
18 * use in embedded systems and Arduino sketches, where you want to perform
19 * periodic tasks (such as sensor readings, status updates, or control actions)
20 * without blocking the main loop.
21 *
22 * ## Features
23 * - Works with Arduino (uses millis()) and desktop (uses std::chrono)
24 * - Non-blocking: call run() regularly to trigger the callback at the right
25 * time
26 * - Easy to start, stop, and change the callback
27 * - Suitable for cooperative multitasking and periodic polling
28 *
29 * ## Usage Example
30 * @code
31 * void myTask(void*) {}
32 * Scheduler sched;
33 * sched.begin(1000, myTask); // Call myTask every 1000 ms
34 * // In your main loop:
35 * while (true) {
36 * sched.run();
37 * // ... other code ...
38 * }
39 * @endcode
40 *
41 * ## Methods
42 * - begin(repeatMs, callback): Start the scheduler with interval and callback
43 * - end(): Stop the scheduler
44 * - setCallback(callback): Change the callback function
45 * - run(): Check and execute the callback if interval elapsed
46 *
47 * ## Applications
48 * - Periodic sensor polling
49 * - Status LED blinking
50 * - Timed control actions
51 * - Cooperative multitasking in embedded systems
52 *
53 * @author Phil Schatzmann
54
55 */
56
57class Scheduler {
58 public:
59 Scheduler() = default;
60
61 void begin(uint16_t repeatMs, void (*cb)(void*), void* ref = nullptr) {
62 repeatMs_ = repeatMs;
63 start_time = millis() + repeatMs;
64 reference = ref;
65 setCallback(cb);
66 is_active_ = repeatMs > 0 && cb != nullptr;
67 }
68
69 void end() { is_active_ = false; }
70
71 void setCallback(void (*cb)(void*)) {
72 // Store the callback function pointer for later use
73 callback = cb;
74 }
75
76 void run() {
77 if (is_active_ && callback && millis() >= start_time) {
78 // Call the callback function
79 callback(nullptr);
80 start_time += repeatMs_; // Schedule the next execution
81 }
82 }
83
84 bool isActive() const { return is_active_; }
85
86#ifndef ARDUINO
87 unsigned long millis() {
88 // Implement a simple millis() function for non-Arduino environments
89 static auto start_time = std::chrono::steady_clock::now();
90 auto now = std::chrono::steady_clock::now();
91 return std::chrono::duration_cast<std::chrono::milliseconds>(now -
92 start_time)
93 .count();
94 }
95#endif
96
97 protected:
98 unsigned long start_time = 0;
99 bool is_active_ = false;
100 void (*callback)(void*) = nullptr;
101 void *reference = nullptr;
102 uint16_t repeatMs_;
103};
104
105} // namespace tinyrobotics
Simple periodic task scheduler for embedded and Arduino environments.
Definition: Scheduler.h:57