I'm creating a class which parts of my code can put messages into, and other parts of my code can pull those messages off and deal with them. I think I need the .add and .delete functionality so when a piece of code comes a long and deals with a message, it can wipe it off of the class. But I don't really need to store more than say 10 objects at any given time.
I tried using SimpleList from Arduino Playground - LibraryList, but it's acting weird, so maybe I'm using it wrong... or maybe there's a completely better way to do things... Here's my code.
#define MOVEMENT_REQUEST 0x01
#define TEXT_UPDATE 0x01
#include <SimpleList.h>
// This is the struct that pollKeys should generate, and it should
// put it on a stack
struct SystemMessage {
uint8_t messageType; // TEXT_UPDATE | MOVEMENT_REQUEST
char data[100];
};
class SystemMessageCourier {
public:
SystemMessageCourier();
void addMessage(SystemMessage systemMessage);
SimpleList <SystemMessage> extractMessages(uint8_t messageType);
SimpleList <SystemMessage> _messages;
uint8_t count();
};
SystemMessageCourier::SystemMessageCourier() {
}
void SystemMessageCourier::addMessage(SystemMessage systemMessage) {
_messages.push_back(systemMessage);
}
// iterate over all _messages
// add matching messages to hits
// remove matching messages from _messages
SimpleList <SystemMessage> SystemMessageCourier::extractMessages(uint8_t messageType) {
SimpleList<SystemMessage> hits;
for (SimpleList<SystemMessage>::iterator itr = _messages.begin(); itr != _messages.end();) {
SystemMessage thisMsg = *itr;
if (thisMsg.messageType == messageType) {
hits.push_back(thisMsg);
_messages.erase(itr);
continue;
}
++itr;
}
return hits;
}
// displays how many messages it has
uint8_t SystemMessageCourier::count() {
uint8_t count = 0;
for (SimpleList<SystemMessage>::iterator itr = _messages.begin(); itr != _messages.end();) {
count++;
++itr;
}
return count;
}
SystemMessageCourier* systemMessageCourier;
void setup() {
Serial.begin(115200);
Serial.println("Starting setup() ");
systemMessageCourier = new SystemMessageCourier();
struct SystemMessage s = { TEXT_UPDATE, "hello" };
systemMessageCourier->addMessage(s);
struct SystemMessage s2 = { TEXT_UPDATE, "good bye" };
systemMessageCourier->addMessage(s2);
SimpleList<SystemMessage> msg = systemMessageCourier->extractMessages(TEXT_UPDATE);
uint8_t theCount = systemMessageCourier->count();
printHex(theCount, 0);
Serial.println("Setup complete");
}
void loop() {
}
void printHex(int num, int precision) {
char tmp[16];
char format[128];
sprintf(format, "0x%%.%dX ", precision);
sprintf(tmp, format, num);
Serial.print(tmp);
}
The code hangs on the line systemMessageCourier->addMessage(s2);