aboutsummaryrefslogtreecommitdiff
path: root/Mussel
diff options
context:
space:
mode:
authorJonas Smedegaard <dr@jones.dk>2025-04-02 14:49:22 +0200
committerJonas Smedegaard <dr@jones.dk>2025-04-02 14:49:22 +0200
commit84d6461dc76df4061a420735a42a3cf29623e81b (patch)
tree74412d641278ff10fbcc7131959a9f05860e9256 /Mussel
parentc181fac0ae52a94a1951214250720668bfca558e (diff)
add voting functions and example
Diffstat (limited to 'Mussel')
-rw-r--r--Mussel/Mussel.cpp31
-rw-r--r--Mussel/Mussel.h14
-rw-r--r--Mussel/examples/voting/voting.ino23
3 files changed, 68 insertions, 0 deletions
diff --git a/Mussel/Mussel.cpp b/Mussel/Mussel.cpp
index 7cd7cf4..67fd10d 100644
--- a/Mussel/Mussel.cpp
+++ b/Mussel/Mussel.cpp
@@ -109,6 +109,9 @@ String Mussel::desc() {
case 6:
_str = "changes state on ON/OFF command";
break;
+ case 10:
+ _str = "handles voting";
+ break;
default:
_str = "undefined [" + static_cast<String>(_attitude) + "]";
break;
@@ -223,6 +226,34 @@ byte Mussel::read() {
return _byte;
}
+/// Function to push data onto the stack
+bool Mussel::push(String id, unsigned long timestamp, int measure) {
+
+ // Check if stack is full
+ if (top >= STACK_SIZE - 1) {
+ Serial.println("Stack Full");
+
+ // Return false if stack is full
+ return false;
+ }
+ top++;
+ idStack[top] = id;
+ timeStack[top] = timestamp;
+ measureStack[top] = measure;
+
+ // Return true on successful push
+ return true;
+}
+
+/// Function to print stack contents
+void Mussel::printStack() {
+ for (int i = top; i >= 0; i--) {
+ Serial.print("ID: "); Serial.print(idStack[i]);
+ Serial.print(", Time: "); Serial.print(timeStack[i]);
+ Serial.print(", measure: "); Serial.println(measureStack[i]);
+ }
+}
+
/// Dump internal variables, formatted for use with Serial Plotter
///
/// @return internal variables as String
diff --git a/Mussel/Mussel.h b/Mussel/Mussel.h
index 79f5017..1e94897 100644
--- a/Mussel/Mussel.h
+++ b/Mussel/Mussel.h
@@ -22,6 +22,9 @@
// milliseconds
#define MUSSEL_DEBOUNCE_DELAY 50U
+// Limited size NOW!! can be transformed into infinite array
+#define STACK_SIZE 1000
+
class Mussel {
public:
@@ -41,6 +44,9 @@ class Mussel {
String desc();
byte read();
String debug();
+ bool push(String id, unsigned long timestamp, int measure);
+ void printStack();
+
private:
int _attitude;
int _pin;
@@ -50,6 +56,14 @@ class Mussel {
#ifdef DHT_H
DHT mussel_dht;
#endif
+
+ // Array to store ID strings
+ String idStack[STACK_SIZE];
+ unsigned long timeStack[STACK_SIZE];
+ int measureStack[STACK_SIZE];
+
+ // Index of the top element in the stack, -1 means stack is empty
+ int top = -1;
};
#endif
diff --git a/Mussel/examples/voting/voting.ino b/Mussel/examples/voting/voting.ino
new file mode 100644
index 0000000..066180a
--- /dev/null
+++ b/Mussel/examples/voting/voting.ino
@@ -0,0 +1,23 @@
+#include <Mussel.h>
+
+// instantiate for vote handling
+Mussel mussel(10);
+
+void setup() {
+ Serial.begin(115200);
+ mussel.begin();
+
+ Serial.printf("\n\nDevice ready: %s\n",
+ mussel.desc().c_str());
+}
+
+void loop() {
+ // Inject sample data
+ mussel.push("A1", millis(), random(90));
+ mussel.push("B2", millis(), random(90));
+ mussel.push("C3", millis(), random(90));
+ Serial.printf("gap:%d\n",
+ mussel.printStack());
+
+ delay(1000);
+}