Page 212

#ifndef FiFoQC_h #define FiFoQC_h class FiFoQC { public:     FiFoQC(size_t dataSize);     ~FiFoQC();     bool queueAdd(void* dta);     void* readFirst();     void* readNext();     bool deleteFirst();     int getQCount();     void zapQueue(); private:     struct FiFoNode     {      void *data;      struct FiFoNode* next;     };     struct FiFoQ     {      struct FiFoNode* head;      struct FiFoNode* tail;      struct FiFoNode* curPos;      int count;      size_t dataSize;     };     struct FiFoQ* FiFoQP; }; #endif

Pages 213 and 214

#include "Arduino.h" #include "FiFoQC.h" FiFoQC::FiFoQC(size_t dataSize) { FiFoQP = malloc(sizeof(struct FiFoQ)); FiFoQP->head = FiFoQP->tail = FiFoQP->curPos = NULL; FiFoQP->dataSize = dataSize; FiFoQP->count = 0; } FiFoQC::~FiFoQC() { this->zapQueue(); free(FiFoQP); } bool FiFoQC::queueAdd(void* dta){ struct FiFoNode* node = malloc(sizeof(struct FiFoNode)); if(node == NULL) {     return false; } node->next = NULL; void* dat = malloc(FiFoQP->dataSize); if(dat == NULL) {     free(node);     return false; } memcpy(dat, dta, FiFoQP->dataSize); node->data = dat; if(FiFoQP->head == NULL) {     FiFoQP->head = FiFoQP->tail = node; } else {     FiFoQP->tail->next = node;     FiFoQP->tail = node; } FiFoQP->count++; return true; } void* FiFoQC::readFirst() { struct FiFoNode* p = FiFoQP->head; if(p) {     FiFoQP->curPos = p;     return p->data; } else {     return FiFoQP->curPos = NULL; } } void* FiFoQC::readNext() { struct FiFoNode* p = FiFoQP->curPos->next; if(p) {     FiFoQP->curPos = p;     return p->data; } else {     return FiFoQP->curPos = NULL; } } bool FiFoQC::deleteFirst() { if(FiFoQP->head == NULL) {     return false; } struct FiFoNode* node = FiFoQP->head; struct FiFoNode* nxt = node->next; free(node->data); free(node); FiFoQP->head = nxt; if(nxt == NULL) {     FiFoQP->tail = NULL; } FiFoQP->count--; return true; } int FiFoQC::getQCount(){ return FiFoQP->count; } void FiFoQC::zapQueue() { bool res = this->deleteFirst(); while(res){     res = this->deleteFirst(); } }

Page 215

#include "FiFoQC.h" template<class T> inline Print &operator <<(Print &obj, T arg) { obj.print(arg); return obj; } struct MyData { int myVal; int yourVal; }; FiFoQC fiFoQ(sizeof(MyData)); // Create a class instance void setup() { Serial.begin(115200); MyData myData; MyData* mDta; for(int i = 1; i < 21; i++) {     myData.myVal = i << 2;     myData.yourVal = i;     fiFoQ.queueAdd(&myData); // Add data items to the queue } Serial << "Queue has " << fiFoQ.getQCount() << " items loaded\n"; Serial << "Now reading records in sequence\n"; mDta = fiFoQ.readFirst(); while(mDta) {     Serial << "Read myval: " << mDta->myVal << " yourVal: " << mDta->yourVal << "\n";     mDta = fiFoQ.readNext(); // read queue items in sequence } Serial << "Now reading and deleting records in sequence\n"; mDta = fiFoQ.readFirst(); while(mDta) {     Serial << "Read myval: " << mDta->myVal << " yourVal: " << mDta->yourVal << "\n";     fiFoQ.deleteFirst();     mDta = fiFoQ.readFirst(); } Serial << "Queue now has " << fiFoQ.getQCount() << " items\n"; fiFoQ.~FiFoQC(); // call the destructor }

page 216

#ifndef MyClass_h #define MyClass_h class MyClass { public:     MyClass(int);     ~MyClass();     int GetVal();     static int getCount(); private:     int someVal;     static int iCount; // declared but also defined in cpp file     static const int mult = 3; }; #endif

Page 217

#include <Arduino.h> #include "MyClass.h" int MyClass::iCount; // iCount needs to be defined and declared in *.h MyClass::MyClass(int sVal) { someVal = sVal; iCount++; } MyClass::~MyClass() { iCount--; } int MyClass::GetVal() { return someVal * mult; } static int MyClass::getCount() { return iCount; }

#include "MyClass.h" #define _NL "\n" template<class T> inline Print &operator <<(Print &obj, T arg) { obj.print(arg); return obj; } void setup() { Serial.begin(115200); MyClass class1(2); MyClass class2(4); Serial << "There are " << MyClass::getCount() << " instances" << _NL; addAnInstance(); Serial << "and now " << MyClass::getCount() << " instances" << _NL; Serial << "Using Static Const " << class1.GetVal() << _NL; } void addAnInstance() { MyClass class3(6); Serial<< "There are now "<< MyClass::getCount()<< " instances"<< _NL; }

Page 218

int (*statPtr)() = &MyClass::getCount; Serial << "Pointer to Static Method gets: " << statPtr() << " instances" << _NL;

#ifndef MyClass_h #define MyClass_h class MyClass { public:     MyClass(int);     int GetVal();     int MyClass::SquareIt(int v); private:     int someVal; }; #endif

Page 219

#include <Arduino.h> #include "MyClass.h" MyClass::MyClass(int sVal) { someVal = sVal; } int MyClass::GetVal() { return someVal; } int MyClass::SquareIt(int v) { return v * v; }

#include "MyClass.h" #define _NL "\n" template<class T> inline Print &operator <<(Print &obj, T arg) { obj.print(arg); return obj; } MyClass class1(42); MyClass *classPointer; void setup() { Serial.begin(115200); classPointer = &class1; int res = classPointer->GetVal(); Serial << "Using class Pointer only: " << res << _NL; }

Page 220

void setup() { Serial.begin(115200); classPointer = &class1; int res = classPointer->GetVal(); Serial << "Using class Pointer only: " << res << _NL; // now create pointer to GetVal() class member int (MyClass::*funPtr)() = &MyClass::GetVal; // now use that pointer with a class instance int res2 = (class1.*funPtr)(); Serial << "Using function pointer: " << res2 << _NL; // now use the class pointer and member pointer together int res3 = (*classPointer.*funPtr)(); Serial << "Using dereferenced class pointer and function pointer: "          << res3 << _NL; int res3a = (classPointer->*funPtr)(); Serial << "Using class pointer and function pointer variant: "          << res3a << _NL; // now try a function with an argument/parameter int (MyClass::*anaPtr)(int) = &MyClass::SquareIt; int res4 = (class1.*anaPtr)(12); Serial << "Using function pointer with parameter: " << res4 << _NL; int res5 = (*classPointer.*anaPtr)(11); Serial << "Using class and function pointer with parameter: "          << res5 << _NL; MyClass class2(3); int res6 = (class2.*funPtr)(); Serial << "Using function pointer to class2: " << res6 << _NL; // now iterating over an array of class instance pointers MyClass *cPointers[] = {&class1, &class2}; for(int i = 0; i < (sizeof(cPointers)/sizeof(cPointers[0])); i++) {     int res7 = (cPointers[i]->*funPtr)();     Serial << "iterating class instance members: " << res7 << _NL; } }

Page 221

int (MyClass::*arrPtr[])()={&MyClass::GetVal, &MyClass::GetDoubleVal}; for(int i = 0; i < (sizeof(cPointers) / sizeof(cPointers[0])); i++) {     for(int j = 0; j < 2; j++) {      int res8 = (cPointers[i]->*arrPtr[j])();      Serial << "Iterating both classes and members: " << res8 << _NL;     } }