-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathArduino_Threads.h
144 lines (129 loc) · 3.99 KB
/
Arduino_Threads.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include <MemoryPool.h>
template<class T>
class Shared // template definition
{
public:
Shared() {
}
operator T() {
osEvent evt = queue.get();
if (evt.status == osEventMessage) {
/* Obtain the oldest inserted element from the queue. */
T * val_ptr = reinterpret_cast<T *>(evt.value.p);
/* Copy the content of T stored in the memory pool since we'll have to free the memory pool afterwards. */
T const tmp_val = *val_ptr;
/* Free the allocated memory in the memory pool. */
memory_pool.free(val_ptr);
/* Return obtained value from queue. */
return tmp_val;
}
return val;
}
T& operator= (const T& other) {
if (queue.full()) {
// invokes operator T() to discard oldest element and free its memory
T discard = *this;
}
val = other;
/* Allocate memory in the memory pool. */
T * val_ptr = memory_pool.alloc();
/* Copy the content of 'other' into the freshly allocated message. */
*val_ptr = other;
/* Insert into queue. */
queue.put(val_ptr);
return (*val_ptr);
}
T& peek() {
return val;
}
T& latest() {
return peek();
}
private:
static size_t constexpr QUEUE_SIZE = 16;
T val;
rtos::MemoryPool<T, QUEUE_SIZE> memory_pool;
rtos::Queue<T, QUEUE_SIZE> queue;
};
#define CONCAT2(x,y) x##y
#define CONCAT(x,y) CONCAT2(x,y)
#define INCF(F) INCF_(F)
#define INCF_(F) #F
#define _macroToString(sequence) #sequence
class ArduinoThreads {
private:
static rtos::EventFlags globalEvents;
uint32_t startFlags;
uint32_t stopFlags;
uint32_t loopDelay;
virtual void setup(void) {};
virtual void loop(void) {};
void execute() {
setup();
// if startFlags have been passed then wait until all the flags are set
// before starting the loop. this is used to synchronize loops from multiple
// sketches.
if (startFlags != 0) {
globalEvents.wait_all(startFlags);
}
// if stopFlags have been passed stop when all the flags are set
// otherwise loop forever
while ( 1 ) {
loop();
// on exit clear the flags that have forced us to stop.
// note that if two groups of sketches stop on common flags
// the first group will clear them so the second group may never
// exit
if (stopFlags!=0) {
if ((globalEvents.get()&stopFlags)!=stopFlags) {
globalEvents.clear(stopFlags);
return;
}
if ((rtos::ThisThread::flags_get()&stopFlags)!=stopFlags) {
rtos::ThisThread::flags_clear(stopFlags);
return;
}
}
// sleep for the time we've been asked to insert between loops
rtos::ThisThread::sleep_for(loopDelay);
}
}
rtos::Thread *t;
protected:
char* _tabname;
public:
// start this sketch
void start(int stacksize = 4096, uint32_t startFlags=0, uint32_t stopFlags=0) {
this->startFlags = startFlags;
this->stopFlags = stopFlags;
loopDelay=0;
t = new rtos::Thread(osPriorityNormal, stacksize, nullptr, _tabname);
t->start(mbed::callback(this, &ArduinoThreads::execute));
}
// kill this sketch
void terminate() {
t->terminate();
}
// send an event to all sketches at the same time
static void broadcastEvent(uint32_t event) {
globalEvents.set(event);
}
// send an event only to this sketch
void sendEvent(uint32_t event) {
t->flags_set(event);
}
// set the rate at which loop function will be called
void setLoopDelay(uint32_t delay) {
loopDelay = delay;
}
};
rtos::EventFlags ArduinoThreads::globalEvents;
#define THD_ENTER(tabname) class CONCAT(tabname, Class) : public ArduinoThreads { \
public: \
CONCAT(tabname, Class)() { _tabname = _macroToString(tabname); } \
private: \
#define THD_DONE(tabname) \
}; \
CONCAT(tabname,Class) tabname;
#include "Wire.h"
#include "SerialDispatcher.h"