SerialOscuinoForFubarino.ino 8.3 KB

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