您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

137 行
3.9KB

  1. #ifndef USBserial_h_
  2. #define USBserial_h_
  3. #include <inttypes.h>
  4. #include "Stream.h"
  5. #define USB_MIDI_SYSEX_MAX 60 // maximum sysex length we can receive
  6. /*
  7. These were originally meant to allow programs written for
  8. Francois Best's MIDI library to be easily used with
  9. Teensy's usbMIDI which implements the same API. However,
  10. the MIDI library definitions have changed, so these names
  11. now conflict. They've never been documented (the PJRC web
  12. page documents usbMIDI.getType() in numbers) so they are
  13. now commented out so usbMIDI and the MIDI library can be
  14. used together without conflict.
  15. #define NoteOff 0
  16. #define NoteOn 1
  17. #define AfterTouchPoly 2
  18. #define ControlChange 3
  19. #define ProgramChange 4
  20. #define AfterTouchChannel 5
  21. #define PitchBend 6
  22. #define SystemExclusive 7
  23. */
  24. class usb_midi_class
  25. {
  26. public:
  27. void sendNoteOff(uint8_t note, uint8_t velocity, uint8_t channel);
  28. void sendNoteOn(uint8_t note, uint8_t velocity, uint8_t channel);
  29. void sendPolyPressure(uint8_t note, uint8_t pressure, uint8_t channel);
  30. void sendControlChange(uint8_t control, uint8_t value, uint8_t channel);
  31. void sendProgramChange(uint8_t program, uint8_t channel);
  32. void sendAfterTouch(uint8_t pressure, uint8_t channel);
  33. void sendPitchBend(uint16_t value, uint8_t channel);
  34. void sendSysEx(uint8_t length, const uint8_t *data);
  35. void send_now(void);
  36. uint8_t analog2velocity(uint16_t val, uint8_t range);
  37. bool read(uint8_t channel=0);
  38. inline uint8_t getType(void) {
  39. return msg_type;
  40. };
  41. inline uint8_t getChannel(void) {
  42. return msg_channel;
  43. };
  44. inline uint8_t getData1(void) {
  45. return msg_data1;
  46. };
  47. inline uint8_t getData2(void) {
  48. return msg_data2;
  49. };
  50. inline uint8_t * getSysExArray(void) {
  51. return msg_sysex;
  52. };
  53. inline void setHandleNoteOff(void (*fptr)(uint8_t channel, uint8_t note, uint8_t velocity)) {
  54. handleNoteOff = fptr;
  55. };
  56. inline void setHandleNoteOn(void (*fptr)(uint8_t channel, uint8_t note, uint8_t velocity)) {
  57. handleNoteOn = fptr;
  58. };
  59. inline void setHandleVelocityChange(void (*fptr)(uint8_t channel, uint8_t note, uint8_t velocity)) {
  60. handleVelocityChange = fptr;
  61. };
  62. inline void setHandleControlChange(void (*fptr)(uint8_t channel, uint8_t control, uint8_t value)) {
  63. handleControlChange = fptr;
  64. };
  65. inline void setHandleProgramChange(void (*fptr)(uint8_t channel, uint8_t program)) {
  66. handleProgramChange = fptr;
  67. };
  68. inline void setHandleAfterTouch(void (*fptr)(uint8_t channel, uint8_t pressure)) {
  69. handleAfterTouch = fptr;
  70. };
  71. inline void setHandlePitchChange(void (*fptr)(uint8_t channel, int pitch)) {
  72. handlePitchChange = fptr;
  73. };
  74. inline void setHandleRealTimeSystem(void (*fptr)(uint8_t realtimebyte)) {
  75. handleRealTimeSystem = fptr;
  76. };
  77. private:
  78. void send_raw(uint8_t b0, uint8_t b1, uint8_t b2, uint8_t b3);
  79. void read_sysex_byte(uint8_t b);
  80. uint8_t msg_channel;
  81. uint8_t msg_type;
  82. uint8_t msg_data1;
  83. uint8_t msg_data2;
  84. uint8_t msg_sysex[USB_MIDI_SYSEX_MAX];
  85. uint8_t msg_sysex_len;
  86. void (*handleNoteOff)(uint8_t ch, uint8_t note, uint8_t vel);
  87. void (*handleNoteOn)(uint8_t ch, uint8_t note, uint8_t vel);
  88. void (*handleVelocityChange)(uint8_t ch, uint8_t note, uint8_t vel);
  89. void (*handleControlChange)(uint8_t ch, uint8_t, uint8_t);
  90. void (*handleProgramChange)(uint8_t ch, uint8_t);
  91. void (*handleAfterTouch)(uint8_t ch, uint8_t);
  92. void (*handlePitchChange)(uint8_t ch, int pitch);
  93. void (*handleRealTimeSystem)(uint8_t rtb);
  94. };
  95. extern usb_midi_class usbMIDI;
  96. class usb_serial_class : public Stream
  97. {
  98. public:
  99. // standard Arduino functions
  100. void begin(long);
  101. void end();
  102. virtual int available();
  103. virtual int read();
  104. virtual int peek();
  105. virtual void flush();
  106. #if ARDUINO >= 100
  107. virtual size_t write(uint8_t);
  108. #else
  109. virtual void write(uint8_t);
  110. #endif
  111. using Print::write;
  112. operator bool();
  113. // Teensy extensions
  114. void send_now(void);
  115. uint32_t baud(void);
  116. uint8_t stopbits(void);
  117. uint8_t paritytype(void);
  118. uint8_t numbits(void);
  119. uint8_t dtr(void);
  120. uint8_t rts(void);
  121. private:
  122. uint8_t readnext(void);
  123. };
  124. extern usb_serial_class Serial;
  125. #endif