Advertisement
Guest User

Untitled

a guest
Apr 27th, 2020
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.06 KB | None | 0 0
  1. //Libraries
  2. #include "Arduino.h"
  3. #include <OSCMessage.h>
  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. #include "eOS.h"
  12.  
  13. //Strings for communication
  14. const String HANDSHAKE_QUERY = "ETCOSC?";
  15. const String HANDSHAKE_REPLY = "OK";
  16. const String PING_QUERY = "enc_hello";
  17. const String PARAMETER_QUERY = "/eos/out/param/";
  18. const String NO_PARAMETER = "none"; // none is a keyword used when there is no parameter
  19.  
  20. //Variable definition
  21. #define PING_AFTER_IDLE_INTERVAL    2500
  22. #define TIMEOUT_AFTER_IDLE_INTERVAL 5000
  23. #define PARAMETER_MAX    14 // number of parameters must even
  24. unsigned long lastMessageRxTime = 0;
  25. bool timeoutPingSent = false;
  26. int8_t idx = 0; // start with parameter index 2 must even
  27.  
  28.  
  29. struct Parameter {
  30.   String name;
  31.   String displayName;
  32.   float value;
  33. };
  34.  
  35. struct Parameter parameter[PARAMETER_MAX] = {
  36.   {NO_PARAMETER, "------"},
  37.   {"Intens"},
  38.   {"Pan"},
  39.   {"Tilt"},
  40.   {"Zoom"},
  41.   {"Edge"},
  42.   {"Iris"},
  43.   {"Diffusn"},
  44.   //{"Hue"},
  45.   //{"Saturatn"},
  46.   {"Red"},
  47.   {"Green"},
  48.   {"Blue"},
  49.   {"Cyan"},
  50.   {"Magenta"},
  51.   {"Yellow"},
  52.   //{"cto", "CTO"},
  53.   //{"frame_assembly", "Assembly"},
  54.   //{"thrust_A", "ThrustA"},
  55.   //{"angle_A", "AngleA"},
  56.   //{"thrust_B", "ThrustB"},
  57.   //{"thrust_B", "AngleB"},
  58.   //{"thrust_C", "ThrustC"},
  59.   //{"angle_C", "AngleC"},
  60.   //{"thrust_D", "ThrustD"},
  61.   //{"angle_D", "AngleD"}
  62. };
  63.  
  64.  
  65.  
  66. //Send Handshake Reply
  67. void initEOS()
  68. {
  69.   SLIPSerial.beginPacket();
  70.   SLIPSerial.write((const uint8_t*)HANDSHAKE_REPLY.c_str(), (size_t)HANDSHAKE_REPLY.length());
  71.   SLIPSerial.endPacket();
  72. }
  73.  
  74. //Acquire OSC message and send to parser
  75. void grabOSCmsg()
  76. {
  77.   static String curMsg;
  78.   int size;
  79.   size = SLIPSerial.available();
  80.   if (size > 0) {
  81.     while (size--) curMsg += (char)(SLIPSerial.read());
  82.   }
  83.   if (SLIPSerial.endofPacket()) {
  84.     parseOSCmsg(curMsg);
  85.     lastMessageRxTime = millis();
  86.     curMsg = String();
  87.     return curMsg;
  88.   }
  89. }
  90.  
  91. //Break apart messages from Eos
  92. void parseOSCmsg(String msg)
  93. {
  94.   static String parseMsg;
  95.   if (msg.indexOf(HANDSHAKE_QUERY) != -1) { // Check to see if this is the handshake string
  96.     initEOS();
  97.     return;
  98.   }
  99.   else {
  100.     OSCMessage oscmsg;
  101.     oscmsg.fill((uint8_t*)msg.c_str(), (int)msg.length());
  102.     parseMsg = PARAMETER_QUERY + parameter[idx].name;
  103.     if (msg.indexOf(parseMsg) != -1) {
  104.       parameter[idx].value = oscmsg.getFloat(0);
  105.       parseMsg = String();
  106.       return;
  107.     }
  108.     parseMsg = PARAMETER_QUERY + parameter[idx + 1].name;
  109.     if (msg.indexOf(parseMsg) != -1) {
  110.       parameter[idx + 1].value = oscmsg.getFloat(0);
  111.       parseMsg = String();
  112.       return;
  113.     }
  114.   }
  115. }
  116.  
  117. void setup()
  118. {
  119.   //Hack from OSC library to deal with a bug
  120.   //SLIPSerial.begin(115200);
  121. //#ifdef BOARD_HAS_USB_SERIAL
  122.  // while (!SerialUSB);
  123. //#else
  124. //  while (!Serial);
  125. //#endif
  126.  
  127.   initEOS(); //for hotplug on boards like Uno (No native USB)
  128. }
  129.  
  130. void loop() {
  131.   grabOSCmsg();
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement