Advertisement
Spectrewiz

Message handling

Jul 13th, 2014
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.96 KB | None | 0 0
  1. #define MAXARGS 8
  2.  
  3. int argsBuffer[MAXARGS];
  4. int argsID = 0;
  5.  
  6. /// <summary>Serial.available() returns how many bytes are stored in the internal serial buffer.  Serial.read() returns the next byte in the and removes it from the serial buffer (which decrements Serial.available() by one).  This code will store all incoming argument bytes into an array called argsBuffer which will be used in the command function.</summary>
  7. void loop(){
  8.   //message handling
  9.   int done = 0, header;
  10.   while(Serial.available() > 0){
  11.     int data = Serial.read();
  12.     if(data >= 128){
  13.       header = data;
  14.       memset(argsBuffer, 0, MAXARGS);
  15.       argsID = 0;
  16.       done = 1;
  17.     }else if (argsID <= MAXARGS){
  18.       argsBuffer[argsID++] = data;
  19.     }
  20.   }
  21.   if(done){
  22.     command(header);
  23.     done = 0;
  24.   }
  25. }
  26.  
  27. void command(int header){
  28.   switch(header){
  29.     case 255:
  30.       //LED ON function
  31.       break;
  32.     case 254:
  33.       //LED OFF function
  34.       break;
  35.     //...
  36.   }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement