@@ -38,36 +38,71 @@ class Shared // template definition
38
38
39
39
class ArduinoThreads {
40
40
private:
41
- rtos::EventFlags *GlobalEvents;
42
- rtos::EventFlags ThreadEvents;
41
+ static rtos::EventFlags globalEvents;
42
+ uint32_t startFlags;
43
+ uint32_t stopFlags;
44
+ uint32_t loopDelay;
43
45
virtual void setup (void ) {};
44
46
virtual void loop (void ) {};
45
- void execute () {
47
+ void execute () {
46
48
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;
49
+ // if startFlags have been passed then wait until all the flags are set
50
+ // before starting the loop. this is used to synchronize loops from multiple
51
+ // sketches.
52
+ globalEvents.wait_all (startFlags);
53
+
54
+ // if stopFlags have been passed stop when all the flags are set
55
+ // otherwise loop forever
56
+ while ( 1 ) {
57
+ loop ();
58
+ // on exit clear the flags that have forced us to stop.
59
+ // note that if two groups of sketches stop on common flags
60
+ // the first group will clear them so the second group may never
61
+ // exit
62
+ if (stopFlags!=0 ) {
63
+ if ((globalEvents.get ()&stopFlags)!=stopFlags) {
64
+ globalEvents.clear (stopFlags);
65
+ return ;
66
+ }
67
+ if ((rtos::ThisThread::flags_get ()&stopFlags)!=stopFlags) {
68
+ rtos::ThisThread::flags_clear (stopFlags);
69
+ return ;
70
+ }
71
+ }
72
+ // sleep for the time we've been asked to insert between loops
73
+ rtos::ThisThread::sleep_for (loopDelay);
74
+ }
75
+ }
76
+ rtos::Thread *t;
54
77
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));
78
+ // start this sketch
79
+ void start ( int stacksize = 4096 , uint32_t startFlags= 0 , uint32_t stopFlags= 0 ) {
80
+ this -> startFlags = startFlags;
81
+ this -> stopFlags = stopFlags ;
82
+ loopDelay= 0 ;
83
+ t = new rtos::Thread (osPriorityNormal, stacksize, nullptr , _macroToString (tabname));
84
+ t-> start (mbed::callback (this , &ArduinoThreads::execute));
62
85
}
86
+ // kill this sketch
63
87
void terminate () {
64
- t. terminate ();
88
+ t-> terminate ();
65
89
}
66
- void stop () {
67
- ThreadEvents.set (LoopStop);
90
+ // send an event to all sketches at the same time
91
+ static void broadcastEvent (uint32_t event) {
92
+ globalEvents.set (event);
93
+ }
94
+ // send an event only to this sketch
95
+ void sendEvent (uint32_t event) {
96
+ t->flags_set (event);
97
+ }
98
+ // set the rate at which loop function will be called
99
+ void setLoopDelay (uint32_t delay) {
100
+ loopDelay = delay;
68
101
}
69
102
};
70
103
104
+ rtos::EventFlags ArduinoThreads::globalEvents;
105
+
71
106
#define THD_ENTER (tabname ) class CONCAT (tabname, Class) : public ArduinoThreads { \
72
107
private:
73
108
0 commit comments