TaskManagerIO
Loading...
Searching...
No Matches
TaskManagerIO.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2018 https://www.thecoderscorner.com (Dave Cherry)..
3 * This product is licensed under an Apache license, see the LICENSE file in the top-level directory.
4 */
5
6#ifndef TASKMANAGER_IO_H
7#define TASKMANAGER_IO_H
8
9#include "TaskPlatformDeps.h"
10#include "TaskTypes.h"
11#include "TaskBlock.h"
12
13#ifdef PARTICLE
14enum InterruptMode;
15#endif // PARTICLE
16
24
25#if defined(IOA_USE_MBED) || defined(BUILD_FOR_PICO_CMAKE)
26#include <cstdint>
28void yield();
30uint32_t millis();
32uint32_t micros();
33#ifdef IOA_USE_MBED
34#define delayMicroseconds(x) wait_us(x)
35#else
36#define delayMicroseconds(x) sleep_us(x)
37#endif // DELAY Microseconds code
38#endif // IOA_USE_MBED
39
43typedef void (*RawIntHandler)();
44
50typedef void (*InterruptFn)(pintype_t pin);
51
60public:
68 virtual void attachInterrupt(pintype_t pin, RawIntHandler fn, uint8_t mode) = 0;
69};
70
72
78class TimePeriod {
79private:
80 uint32_t amount: 24;
81 uint32_t unit: 7;
82 uint32_t repeating: 1;
83public:
84 TimePeriod();
85 TimePeriod(uint32_t amount, TimerUnit unit, bool repeat);
86
87 TimePeriod(const TimePeriod& other) =default;
88 TimePeriod& operator= (const TimePeriod& other) =default;
89
90 uint32_t getAmount() const {
91 return amount;
92 }
93
94 TimerUnit getUnit() const {
95 return (TimerUnit)unit;
96 }
97
98 bool getRepeating() const {
99 return repeating != 0;
100 }
101};
102
108inline TimePeriod repeatSeconds(uint32_t seconds) { return {seconds, TIME_SECONDS, true}; }
109
115inline TimePeriod repeatMillis(uint32_t millis) { return {millis, TIME_MILLIS, true}; }
116
122inline TimePeriod repeatMicros(uint32_t micros) { return {micros, TIME_MICROS, true}; }
123
129inline TimePeriod onceSeconds(uint32_t seconds) { return {seconds, TIME_SECONDS, false}; }
130
136inline TimePeriod onceMillis(uint32_t millis) { return {millis, TIME_MILLIS, false}; }
137
143inline TimePeriod onceMicros(uint32_t micros) { return {micros, TIME_MICROS, false}; }
144
159protected:
160 // the memory that holds all the tasks is an array of task blocks, allocated on demand
161 TaskBlock* volatile taskBlocks[DEFAULT_TASK_BLOCKS]; // task blocks never ever move in memory, they are not volatile but the pointer is
162 volatile taskid_t numberOfBlocks; // this holds the current number of blocks available.
163
164 // here we have a linked list of tasks, this linked list is in time order, nearest task first.
165 tm_internal::TimerTaskAtomicPtr first;
166
167 // interrupt handling variables, store the interrupt state and probable pin cause if applicable
168 volatile pintype_t lastInterruptTrigger;
169 volatile bool interrupted;
170 volatile InterruptFn interruptCallback;
171
172 tm_internal::TmAtomicBool memLockerFlag; // memory and list operations are locked by this flag using the TmSpinLocker
173 tm_internal::TimerTaskAtomicPtr runningTask;
174public:
179 TaskManager();
180 ~TaskManager();
181
193 inline taskid_t execute(TimerFn workToDo) {
194 return scheduleOnce(2, workToDo, TIME_MICROS);
195 }
196
209 inline taskid_t execute(Executable* execToDo, bool deleteWhenDone = false) {
210 return scheduleOnce(2, execToDo, TIME_MICROS, deleteWhenDone);
211 }
212
221 taskid_t schedule(const TimePeriod& when, TimerFn timerFunction);
222
231 taskid_t schedule(const TimePeriod& when, Executable* execRef, bool deleteWhenDone = false);
232
233
240 taskid_t scheduleOnce(uint32_t when, TimerFn timerFunction, TimerUnit timeUnit = TIME_MILLIS);
241
250 taskid_t scheduleOnce(uint32_t when, Executable* execRef, TimerUnit timeUnit = TIME_MILLIS, bool deleteWhenDone = false);
251
258 taskid_t scheduleFixedRate(uint32_t when, TimerFn timerFunction, TimerUnit timeUnit = TIME_MILLIS);
259
268 taskid_t scheduleFixedRate(uint32_t when, Executable* execRef, TimerUnit timeUnit = TIME_MILLIS, bool deleteWhenDone = false);
269
276 taskid_t registerEvent(BaseEvent* eventToAdd, bool deleteWhenDone = false);
277
282 ISR_ATTR void triggerEvents() {
283 lastInterruptTrigger = 0xff; // 0xff is the shorthand for event trigger basically.
284 interrupted = true;
285 }
286
294 void addInterrupt(InterruptAbstraction* interruptAbstraction, pintype_t pin, uint8_t mode);
295
302 void setInterruptCallback(InterruptFn handler);
303
308 void cancelTask(taskid_t task);
309
317 void setTaskEnabled(taskid_t task, bool ena);
318
324 virtual void yieldForMicros(uint32_t micros);
325
330 void runLoop();
331
335 static void markInterrupted(pintype_t interruptNo);
336
340 void reset() {
341 // all the slots should be cleared
342 for(taskid_t i =0; i<numberOfBlocks; i++) {
343 taskBlocks[i]->clearAll();
344 }
345 // the queue must be completely cleared too.
346 tm_internal::atomicWritePtr(&first, nullptr);
347 }
348
359 char* checkAvailableSlots(char* slotData, size_t slotDataSize) const;
360
365 return tm_internal::atomicReadPtr(&first);
366 }
367
374
380 uint32_t microsToNextTask() {
381 auto maybeTask = tm_internal::atomicReadPtr(&first);
382 if(maybeTask == nullptr) return 600 * 1000000U; // wait for 10 minutes if there's nothing to do
383 else return maybeTask->microsFromNow();
384 }
385
392 TimerTask* getRunningTask() { return runningTask; }
393
394 friend class TaskExecutionRecorder;
395private:
401 taskid_t findFreeTask();
402
410 void removeFromQueue(TimerTask* task);
411
416 void putItemIntoQueue(TimerTask* tm);
417
421 void dealWithInterrupt();
422};
423
425extern TaskManager taskManager;
426
430#define millisToMicros(x) ((x)*1000UL)
431
435#define secondsToMicros(x) ((x)*1000000UL)
436
440#define secondsToMillis(x) ((x)*1000UL)
441
442#endif //TASKMANAGER_IO_H
An internal class definition that is the representation of a task.
void(* InterruptFn)(pintype_t pin)
Definition TaskManagerIO.h:50
uint32_t micros()
TimePeriod onceSeconds(uint32_t seconds)
Definition TaskManagerIO.h:129
TimePeriod onceMicros(uint32_t micros)
Definition TaskManagerIO.h:143
TimePeriod repeatMicros(uint32_t micros)
Definition TaskManagerIO.h:122
void yield()
Definition TaskManagerIO.cpp:21
void(* RawIntHandler)()
Definition TaskManagerIO.h:43
TimePeriod repeatSeconds(uint32_t seconds)
Definition TaskManagerIO.h:108
uint32_t millis()
TimePeriod repeatMillis(uint32_t millis)
Definition TaskManagerIO.h:115
TimePeriod onceMillis(uint32_t millis)
Definition TaskManagerIO.h:136
provides the platform specific configuration for task manager
TimerTask * atomicReadPtr(TimerTaskAtomicPtr *pPtr)
Definition TaskPlatformDeps.h:106
void atomicWritePtr(TimerTaskAtomicPtr *pPtr, TimerTask *volatile newValue)
Definition TaskPlatformDeps.h:116
This class represents the core tasks that are added to task manager, and the TimerTask object itself.
std::function< void()> TimerFn
Definition TaskTypes.h:120
TimerUnit
Definition TaskTypes.h:128
@ TIME_SECONDS
Definition TaskTypes.h:132
@ TIME_MILLIS
Definition TaskTypes.h:134
@ TIME_MICROS
Definition TaskTypes.h:130
unsigned int taskid_t
Definition TaskTypes.h:22
Definition TaskTypes.h:57
Definition TaskTypes.h:28
Definition TaskManagerIO.h:59
virtual void attachInterrupt(pintype_t pin, RawIntHandler fn, uint8_t mode)=0
Definition TaskBlock.h:26
Definition TaskManagerIO.cpp:213
Definition TaskManagerIO.h:158
void addInterrupt(InterruptAbstraction *interruptAbstraction, pintype_t pin, uint8_t mode)
Definition TaskManagerIO.cpp:423
void reset()
Definition TaskManagerIO.h:340
char * checkAvailableSlots(char *slotData, size_t slotDataSize) const
Definition TaskManagerIO.cpp:451
uint32_t microsToNextTask()
Definition TaskManagerIO.h:380
void runLoop()
Definition TaskManagerIO.cpp:255
taskid_t execute(TimerFn workToDo)
Definition TaskManagerIO.h:193
taskid_t registerEvent(BaseEvent *eventToAdd, bool deleteWhenDone=false)
Definition TaskManagerIO.cpp:171
static void markInterrupted(pintype_t interruptNo)
Definition TaskManagerIO.cpp:65
taskid_t scheduleOnce(uint32_t when, TimerFn timerFunction, TimerUnit timeUnit=TIME_MILLIS)
Definition TaskManagerIO.cpp:131
void cancelTask(taskid_t task)
Definition TaskManagerIO.cpp:190
TaskManager()
Definition TaskManagerIO.cpp:70
void setTaskEnabled(taskid_t task, bool ena)
Definition TaskManagerIO.cpp:181
TimerTask * getTask(taskid_t task)
Definition TaskManagerIO.cpp:469
taskid_t scheduleFixedRate(uint32_t when, TimerFn timerFunction, TimerUnit timeUnit=TIME_MILLIS)
Definition TaskManagerIO.cpp:141
TimerTask * getRunningTask()
Definition TaskManagerIO.h:392
taskid_t schedule(const TimePeriod &when, TimerFn timerFunction)
Definition TaskManagerIO.cpp:477
virtual void yieldForMicros(uint32_t micros)
Definition TaskManagerIO.cpp:202
taskid_t execute(Executable *execToDo, bool deleteWhenDone=false)
Definition TaskManagerIO.h:209
ISR_ATTR void triggerEvents()
Definition TaskManagerIO.h:282
void setInterruptCallback(InterruptFn handler)
Definition TaskManagerIO.cpp:447
TimerTask * getFirstTask()
Definition TaskManagerIO.h:364
Definition TaskManagerIO.h:78
Definition TaskTypes.h:163