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.

UDPReceive.ino 2.1KB

3 vuotta sitten
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #include <Ethernet.h>
  2. #include <EthernetUdp.h>
  3. #include <SPI.h>
  4. #include <OSCBundle.h>
  5. #include <OSCBoards.h>
  6. /*
  7. * UDPReceiveOSC
  8. * Set a tone according to incoming OSC control
  9. * Adrian Freed
  10. */
  11. //UDP communication
  12. EthernetUDP Udp;
  13. byte mac[] = {
  14. 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // you can find this written on the board of some Arduino Ethernets or shields
  15. //the Arduino's IP
  16. IPAddress ip(128, 32, 122, 252);
  17. //port numbers
  18. const unsigned int inPort = 8888;
  19. //converts the pin to an osc address
  20. char * numToOSCAddress( int pin){
  21. static char s[10];
  22. int i = 9;
  23. s[i--]= '\0';
  24. do
  25. {
  26. s[i] = "0123456789"[pin % 10];
  27. --i;
  28. pin /= 10;
  29. }
  30. while(pin && i);
  31. s[i] = '/';
  32. return &s[i];
  33. }
  34. /**
  35. * TONE
  36. *
  37. * square wave output "/tone"
  38. *
  39. * format:
  40. * /tone/pin
  41. *
  42. * (digital value) (float value) = freqency in Hz
  43. * (no value) disable tone
  44. *
  45. **/
  46. void routeTone(OSCMessage &msg, int addrOffset ){
  47. //iterate through all the analog pins
  48. for(byte pin = 0; pin < NUM_DIGITAL_PINS; pin++){
  49. //match against the pin number strings
  50. int pinMatched = msg.match(numToOSCAddress(pin), addrOffset);
  51. if(pinMatched){
  52. unsigned int frequency = 0;
  53. //if it has an int, then it's an integers frequency in Hz
  54. if (msg.isInt(0)){
  55. frequency = msg.getInt(0);
  56. } //otherwise it's a floating point frequency in Hz
  57. else if(msg.isFloat(0)){
  58. frequency = msg.getFloat(0);
  59. }
  60. else
  61. noTone(pin);
  62. if(frequency>0)
  63. {
  64. if(msg.isInt(1))
  65. tone(pin, frequency, msg.getInt(1));
  66. else
  67. tone(pin, frequency);
  68. }
  69. }
  70. }
  71. }
  72. void setup() {
  73. //setup ethernet part
  74. Ethernet.begin(mac,ip);
  75. Udp.begin(inPort);
  76. }
  77. //reads and dispatches the incoming message
  78. void loop(){
  79. OSCBundle bundleIN;
  80. int size;
  81. if( (size = Udp.parsePacket())>0)
  82. {
  83. while(size--)
  84. bundleIN.fill(Udp.read());
  85. if(!bundleIN.hasError())
  86. bundleIN.route("/tone", routeTone);
  87. }
  88. }