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

336 行
13KB

  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. class usb_midi_class
  7. {
  8. public:
  9. // Message type names for compatibility with Arduino MIDI library 4.3.1
  10. enum MidiType {
  11. InvalidType = 0x00, // For notifying errors
  12. NoteOff = 0x80, // Note Off
  13. NoteOn = 0x90, // Note On
  14. AfterTouchPoly = 0xA0, // Polyphonic AfterTouch
  15. ControlChange = 0xB0, // Control Change / Channel Mode
  16. ProgramChange = 0xC0, // Program Change
  17. AfterTouchChannel = 0xD0, // Channel (monophonic) AfterTouch
  18. PitchBend = 0xE0, // Pitch Bend
  19. SystemExclusive = 0xF0, // System Exclusive
  20. TimeCodeQuarterFrame = 0xF1, // System Common - MIDI Time Code Quarter Frame
  21. SongPosition = 0xF2, // System Common - Song Position Pointer
  22. SongSelect = 0xF3, // System Common - Song Select
  23. TuneRequest = 0xF6, // System Common - Tune Request
  24. Clock = 0xF8, // System Real Time - Timing Clock
  25. Start = 0xFA, // System Real Time - Start
  26. Continue = 0xFB, // System Real Time - Continue
  27. Stop = 0xFC, // System Real Time - Stop
  28. ActiveSensing = 0xFE, // System Real Time - Active Sensing
  29. SystemReset = 0xFF, // System Real Time - System Reset
  30. };
  31. void begin(void) { }
  32. void end(void) { }
  33. void sendNoteOff(uint8_t note, uint8_t velocity, uint8_t channel, uint8_t cable=0) {
  34. send(0x80, note, velocity, channel, cable);
  35. }
  36. void sendNoteOn(uint8_t note, uint8_t velocity, uint8_t channel, uint8_t cable=0) {
  37. send(0x90, note, velocity, channel, cable);
  38. }
  39. void sendPolyPressure(uint8_t note, uint8_t pressure, uint8_t channel, uint8_t cable=0) {
  40. send(0xA0, note, pressure, channel, cable);
  41. }
  42. void sendAfterTouchPoly(uint8_t note, uint8_t pressure, uint8_t channel, uint8_t cable=0) {
  43. send(0xA0, note, pressure, channel, cable);
  44. }
  45. void sendControlChange(uint8_t control, uint8_t value, uint8_t channel, uint8_t cable=0) {
  46. send(0xB0, control, value, channel, cable);
  47. }
  48. void sendProgramChange(uint8_t program, uint8_t channel, uint8_t cable=0) {
  49. send(0xC0, program, 0, channel, cable);
  50. }
  51. void sendAfterTouch(uint8_t pressure, uint8_t channel, uint8_t cable=0) {
  52. send(0xD0, pressure, 0, channel, cable);
  53. }
  54. void sendPitchBend(int value, uint8_t channel, uint8_t cable=0) {
  55. if (value < -8192) {
  56. value = -8192;
  57. } else if (value > 8191) {
  58. value = 8191;
  59. }
  60. value += 8192;
  61. send(0xE0, value, value >> 7, channel, cable);
  62. }
  63. void sendSysEx(uint16_t length, const uint8_t *data, bool hasTerm=false, uint8_t cable=0) {
  64. if (cable > 0) return;
  65. if (hasTerm) {
  66. sendSysEx_BufferHasTerm(length, data);
  67. } else {
  68. sendSysEx_AddTermBytes(length, data);
  69. }
  70. }
  71. void sendRealTime(uint8_t type, uint8_t cable=0) __attribute__((always_inline)) __attribute__((always_inline)) {
  72. switch (type) {
  73. case 0xF8: // Clock
  74. case 0xFA: // Start
  75. case 0xFB: // Continue
  76. case 0xFC: // Stop
  77. case 0xFE: // ActiveSensing
  78. case 0xFF: // SystemReset
  79. send(type, 0, 0, 0, cable);
  80. break;
  81. default: // Invalid Real Time marker
  82. break;
  83. }
  84. }
  85. void sendTimeCodeQuarterFrame(uint8_t type, uint8_t value, uint8_t cable=0) __attribute__((always_inline)) __attribute__((always_inline)) {
  86. send(0xF1, ((type & 0x07) << 4) | (value & 0x0F), 0, 0, cable);
  87. }
  88. void sendSongPosition(uint16_t beats, uint8_t cable=0) __attribute__((always_inline)) {
  89. send(0xF2, beats, beats >> 7, 0, cable);
  90. }
  91. void sendSongSelect(uint8_t song, uint8_t cable=0) __attribute__((always_inline)) {
  92. send(0xF3, song, 0, 0, cable);
  93. }
  94. void sendTuneRequest(uint8_t cable=0) __attribute__((always_inline)) {
  95. send(0xF6, 0, 0, 0, cable);
  96. }
  97. void beginRpn(uint16_t number, uint8_t channel, uint8_t cable=0) __attribute__((always_inline)) {
  98. sendControlChange(101, number >> 7, channel, cable);
  99. sendControlChange(100, number, channel, cable);
  100. }
  101. void sendRpnValue(uint16_t value, uint8_t channel, uint8_t cable=0) __attribute__((always_inline)) {
  102. sendControlChange(6, value >> 7, channel, cable);
  103. sendControlChange(38, value, channel, cable);
  104. }
  105. void sendRpnIncrement(uint8_t amount, uint8_t channel, uint8_t cable=0) __attribute__((always_inline)) {
  106. sendControlChange(96, amount, channel, cable);
  107. }
  108. void sendRpnDecrement(uint8_t amount, uint8_t channel, uint8_t cable=0) __attribute__((always_inline)) {
  109. sendControlChange(97, amount, channel, cable);
  110. }
  111. void endRpn(uint8_t channel, uint8_t cable=0) __attribute__((always_inline)) {
  112. sendControlChange(101, 0x7F, channel, cable);
  113. sendControlChange(100, 0x7F, channel, cable);
  114. }
  115. void beginNrpn(uint16_t number, uint8_t channel, uint8_t cable=0) __attribute__((always_inline)) {
  116. sendControlChange(99, number >> 7, channel, cable);
  117. sendControlChange(98, number, channel, cable);
  118. }
  119. void sendNrpnValue(uint16_t value, uint8_t channel, uint8_t cable=0) __attribute__((always_inline)) {
  120. sendControlChange(6, value >> 7, channel, cable);
  121. sendControlChange(38, value, channel, cable);
  122. }
  123. void sendNrpnIncrement(uint8_t amount, uint8_t channel, uint8_t cable=0) __attribute__((always_inline)) {
  124. sendControlChange(96, amount, channel, cable);
  125. }
  126. void sendNrpnDecrement(uint8_t amount, uint8_t channel, uint8_t cable=0) __attribute__((always_inline)) {
  127. sendControlChange(97, amount, channel, cable);
  128. }
  129. void endNrpn(uint8_t channel, uint8_t cable=0) __attribute__((always_inline)) {
  130. sendControlChange(99, 0x7F, channel, cable);
  131. sendControlChange(98, 0x7F, channel, cable);
  132. }
  133. void send(uint8_t type, uint8_t data1, uint8_t data2, uint8_t channel, uint8_t cable) __attribute__((always_inline)) {
  134. if (cable > 0) return;
  135. if (type < 0xF0) {
  136. if (type < 0x80) return;
  137. send_raw(type >> 4, (type & 0xF0) | ((channel - 1) & 0x0F),
  138. data1 & 0x7F, data2 & 0x7F);
  139. } else if (type >= 0xF8 || type == 0xF6) {
  140. send_raw(0x0F, type, 0, 0);
  141. } else if (type == 0xF1 || type == 0xF3) {
  142. send_raw(0x02, type, data1 & 0x7F, 0);
  143. } else if (type == 0xF2) {
  144. send_raw(0x03, type, data1 & 0x7F, data2 & 0x7F);
  145. }
  146. }
  147. void send_now(void);
  148. uint8_t analog2velocity(uint16_t val, uint8_t range);
  149. bool read(uint8_t channel=0);
  150. inline uint8_t getType(void) {
  151. return msg_type;
  152. }
  153. uint8_t getCable(void) {
  154. return 0;
  155. }
  156. uint8_t getChannel(void) {
  157. return msg_channel;
  158. }
  159. uint8_t getData1(void) {
  160. return msg_data1;
  161. }
  162. uint8_t getData2(void) {
  163. return msg_data2;
  164. }
  165. uint8_t * getSysExArray(void) {
  166. return msg_sysex;
  167. }
  168. void setHandleNoteOff(void (*fptr)(uint8_t channel, uint8_t note, uint8_t velocity)) {
  169. // type: 0x80 NoteOff
  170. handleNoteOff = fptr;
  171. }
  172. void setHandleNoteOn(void (*fptr)(uint8_t channel, uint8_t note, uint8_t velocity)) {
  173. // type: 0x90 NoteOn
  174. handleNoteOn = fptr;
  175. }
  176. void setHandleVelocityChange(void (*fptr)(uint8_t channel, uint8_t note, uint8_t velocity)) {
  177. // type: 0xA0 AfterTouchPoly
  178. handleVelocityChange = fptr;
  179. }
  180. void setHandleAfterTouchPoly(void (*fptr)(uint8_t channel, uint8_t note, uint8_t pressure)) {
  181. // type: 0xA0 AfterTouchPoly
  182. handleVelocityChange = fptr;
  183. }
  184. void setHandleControlChange(void (*fptr)(uint8_t channel, uint8_t control, uint8_t value)) {
  185. // type: 0xB0 ControlChange
  186. handleControlChange = fptr;
  187. }
  188. void setHandleProgramChange(void (*fptr)(uint8_t channel, uint8_t program)) {
  189. // type: 0xC0 ProgramChange
  190. handleProgramChange = fptr;
  191. }
  192. void setHandleAfterTouch(void (*fptr)(uint8_t channel, uint8_t pressure)) {
  193. // type: 0xD0 AfterTouchChannel
  194. handleAfterTouch = fptr;
  195. }
  196. void setHandleAfterTouchChannel(void (*fptr)(uint8_t channel, uint8_t pressure)) {
  197. // type: 0xD0 AfterTouchChannel
  198. handleAfterTouch = fptr;
  199. }
  200. void setHandlePitchChange(void (*fptr)(uint8_t channel, int pitch)) {
  201. // type: 0xE0 PitchBend
  202. handlePitchChange = fptr;
  203. }
  204. void setHandleSysEx(void (*fptr)(const uint8_t *data, uint16_t length, bool complete)) {
  205. // type: 0xF0 SystemExclusive - multiple calls for message bigger than buffer
  206. handleSysExPartial = (void (*)(const uint8_t *, uint16_t, uint8_t))fptr;
  207. }
  208. void setHandleSystemExclusive(void (*fptr)(const uint8_t *data, uint16_t length, bool complete)) {
  209. // type: 0xF0 SystemExclusive - multiple calls for message bigger than buffer
  210. handleSysExPartial = (void (*)(const uint8_t *, uint16_t, uint8_t))fptr;
  211. }
  212. void setHandleSystemExclusive(void (*fptr)(uint8_t *data, unsigned int size)) {
  213. // type: 0xF0 SystemExclusive - single call, message larger than buffer is truncated
  214. handleSysExComplete = fptr;
  215. }
  216. void setHandleTimeCodeQuarterFrame(void (*fptr)(uint8_t data)) {
  217. // type: 0xF1 TimeCodeQuarterFrame
  218. handleTimeCodeQuarterFrame = fptr;
  219. }
  220. void setHandleSongPosition(void (*fptr)(uint16_t beats)) {
  221. // type: 0xF2 SongPosition
  222. handleSongPosition = fptr;
  223. }
  224. void setHandleSongSelect(void (*fptr)(uint8_t songnumber)) {
  225. // type: 0xF3 SongSelect
  226. handleSongSelect = fptr;
  227. }
  228. void setHandleTuneRequest(void (*fptr)(void)) {
  229. // type: 0xF6 TuneRequest
  230. handleTuneRequest = fptr;
  231. }
  232. void setHandleClock(void (*fptr)(void)) {
  233. // type: 0xF8 Clock
  234. handleClock = fptr;
  235. }
  236. void setHandleStart(void (*fptr)(void)) {
  237. // type: 0xFA Start
  238. handleStart = fptr;
  239. }
  240. void setHandleContinue(void (*fptr)(void)) {
  241. // type: 0xFB Continue
  242. handleContinue = fptr;
  243. }
  244. void setHandleStop(void (*fptr)(void)) {
  245. // type: 0xFC Stop
  246. handleStop = fptr;
  247. }
  248. void setHandleActiveSensing(void (*fptr)(void)) {
  249. // type: 0xFE ActiveSensing
  250. handleActiveSensing = fptr;
  251. }
  252. void setHandleSystemReset(void (*fptr)(void)) {
  253. // type: 0xFF SystemReset
  254. handleSystemReset = fptr;
  255. }
  256. void setHandleRealTimeSystem(void (*fptr)(uint8_t realtimebyte)) {
  257. // type: 0xF8-0xFF - if more specific handler not configured
  258. handleRealTimeSystem = fptr;
  259. }
  260. private:
  261. void send_raw(uint8_t b0, uint8_t b1, uint8_t b2, uint8_t b3);
  262. uint32_t midiusb_available();
  263. void midiusb_read(uint8_t *buf);
  264. void sendSysEx_BufferHasTerm(uint16_t length, const uint8_t *data);
  265. void sendSysEx_AddTermBytes(uint16_t length, const uint8_t *data);
  266. void read_sysex_byte(uint8_t b);
  267. uint8_t msg_channel;
  268. uint8_t msg_type;
  269. uint8_t msg_data1;
  270. uint8_t msg_data2;
  271. uint8_t msg_sysex[USB_MIDI_SYSEX_MAX];
  272. uint16_t msg_sysex_len;
  273. void (*handleNoteOff)(uint8_t ch, uint8_t note, uint8_t vel);
  274. void (*handleNoteOn)(uint8_t ch, uint8_t note, uint8_t vel);
  275. void (*handleVelocityChange)(uint8_t ch, uint8_t note, uint8_t vel);
  276. void (*handleControlChange)(uint8_t ch, uint8_t, uint8_t);
  277. void (*handleProgramChange)(uint8_t ch, uint8_t);
  278. void (*handleAfterTouch)(uint8_t ch, uint8_t);
  279. void (*handlePitchChange)(uint8_t ch, int pitch);
  280. void (*handleSysExPartial)(const uint8_t *data, uint16_t length, uint8_t complete);
  281. void (*handleSysExComplete)(uint8_t *data, unsigned int size);
  282. void (*handleTimeCodeQuarterFrame)(uint8_t data);
  283. void (*handleSongPosition)(uint16_t beats);
  284. void (*handleSongSelect)(uint8_t songnumber);
  285. void (*handleTuneRequest)(void);
  286. void (*handleClock)(void);
  287. void (*handleStart)(void);
  288. void (*handleContinue)(void);
  289. void (*handleStop)(void);
  290. void (*handleActiveSensing)(void);
  291. void (*handleSystemReset)(void);
  292. void (*handleRealTimeSystem)(uint8_t rtb);
  293. friend class MIDI_;
  294. };
  295. extern usb_midi_class usbMIDI;
  296. class usb_serial_class : public Stream
  297. {
  298. public:
  299. // standard Arduino functions
  300. void begin(long);
  301. void end();
  302. virtual int available();
  303. virtual int read();
  304. virtual int peek();
  305. virtual void flush();
  306. #if ARDUINO >= 100
  307. virtual size_t write(uint8_t);
  308. #else
  309. virtual void write(uint8_t);
  310. #endif
  311. using Print::write;
  312. operator bool();
  313. // Teensy extensions
  314. void send_now(void);
  315. uint32_t baud(void);
  316. uint8_t stopbits(void);
  317. uint8_t paritytype(void);
  318. uint8_t numbits(void);
  319. uint8_t dtr(void);
  320. uint8_t rts(void);
  321. private:
  322. uint8_t readnext(void);
  323. };
  324. extern usb_serial_class Serial;
  325. #endif