PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

SerialReceive.ino 1.5KB

3 år sedan
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. #if ARDUINO >= 100
  43. while(!Serial)
  44. ; // Leonardo bug
  45. #endif
  46. }
  47. //reads and dispatches the incoming message
  48. void loop(){
  49. OSCBundle bundleIN;
  50. int size;
  51. while(!SLIPSerial.endofPacket())
  52. if( (size =SLIPSerial.available()) > 0)
  53. {
  54. while(size--)
  55. bundleIN.fill(SLIPSerial.read());
  56. }
  57. if(!bundleIN.hasError())
  58. bundleIN.dispatch("/led", LEDcontrol);
  59. }