SerialReceive.ino 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*
  2. * Set the LED according to incoming OSC control
  3. */
  4. #include <OSCBundle.h>
  5. #include <OSCBoards.h>
  6. #ifdef BOARD_HAS_USB_SERIAL
  7. #include <SLIPEncodedUSBSerial.h>
  8. SLIPEncodedUSBSerial SLIPSerial( thisBoardsSerialUSB );
  9. #else
  10. #include <SLIPEncodedSerial.h>
  11. SLIPEncodedSerial SLIPSerial(Serial1);
  12. #endif
  13. void LEDcontrol(OSCMessage &msg)
  14. {
  15. if (msg.isInt(0))
  16. {
  17. pinMode(LED_BUILTIN, OUTPUT);
  18. digitalWrite(LED_BUILTIN, (msg.getInt(0) > 0)? HIGH: LOW);
  19. }
  20. else if(msg.isString(0))
  21. {
  22. int length=msg.getDataLength(0);
  23. if(length<5)
  24. {
  25. char str[length];
  26. msg.getString(0,str,length);
  27. if((strcmp("on", str)==0)|| (strcmp("On",str)==0))
  28. {
  29. pinMode(LED_BUILTIN, OUTPUT);
  30. digitalWrite(LED_BUILTIN, HIGH);
  31. }
  32. else if((strcmp("Of", str)==0)|| (strcmp("off",str)==0))
  33. {
  34. pinMode(LED_BUILTIN, OUTPUT);
  35. digitalWrite(LED_BUILTIN, LOW);
  36. }
  37. }
  38. }
  39. }
  40. void setup() {
  41. SLIPSerial.begin(9600); // set this as high as you can reliably run on your platform
  42. }
  43. //reads and dispatches the incoming message
  44. void loop(){
  45. OSCBundle bundleIN;
  46. int size;
  47. while(!SLIPSerial.endofPacket())
  48. if( (size =SLIPSerial.available()) > 0)
  49. {
  50. while(size--)
  51. bundleIN.fill(SLIPSerial.read());
  52. }
  53. if(!bundleIN.hasError())
  54. bundleIN.dispatch("/led", LEDcontrol);
  55. }