Teensy 4.1 core updated for C++20
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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