Skip to content
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

examples with structs #7

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
94 changes: 94 additions & 0 deletions examples/ExamplesWithStruct/Example1/Example1.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
If you want to use this library with structs, this exapmle can be useful.
*/
#include <Arduino.h>
#include <Streaming.h>
#include <Vector.h>

struct DataPackage {
int id;
int value;
};

const long BAUD = 115200;
const int ELEMENT_COUNT_MAX = 10;
typedef Vector<DataPackage> DataPackageVector;
const size_t DELAY = 2500;


DataPackage storage_array[ELEMENT_COUNT_MAX];
DataPackageVector dataPackageVector;

void AddDataPackage(int id, int value) {
DataPackage dataPackage;
dataPackage.id = id;
dataPackage.value = value;
dataPackageVector.push_back(dataPackage);
}

void RemoveDataPackage(int id) {
for (int i = 0; i < dataPackageVector.size(); i++) {
if (dataPackageVector[i].id == id) {
dataPackageVector.remove(i);
break;
}
}
}

void PrintDataPackageVector() {
if (dataPackageVector.empty()) {
Serial << "DataPackageVector is empty" << endl;
return;
}
Serial << "DataPackageVector size: " << dataPackageVector.size() << endl;
size_t counter = 0;
for (DataPackage dataPackage : dataPackageVector) {
Serial << counter << "-> " << "id: " << dataPackage.id << " value: " << dataPackage.value << endl;
counter++;
}
}

// if given id is not found in the data package vector, return false
bool FindDataPackage(int id) {
for (DataPackage dataPackage : dataPackageVector) {
if (dataPackage.id == id) {
dataPackage = dataPackage;
return true;
}
}
return false;
}

void setup() {
Serial.begin(BAUD);
while (!Serial) {
// wait for serial port to connect.
}

dataPackageVector.setStorage(storage_array, 0);
}

void loop() {
// pick a random number as id between 0 and 10
// if it is not already in the vector, add it
// if it is in the vector, remove it
size_t id = random(0, 10);

if (FindDataPackage(id)) {
// remove the data package
RemoveDataPackage(id);
// print removed data package
Serial << "Removed data package with id: " << id << endl;
} else {
// choose a random number between 0 and 100
int value = random(0, 100);
// add the data package
AddDataPackage(id, value);
// print added data package id and value
Serial << "Added data package with id: " << id << " value: " << value << endl;
}
// print the data package vector
PrintDataPackageVector();
Serial << "------------------------------" << endl;
delay(DELAY);
}
99 changes: 99 additions & 0 deletions examples/ExamplesWithStruct/Example2/Example2.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
In any case if you need to allocate structs in the heap, this example can be useful.
*/
#include <Arduino.h>
#include <Streaming.h>
#include <Vector.h>

#define EasyMalloc(type, size) ((type *)malloc(sizeof(type) * size))

struct DataPackage {
int id;
int value;
};

const long BAUD = 115200;
const int ELEMENT_COUNT_MAX = 10;
typedef Vector<DataPackage *> DataPackageVector;
const unsigned int DELAY = 2500;

DataPackageVector dataPackageVector;

void AddDataPackage(int id, int value) {
DataPackage *dataPackage = EasyMalloc(struct DataPackage, 1);
dataPackage->id = id;
dataPackage->value = value;
dataPackageVector.push_back(dataPackage);
}

void RemoveDataPackage(int id) {
for (int i = 0; i < dataPackageVector.size(); i++) {
if (dataPackageVector[i]->id == id) {
free(dataPackageVector[i]);
dataPackageVector.remove(i);
break;
}
}
}

void PrintDataPackageVector() {
if (dataPackageVector.empty()) {
Serial << "DataPackageVector is empty" << endl;
return;
}
Serial << "DataPackageVector size: " << dataPackageVector.size() << endl;
unsigned int counter = 0;
for (DataPackage *dataPackage : dataPackageVector) {
Serial << counter << "-> " << "id: " << dataPackage->id << " value: " << dataPackage->value << endl;
counter++;
}
}

// if given id is not found in the data package vector, return false
bool FindDataPackage(int id) {
for (DataPackage *dataPackage : dataPackageVector) {
if (dataPackage->id == id) {
dataPackage = dataPackage;
return true;
}
}
return false;
}

void setup() {
Serial.begin(BAUD);
while (!Serial) {
// wait for serial port to connect.
}

dataPackageVector.setStorage(
EasyMalloc(struct DataPackage *, ELEMENT_COUNT_MAX), // allocated memory
ELEMENT_COUNT_MAX, // max size of the vector
0 // current size of the vector
);
}

void loop() {
// pick a random number as id between 0 and 10
// if it is not already in the vector, add it
// if it is in the vector, remove it
unsigned int id = random(0, 10);

if (FindDataPackage(id)) {
// remove the data package
RemoveDataPackage(id);
// print removed data package
Serial << "Removed data package with id: " << id << endl;
} else {
// choose a random number between 0 and 100
int value = random(0, 100);
// add the data package
AddDataPackage(id, value);
// print added data package
Serial << "Added data package with id: " << id << " value: " << value << endl;
}
// print the data package vector
PrintDataPackageVector();
Serial << "------------------------------" << endl;
delay(DELAY);
}