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.

пре 11 година
пре 11 година
пре 10 година
пре 11 година
пре 10 година
пре 11 година
пре 11 година
пре 11 година
пре 11 година
пре 11 година
пре 11 година
пре 7 година
пре 11 година
пре 11 година
пре 11 година
пре 11 година
пре 11 година
пре 11 година
пре 7 година
пре 11 година
пре 7 година
пре 7 година
пре 11 година
пре 7 година
пре 11 година
пре 7 година
пре 11 година
пре 7 година
пре 11 година
пре 7 година
пре 11 година
пре 7 година
пре 11 година
пре 7 година
пре 11 година
пре 7 година
пре 7 година
пре 11 година
пре 7 година
пре 11 година
пре 7 година
пре 11 година
пре 7 година
пре 7 година
пре 11 година
пре 7 година
пре 7 година
пре 7 година
пре 11 година
пре 11 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /* Teensyduino Core Library
  2. * http://www.pjrc.com/teensy/
  3. * Copyright (c) 2017 PJRC.COM, LLC.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining
  6. * a copy of this software and associated documentation files (the
  7. * "Software"), to deal in the Software without restriction, including
  8. * without limitation the rights to use, copy, modify, merge, publish,
  9. * distribute, sublicense, and/or sell copies of the Software, and to
  10. * permit persons to whom the Software is furnished to do so, subject to
  11. * the following conditions:
  12. *
  13. * 1. The above copyright notice and this permission notice shall be
  14. * included in all copies or substantial portions of the Software.
  15. *
  16. * 2. If the Software is incorporated into a build system that allows
  17. * selection among a list of target devices, then similar target
  18. * devices manufactured by PJRC.COM must be included in the list of
  19. * target devices and selectable in the same manner.
  20. *
  21. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  25. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  26. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  27. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  28. * SOFTWARE.
  29. */
  30. #ifndef USBmidi_h_
  31. #define USBmidi_h_
  32. #include "usb_desc.h"
  33. #if defined(MIDI_INTERFACE)
  34. #include <inttypes.h>
  35. // maximum sysex length we can receive
  36. #if defined(__MKL26Z64__) || defined(__MK20DX128__)
  37. #define USB_MIDI_SYSEX_MAX 60
  38. #else
  39. #define USB_MIDI_SYSEX_MAX 290
  40. #endif
  41. // C language implementation
  42. #ifdef __cplusplus
  43. extern "C" {
  44. #endif
  45. void usb_midi_write_packed(uint32_t n);
  46. void usb_midi_send_sysex_buffer_has_term(const uint8_t *data, uint32_t length, uint8_t cable);
  47. void usb_midi_send_sysex_add_term_bytes(const uint8_t *data, uint32_t length, uint8_t cable);
  48. void usb_midi_flush_output(void);
  49. int usb_midi_read(uint32_t channel);
  50. uint32_t usb_midi_available(void);
  51. uint32_t usb_midi_read_message(void);
  52. extern uint8_t usb_midi_msg_cable;
  53. extern uint8_t usb_midi_msg_channel;
  54. extern uint8_t usb_midi_msg_type;
  55. extern uint8_t usb_midi_msg_data1;
  56. extern uint8_t usb_midi_msg_data2;
  57. extern uint8_t usb_midi_msg_sysex[USB_MIDI_SYSEX_MAX];
  58. extern uint16_t usb_midi_msg_sysex_len;
  59. extern void (*usb_midi_handleNoteOff)(uint8_t ch, uint8_t note, uint8_t vel);
  60. extern void (*usb_midi_handleNoteOn)(uint8_t ch, uint8_t note, uint8_t vel);
  61. extern void (*usb_midi_handleVelocityChange)(uint8_t ch, uint8_t note, uint8_t vel);
  62. extern void (*usb_midi_handleControlChange)(uint8_t ch, uint8_t control, uint8_t value);
  63. extern void (*usb_midi_handleProgramChange)(uint8_t ch, uint8_t program);
  64. extern void (*usb_midi_handleAfterTouch)(uint8_t ch, uint8_t pressure);
  65. extern void (*usb_midi_handlePitchChange)(uint8_t ch, int pitch);
  66. extern void (*usb_midi_handleSysExPartial)(const uint8_t *data, uint16_t length, uint8_t complete);
  67. extern void (*usb_midi_handleSysExComplete)(uint8_t *data, unsigned int size);
  68. extern void (*usb_midi_handleTimeCodeQuarterFrame)(uint8_t data);
  69. extern void (*usb_midi_handleSongPosition)(uint16_t beats);
  70. extern void (*usb_midi_handleSongSelect)(uint8_t songnumber);
  71. extern void (*usb_midi_handleTuneRequest)(void);
  72. extern void (*usb_midi_handleClock)(void);
  73. extern void (*usb_midi_handleStart)(void);
  74. extern void (*usb_midi_handleContinue)(void);
  75. extern void (*usb_midi_handleStop)(void);
  76. extern void (*usb_midi_handleActiveSensing)(void);
  77. extern void (*usb_midi_handleSystemReset)(void);
  78. extern void (*usb_midi_handleRealTimeSystem)(uint8_t rtb);
  79. #ifdef __cplusplus
  80. }
  81. #endif
  82. // To test receiving on Linux, run "aseqdump -l" to list sequencer devices.
  83. //
  84. // Port Client name Port name
  85. // 0:0 System Timer
  86. // 0:1 System Announce
  87. // 14:0 Midi Through Midi Through Port-0
  88. // 24:0 Teensy MIDI Teensy MIDI MIDI 1
  89. // 28:0 AKM320 AKM320 MIDI 1
  90. //
  91. // Then run "aseqdump -p 24:0" to view the MIDI messages.
  92. //
  93. // Waiting for data. Press Ctrl+C to end.
  94. // Source Event Ch Data
  95. // 24:0 Note on 0, note 61, velocity 99
  96. // 24:0 Note off 0, note 61, velocity 0
  97. // 24:0 Note on 0, note 62, velocity 99
  98. // 24:0 Note off 0, note 62, velocity 0
  99. // 24:0 Note on 0, note 64, velocity 99
  100. // 24:0 Note off 0, note 64, velocity 0
  101. // C++ interface
  102. #ifdef __cplusplus
  103. class usb_midi_class
  104. {
  105. public:
  106. // Message type names for compatibility with Arduino MIDI library 4.3.1
  107. enum MidiType {
  108. InvalidType = 0x00, // For notifying errors
  109. NoteOff = 0x80, // Note Off
  110. NoteOn = 0x90, // Note On
  111. AfterTouchPoly = 0xA0, // Polyphonic AfterTouch
  112. ControlChange = 0xB0, // Control Change / Channel Mode
  113. ProgramChange = 0xC0, // Program Change
  114. AfterTouchChannel = 0xD0, // Channel (monophonic) AfterTouch
  115. PitchBend = 0xE0, // Pitch Bend
  116. SystemExclusive = 0xF0, // System Exclusive
  117. TimeCodeQuarterFrame = 0xF1, // System Common - MIDI Time Code Quarter Frame
  118. SongPosition = 0xF2, // System Common - Song Position Pointer
  119. SongSelect = 0xF3, // System Common - Song Select
  120. TuneRequest = 0xF6, // System Common - Tune Request
  121. Clock = 0xF8, // System Real Time - Timing Clock
  122. Start = 0xFA, // System Real Time - Start
  123. Continue = 0xFB, // System Real Time - Continue
  124. Stop = 0xFC, // System Real Time - Stop
  125. ActiveSensing = 0xFE, // System Real Time - Active Sensing
  126. SystemReset = 0xFF, // System Real Time - System Reset
  127. };
  128. void begin(void) { }
  129. void end(void) { }
  130. void sendNoteOff(uint8_t note, uint8_t velocity, uint8_t channel, uint8_t cable=0) __attribute__((always_inline)) {
  131. send(0x80, note, velocity, channel, cable);
  132. }
  133. void sendNoteOn(uint8_t note, uint8_t velocity, uint8_t channel, uint8_t cable=0) __attribute__((always_inline)) {
  134. send(0x90, note, velocity, channel, cable);
  135. }
  136. void sendPolyPressure(uint8_t note, uint8_t pressure, uint8_t channel, uint8_t cable=0) __attribute__((always_inline)) {
  137. send(0xA0, note, pressure, channel, cable);
  138. }
  139. void sendAfterTouch(uint8_t note, uint8_t pressure, uint8_t channel, uint8_t cable=0) __attribute__((always_inline)) {
  140. send(0xA0, note, pressure, channel, cable);
  141. }
  142. void sendControlChange(uint8_t control, uint8_t value, uint8_t channel, uint8_t cable=0) __attribute__((always_inline)) {
  143. send(0xB0, control, value, channel, cable);
  144. }
  145. void sendProgramChange(uint8_t program, uint8_t channel, uint8_t cable=0) __attribute__((always_inline)) {
  146. send(0xC0, program, 0, channel, cable);
  147. }
  148. void sendAfterTouch(uint8_t pressure, uint8_t channel, uint8_t cable=0) __attribute__((always_inline)) {
  149. send(0xD0, pressure, 0, channel, cable);
  150. }
  151. void sendPitchBend(uint16_t value, uint8_t channel, uint8_t cable=0) __attribute__((always_inline)) {
  152. // MIDI 4.3 takes -8192 to +8191. We take 0 to 16383
  153. // MIDI 4.3 also has version that takes float -1.0 to +1.0
  154. send(0xE0, value, value >> 7, channel, cable);
  155. }
  156. void sendSysEx(uint32_t length, const uint8_t *data, bool hasTerm=true, uint8_t cable=0) __attribute__((always_inline)) {
  157. if (cable >= MIDI_NUM_CABLES) return;
  158. if (hasTerm) {
  159. usb_midi_send_sysex_buffer_has_term(data, length, cable);
  160. } else {
  161. usb_midi_send_sysex_add_term_bytes(data, length, cable);
  162. }
  163. }
  164. void sendRealTime(uint8_t type, uint8_t cable=0) __attribute__((always_inline)) __attribute__((always_inline)) {
  165. switch (type) {
  166. case 0xF8: // Clock
  167. case 0xFA: // Start
  168. case 0xFB: // Continue
  169. case 0xFC: // Stop
  170. case 0xFE: // ActiveSensing
  171. case 0xFF: // SystemReset
  172. send(type, 0, 0, 0, cable);
  173. break;
  174. default: // Invalid Real Time marker
  175. break;
  176. }
  177. }
  178. void sendTimeCodeQuarterFrame(uint8_t type, uint8_t value, uint8_t cable=0) __attribute__((always_inline)) __attribute__((always_inline)) {
  179. send(0xF1, ((type & 0x07) << 4) | (value & 0x0F), 0, 0, cable);
  180. }
  181. //void sendTimeCodeQuarterFrame(uint8_t data, uint8_t cable=0) __attribute__((always_inline)) {
  182. // MIDI 4.3 has this, but we can't implement with cable param
  183. //send(0xF1, data, 0, 0, cable);
  184. //}
  185. void sendSongPosition(uint16_t beats, uint8_t cable=0) __attribute__((always_inline)) {
  186. send(0xF2, beats, beats >> 7, 0, cable);
  187. }
  188. void sendSongSelect(uint8_t song, uint8_t cable=0) __attribute__((always_inline)) {
  189. send(0xF3, song, 0, 0, cable);
  190. }
  191. void sendTuneRequest(uint8_t cable=0) __attribute__((always_inline)) {
  192. send(0xF6, 0, 0, 0, cable);
  193. }
  194. void beginRpn(uint16_t number, uint8_t channel, uint8_t cable=0) __attribute__((always_inline)) {
  195. sendControlChange(101, number >> 7, channel, cable);
  196. sendControlChange(100, number, channel, cable);
  197. }
  198. void sendRpnValue(uint16_t value, uint8_t channel, uint8_t cable=0) __attribute__((always_inline)) {
  199. sendControlChange(6, value >> 7, channel, cable);
  200. sendControlChange(38, value, channel, cable);
  201. }
  202. void sendRpnValue(uint8_t msb, uint8_t lsb, uint8_t channel, uint8_t cable=0) __attribute__((always_inline)) {
  203. sendControlChange(6, msb, channel, cable);
  204. sendControlChange(38, lsb, channel, cable);
  205. }
  206. void sendRpnIncrement(uint8_t amount, uint8_t channel, uint8_t cable=0) __attribute__((always_inline)) {
  207. sendControlChange(96, amount, channel, cable);
  208. }
  209. void sendRpnDecrement(uint8_t amount, uint8_t channel, uint8_t cable=0) __attribute__((always_inline)) {
  210. sendControlChange(97, amount, channel, cable);
  211. }
  212. void endRpn(uint8_t channel, uint8_t cable=0) __attribute__((always_inline)) {
  213. sendControlChange(101, 0x7F, channel, cable);
  214. sendControlChange(100, 0x7F, channel, cable);
  215. }
  216. void beginNrpn(uint16_t number, uint8_t channel, uint8_t cable=0) __attribute__((always_inline)) {
  217. sendControlChange(99, number >> 7, channel, cable);
  218. sendControlChange(98, number, channel, cable);
  219. }
  220. void sendNrpnValue(uint16_t value, uint8_t channel, uint8_t cable=0) __attribute__((always_inline)) {
  221. sendControlChange(6, value >> 7, channel, cable);
  222. sendControlChange(38, value, channel, cable);
  223. }
  224. void sendNrpnValue(uint8_t msb, uint8_t lsb, uint8_t channel, uint8_t cable=0) __attribute__((always_inline)) {
  225. sendControlChange(6, msb, channel, cable);
  226. sendControlChange(38, lsb, channel, cable);
  227. }
  228. void sendNrpnIncrement(uint8_t amount, uint8_t channel, uint8_t cable=0) __attribute__((always_inline)) {
  229. sendControlChange(96, amount, channel, cable);
  230. }
  231. void sendNrpnDecrement(uint8_t amount, uint8_t channel, uint8_t cable=0) __attribute__((always_inline)) {
  232. sendControlChange(97, amount, channel, cable);
  233. }
  234. void endNrpn(uint8_t channel, uint8_t cable=0) __attribute__((always_inline)) {
  235. sendControlChange(99, 0x7F, channel, cable);
  236. sendControlChange(98, 0x7F, channel, cable);
  237. }
  238. void send(uint8_t type, uint8_t data1, uint8_t data2, uint8_t channel, uint8_t cable) __attribute__((always_inline)) {
  239. if (cable >= MIDI_NUM_CABLES) return;
  240. if (type < 0xF0) {
  241. if (type < 0x80) return;
  242. type &= 0xF0;
  243. usb_midi_write_packed((type << 8) | (type >> 4) | ((cable & 0x0F) << 4)
  244. | (((channel - 1) & 0x0F) << 8) | ((data1 & 0x7F) << 16)
  245. | ((data2 & 0x7F) << 24));
  246. } else if (type >= 0xF8 || type == 0xF6) {
  247. usb_midi_write_packed((type << 8) | 0x0F | ((cable & 0x0F) << 4));
  248. } else if (type == 0xF1 || type == 0xF3) {
  249. usb_midi_write_packed((type << 8) | 0x02 | ((cable & 0x0F) << 4)
  250. | ((data1 & 0x7F) << 16));
  251. } else if (type == 0xF2) {
  252. usb_midi_write_packed((type << 8) | 0x03 | ((cable & 0x0F) << 4)
  253. | ((data1 & 0x7F) << 16) | ((data2 & 0x7F) << 24));
  254. }
  255. }
  256. void send_now(void) __attribute__((always_inline)) {
  257. usb_midi_flush_output();
  258. }
  259. uint8_t analog2velocity(uint16_t val, uint8_t range);
  260. bool read(uint8_t channel=0) __attribute__((always_inline)) {
  261. return usb_midi_read(channel);
  262. }
  263. uint8_t getType(void) __attribute__((always_inline)) {
  264. return usb_midi_msg_type;
  265. }
  266. uint8_t getCable(void) __attribute__((always_inline)) {
  267. return usb_midi_msg_cable;
  268. }
  269. uint8_t getChannel(void) __attribute__((always_inline)) {
  270. return usb_midi_msg_channel;
  271. }
  272. uint8_t getData1(void) __attribute__((always_inline)) {
  273. return usb_midi_msg_data1;
  274. }
  275. uint8_t getData2(void) __attribute__((always_inline)) {
  276. return usb_midi_msg_data2;
  277. }
  278. uint8_t * getSysExArray(void) __attribute__((always_inline)) {
  279. return usb_midi_msg_sysex;
  280. }
  281. void setHandleNoteOff(void (*fptr)(uint8_t channel, uint8_t note, uint8_t velocity)) {
  282. // type: 0x80 NoteOff
  283. usb_midi_handleNoteOff = fptr;
  284. }
  285. void setHandleNoteOn(void (*fptr)(uint8_t channel, uint8_t note, uint8_t velocity)) {
  286. // type: 0x90 NoteOn
  287. usb_midi_handleNoteOn = fptr;
  288. }
  289. void setHandleVelocityChange(void (*fptr)(uint8_t channel, uint8_t note, uint8_t velocity)) {
  290. // type: 0xA0 AfterTouchPoly
  291. usb_midi_handleVelocityChange = fptr;
  292. }
  293. void setHandleAfterTouchPoly(void (*fptr)(uint8_t channel, uint8_t note, uint8_t pressure)) {
  294. // type: 0xA0 AfterTouchPoly
  295. usb_midi_handleVelocityChange = fptr;
  296. }
  297. void setHandleControlChange(void (*fptr)(uint8_t channel, uint8_t control, uint8_t value)) {
  298. // type: 0xB0 ControlChange
  299. usb_midi_handleControlChange = fptr;
  300. }
  301. void setHandleProgramChange(void (*fptr)(uint8_t channel, uint8_t program)) {
  302. // type: 0xC0 ProgramChange
  303. usb_midi_handleProgramChange = fptr;
  304. }
  305. void setHandleAfterTouch(void (*fptr)(uint8_t channel, uint8_t pressure)) {
  306. // type: 0xD0 AfterTouchChannel
  307. usb_midi_handleAfterTouch = fptr;
  308. }
  309. void setHandleAfterTouchChannel(void (*fptr)(uint8_t channel, uint8_t pressure)) {
  310. // type: 0xD0 AfterTouchChannel
  311. usb_midi_handleAfterTouch = fptr;
  312. }
  313. void setHandlePitchChange(void (*fptr)(uint8_t channel, int pitch)) {
  314. // type: 0xE0 PitchBend
  315. usb_midi_handlePitchChange = fptr;
  316. }
  317. void setHandleSysEx(void (*fptr)(const uint8_t *data, uint16_t length, bool complete)) {
  318. // type: 0xF0 SystemExclusive - multiple calls for message bigger than buffer
  319. usb_midi_handleSysExPartial = (void (*)(const uint8_t *, uint16_t, uint8_t))fptr;
  320. }
  321. void setHandleSystemExclusive(void (*fptr)(const uint8_t *data, uint16_t length, bool complete)) {
  322. // type: 0xF0 SystemExclusive - multiple calls for message bigger than buffer
  323. usb_midi_handleSysExPartial = (void (*)(const uint8_t *, uint16_t, uint8_t))fptr;
  324. }
  325. void setHandleSystemExclusive(void (*fptr)(uint8_t *data, unsigned int size)) {
  326. // type: 0xF0 SystemExclusive - single call, message larger than buffer is truncated
  327. usb_midi_handleSysExComplete = fptr;
  328. }
  329. void setHandleTimeCodeQuarterFrame(void (*fptr)(uint8_t data)) {
  330. // type: 0xF1 TimeCodeQuarterFrame
  331. usb_midi_handleTimeCodeQuarterFrame = fptr;
  332. }
  333. void setHandleSongPosition(void (*fptr)(uint16_t beats)) {
  334. // type: 0xF2 SongPosition
  335. usb_midi_handleSongPosition = fptr;
  336. }
  337. void setHandleSongSelect(void (*fptr)(uint8_t songnumber)) {
  338. // type: 0xF3 SongSelect
  339. usb_midi_handleSongSelect = fptr;
  340. }
  341. void setHandleTuneRequest(void (*fptr)(void)) {
  342. // type: 0xF6 TuneRequest
  343. usb_midi_handleTuneRequest = fptr;
  344. }
  345. void setHandleClock(void (*fptr)(void)) {
  346. // type: 0xF8 Clock
  347. usb_midi_handleClock = fptr;
  348. }
  349. void setHandleStart(void (*fptr)(void)) {
  350. // type: 0xFA Start
  351. usb_midi_handleStart = fptr;
  352. }
  353. void setHandleContinue(void (*fptr)(void)) {
  354. // type: 0xFB Continue
  355. usb_midi_handleContinue = fptr;
  356. }
  357. void setHandleStop(void (*fptr)(void)) {
  358. // type: 0xFC Stop
  359. usb_midi_handleStop = fptr;
  360. }
  361. void setHandleActiveSensing(void (*fptr)(void)) {
  362. // type: 0xFE ActiveSensing
  363. usb_midi_handleActiveSensing = fptr;
  364. }
  365. void setHandleSystemReset(void (*fptr)(void)) {
  366. // type: 0xFF SystemReset
  367. usb_midi_handleSystemReset = fptr;
  368. }
  369. void setHandleRealTimeSystem(void (*fptr)(uint8_t realtimebyte)) {
  370. // type: 0xF8-0xFF - if more specific handler not configured
  371. usb_midi_handleRealTimeSystem = fptr;
  372. };
  373. };
  374. extern usb_midi_class usbMIDI;
  375. #endif // __cplusplus
  376. #endif // MIDI_INTERFACE
  377. #endif // USBmidi_h_