SerialCallResponse.ino 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. Serial Call Response
  3. Send responses to calls for information from a remote host
  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. OSCBundle bundleOUT;
  15. /**
  16. * ANALOG
  17. *
  18. * called when the address matches "/a"
  19. *
  20. * format:
  21. * /analog/(pin)
  22. * /u = analogRead with pullup
  23. *
  24. **/
  25. void routeAnalog(OSCMessage &msg, int addrOffset ){
  26. int pinMatched;
  27. pinMatched = msg.match("/0", addrOffset);
  28. if(pinMatched){
  29. if (msg.fullMatch("/u", pinMatched+addrOffset)) pinMode(analogInputToDigitalPin(0), INPUT_PULLUP); //set the pullup
  30. //do the analog read and send the results
  31. bundleOUT.add("/analog/0").add((int32_t)analogRead(0));
  32. }
  33. pinMatched = msg.match("/1", addrOffset);
  34. if(pinMatched){
  35. if (msg.fullMatch("/u", pinMatched+addrOffset)) pinMode(analogInputToDigitalPin(1), INPUT_PULLUP); //set the pullup
  36. //do the analog read and send the results
  37. bundleOUT.add("/analog/1").add((int32_t)analogRead(1));
  38. }
  39. pinMatched = msg.match("/2", addrOffset);
  40. if(pinMatched){
  41. if (msg.fullMatch("/u", pinMatched+addrOffset)) pinMode(analogInputToDigitalPin(2), INPUT_PULLUP); //set the pullup
  42. //do the analog read and send the results
  43. bundleOUT.add("/analog/2").add((int32_t)analogRead(2));
  44. }
  45. pinMatched = msg.match("/3", addrOffset);
  46. if(pinMatched){
  47. if (msg.fullMatch("/u", pinMatched+addrOffset)) pinMode(analogInputToDigitalPin(3), INPUT_PULLUP); //set the pullup
  48. //do the analog read and send the results
  49. bundleOUT.add("/analog/3").add((int32_t)analogRead(3));
  50. }
  51. pinMatched = msg.match("/4", addrOffset);
  52. if(pinMatched){
  53. if (msg.fullMatch("/u", pinMatched+addrOffset)) pinMode(analogInputToDigitalPin(4), INPUT_PULLUP); //set the pullup
  54. //do the analog read and send the results
  55. bundleOUT.add("/analog/4").add((int32_t)analogRead(4));
  56. }
  57. pinMatched = msg.match("/5", addrOffset);
  58. if(pinMatched){
  59. if (msg.fullMatch("/u", pinMatched+addrOffset)) pinMode(analogInputToDigitalPin(5), INPUT_PULLUP); //set the pullup
  60. //do the analog read and send the results
  61. bundleOUT.add("/analog/5").add((int32_t)analogRead(5));
  62. }
  63. }
  64. /**
  65. * MAIN METHODS
  66. *
  67. * setup and loop, bundle receiving/sending, initial routing
  68. */
  69. void setup() {
  70. SLIPSerial.begin(9600); // set this as high as you can reliably run on your platform
  71. }
  72. //reads and routes the incoming messages
  73. void loop(){
  74. OSCBundle bundleIN;
  75. int size;
  76. while(!SLIPSerial.endofPacket())
  77. if ((size =SLIPSerial.available()) > 0)
  78. {
  79. while(size--)
  80. bundleIN.fill(SLIPSerial.read());
  81. }
  82. if(!bundleIN.hasError())
  83. {
  84. bundleIN.route("/analog", routeAnalog);
  85. //send the outgoing response message
  86. SLIPSerial.beginPacket();
  87. bundleOUT.send(SLIPSerial);
  88. SLIPSerial.endPacket();
  89. bundleOUT.empty(); // empty the bundle ready to use for new messages
  90. }
  91. }