Skip to content

Instantly share code, notes, and snippets.

@kasperkamperman
Last active April 21, 2016 15:44
Show Gist options
  • Save kasperkamperman/7e827bb481dfcdb4cb52 to your computer and use it in GitHub Desktop.
Save kasperkamperman/7e827bb481dfcdb4cb52 to your computer and use it in GitHub Desktop.
Example to receive serial commands for use with FastLED. FastLED itself is not implemented. This just replies the received command.
/* Structure to receive 12 byte commands.
* Protocol idea:
* https://drive.google.com/file/d/0B3Tn6oRmh71oS3FNQ2hsODVwS1E
*
* Processing program to send commands:
* https://gist.github.com/kasperkamperman/6c7c256f7042a3ca5118
*
* Thanks to Daniel Garcia for the command structure example.
*/
// packing is necessary for Teensy and RFduino otherwise extra padding is added
// http://stackoverflow.com/questions/5473189/what-is-a-packed-structure-in-c
// typedef struct __attribute__((__packed__))
// added a padding byte instead after a tip from Daniel. Probably faster then packing.
struct Cmd {
uint8_t type;
uint8_t typeId;
uint8_t cmdId;
uint8_t paddingByte;
uint16_t cmdValue0;
uint16_t cmdValue1;
uint16_t cmdValue2;
uint16_t cmdValue3;
}
Cmd aCmd;
void setup() {
Serial.begin(57600);
}
void loop() {
// check if we have incoming data on our serialport
while(Serial.available() > 0) {
Serial.readBytes((char*) &aCmd,sizeof(Cmd));
sendReply();
}
}
// example for receiving the structure with Bluetooth RFDuino instead of through serial port.
// void RFduinoBLE_onReceive( char *data, int len) {
// if(len>=sizeof(Cmd)) {
// memcpy(&aCmd, &data[0], sizeof(Cmd));
// }
// sendReply();
// }
void sendReply() {
// test if we received the values
Serial.print(aCmd.type);
Serial.print(",");
Serial.print(aCmd.typeId);
Serial.print(",");
Serial.print(aCmd.cmdId);
Serial.print(",");
Serial.print(aCmd.cmdValue0);
Serial.print(",");
Serial.print(aCmd.cmdValue1);
Serial.print(",");
Serial.print(aCmd.cmdValue2);
Serial.print(",");
Serial.print(aCmd.cmdValue3);
Serial.println();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment