PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

115 líneas
3.3KB

  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. #if ARDUINO >= 100
  72. while(!Serial)
  73. ; // Leonardo bug
  74. #endif
  75. }
  76. //reads and routes the incoming messages
  77. void loop(){
  78. OSCBundle bundleIN;
  79. int size;
  80. while(!SLIPSerial.endofPacket())
  81. if ((size =SLIPSerial.available()) > 0)
  82. {
  83. while(size--)
  84. bundleIN.fill(SLIPSerial.read());
  85. }
  86. if(!bundleIN.hasError())
  87. {
  88. bundleIN.route("/analog", routeAnalog);
  89. //send the outgoing response message
  90. SLIPSerial.beginPacket();
  91. bundleOUT.send(SLIPSerial);
  92. SLIPSerial.endPacket();
  93. bundleOUT.empty(); // empty the bundle ready to use for new messages
  94. }
  95. }