PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

412 行
11KB

  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) = freqency 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. #define NTPINS 11
  192. const int cpins[NTPINS] = {22,23,19,18,17,16,15,0,1,3,4 };
  193. #else
  194. #define NTPINS 12
  195. const int cpins[NTPINS] = {22,23,19,18,17,16,15,0,1,25,32, 33 };
  196. #endif
  197. void routeTouch(OSCMessage &msg, int addrOffset )
  198. {
  199. for(int i=0;i<NTPINS;++i)
  200. {
  201. const char *name = numToOSCAddress(cpins[i]);
  202. int pinMatched = msg.match(name, addrOffset);
  203. if(pinMatched)
  204. {
  205. char outputAddress[9];
  206. strcpy(outputAddress, "/c");
  207. strcat(outputAddress, name);
  208. {
  209. OSCMessage msgOut(outputAddress); msgOut.add(touchRead(cpins[i]));
  210. SLIPSerial.beginPacket(); msgOut.send(SLIPSerial); SLIPSerial.endPacket();
  211. }
  212. }
  213. }
  214. }
  215. #endif
  216. #ifdef BOARD_HAS_DIE_POWER_SUPPLY_MEASUREMENT
  217. #if defined(__MK20DX128__) || defined(__MK20DX256__)|| defined(__MKL26Z64__)
  218. float getSupplyVoltage()
  219. {
  220. int val = analogRead(39);
  221. return val>0? (1.20f*1023/val):0.0f;
  222. }
  223. #else
  224. // power supply measurement on some Arduinos
  225. float getSupplyVoltage(){
  226. // powersupply
  227. int result;
  228. // Read 1.1V reference against AVcc
  229. #if defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
  230. ADMUX = _BV(REFS0) | _BV(MUX4) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
  231. #elif defined (__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__)
  232. ADMUX = _BV(MUX5) | _BV(MUX0);
  233. #elif defined (__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__)
  234. ADMUX = _BV(MUX3) | _BV(MUX2);
  235. #elif defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB1286__)
  236. ADMUX = 0x40| _BV(MUX4) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1) ;
  237. ADCSRB = 0;
  238. #else
  239. ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
  240. #endif
  241. delayMicroseconds(300); // wait for Vref to settle
  242. ADCSRA |= _BV(ADSC); // Convert
  243. while (bit_is_set(ADCSRA,ADSC));
  244. result = ADCL;
  245. result |= ADCH<<8;
  246. float supplyvoltage = 1.1264f *1023 / result;
  247. return supplyvoltage;
  248. }
  249. #endif
  250. #endif
  251. #ifdef BOARD_HAS_DIE_TEMPERATURE_SENSOR
  252. #if defined(__MK20DX128__) || defined(__MK20DX256__)|| defined(__MKL26Z64__)
  253. float getTemperature()
  254. {
  255. analogReference(INTERNAL);
  256. delay(1);
  257. int val = analogRead(38); // seems to be flakey
  258. analogReference(DEFAULT);
  259. return val; //need to compute something here to get to degrees C
  260. }
  261. #else
  262. // temperature
  263. float getTemperature(){
  264. int result;
  265. #if defined(__AVR_ATmega32U4__)
  266. ADMUX = _BV(REFS1) | _BV(REFS0) | _BV(MUX2) | _BV(MUX1) | _BV(MUX0);
  267. ADCSRB = _BV(MUX5);
  268. #else
  269. ADMUX = _BV(REFS1) | _BV(REFS0) | _BV(MUX3);
  270. #endif
  271. delayMicroseconds(200); // wait for Vref to settle
  272. ADCSRA |= _BV(ADSC); // Convert
  273. while (bit_is_set(ADCSRA,ADSC));
  274. result = ADCL;
  275. result |= ADCH<<8;
  276. analogReference(DEFAULT);
  277. return result/1023.0f;
  278. }
  279. #endif
  280. #endif
  281. /**
  282. * SYSTEM MESSAGES
  283. *
  284. * expected format:
  285. * /s
  286. * /m = microseconds
  287. * /d = number of digital pins
  288. * /a = number of analog pins
  289. * /l integer = set the led
  290. * /t = temperature
  291. * /s = power supply voltage
  292. */
  293. //
  294. void routeSystem(OSCMessage &msg, int addrOffset ){
  295. #ifdef BOARD_HAS_DIE_TEMPERATURE_SENSOR
  296. if (msg.fullMatch("/t", addrOffset)){
  297. { OSCMessage msgOut("/s/t"); msgOut.add(getTemperature()); SLIPSerial.beginPacket();msgOut.send(SLIPSerial); SLIPSerial.endPacket(); }
  298. }
  299. #endif
  300. #ifdef BOARD_HAS_DIE_POWER_SUPPLY_MEASUREMENT
  301. if (msg.fullMatch("/s", addrOffset)){
  302. { OSCMessage msgOut("/s/s"); msgOut.add(getSupplyVoltage()); SLIPSerial.beginPacket();msgOut.send(SLIPSerial); SLIPSerial.endPacket(); }
  303. }
  304. #endif
  305. if (msg.fullMatch("/m", addrOffset)){
  306. { OSCMessage msgOut("/s/m"); msgOut.add((int32_t)micros()); SLIPSerial.beginPacket();msgOut.send(SLIPSerial); SLIPSerial.endPacket(); }
  307. }
  308. if (msg.fullMatch("/d", addrOffset)){
  309. { OSCMessage msgOut("/s/d"); msgOut.add(NUM_DIGITAL_PINS); SLIPSerial.beginPacket();msgOut.send(SLIPSerial); SLIPSerial.endPacket(); }
  310. }
  311. if (msg.fullMatch("/a", addrOffset)){
  312. { OSCMessage msgOut("/s/a"); msgOut.add(NUM_ANALOG_INPUTS); SLIPSerial.beginPacket();msgOut.send(SLIPSerial); SLIPSerial.endPacket(); }
  313. }
  314. if (msg.fullMatch("/l", addrOffset)){
  315. if (msg.isInt(0)){
  316. pinMode(LED_BUILTIN, OUTPUT);
  317. int i = msg.getInt(0);
  318. pinMode(LED_BUILTIN, OUTPUT);
  319. digitalWrite(LED_BUILTIN, (i > 0)? HIGH: LOW);
  320. { OSCMessage msgOut("/s/l"); msgOut.add(i); SLIPSerial.beginPacket();msgOut.send(SLIPSerial); SLIPSerial.endPacket(); }
  321. }
  322. }
  323. }
  324. /**
  325. * MAIN METHODS
  326. *
  327. * setup and loop, bundle receiving/sending, initial routing
  328. */
  329. void setup() {
  330. SLIPSerial.begin(9600); // set this as high as you can reliably run on your platform
  331. #if ARDUINO >= 100
  332. while(!Serial)
  333. ; // Leonardo bug
  334. #endif
  335. }
  336. //reads and routes the incoming messages
  337. void loop(){
  338. OSCBundle bundleIN;
  339. int size;
  340. while(!SLIPSerial.endofPacket())
  341. if ((size =SLIPSerial.available()) > 0)
  342. {
  343. while(size--)
  344. bundleIN.fill(SLIPSerial.read());
  345. }
  346. if(!bundleIN.hasError())
  347. {
  348. bundleIN.route("/s", routeSystem);
  349. bundleIN.route("/a", routeAnalog);
  350. bundleIN.route("/d", routeDigital);
  351. #ifdef BOARD_HAS_TONE
  352. bundleIN.route("/tone", routeTone);
  353. #endif
  354. #ifdef BOARD_HAS_CAPACITANCE_SENSING
  355. bundleIN.route("/c", routeTouch);
  356. #endif
  357. }
  358. }