PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UDPCallResponse.ino 4.4KB

4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. UDP Call Response
  3. Send responses to calls for information from a remote host
  4. */
  5. #include <Ethernet.h>
  6. #include <EthernetUdp.h>
  7. #include <SPI.h>
  8. #include <OSCBundle.h>
  9. #include <OSCBoards.h>
  10. //UDP communication
  11. EthernetUDP Udp;
  12. //the Arduino's IP
  13. IPAddress ip(128, 32, 122, 252);
  14. //port numbers
  15. const unsigned int inPort = 8888;
  16. const unsigned int outPort = 9999;
  17. //everything on the network needs a unique MAC
  18. #if defined(__MK20DX128__)
  19. // Teensy 3 has MAC burned in
  20. static byte mac[6];
  21. void read(uint8_t word, uint8_t *mac, uint8_t offset) {
  22. FTFL_FCCOB0 = 0x41; // Selects the READONCE command
  23. FTFL_FCCOB1 = word; // read the given word of read once area
  24. // launch command and wait until complete
  25. FTFL_FSTAT = FTFL_FSTAT_CCIF;
  26. while(!(FTFL_FSTAT & FTFL_FSTAT_CCIF));
  27. *(mac+offset) = FTFL_FCCOB5; // collect only the top three bytes,
  28. *(mac+offset+1) = FTFL_FCCOB6; // in the right orientation (big endian).
  29. *(mac+offset+2) = FTFL_FCCOB7; // Skip FTFL_FCCOB4 as it's always 0.
  30. }
  31. void read_mac() {
  32. read(0xe,mac,0);
  33. read(0xf,mac,3);
  34. }
  35. #else
  36. void read_mac() {}
  37. byte mac[] = {
  38. 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // you can find this written on the board of some Arduino Ethernets or shields
  39. #endif
  40. //outgoing messages
  41. OSCBundle bundleOUT;
  42. //converts the pin to an osc address
  43. char * numToOSCAddress( int pin){
  44. static char s[10];
  45. int i = 9;
  46. s[i--]= '\0';
  47. do
  48. {
  49. s[i] = "0123456789"[pin % 10];
  50. --i;
  51. pin /= 10;
  52. }
  53. while(pin && i);
  54. s[i] = '/';
  55. return &s[i];
  56. }
  57. /**
  58. * ANALOG
  59. *
  60. * called when the address matches "/a"
  61. *
  62. * format:
  63. * /analog/(pin)
  64. * /u = analogRead with pullup
  65. *
  66. **/
  67. void routeAnalog(OSCMessage &msg, int addrOffset ){
  68. int pinMatched;
  69. pinMatched = msg.match("/0", addrOffset);
  70. if(pinMatched){
  71. if (msg.fullMatch("/u", pinMatched+addrOffset)) pinMode(analogInputToDigitalPin(0), INPUT_PULLUP); //set the pullup
  72. //do the analog read and send the results
  73. bundleOUT.add("/analog/0").add((int32_t)analogRead(0));
  74. }
  75. pinMatched = msg.match("/1", addrOffset);
  76. if(pinMatched){
  77. if (msg.fullMatch("/u", pinMatched+addrOffset)) pinMode(analogInputToDigitalPin(1), INPUT_PULLUP); //set the pullup
  78. //do the analog read and send the results
  79. bundleOUT.add("/analog/1").add((int32_t)analogRead(1));
  80. }
  81. pinMatched = msg.match("/2", addrOffset);
  82. if(pinMatched){
  83. if (msg.fullMatch("/u", pinMatched+addrOffset)) pinMode(analogInputToDigitalPin(2), INPUT_PULLUP); //set the pullup
  84. //do the analog read and send the results
  85. bundleOUT.add("/analog/2").add((int32_t)analogRead(2));
  86. }
  87. pinMatched = msg.match("/3", addrOffset);
  88. if(pinMatched){
  89. if (msg.fullMatch("/u", pinMatched+addrOffset)) pinMode(analogInputToDigitalPin(3), INPUT_PULLUP); //set the pullup
  90. //do the analog read and send the results
  91. bundleOUT.add("/analog/3").add((int32_t)analogRead(3));
  92. }
  93. pinMatched = msg.match("/4", addrOffset);
  94. if(pinMatched){
  95. if (msg.fullMatch("/u", pinMatched+addrOffset)) pinMode(analogInputToDigitalPin(4), INPUT_PULLUP); //set the pullup
  96. //do the analog read and send the results
  97. bundleOUT.add("/analog/4").add((int32_t)analogRead(4));
  98. }
  99. pinMatched = msg.match("/5", addrOffset);
  100. if(pinMatched){
  101. if (msg.fullMatch("/u", pinMatched+addrOffset)) pinMode(analogInputToDigitalPin(5), INPUT_PULLUP); //set the pullup
  102. //do the analog read and send the results
  103. bundleOUT.add("/analog/5").add((int32_t)analogRead(5));
  104. }
  105. }
  106. /**
  107. * MAIN METHODS
  108. *
  109. * setup and loop, bundle receiving/sending, initial routing
  110. */
  111. void setup() {
  112. //setup ethernet part
  113. read_mac();
  114. Ethernet.begin(mac,ip);
  115. Udp.begin(inPort);
  116. }
  117. //reads and routes the incoming messages
  118. void loop(){
  119. OSCBundle bundleIN;
  120. int size;
  121. if( (size = Udp.parsePacket())>0)
  122. {
  123. // unsigned int outPort = Udp.remotePort();
  124. while(size--)
  125. bundleIN.fill(Udp.read());
  126. if(!bundleIN.hasError())
  127. bundleIN.route("/analog", routeAnalog);
  128. // send the response bundle back to where the request came from
  129. Udp.beginPacket(Udp.remoteIP(), outPort);
  130. bundleOUT.send(Udp);
  131. Udp.endPacket();
  132. bundleOUT.empty(); // empty the bundle ready to use for new messages
  133. }
  134. }