Teensy 4.1 core updated for C++20
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

413 rindas
17KB

  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. /*
  36. These were originally meant to allow programs written for
  37. Francois Best's MIDI library to be easily used with
  38. Teensy's usbMIDI which implements the same API. However,
  39. the MIDI library definitions have changed, so these names
  40. now conflict. They've never been documented (the PJRC web
  41. page documents usbMIDI.getType() in numbers) so they are
  42. now commented out so usbMIDI and the MIDI library can be
  43. used together without conflict.
  44. #define NoteOff 0
  45. #define NoteOn 1
  46. #define AfterTouchPoly 2
  47. #define ControlChange 3
  48. #define ProgramChange 4
  49. #define AfterTouchChannel 5
  50. #define PitchBend 6
  51. #define SystemExclusive 7
  52. */
  53. // maximum sysex length we can receive
  54. #if defined(__MKL26Z64__) || defined(__MK20DX128__)
  55. #define USB_MIDI_SYSEX_MAX 60
  56. #else
  57. #define USB_MIDI_SYSEX_MAX 290
  58. #endif
  59. // C language implementation
  60. #ifdef __cplusplus
  61. extern "C" {
  62. #endif
  63. void usb_midi_write_packed(uint32_t n);
  64. void usb_midi_send_sysex(const uint8_t *data, uint32_t length, uint8_t cable);
  65. void usb_midi_flush_output(void);
  66. int usb_midi_read(uint32_t channel);
  67. uint32_t usb_midi_available(void);
  68. uint32_t usb_midi_read_message(void);
  69. extern uint8_t usb_midi_msg_cable;
  70. extern uint8_t usb_midi_msg_channel;
  71. extern uint8_t usb_midi_msg_type;
  72. extern uint8_t usb_midi_msg_data1;
  73. extern uint8_t usb_midi_msg_data2;
  74. extern uint8_t usb_midi_msg_sysex[USB_MIDI_SYSEX_MAX];
  75. extern uint16_t usb_midi_msg_sysex_len;
  76. extern void (*usb_midi_handleNoteOff)(uint8_t ch, uint8_t note, uint8_t vel);
  77. extern void (*usb_midi_handleNoteOn)(uint8_t ch, uint8_t note, uint8_t vel);
  78. extern void (*usb_midi_handleVelocityChange)(uint8_t ch, uint8_t note, uint8_t vel);
  79. extern void (*usb_midi_handleControlChange)(uint8_t ch, uint8_t control, uint8_t value);
  80. extern void (*usb_midi_handleProgramChange)(uint8_t ch, uint8_t program);
  81. extern void (*usb_midi_handleAfterTouch)(uint8_t ch, uint8_t pressure);
  82. extern void (*usb_midi_handlePitchChange)(uint8_t ch, int pitch);
  83. extern void (*usb_midi_handleSysExPartial)(const uint8_t *data, uint16_t length, uint8_t complete);
  84. extern void (*usb_midi_handleSysExComplete)(uint8_t *data, unsigned int size);
  85. extern void (*usb_midi_handleTimeCodeQuarterFrame)(uint8_t data);
  86. extern void (*usb_midi_handleSongPosition)(uint16_t beats);
  87. extern void (*usb_midi_handleSongSelect)(uint8_t songnumber);
  88. extern void (*usb_midi_handleTuneRequest)(void);
  89. extern void (*usb_midi_handleClock)(void);
  90. extern void (*usb_midi_handleStart)(void);
  91. extern void (*usb_midi_handleContinue)(void);
  92. extern void (*usb_midi_handleStop)(void);
  93. extern void (*usb_midi_handleActiveSensing)(void);
  94. extern void (*usb_midi_handleSystemReset)(void);
  95. extern void (*usb_midi_handleRealTimeSystem)(uint8_t rtb);
  96. #ifdef __cplusplus
  97. }
  98. #endif
  99. // To test receiving on Linux, run "aseqdump -l" to list sequencer devices.
  100. //
  101. // Port Client name Port name
  102. // 0:0 System Timer
  103. // 0:1 System Announce
  104. // 14:0 Midi Through Midi Through Port-0
  105. // 24:0 Teensy MIDI Teensy MIDI MIDI 1
  106. // 28:0 AKM320 AKM320 MIDI 1
  107. //
  108. // Then run "aseqdump -p 24:0" to view the MIDI messages.
  109. //
  110. // Waiting for data. Press Ctrl+C to end.
  111. // Source Event Ch Data
  112. // 24:0 Note on 0, note 61, velocity 99
  113. // 24:0 Note off 0, note 61, velocity 0
  114. // 24:0 Note on 0, note 62, velocity 99
  115. // 24:0 Note off 0, note 62, velocity 0
  116. // 24:0 Note on 0, note 64, velocity 99
  117. // 24:0 Note off 0, note 64, velocity 0
  118. // C++ interface
  119. #ifdef __cplusplus
  120. class usb_midi_class
  121. {
  122. public:
  123. // Message type names for compatibility with Arduino MIDI library 4.3.1
  124. enum MidiType {
  125. InvalidType = 0x00, // For notifying errors
  126. NoteOff = 0x80, // Note Off
  127. NoteOn = 0x90, // Note On
  128. AfterTouchPoly = 0xA0, // Polyphonic AfterTouch
  129. ControlChange = 0xB0, // Control Change / Channel Mode
  130. ProgramChange = 0xC0, // Program Change
  131. AfterTouchChannel = 0xD0, // Channel (monophonic) AfterTouch
  132. PitchBend = 0xE0, // Pitch Bend
  133. SystemExclusive = 0xF0, // System Exclusive
  134. TimeCodeQuarterFrame = 0xF1, // System Common - MIDI Time Code Quarter Frame
  135. SongPosition = 0xF2, // System Common - Song Position Pointer
  136. SongSelect = 0xF3, // System Common - Song Select
  137. TuneRequest = 0xF6, // System Common - Tune Request
  138. Clock = 0xF8, // System Real Time - Timing Clock
  139. Start = 0xFA, // System Real Time - Start
  140. Continue = 0xFB, // System Real Time - Continue
  141. Stop = 0xFC, // System Real Time - Stop
  142. ActiveSensing = 0xFE, // System Real Time - Active Sensing
  143. SystemReset = 0xFF, // System Real Time - System Reset
  144. };
  145. void begin(void) { }
  146. void end(void) { }
  147. void sendNoteOff(uint8_t note, uint8_t velocity, uint8_t channel, uint8_t cable=0) __attribute__((always_inline)) {
  148. send(0x80, note, velocity, channel, cable);
  149. }
  150. void sendNoteOn(uint8_t note, uint8_t velocity, uint8_t channel, uint8_t cable=0) __attribute__((always_inline)) {
  151. send(0x90, note, velocity, channel, cable);
  152. }
  153. void sendPolyPressure(uint8_t note, uint8_t pressure, uint8_t channel, uint8_t cable=0) __attribute__((always_inline)) {
  154. send(0xA0, note, pressure, channel, cable);
  155. }
  156. void sendAfterTouch(uint8_t note, uint8_t pressure, uint8_t channel, uint8_t cable=0) __attribute__((always_inline)) {
  157. send(0xA0, note, pressure, channel, cable);
  158. }
  159. void sendControlChange(uint8_t control, uint8_t value, uint8_t channel, uint8_t cable=0) __attribute__((always_inline)) {
  160. send(0xB0, control, value, channel, cable);
  161. }
  162. void sendProgramChange(uint8_t program, uint8_t channel, uint8_t cable=0) __attribute__((always_inline)) {
  163. send(0xC0, program, 0, channel, cable);
  164. }
  165. void sendAfterTouch(uint8_t pressure, uint8_t channel, uint8_t cable=0) __attribute__((always_inline)) {
  166. send(0xD0, pressure, 0, channel, cable);
  167. }
  168. void sendPitchBend(uint16_t value, uint8_t channel, uint8_t cable=0) __attribute__((always_inline)) {
  169. // MIDI 4.3 takes -8192 to +8191. We take 0 to 16383
  170. // MIDI 4.3 also has version that takes float -1.0 to +1.0
  171. send(0xE0, value, value >> 7, channel, cable);
  172. }
  173. void sendSysEx(uint32_t length, const uint8_t *data, bool hasTerm=false, uint8_t cable=0) __attribute__((always_inline)) {
  174. if (cable >= MIDI_NUM_CABLES) return;
  175. if (hasTerm) {
  176. if (length > 2) {
  177. usb_midi_send_sysex(data + 1, length - 2, cable);
  178. }
  179. } else {
  180. usb_midi_send_sysex(data, length, cable);
  181. }
  182. }
  183. void sendRealTime(uint8_t type, uint8_t cable=0) __attribute__((always_inline)) __attribute__((always_inline)) {
  184. switch (type) {
  185. case 0xF8: // Clock
  186. case 0xFA: // Start
  187. case 0xFB: // Continue
  188. case 0xFC: // Stop
  189. case 0xFE: // ActiveSensing
  190. case 0xFF: // SystemReset
  191. send(type, 0, 0, 0, cable);
  192. break;
  193. default: // Invalid Real Time marker
  194. break;
  195. }
  196. }
  197. void sendTimeCodeQuarterFrame(uint8_t type, uint8_t value, uint8_t cable=0) __attribute__((always_inline)) __attribute__((always_inline)) {
  198. send(0xF1, ((type & 0x07) << 4) | (value & 0x0F), 0, 0, cable);
  199. }
  200. //void sendTimeCodeQuarterFrame(uint8_t data, uint8_t cable=0) __attribute__((always_inline)) {
  201. // MIDI 4.3 has this, but we can't implement with cable param
  202. //send(0xF1, data, 0, 0, cable);
  203. //}
  204. void sendSongPosition(uint16_t beats, uint8_t cable=0) __attribute__((always_inline)) {
  205. send(0xF2, beats, beats >> 7, 0, cable);
  206. }
  207. void sendSongSelect(uint8_t song, uint8_t cable=0) __attribute__((always_inline)) {
  208. send(0xF3, song, 0, 0, cable);
  209. }
  210. void sendTuneRequest(uint8_t cable=0) __attribute__((always_inline)) {
  211. send(0xF6, 0, 0, 0, cable);
  212. }
  213. void beginRpn(uint16_t number, uint8_t channel, uint8_t cable=0) __attribute__((always_inline)) {
  214. sendControlChange(101, number >> 7, channel, cable);
  215. sendControlChange(100, number, channel, cable);
  216. }
  217. void sendRpnValue(uint16_t value, uint8_t channel, uint8_t cable=0) __attribute__((always_inline)) {
  218. sendControlChange(6, value >> 7, channel, cable);
  219. sendControlChange(38, value, channel, cable);
  220. }
  221. void sendRpnValue(uint8_t msb, uint8_t lsb, uint8_t channel, uint8_t cable=0) __attribute__((always_inline)) {
  222. sendControlChange(6, msb, channel, cable);
  223. sendControlChange(38, lsb, channel, cable);
  224. }
  225. void sendRpnIncrement(uint8_t amount, uint8_t channel, uint8_t cable=0) __attribute__((always_inline)) {
  226. sendControlChange(96, amount, channel, cable);
  227. }
  228. void sendRpnDecrement(uint8_t amount, uint8_t channel, uint8_t cable=0) __attribute__((always_inline)) {
  229. sendControlChange(97, amount, channel, cable);
  230. }
  231. void endRpn(uint8_t channel, uint8_t cable=0) __attribute__((always_inline)) {
  232. sendControlChange(101, 0x7F, channel, cable);
  233. sendControlChange(100, 0x7F, channel, cable);
  234. }
  235. void beginNrpn(uint16_t number, uint8_t channel, uint8_t cable=0) __attribute__((always_inline)) {
  236. sendControlChange(99, number >> 7, channel, cable);
  237. sendControlChange(98, number, channel, cable);
  238. }
  239. void sendNrpnValue(uint16_t value, uint8_t channel, uint8_t cable=0) __attribute__((always_inline)) {
  240. sendControlChange(6, value >> 7, channel, cable);
  241. sendControlChange(38, value, channel, cable);
  242. }
  243. void sendNrpnValue(uint8_t msb, uint8_t lsb, uint8_t channel, uint8_t cable=0) __attribute__((always_inline)) {
  244. sendControlChange(6, msb, channel, cable);
  245. sendControlChange(38, lsb, channel, cable);
  246. }
  247. void sendNrpnIncrement(uint8_t amount, uint8_t channel, uint8_t cable=0) __attribute__((always_inline)) {
  248. sendControlChange(96, amount, channel, cable);
  249. }
  250. void sendNrpnDecrement(uint8_t amount, uint8_t channel, uint8_t cable=0) __attribute__((always_inline)) {
  251. sendControlChange(97, amount, channel, cable);
  252. }
  253. void endNrpn(uint8_t channel, uint8_t cable=0) __attribute__((always_inline)) {
  254. sendControlChange(99, 0x7F, channel, cable);
  255. sendControlChange(98, 0x7F, channel, cable);
  256. }
  257. void send(uint8_t type, uint8_t data1, uint8_t data2, uint8_t channel, uint8_t cable) __attribute__((always_inline)) {
  258. if (cable >= MIDI_NUM_CABLES) return;
  259. if (type < 0xF0) {
  260. if (type < 0x80) return;
  261. type &= 0xF0;
  262. usb_midi_write_packed((type << 8) | (type >> 4) | ((cable & 0x0F) << 4)
  263. | (((channel - 1) & 0x0F) << 8) | ((data1 & 0x7F) << 16)
  264. | ((data2 & 0x7F) << 24));
  265. } else if (type >= 0xF8 || type == 0xF6) {
  266. usb_midi_write_packed((type << 8) | 0x0F | ((cable & 0x0F) << 4));
  267. } else if (type == 0xF1 || type == 0xF3) {
  268. usb_midi_write_packed((type << 8) | 0x02 | ((cable & 0x0F) << 4)
  269. | ((data1 & 0x7F) << 16));
  270. } else if (type == 0xF2) {
  271. usb_midi_write_packed((type << 8) | 0x03 | ((cable & 0x0F) << 4)
  272. | ((data1 & 0x7F) << 16) | ((data2 & 0x7F) << 24));
  273. }
  274. }
  275. void send_now(void) __attribute__((always_inline)) {
  276. usb_midi_flush_output();
  277. }
  278. uint8_t analog2velocity(uint16_t val, uint8_t range);
  279. bool read(uint8_t channel=0) __attribute__((always_inline)) {
  280. return usb_midi_read(channel);
  281. }
  282. uint8_t getType(void) __attribute__((always_inline)) {
  283. return usb_midi_msg_type;
  284. }
  285. uint8_t getCable(void) __attribute__((always_inline)) {
  286. return usb_midi_msg_cable;
  287. }
  288. uint8_t getChannel(void) __attribute__((always_inline)) {
  289. return usb_midi_msg_channel;
  290. }
  291. uint8_t getData1(void) __attribute__((always_inline)) {
  292. return usb_midi_msg_data1;
  293. }
  294. uint8_t getData2(void) __attribute__((always_inline)) {
  295. return usb_midi_msg_data2;
  296. }
  297. uint8_t * getSysExArray(void) __attribute__((always_inline)) {
  298. return usb_midi_msg_sysex;
  299. }
  300. void setHandleNoteOff(void (*fptr)(uint8_t channel, uint8_t note, uint8_t velocity)) {
  301. // type: 0x80 NoteOff
  302. usb_midi_handleNoteOff = fptr;
  303. }
  304. void setHandleNoteOn(void (*fptr)(uint8_t channel, uint8_t note, uint8_t velocity)) {
  305. // type: 0x90 NoteOn
  306. usb_midi_handleNoteOn = fptr;
  307. }
  308. void setHandleVelocityChange(void (*fptr)(uint8_t channel, uint8_t note, uint8_t velocity)) {
  309. // type: 0xA0 AfterTouchPoly
  310. usb_midi_handleVelocityChange = fptr;
  311. }
  312. void setHandleAfterTouchPoly(void (*fptr)(uint8_t channel, uint8_t note, uint8_t pressure)) {
  313. // type: 0xA0 AfterTouchPoly
  314. usb_midi_handleVelocityChange = fptr;
  315. }
  316. void setHandleControlChange(void (*fptr)(uint8_t channel, uint8_t control, uint8_t value)) {
  317. // type: 0xB0 ControlChange
  318. usb_midi_handleControlChange = fptr;
  319. }
  320. void setHandleProgramChange(void (*fptr)(uint8_t channel, uint8_t program)) {
  321. // type: 0xC0 ProgramChange
  322. usb_midi_handleProgramChange = fptr;
  323. }
  324. void setHandleAfterTouch(void (*fptr)(uint8_t channel, uint8_t pressure)) {
  325. // type: 0xD0 AfterTouchChannel
  326. usb_midi_handleAfterTouch = fptr;
  327. }
  328. void setHandleAfterTouchChannel(void (*fptr)(uint8_t channel, uint8_t pressure)) {
  329. // type: 0xD0 AfterTouchChannel
  330. usb_midi_handleAfterTouch = fptr;
  331. }
  332. void setHandlePitchChange(void (*fptr)(uint8_t channel, int pitch)) {
  333. // type: 0xE0 PitchBend
  334. usb_midi_handlePitchChange = fptr;
  335. }
  336. void setHandleSysEx(void (*fptr)(const uint8_t *data, uint16_t length, bool complete)) {
  337. // type: 0xF0 SystemExclusive - multiple calls for message bigger than buffer
  338. usb_midi_handleSysExPartial = (void (*)(const uint8_t *, uint16_t, uint8_t))fptr;
  339. }
  340. void setHandleSystemExclusive(void (*fptr)(const uint8_t *data, uint16_t length, bool complete)) {
  341. // type: 0xF0 SystemExclusive - multiple calls for message bigger than buffer
  342. usb_midi_handleSysExPartial = (void (*)(const uint8_t *, uint16_t, uint8_t))fptr;
  343. }
  344. void setHandleSystemExclusive(void (*fptr)(uint8_t *data, unsigned int size)) {
  345. // type: 0xF0 SystemExclusive - single call, message larger than buffer is truncated
  346. usb_midi_handleSysExComplete = fptr;
  347. }
  348. void setHandleTimeCodeQuarterFrame(void (*fptr)(uint8_t data)) {
  349. // type: 0xF1 TimeCodeQuarterFrame
  350. usb_midi_handleTimeCodeQuarterFrame = fptr;
  351. }
  352. void setHandleSongPosition(void (*fptr)(uint16_t beats)) {
  353. // type: 0xF2 SongPosition
  354. usb_midi_handleSongPosition = fptr;
  355. }
  356. void setHandleSongSelect(void (*fptr)(uint8_t songnumber)) {
  357. // type: 0xF3 SongSelect
  358. usb_midi_handleSongSelect = fptr;
  359. }
  360. void setHandleTuneRequest(void (*fptr)(void)) {
  361. // type: 0xF6 TuneRequest
  362. usb_midi_handleTuneRequest = fptr;
  363. }
  364. void setHandleClock(void (*fptr)(void)) {
  365. // type: 0xF8 Clock
  366. usb_midi_handleClock = fptr;
  367. }
  368. void setHandleStart(void (*fptr)(void)) {
  369. // type: 0xFA Start
  370. usb_midi_handleStart = fptr;
  371. }
  372. void setHandleContinue(void (*fptr)(void)) {
  373. // type: 0xFB Continue
  374. usb_midi_handleContinue = fptr;
  375. }
  376. void setHandleStop(void (*fptr)(void)) {
  377. // type: 0xFC Stop
  378. usb_midi_handleStop = fptr;
  379. }
  380. void setHandleActiveSensing(void (*fptr)(void)) {
  381. // type: 0xFE ActiveSensing
  382. usb_midi_handleActiveSensing = fptr;
  383. }
  384. void setHandleSystemReset(void (*fptr)(void)) {
  385. // type: 0xFF SystemReset
  386. usb_midi_handleSystemReset = fptr;
  387. }
  388. void setHandleRealTimeSystem(void (*fptr)(uint8_t realtimebyte)) {
  389. // type: 0xF8-0xFF - if more specific handler not configured
  390. usb_midi_handleRealTimeSystem = fptr;
  391. };
  392. };
  393. extern usb_midi_class usbMIDI;
  394. #endif // __cplusplus
  395. #endif // MIDI_INTERFACE
  396. #endif // USBmidi_h_