SerialEcho.ino 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /*
  2. Serial USB ports are bidirectional.
  3. This example can be extended to build routers and forwarders of OSC packets
  4. */
  5. #include <OSCBundle.h>
  6. #include <OSCBoards.h>
  7. #ifdef BOARD_HAS_USB_SERIAL
  8. #include <SLIPEncodedUSBSerial.h>
  9. SLIPEncodedUSBSerial SLIPSerial( thisBoardsSerialUSB );
  10. #else
  11. #include <SLIPEncodedSerial.h>
  12. SLIPEncodedSerial SLIPSerial(Serial1);
  13. #endif
  14. void setup() {
  15. //begin SLIPSerial just like Serial
  16. SLIPSerial.begin(9600); // set this as high as you can reliably run on your platform
  17. }
  18. void loop(){
  19. OSCBundle bndl;
  20. int size;
  21. //receive a bundle
  22. while(!SLIPSerial.endofPacket())
  23. if( (size =SLIPSerial.available()) > 0)
  24. {
  25. while(size--)
  26. bndl.fill(SLIPSerial.read());
  27. }
  28. if(!bndl.hasError())
  29. {
  30. static int32_t sequencenumber=0;
  31. // we can sneak an addition onto the end of the bundle
  32. bndl.add("/micros").add((int32_t)micros()); // (int32_t) is the type of OSC Integers
  33. bndl.add("/sequencenumber").add(sequencenumber++);
  34. bndl.add("/digital/5").add(digitalRead(5)==HIGH);
  35. bndl.add("/lsb").add((sequencenumber &1)==1);
  36. SLIPSerial.beginPacket(); // mark the beginning of the OSC Packet
  37. bndl.send(SLIPSerial);
  38. SLIPSerial.endPacket();
  39. }
  40. }