File tree 4 files changed +43
-7
lines changed
4 files changed +43
-7
lines changed Original file line number Diff line number Diff line change @@ -3,18 +3,25 @@ class Shared // template definition
3
3
{
4
4
public:
5
5
Shared () {
6
- queue = new rtos::Queue<Shared<T>, 16 >;
7
6
}
8
- operator T () const {
9
- osEvent evt = queue-> get ();
7
+ operator T () {
8
+ osEvent evt = queue. get ();
10
9
if (evt.status == osEventMessage) {
11
- Shared<T> *x = (Shared<T>*)evt.value .p ;
12
- return x->val ;
10
+ T x = *((T*)evt.value .p );
11
+ delete (T*)evt.value .p ;
12
+ return x;
13
13
}
14
+ return val;
14
15
}
15
16
T& operator = (const T& other) {
17
+ if (queue.full ()) {
18
+ // invokes operator T() to discard oldest element and free its memory
19
+ T discard = *this ;
20
+ }
16
21
val = other;
17
- queue->put (this );
22
+ T* obj = new T (val);
23
+ queue.put (obj);
24
+ return (*obj);
18
25
}
19
26
T& peek () {
20
27
return val;
@@ -24,7 +31,7 @@ class Shared // template definition
24
31
}
25
32
private:
26
33
T val;
27
- rtos::Queue<Shared<T> , 16 > * queue;
34
+ rtos::Queue<T , 16 > queue;
28
35
};
29
36
30
37
#define CONCAT2 (x,y ) x##y
Original file line number Diff line number Diff line change
1
+ void setup() {
2
+ // put your setup code here, to run once:
3
+
4
+ }
5
+
6
+ int i = 0;
7
+
8
+ void loop() {
9
+ // Continuously pump counter
10
+ delay(100);
11
+ i++;
12
+ counter = i;
13
+ }
Original file line number Diff line number Diff line change
1
+ void setup () {
2
+ // put your setup code here, to run once:
3
+ Serial.begin (115200 );
4
+ while (!Serial) {}
5
+ Enqueue .start ();
6
+ Serial.println (" start" );
7
+ }
8
+
9
+ void loop () {
10
+ // put your main code here, to run repeatedly:
11
+ delay (1000 );
12
+ for (int i = 0 ; i < 10 ; i++) {
13
+ Serial.println (counter);
14
+ }
15
+ }
Original file line number Diff line number Diff line change
1
+ Shared < int > counter ;
You can’t perform that action at this time.
0 commit comments