SerialOscuinowithMessages.ino 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. #include <OSCBundle.h>
  2. #include <OSCBoards.h>
  3. #ifdef BOARD_HAS_USB_SERIAL
  4. #include <SLIPEncodedUSBSerial.h>
  5. SLIPEncodedUSBSerial SLIPSerial( thisBoardsSerialUSB );
  6. #else
  7. #include <SLIPEncodedSerial.h>
  8. SLIPEncodedSerial SLIPSerial(Serial1);
  9. #endif
  10. //converts the pin to an osc address
  11. char * numToOSCAddress( int pin){
  12. static char s[10];
  13. int i = 9;
  14. s[i--]= '\0';
  15. do
  16. {
  17. s[i] = "0123456789"[pin % 10];
  18. --i;
  19. pin /= 10;
  20. }
  21. while(pin && i);
  22. s[i] = '/';
  23. return &s[i];
  24. }
  25. /**
  26. * ROUTES
  27. *
  28. * these are where the routing functions go
  29. *
  30. */
  31. /**
  32. * DIGITAL
  33. *
  34. * called when address matched "/d"
  35. * expected format:
  36. * /d/(pin)
  37. * /u = digitalRead with pullup
  38. * (no value) = digitalRead without pullup
  39. * (value) = digital write on that pin
  40. *
  41. */
  42. void routeDigital(OSCMessage &msg, int addrOffset ){
  43. //match input or output
  44. for(byte pin = 0; pin < NUM_DIGITAL_PINS; pin++){
  45. //match against the pin number strings
  46. int pinMatched = msg.match(numToOSCAddress(pin), addrOffset);
  47. if(pinMatched){
  48. //if it has an int, then it's a digital write
  49. if (msg.isInt(0)){
  50. pinMode(pin, OUTPUT);
  51. digitalWrite(pin, (msg.getInt(0)>0) ? HIGH:LOW);
  52. } //otherwise it's an analog read
  53. else if(msg.isFloat(0)){
  54. analogWrite(pin, (int)(msg.getFloat(0)*255.0f));
  55. }
  56. //otherwise it's an digital read
  57. //with a pullup?
  58. else if (msg.fullMatch("/u", pinMatched+addrOffset)){
  59. //set the pullup
  60. pinMode(pin, INPUT_PULLUP);
  61. //setup the output address which should be /d/(pin)/u
  62. char outputAddress[9];
  63. strcpy(outputAddress, "/d");
  64. strcat(outputAddress, numToOSCAddress(pin));
  65. strcat(outputAddress,"/u");
  66. //do the digital read and send the results
  67. {
  68. OSCMessage msgOut(outputAddress); msgOut.add(digitalRead(pin));
  69. SLIPSerial.beginPacket(); msgOut.send(SLIPSerial); SLIPSerial.endPacket();
  70. }
  71. } //else without a pullup
  72. else {
  73. //set the pinmode
  74. pinMode(pin, INPUT);
  75. //setup the output address which should be /d/(pin)
  76. char outputAddress[6];
  77. strcpy(outputAddress, "/d");
  78. strcat(outputAddress, numToOSCAddress(pin));
  79. //do the digital read and send the results
  80. {
  81. OSCMessage msgOut(outputAddress); msgOut.add(digitalRead(pin));
  82. SLIPSerial.beginPacket(); msgOut.send(SLIPSerial); SLIPSerial.endPacket();
  83. }
  84. }
  85. }
  86. }
  87. }
  88. /**
  89. * ANALOG
  90. *
  91. * called when the address matches "/a"
  92. *
  93. * format:
  94. * /a/(pin)
  95. * /u = analogRead with pullup
  96. * (no value) = analogRead without pullup
  97. * (digital value) = digital write on that pin
  98. * (float value) = analogWrite on that pin
  99. *
  100. **/
  101. void routeAnalog(OSCMessage &msg, int addrOffset ){
  102. //iterate through all the analog pins
  103. for(byte pin = 0; pin < NUM_ANALOG_INPUTS; pin++){
  104. //match against the pin number strings
  105. int pinMatched = msg.match(numToOSCAddress(pin), addrOffset);
  106. if(pinMatched){
  107. //if it has an int, then it's a digital write
  108. if (msg.isInt(0)){
  109. pinMode(analogInputToDigitalPin(pin), OUTPUT);
  110. digitalWrite(analogInputToDigitalPin(pin), (msg.getInt(0) > 0)? HIGH: LOW);
  111. } //otherwise it's an analog read
  112. else if(msg.isFloat(0)){
  113. analogWrite(pin, (int)(msg.getFloat(0)*255.0f));
  114. }
  115. #ifdef BOARD_HAS_ANALOG_PULLUP
  116. //with a pullup?
  117. else if (msg.fullMatch("/u", pinMatched+addrOffset)){
  118. //set the pullup
  119. pinMode(analogInputToDigitalPin(pin), INPUT_PULLUP);
  120. //setup the output address which should be /a/(pin)/u
  121. char outputAddress[9];
  122. strcpy(outputAddress, "/a");
  123. strcat(outputAddress, numToOSCAddress(pin));
  124. strcat(outputAddress,"/u");
  125. //do the analog read and send the results
  126. {
  127. OSCMessage msgOut(outputAddress); msgOut.add((int32_t)analogRead(pin));
  128. SLIPSerial.beginPacket();msgOut.send(SLIPSerial); SLIPSerial.endPacket();
  129. }
  130. } //else without a pullup
  131. #endif
  132. else {
  133. //set the pinmode
  134. // This fails on Arduino 1.04 on Leanardo, I added this to fix it: #define analogInputToDigitalPin(p) (p+18)
  135. pinMode(analogInputToDigitalPin(pin), INPUT);
  136. //setup the output address which should be /a/(pin)
  137. char outputAddress[6];
  138. strcpy(outputAddress, "/a");
  139. strcat(outputAddress, numToOSCAddress(pin));
  140. //do the analog read and send the results
  141. {
  142. OSCMessage msgOut(outputAddress); msgOut.add((int32_t)analogRead(pin));
  143. SLIPSerial.beginPacket(); msgOut.send(SLIPSerial); SLIPSerial.endPacket();
  144. }
  145. }
  146. }
  147. }
  148. }
  149. #ifdef BOARD_HAS_TONE
  150. /**
  151. * TONE
  152. *
  153. * square wave output "/tone"
  154. *
  155. * format:
  156. * /tone/pin
  157. *
  158. * (digital value) (float value) = frequency in Hz
  159. * (no value) disable tone
  160. *
  161. **/
  162. void routeTone(OSCMessage &msg, int addrOffset ){
  163. //iterate through all the analog pins
  164. for(byte pin = 0; pin < NUM_DIGITAL_PINS; pin++){
  165. //match against the pin number strings
  166. int pinMatched = msg.match(numToOSCAddress(pin), addrOffset);
  167. if(pinMatched){
  168. unsigned int frequency = 0;
  169. //if it has an int, then it's an integers frequency in Hz
  170. if (msg.isInt(0)){
  171. frequency = msg.getInt(0);
  172. } //otherwise it's a floating point frequency in Hz
  173. else if(msg.isFloat(0)){
  174. frequency = msg.getFloat(0);
  175. }
  176. else
  177. noTone(pin);
  178. if(frequency>0)
  179. {
  180. if(msg.isInt(1))
  181. tone(pin, frequency, msg.getInt(1));
  182. else
  183. tone(pin, frequency);
  184. }
  185. }
  186. }
  187. }
  188. #endif
  189. #ifdef BOARD_HAS_CAPACITANCE_SENSING
  190. #if defined(__MKL26Z64__)
  191. // teensy 3.0LC
  192. #define NTPINS 11
  193. const int cpins[NTPINS] = {22,23,19,18,17,16,15,0,1,3,4 };
  194. #elif defined(__MK66FX1M0__)
  195. // teensy 3.6
  196. #define NTPINS 12
  197. const int cpins[NTPINS] = {0,1,14,15,16,17,18,19,22,23,29,30 };
  198. #else
  199. //Teensy 3.1 3.2
  200. #define NTPINS 12
  201. const int cpins[NTPINS] = {22,23,19,18,17,16,15,0,1,25,32, 33 };
  202. #endif
  203. void routeTouch(OSCMessage &msg, int addrOffset )
  204. {
  205. for(int i=0;i<NTPINS;++i)
  206. {
  207. const char *name = numToOSCAddress(cpins[i]);
  208. int pinMatched = msg.match(name, addrOffset);
  209. if(pinMatched)
  210. {
  211. char outputAddress[9];
  212. strcpy(outputAddress, "/c");
  213. strcat(outputAddress, name);
  214. {
  215. OSCMessage msgOut(outputAddress); msgOut.add(touchRead(cpins[i]));
  216. SLIPSerial.beginPacket(); msgOut.send(SLIPSerial); SLIPSerial.endPacket();
  217. }
  218. }
  219. }
  220. }
  221. #endif
  222. /**
  223. * SYSTEM MESSAGES
  224. *
  225. * expected format:
  226. * /s
  227. * /m = microseconds
  228. * /d = number of digital pins
  229. * /a = number of analog pins
  230. * /l integer = set the led
  231. * /t = temperature
  232. * /s = power supply voltage
  233. */
  234. //
  235. void routeSystem(OSCMessage &msg, int addrOffset ){
  236. #ifdef BOARD_HAS_DIE_TEMPERATURE_SENSOR
  237. if (msg.fullMatch("/t", addrOffset)){
  238. { OSCMessage msgOut("/s/t"); msgOut.add(getTemperature()); SLIPSerial.beginPacket();msgOut.send(SLIPSerial); SLIPSerial.endPacket(); }
  239. }
  240. #endif
  241. #ifdef BOARD_HAS_DIE_POWER_SUPPLY_MEASUREMENT
  242. if (msg.fullMatch("/s", addrOffset)){
  243. { OSCMessage msgOut("/s/s"); msgOut.add(getSupplyVoltage()); SLIPSerial.beginPacket();msgOut.send(SLIPSerial); SLIPSerial.endPacket(); }
  244. }
  245. #endif
  246. if (msg.fullMatch("/m", addrOffset)){
  247. { OSCMessage msgOut("/s/m"); msgOut.add((int32_t)micros()); SLIPSerial.beginPacket();msgOut.send(SLIPSerial); SLIPSerial.endPacket(); }
  248. }
  249. if (msg.fullMatch("/d", addrOffset)){
  250. { OSCMessage msgOut("/s/d"); msgOut.add(NUM_DIGITAL_PINS); SLIPSerial.beginPacket();msgOut.send(SLIPSerial); SLIPSerial.endPacket(); }
  251. }
  252. if (msg.fullMatch("/a", addrOffset)){
  253. { OSCMessage msgOut("/s/a"); msgOut.add(NUM_ANALOG_INPUTS); SLIPSerial.beginPacket();msgOut.send(SLIPSerial); SLIPSerial.endPacket(); }
  254. }
  255. if (msg.fullMatch("/l", addrOffset)){
  256. if (msg.isInt(0)){
  257. pinMode(LED_BUILTIN, OUTPUT);
  258. int i = msg.getInt(0);
  259. pinMode(LED_BUILTIN, OUTPUT);
  260. digitalWrite(LED_BUILTIN, (i > 0)? HIGH: LOW);
  261. { OSCMessage msgOut("/s/l"); msgOut.add(i); SLIPSerial.beginPacket();msgOut.send(SLIPSerial); SLIPSerial.endPacket(); }
  262. }
  263. }
  264. }
  265. /**
  266. * MAIN METHODS
  267. *
  268. * setup and loop, bundle receiving/sending, initial routing
  269. */
  270. void setup() {
  271. SLIPSerial.begin(9600); // set this as high as you can reliably run on your platform
  272. }
  273. //reads and routes the incoming messages
  274. void loop(){
  275. OSCBundle bundleIN;
  276. int size;
  277. while(!SLIPSerial.endofPacket())
  278. if ((size =SLIPSerial.available()) > 0)
  279. {
  280. while(size--)
  281. bundleIN.fill(SLIPSerial.read());
  282. }
  283. if(!bundleIN.hasError())
  284. {
  285. bundleIN.route("/s", routeSystem);
  286. bundleIN.route("/a", routeAnalog);
  287. bundleIN.route("/d", routeDigital);
  288. #ifdef BOARD_HAS_TONE
  289. bundleIN.route("/tone", routeTone);
  290. #endif
  291. #ifdef BOARD_HAS_CAPACITANCE_SENSING
  292. bundleIN.route("/c", routeTouch);
  293. #endif
  294. }
  295. }