osc.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. //OSC via udp
  2. #include <WiFiUdp.h>
  3. #include <OSCMessage.h>
  4. WiFiUDP UDP; //udp listener
  5. /* UDP CONFIGURATION */
  6. //int UDP_In_Port = 9000; //udp port input for ESP
  7. //
  8. ////default address and port to send to (IP read from config)
  9. //IPAddress UDP_Out_IP ;
  10. //int UDP_Out_Port = 8000 ;
  11. IPAddress ipMulti(239, 0, 0, 56);
  12. unsigned int portMulti = 12345; // local port to listen on
  13. template <typename TYPE> void sendOSC(const char * adress, TYPE parameter);
  14. template <typename TYPE> void sendOSC(const char * adress, TYPE parameter) {
  15. OSCMessage OSCmsg(adress);
  16. OSCmsg.add(parameter);
  17. UDP.beginPacketMulticast(ipMulti, portMulti,WiFi.localIP());
  18. OSCmsg.send(UDP); // send the bytes to the SLIP stream
  19. UDP.endPacket(); // mark the end of the OSC Packet
  20. OSCmsg.empty(); // free space occupied by message
  21. }
  22. void printTest(OSCMessage &msg) {
  23. Serial.print("OSC type : ");
  24. Serial.println (msg.getType(0));
  25. sendOSC("/received", (int) msg.getType(0));
  26. }
  27. void updatePotValue(OSCMessage &msg) {
  28. // message /ESP_NAME/pot pot#[0..3] value[0..255]
  29. potValueUpdate[msg.getInt(0)] = msg.getInt(1);
  30. sendOSC("/updated", "pot" );
  31. //Serial.print("update pot : " + msg.getInt(0) + " ");
  32. Serial.println (potValueUpdate[msg.getInt(0)]);
  33. }
  34. void setup_OSC(){
  35. ipMulti.fromString(UDP_IP);
  36. String UDP_PORT_string = UDP_PORT;
  37. portMulti = UDP_PORT_string.toInt();
  38. Serial.print("connecting udp to ");
  39. Serial.print(ipMulti);
  40. Serial.print(":");
  41. Serial.println(portMulti);
  42. UDP.beginMulticast(WiFi.localIP(), ipMulti, portMulti);
  43. // OSCMessage OSCmsg("/connected");
  44. // for (int i=0 ; i<4 ; i++){
  45. // OSCmsg.add(WiFi.localIP()[i]);
  46. // }
  47. // UDP.beginPacketMulticast(ipMulti, portMulti,WiFi.localIP());
  48. // OSCmsg.send(UDP); // send the bytes to the SLIP stream
  49. // UDP.endPacket(); // mark the end of the OSC Packet
  50. // OSCmsg.empty(); // free space occupied by message
  51. sendOSC("/connected", ESP_NAME);
  52. }
  53. void handle_OSC() {
  54. OSCMessage OSCin;
  55. int size;
  56. if ( (size = UDP.parsePacket()) > 0) {
  57. while (size--)
  58. OSCin.fill(UDP.read());
  59. //Declare valid OSC messages here
  60. if (!OSCin.hasError()) {
  61. OSCin.dispatch("/test", printTest);
  62. if (OSCin.match(ESP_NAME)){
  63. Serial.println("matched");
  64. OSCin.dispatch("/ESP_TENS_*/pot", updatePotValue);
  65. }
  66. }
  67. }
  68. }