Skip to content

Instantly share code, notes, and snippets.

@rlogiacco
Created December 12, 2018 11:25
Show Gist options
  • Save rlogiacco/397e6a81cfd96497fa2957a2b0af309f to your computer and use it in GitHub Desktop.
Save rlogiacco/397e6a81cfd96497fa2957a2b0af309f to your computer and use it in GitHub Desktop.
Testing new CircularBuffer impl
#include "CircularBuffer.h"
#define SAMPLE_PIN A0
namespace Data {
typedef struct {
int value;
unsigned long time;
} Struct;
void print(Struct r) {
Serial.print(r.time);
Serial.print(" ");
Serial.print(r.value);
}
class Object {
public:
Object(int value, unsigned long time) {
_value = value;
_time = time;
}
~Object() {};
void print(Print* out) {
out->print(_time);
out->print(" ");
out->print(_value);
}
int value() {
return _value;
}
unsigned long time() {
return _time;
}
private:
int _value;
unsigned long _time;
};
}
void setup() {
Serial.begin(9600);
pinMode(SAMPLE_PIN, INPUT);
}
//using T = int;
//using T = Data::Struct;
using T = Data::Object;
//using T = Data::Object*;
T prev = T{0,0}; // struct and obj
CircularBuffer<T, 250, byte> buffer;
void loop() {
//T record = analogRead(SAMPLE_PIN); //int
T record = T{analogRead(SAMPLE_PIN), millis()}; // struct and obj
//T record = new Data::Object(analogRead(SAMPLE_PIN), millis()); // ptr
buffer.push(record);
Serial.println(buffer.size());
delay(50);
if (buffer.isFull()) {
if (prev.time() > buffer.first().time()) {
Serial.print("ERROR ");Serial.print(prev.time());Serial.print(":");Serial.println(buffer.first().time());
}
prev = buffer.first();
unsigned long time = buffer.last().time();
Serial.print("DUMP & CHECK "); Serial.println(time);
while (!buffer.isEmpty()) {
T item = buffer.shift();
//Serial.println(item); // int
//Data::print(item); // struct
item.print(&Serial); // obj
if (item.time() > time) {
Serial.println("ERROR");
}
//item->print(&Serial); // ptr
if (buffer.size() > 0) {
Serial.print(", ");
} else {
Serial.println();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment