osc.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #include <OSCMessage.h>
  2. #include <Ethernet.h>
  3. #include <EthernetUdp.h>
  4. #include <SPI.h>
  5. #include <OSCBoards.h>
  6. #ifdef BOARD_HAS_USB_SERIAL
  7. #include <SLIPEncodedUSBSerial.h>
  8. SLIPEncodedUSBSerial SLIPSerial( thisBoardsSerialUSB );
  9. #else
  10. #include <SLIPEncodedSerial.h>
  11. SLIPEncodedSerial SLIPSerial(Serial);
  12. #endif
  13. //// UTILITIES /////////
  14. template <typename TYPE> void sendOSC(const char * adress, TYPE parameter) {
  15. OSCMessage OSCmsg(adress);
  16. OSCmsg.add(parameter);
  17. SLIPSerial.beginPacket();
  18. //Udp.beginPacket(outIp, outPort);
  19. OSCmsg.send(SLIPSerial); // send the bytes to the SLIP stream
  20. // OSCmsg.send(Udp); // send the bytes to the SLIP stream
  21. SLIPSerial.endPacket();
  22. //Udp.endPacket(); // mark the end of the OSC Packet
  23. OSCmsg.empty(); // free space occupied by message
  24. }
  25. void printTest(OSCMessage &msg) {
  26. sendOSC("/still", "up and running");
  27. }
  28. ///// STEPPER /////////
  29. //void init_Stepper_OSC(OSCMessage &msg) {
  30. // float maximum = init_Stepper();
  31. // sendOSC("/stepper/maxPos",maximum );
  32. //}
  33. //
  34. void moveTo_Stepper_OSC (OSCMessage &msg){
  35. long int distance = moveTo_Stepper(msg.getInt(0));
  36. sendOSC("/stepper/distanceToGo",distance );
  37. // while (!stepper.distanceToGo()==0)
  38. // stepper.run();
  39. }
  40. void move_Stepper_OSC (OSCMessage &msg){
  41. long int distance = move_Stepper(msg.getInt(0));
  42. sendOSC("/stepper/distanceToGo",distance );
  43. }
  44. //
  45. //void moveToCm_Stepper_OSC (OSCMessage &msg){
  46. // long int distance = moveToCm_Stepper(msg.getFloat(0));
  47. // sendOSC("/stepper/distanceToGo/cm",distance );
  48. //}
  49. //
  50. //void moveCm_Stepper_OSC (OSCMessage &msg){
  51. // long int distance = moveCm_Stepper(msg.getFloat(0));
  52. // sendOSC("/stepper/distanceToGo/cm",distance );
  53. //}
  54. void maxSpeed_Stepper (OSCMessage &msg){
  55. long int max_speed = maxSpeed_Stepper(msg.getInt(0));
  56. sendOSC("/stepper/maxSpeed",max_speed );
  57. }
  58. void acceleration_Stepper (OSCMessage &msg){
  59. long int acceleration = acceleration_Stepper(msg.getInt(0));
  60. sendOSC("/stepper/acceleration",acceleration );
  61. }
  62. void currentPos_Stepper_OSC(OSCMessage &msg) {
  63. sendOSC("/stepper/currentPos", stepper.currentPosition());
  64. }
  65. ////// OSC DECLARATIONS /////////
  66. void setup_OSC(){
  67. SLIPSerial.begin(115200);
  68. // delay (100);
  69. sendOSC("/ready", "to go");
  70. }
  71. void handleOSCIn() {
  72. //define OSC message to function relationships
  73. OSCMessage OSCin;
  74. int size;
  75. if (SLIPSerial.available()) {
  76. while (!SLIPSerial.endofPacket())
  77. if ( (size = SLIPSerial.available()) > 0)
  78. {
  79. while (size--)
  80. OSCin.fill(SLIPSerial.read());
  81. }
  82. }
  83. //Declare valid OSC messages here
  84. if (!OSCin.hasError()) {
  85. OSCin.dispatch("/test", printTest);
  86. // //STEPPER CONTROL
  87. // OSCin.dispatch("/stepper/init", init_Stepper_OSC);
  88. OSCin.dispatch("/stepper/currentPos", currentPos_Stepper_OSC);
  89. OSCin.dispatch("/stepper/moveTo", moveTo_Stepper_OSC);
  90. OSCin.dispatch("/stepper/move", move_Stepper_OSC);
  91. // OSCin.dispatch("/stepper/moveTo/cm", moveToCm_Stepper_OSC);
  92. // OSCin.dispatch("/stepper/move/cm", moveCm_Stepper_OSC);
  93. OSCin.dispatch("/stepper/maxSpeed", maxSpeed_Stepper);
  94. OSCin.dispatch("/stepper/acceleration", acceleration_Stepper);
  95. // OSCin.dispatch("/stepper/randomMove", randomMove_Stepper_OSC);
  96. //
  97. // //LEDS
  98. // OSCin.dispatch("/led/value", valueLed_OSC);
  99. // OSCin.dispatch("/led/sensor", sensorLed_OSC);
  100. // OSCin.dispatch("/led/lighting", lightingLed_OSC);
  101. }
  102. // }
  103. }