Created
August 8, 2011 17:21
-
-
Save davisford/1132223 to your computer and use it in GitHub Desktop.
Hygro Petal => Add In CmdMessenger
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// CmdMessenger library (included in local libraries folder) | |
// Origin: https://github.com/dreamcat4/cmdmessenger | |
#include "CmdMessenger.h" | |
// Streaming4 library (included in local libraries folder) | |
// Origin: http://arduiniana.org/libraries/streaming/ | |
#include "Streaming.h" | |
// Mustnt conflict / collide with our message payload data. Fine if we use base64 library ^^ above | |
char field_separator = ','; | |
char command_separator = ';'; | |
// Attach a new CmdMessenger object to the default Serial port | |
CmdMessenger cmdMessenger = CmdMessenger(Serial, field_separator, command_separator); | |
// __________________ C M D L I S T I N G ( T X / R X ) ____________________ | |
enum | |
{ | |
kCOMM_ERROR = 000, // serial port comm error | |
kACK = 001, // ack command was received | |
kARDUINO_READY = 002, // after setup | |
kERR = 003, // bad command or error | |
kSEND_CMDS_END, // DO NOT DELETE | |
}; | |
messengerCallbackFunction messengerCallbacks[] = | |
{ | |
NULL | |
}; | |
// __________________ D E F A U L T C A L L B A C K S ________________________ | |
// command 2 | |
void arduino_ready() | |
{ | |
cmdMessenger.sendCmd(kACK,"Arduino ready"); | |
} | |
// command error | |
void unknownCmd() | |
{ | |
cmdMessenger.sendCmd(kERR,"Unknown command"); | |
} | |
void attach_callbacks(messengerCallbackFunction* callbacks) | |
{ | |
int i = 0; | |
int offset = kSEND_CMDS_END; | |
while(callbacks[i]) | |
{ | |
cmdMessenger.attach(offset+i, callbacks[i]); | |
i++; | |
} | |
} | |
//------------------ S E T U P ( ) ------------------------------------------- | |
void setup() | |
{ | |
// Listen on serial connection for messages from the pc | |
// Daisy v1 must be set to 57600 for Bluetooth comms | |
// If you use the serial-USB adapter, you can go to 115200 | |
Serial.begin(57600); | |
// cmdMessenger.discard_LF_CR(); // Useful if your terminal appends CR/LF, and you wish to remove them | |
cmdMessenger.print_LF_CR(); // Make output more readable whilst debugging in Arduino Serial Monitor | |
// Attach default / generic callback methods | |
cmdMessenger.attach(kARDUINO_READY, arduino_ready); | |
cmdMessenger.attach(unknownCmd); | |
// Attach my application's user-defined callback methods | |
attach_callbacks(messengerCallbacks); | |
arduino_ready(); | |
} | |
// ------------------ M A I N ( ) -------------------------------------------- | |
// Timeout handling | |
long timeoutInterval = 2000; // 2 seconds | |
long previousMillis = 0; | |
int counter = 0; | |
void timeout() | |
{ | |
// add code in here you want to | |
// execute every timeoutInterval seconds | |
} | |
void loop() | |
{ | |
// Process incoming serial data, if any | |
cmdMessenger.feedinSerialData(); | |
// handle timeout function, if any | |
if ( millis() - previousMillis > timeoutInterval ) | |
{ | |
timeout(); | |
previousMillis = millis(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment