From 692278fe292972e4de832135125da25d58008345 Mon Sep 17 00:00:00 2001 From: ArchitecturalSpies Date: Thu, 1 Sep 2022 14:44:02 +0300 Subject: [PATCH 1/2] examples with structs I have added examples of how to use this library with structs and heap allocated structs. --- examples/ExamplesWithStruct/Example1.ino | 94 ++++++++++++++++++++++ examples/ExamplesWithStruct/Example2.ino | 99 ++++++++++++++++++++++++ 2 files changed, 193 insertions(+) create mode 100644 examples/ExamplesWithStruct/Example1.ino create mode 100644 examples/ExamplesWithStruct/Example2.ino diff --git a/examples/ExamplesWithStruct/Example1.ino b/examples/ExamplesWithStruct/Example1.ino new file mode 100644 index 0000000..86bd3f3 --- /dev/null +++ b/examples/ExamplesWithStruct/Example1.ino @@ -0,0 +1,94 @@ +/* + If you want to use this library with structs, this exapmle can be useful. +*/ +#include +#include +#include + +struct DataPackage { + int id; + int value; +}; + +const long BAUD = 115200; +const int ELEMENT_COUNT_MAX = 10; +typedef Vector 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); +} \ No newline at end of file diff --git a/examples/ExamplesWithStruct/Example2.ino b/examples/ExamplesWithStruct/Example2.ino new file mode 100644 index 0000000..3038738 --- /dev/null +++ b/examples/ExamplesWithStruct/Example2.ino @@ -0,0 +1,99 @@ +/* + In any case if you need to allocate structs in the heap, this example can be useful. +*/ +#include +#include +#include + +#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 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); +} \ No newline at end of file From 7cc1b8147b0856bdd86bdc981b8a2dcee978d279 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samet=20Yaz=C4=B1c=C4=B1?= Date: Mon, 5 Sep 2022 14:31:15 +0300 Subject: [PATCH 2/2] struct examples directory update --- examples/ExamplesWithStruct/{ => Example1}/Example1.ino | 0 examples/ExamplesWithStruct/{ => Example2}/Example2.ino | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename examples/ExamplesWithStruct/{ => Example1}/Example1.ino (100%) rename examples/ExamplesWithStruct/{ => Example2}/Example2.ino (100%) diff --git a/examples/ExamplesWithStruct/Example1.ino b/examples/ExamplesWithStruct/Example1/Example1.ino similarity index 100% rename from examples/ExamplesWithStruct/Example1.ino rename to examples/ExamplesWithStruct/Example1/Example1.ino diff --git a/examples/ExamplesWithStruct/Example2.ino b/examples/ExamplesWithStruct/Example2/Example2.ino similarity index 100% rename from examples/ExamplesWithStruct/Example2.ino rename to examples/ExamplesWithStruct/Example2/Example2.ino