From 8e4739f307da00d07b861866931e65c1f8d8332d Mon Sep 17 00:00:00 2001 From: Martino Facchin Date: Mon, 21 Sep 2020 12:41:15 +0200 Subject: [PATCH 1/4] Properly handle the queue (keep history) --- Arduino_Threads.h | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/Arduino_Threads.h b/Arduino_Threads.h index 9c2f230..69dc7c1 100644 --- a/Arduino_Threads.h +++ b/Arduino_Threads.h @@ -3,18 +3,20 @@ class Shared // template definition { public: Shared() { - queue = new rtos::Queue, 16>; + queue = new rtos::Queue; } operator T() const { osEvent evt = queue->get(); if (evt.status == osEventMessage) { - Shared *x = (Shared*)evt.value.p; - return x->val; + T x = *((T*)evt.value.p); + delete (T*)evt.value.p; + return x; } } T& operator= (const T& other) { val = other; - queue->put(this); + T* obj = new T(val); + queue->put(obj); } T& peek() { return val; @@ -24,7 +26,7 @@ class Shared // template definition } private: T val; - rtos::Queue, 16> *queue; + rtos::Queue* queue; }; #define CONCAT2(x,y) x##y From f5235ce2127383329ebf428268addcbee575c026 Mon Sep 17 00:00:00 2001 From: Martino Facchin Date: Mon, 21 Sep 2020 13:11:29 +0200 Subject: [PATCH 2/4] Demonstrate queue proper functionality --- examples/Enqueue_test/Enqueue.inot | 13 +++++++++++++ examples/Enqueue_test/Enqueue_test.ino | 15 +++++++++++++++ examples/Enqueue_test/SharedVariables.h | 1 + 3 files changed, 29 insertions(+) create mode 100644 examples/Enqueue_test/Enqueue.inot create mode 100644 examples/Enqueue_test/Enqueue_test.ino create mode 100644 examples/Enqueue_test/SharedVariables.h diff --git a/examples/Enqueue_test/Enqueue.inot b/examples/Enqueue_test/Enqueue.inot new file mode 100644 index 0000000..f7cfaf4 --- /dev/null +++ b/examples/Enqueue_test/Enqueue.inot @@ -0,0 +1,13 @@ +void setup() { + // put your setup code here, to run once: + +} + +int i = 0; + +void loop() { + // Continuously pump counter + delay(100); + i++; + counter = i; +} diff --git a/examples/Enqueue_test/Enqueue_test.ino b/examples/Enqueue_test/Enqueue_test.ino new file mode 100644 index 0000000..c0f3b1d --- /dev/null +++ b/examples/Enqueue_test/Enqueue_test.ino @@ -0,0 +1,15 @@ +void setup() { + // put your setup code here, to run once: + Serial.begin(115200); + while (!Serial) {} + Enqueue.start(); + Serial.println("start"); +} + +void loop() { + // put your main code here, to run repeatedly: + delay(1000); + for (int i = 0; i < 10; i++) { + Serial.println(counter); + } +} diff --git a/examples/Enqueue_test/SharedVariables.h b/examples/Enqueue_test/SharedVariables.h new file mode 100644 index 0000000..51ea60e --- /dev/null +++ b/examples/Enqueue_test/SharedVariables.h @@ -0,0 +1 @@ +Shared counter; From 8696075ffa2711d7bc13e42992452f99444481b5 Mon Sep 17 00:00:00 2001 From: Martino Facchin Date: Mon, 21 Sep 2020 13:14:03 +0200 Subject: [PATCH 3/4] PROPOSAL: discard oldest value if the queue is full Also fixes @aentinger comment about return types --- Arduino_Threads.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Arduino_Threads.h b/Arduino_Threads.h index 69dc7c1..304d9c9 100644 --- a/Arduino_Threads.h +++ b/Arduino_Threads.h @@ -14,9 +14,14 @@ class Shared // template definition } } T& operator= (const T& other) { + if (queue->full()) { + // invokes operator T() + T discard = *this; + } val = other; T* obj = new T(val); queue->put(obj); + return (*obj); } T& peek() { return val; From 5bd66e7316431f62d4484e3ba4c249c967b78278 Mon Sep 17 00:00:00 2001 From: Martino Facchin Date: Mon, 21 Sep 2020 17:31:15 +0200 Subject: [PATCH 4/4] Remove dynamic memory allocation --- Arduino_Threads.h | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/Arduino_Threads.h b/Arduino_Threads.h index 304d9c9..38c8634 100644 --- a/Arduino_Threads.h +++ b/Arduino_Threads.h @@ -3,24 +3,24 @@ class Shared // template definition { public: Shared() { - queue = new rtos::Queue; } - operator T() const { - osEvent evt = queue->get(); + operator T() { + osEvent evt = queue.get(); if (evt.status == osEventMessage) { T x = *((T*)evt.value.p); delete (T*)evt.value.p; return x; } + return val; } T& operator= (const T& other) { - if (queue->full()) { - // invokes operator T() + if (queue.full()) { + // invokes operator T() to discard oldest element and free its memory T discard = *this; } val = other; T* obj = new T(val); - queue->put(obj); + queue.put(obj); return (*obj); } T& peek() { @@ -31,7 +31,7 @@ class Shared // template definition } private: T val; - rtos::Queue* queue; + rtos::Queue queue; }; #define CONCAT2(x,y) x##y