Skip to content

Commit d7c9b97

Browse files
committed
added subclassing and methods for thread synchronization
1 parent 3ce2fba commit d7c9b97

File tree

1 file changed

+34
-65
lines changed

1 file changed

+34
-65
lines changed

Diff for: Arduino_Threads.h

+34-65
Original file line numberDiff line numberDiff line change
@@ -33,78 +33,47 @@ class Shared // template definition
3333
#define INCF(F) INCF_(F)
3434
#define INCF_(F) #F
3535

36-
class ArduinoThreadClass {
37-
public:
38-
void add(rtos::Thread* me) {
39-
if (controller == NULL) {
40-
controller = new rtos::Thread(osPriorityHigh, 512);
41-
controller->start(mbed::callback(this, &ArduinoThreadClass::loop));
42-
}
43-
list[idx++] = me;
44-
}
45-
46-
void ping(rtos::Thread* me) {
47-
for (int i=0; i < idx; i++) {
48-
if (me == list[i]) {
49-
timestamps[i] = millis();
50-
}
51-
}
52-
}
36+
#define _macroToString(sequence) #sequence
5337

54-
private:
5538

56-
void loop() {
57-
while (1) {
58-
for (int i = 0; i < idx; i++) {
59-
if (millis() - timestamps[i] > 5000) {
60-
// list[i]->terminate();
61-
// TODO: reorder threads and make list[i] == NULL
62-
// Serial.println(list[i]->get_name() + String(" killed "));
63-
}
64-
}
65-
delay(1000);
39+
class ArduinoThreads {
40+
private:
41+
rtos::EventFlags *GlobalEvents;
42+
rtos::EventFlags ThreadEvents;
43+
virtual void setup(void) {};
44+
virtual void loop(void) {};
45+
void execute() {
46+
setup();
47+
if (GlobalEvents!=NULL) GlobalEvents->wait_all(LoopStart);
48+
while ( (GlobalEvents==NULL || !(GlobalEvents->get()&LoopStop) ) &&
49+
!(ThreadEvents.get()&LoopStop) ) {
50+
loop();
51+
}
52+
}
53+
rtos::Thread t;
54+
public:
55+
typedef enum {
56+
LoopStart= (1<<0),
57+
LoopStop= (1<<1)
58+
} Events;
59+
void start(rtos::EventFlags *event = NULL) {
60+
GlobalEvents = event;
61+
t.start(mbed::callback(this, &ArduinoThreads::execute));
62+
}
63+
void terminate() {
64+
t.terminate();
65+
}
66+
void stop() {
67+
ThreadEvents.set(LoopStop);
6668
}
67-
}
68-
69-
rtos::Thread* controller = NULL;
70-
rtos::Thread* list[10] = {NULL};
71-
uint32_t timestamps[10] = {0};
72-
int idx = 0;
7369
};
7470

75-
ArduinoThreadClass ArduinoThread;
76-
77-
#define _macroToString(sequence) #sequence
78-
79-
#define THD_ENTER(tabname) class CONCAT(tabname, Class) { \
71+
#define THD_ENTER(tabname) class CONCAT(tabname, Class) : public ArduinoThreads { \
72+
private:
8073

81-
#define THD_DONE(tabname) private: \
82-
void execute() { \
83-
ArduinoThread.add(t); \
84-
setup(); \
85-
while (1) { \
86-
loop(); \
87-
ArduinoThread.ping(t); \
88-
} \
89-
} \
90-
rtos::Thread* t; \
91-
\
92-
public: \
93-
void start(int stacksize = 4096) { \
94-
t = new rtos::Thread(osPriorityNormal, stacksize, nullptr, _macroToString(tabname)); \
95-
t->start(mbed::callback(this, &CONCAT(tabname,Class)::execute)); \
96-
} \
97-
void begin() { \
98-
start(); \
99-
} \
100-
}; \
74+
#define THD_DONE(tabname) \
75+
}; \
10176
CONCAT(tabname,Class) tabname;
10277

103-
/*
104-
#define NEWTHREAD(tabname) THD_ENTER(tabname) \
105-
\#include CONCAT(tabname, .h) \
106-
THD_DONE(tabname)
107-
*/
108-
10978
#include "Wire.h"
11079
#include "SerialDispatcher.h"

0 commit comments

Comments
 (0)