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

73 行
1.3KB

  1. /*
  2. * Control a servo according to incoming OSC control
  3. *
  4. */
  5. #include <OSCBundle.h>
  6. #include <OSCBoards.h>
  7. #include <Servo.h>
  8. #ifdef BOARD_HAS_USB_SERIAL
  9. #include <SLIPEncodedUSBSerial.h>
  10. SLIPEncodedUSBSerial SLIPSerial( thisBoardsSerialUSB );
  11. #else
  12. #include <SLIPEncodedSerial.h>
  13. SLIPEncodedSerial SLIPSerial(Serial1);
  14. #endif
  15. Servo myservo;
  16. void servoControl(OSCMessage &msg)
  17. {
  18. if (msg.isInt(0))
  19. {
  20. myservo.write(msg.getInt(0));
  21. }
  22. #ifdef TEMPoraray
  23. else if (msg.isFloat(0))
  24. {
  25. //test if that pin is a PWM
  26. if (digitalPinHasPWM(LED_BUILTIN))
  27. {
  28. pinMode(LED_BUILTIN, OUTPUT);
  29. analogWrite(LED_BUILTIN, (int)(msg.getFloat(0)*127.0f));
  30. }
  31. else
  32. SoftPWMSet(LED_BUILTIN, (int)(msg.getFloat(0)*127.0f));
  33. }
  34. #endif
  35. }
  36. void setup() {
  37. SLIPSerial.begin(9600);
  38. myservo.attach(13);
  39. myservo.write(90);
  40. #if ARDUINO >= 100
  41. while(!Serial)
  42. ; // Leonardo bug
  43. #endif
  44. }
  45. //reads and dispatches the incoming message
  46. void loop(){
  47. OSCBundle bundleIN;
  48. int size;
  49. while(!SLIPSerial.endofPacket())
  50. if( (size =SLIPSerial.available()) > 0)
  51. {
  52. while(size--)
  53. bundleIN.fill(SLIPSerial.read());
  54. }
  55. if(!bundleIN.hasError())
  56. bundleIN.dispatch("/servo", servoControl);
  57. }