Skip to content

[RFC] Use raw queue elements #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions Arduino_Threads.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,25 @@ class Shared // template definition
{
public:
Shared() {
queue = new rtos::Queue<Shared<T>, 16>;
}
operator T() const {
osEvent evt = queue->get();
operator T() {
osEvent evt = queue.get();
if (evt.status == osEventMessage) {
Shared<T> *x = (Shared<T>*)evt.value.p;
return x->val;
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() to discard oldest element and free its memory
T discard = *this;
}
val = other;
queue->put(this);
T* obj = new T(val);
queue.put(obj);
return (*obj);
}
T& peek() {
return val;
Expand All @@ -24,7 +31,7 @@ class Shared // template definition
}
private:
T val;
rtos::Queue<Shared<T>, 16> *queue;
rtos::Queue<T, 16> queue;
};

#define CONCAT2(x,y) x##y
Expand Down
13 changes: 13 additions & 0 deletions examples/Enqueue_test/Enqueue.inot
Original file line number Diff line number Diff line change
@@ -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;
}
15 changes: 15 additions & 0 deletions examples/Enqueue_test/Enqueue_test.ino
Original file line number Diff line number Diff line change
@@ -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);
}
}
1 change: 1 addition & 0 deletions examples/Enqueue_test/SharedVariables.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Shared<int> counter;