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.

315 lines
13KB

  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_handleSysEx)(const uint8_t *data, uint16_t length, uint8_t complete);
  84. extern void (*usb_midi_handleRealTimeSystem)(uint8_t rtb);
  85. extern void (*usb_midi_handleTimeCodeQuarterFrame)(uint16_t data);
  86. #ifdef __cplusplus
  87. }
  88. #endif
  89. // To test receiving on Linux, run "aseqdump -l" to list sequencer devices.
  90. //
  91. // Port Client name Port name
  92. // 0:0 System Timer
  93. // 0:1 System Announce
  94. // 14:0 Midi Through Midi Through Port-0
  95. // 24:0 Teensy MIDI Teensy MIDI MIDI 1
  96. // 28:0 AKM320 AKM320 MIDI 1
  97. //
  98. // Then run "aseqdump -p 24:0" to view the MIDI messages.
  99. //
  100. // Waiting for data. Press Ctrl+C to end.
  101. // Source Event Ch Data
  102. // 24:0 Note on 0, note 61, velocity 99
  103. // 24:0 Note off 0, note 61, velocity 0
  104. // 24:0 Note on 0, note 62, velocity 99
  105. // 24:0 Note off 0, note 62, velocity 0
  106. // 24:0 Note on 0, note 64, velocity 99
  107. // 24:0 Note off 0, note 64, velocity 0
  108. // C++ interface
  109. #ifdef __cplusplus
  110. class usb_midi_class
  111. {
  112. public:
  113. void begin(void) { }
  114. void end(void) { }
  115. void sendNoteOff(uint8_t note, uint8_t velocity, uint8_t channel, uint8_t cable=0) __attribute__((always_inline)) {
  116. send(0x80, note, velocity, channel, cable);
  117. };
  118. void sendNoteOn(uint8_t note, uint8_t velocity, uint8_t channel, uint8_t cable=0) __attribute__((always_inline)) {
  119. send(0x90, note, velocity, channel, cable);
  120. };
  121. void sendPolyPressure(uint8_t note, uint8_t pressure, uint8_t channel, uint8_t cable=0) __attribute__((always_inline)) {
  122. send(0xA0, note, pressure, channel, cable);
  123. };
  124. void sendAfterTouch(uint8_t note, uint8_t pressure, uint8_t channel, uint8_t cable=0) __attribute__((always_inline)) {
  125. send(0xA0, note, pressure, channel, cable);
  126. };
  127. void sendControlChange(uint8_t control, uint8_t value, uint8_t channel, uint8_t cable=0) __attribute__((always_inline)) {
  128. send(0xB0, control, value, channel, cable);
  129. };
  130. void sendProgramChange(uint8_t program, uint8_t channel, uint8_t cable=0) __attribute__((always_inline)) {
  131. send(0xC0, program, 0, channel, cable);
  132. };
  133. void sendAfterTouch(uint8_t pressure, uint8_t channel, uint8_t cable=0) __attribute__((always_inline)) {
  134. send(0xD0, pressure, 0, channel, cable);
  135. };
  136. void sendPitchBend(uint16_t value, uint8_t channel, uint8_t cable=0) __attribute__((always_inline)) {
  137. // MIDI 4.3 takes -8192 to +8191. We take 0 to 16383
  138. // MIDI 4.3 also has version that takes float -1.0 to +1.0
  139. send(0xE0, value, value >> 7, channel, cable);
  140. };
  141. void sendSysEx(uint32_t length, const uint8_t *data, bool hasTerm=false, uint8_t cable=0) __attribute__((always_inline)) {
  142. if (cable >= MIDI_NUM_CABLES) return;
  143. if (hasTerm) {
  144. if (length > 2) {
  145. usb_midi_send_sysex(data + 1, length - 2, cable);
  146. }
  147. } else {
  148. usb_midi_send_sysex(data, length, cable);
  149. }
  150. };
  151. void sendRealTime(uint8_t type, uint8_t cable=0) __attribute__((always_inline)) __attribute__((always_inline)) {
  152. switch (type) {
  153. case 0xF8: // Clock
  154. case 0xFA: // Start
  155. case 0xFB: // Continue
  156. case 0xFC: // Stop
  157. case 0xFE: // ActiveSensing
  158. case 0xFF: // SystemReset
  159. send(type, 0, 0, 0, cable);
  160. break;
  161. default: // Invalid Real Time marker
  162. break;
  163. }
  164. };
  165. void sendTimeCodeQuarterFrame(uint8_t type, uint8_t value, uint8_t cable=0) __attribute__((always_inline)) __attribute__((always_inline)) {
  166. send(0xF1, ((type & 0x07) << 4) | (value & 0x0F), 0, 0, cable);
  167. };
  168. //void sendTimeCodeQuarterFrame(uint8_t data, uint8_t cable=0) __attribute__((always_inline)) {
  169. // MIDI 4.3 has this, but we can't implement with cable param
  170. //send(0xF1, data, 0, 0, cable);
  171. //};
  172. void sendSongPosition(uint16_t beats, uint8_t cable=0) __attribute__((always_inline)) {
  173. send(0xF2, beats, beats >> 7, 0, cable);
  174. };
  175. void sendSongSelect(uint8_t song, uint8_t cable=0) __attribute__((always_inline)) {
  176. send(0xF4, song, 0, 0, cable);
  177. };
  178. void sendTuneRequest(uint8_t cable=0) __attribute__((always_inline)) {
  179. send(0xF6, 0, 0, 0, cable);
  180. };
  181. void beginRpn(uint16_t number, uint8_t channel, uint8_t cable=0) __attribute__((always_inline)) {
  182. sendControlChange(101, number >> 7, channel, cable);
  183. sendControlChange(100, number, channel, cable);
  184. };
  185. void sendRpnValue(uint16_t value, uint8_t channel, uint8_t cable=0) __attribute__((always_inline)) {
  186. sendControlChange(6, value >> 7, channel, cable);
  187. sendControlChange(38, value, channel, cable);
  188. };
  189. void sendRpnValue(uint8_t msb, uint8_t lsb, uint8_t channel, uint8_t cable=0) __attribute__((always_inline)) {
  190. sendControlChange(6, msb, channel, cable);
  191. sendControlChange(38, lsb, channel, cable);
  192. };
  193. void sendRpnIncrement(uint8_t amount, uint8_t channel, uint8_t cable=0) __attribute__((always_inline)) {
  194. sendControlChange(96, amount, channel, cable);
  195. };
  196. void sendRpnDecrement(uint8_t amount, uint8_t channel, uint8_t cable=0) __attribute__((always_inline)) {
  197. sendControlChange(97, amount, channel, cable);
  198. };
  199. void endRpn(uint8_t channel, uint8_t cable=0) __attribute__((always_inline)) {
  200. sendControlChange(101, 0x7F, channel, cable);
  201. sendControlChange(100, 0x7F, channel, cable);
  202. };
  203. void beginNrpn(uint16_t number, uint8_t channel, uint8_t cable=0) __attribute__((always_inline)) {
  204. sendControlChange(99, number >> 7, channel, cable);
  205. sendControlChange(98, number, channel, cable);
  206. }
  207. void sendNrpnValue(uint16_t value, uint8_t channel, uint8_t cable=0) __attribute__((always_inline)) {
  208. sendControlChange(6, value >> 7, channel, cable);
  209. sendControlChange(38, value, channel, cable);
  210. }
  211. void sendNrpnValue(uint8_t msb, uint8_t lsb, uint8_t channel, uint8_t cable=0) __attribute__((always_inline)) {
  212. sendControlChange(6, msb, channel, cable);
  213. sendControlChange(38, lsb, channel, cable);
  214. }
  215. void sendNrpnIncrement(uint8_t amount, uint8_t channel, uint8_t cable=0) __attribute__((always_inline)) {
  216. sendControlChange(96, amount, channel, cable);
  217. }
  218. void sendNrpnDecrement(uint8_t amount, uint8_t channel, uint8_t cable=0) __attribute__((always_inline)) {
  219. sendControlChange(97, amount, channel, cable);
  220. }
  221. void endNrpn(uint8_t channel, uint8_t cable=0) __attribute__((always_inline)) {
  222. sendControlChange(99, 0x7F, channel, cable);
  223. sendControlChange(98, 0x7F, channel, cable);
  224. }
  225. void send(uint8_t type, uint8_t data1, uint8_t data2, uint8_t channel, uint8_t cable) __attribute__((always_inline)) {
  226. if (cable >= MIDI_NUM_CABLES) return;
  227. if (type < 0xF0) {
  228. if (type < 0x80) return;
  229. type &= 0xF0;
  230. usb_midi_write_packed((type << 8) | (type >> 4) | ((cable & 0x0F) << 4)
  231. | (((channel - 1) & 0x0F) << 8) | ((data1 & 0x7F) << 16)
  232. | ((data2 & 0x7F) << 24));
  233. } else {
  234. usb_midi_write_packed((type << 8) | 0x0F | ((cable & 0x0F) << 4)
  235. | ((data1 & 0x7F) << 16) | ((data2 & 0x7F) << 24));
  236. }
  237. };
  238. void send_now(void) __attribute__((always_inline)) {
  239. usb_midi_flush_output();
  240. };
  241. uint8_t analog2velocity(uint16_t val, uint8_t range);
  242. bool read(uint8_t channel=0) __attribute__((always_inline)) {
  243. return usb_midi_read(channel);
  244. };
  245. inline uint8_t getType(void) __attribute__((always_inline)) {
  246. return usb_midi_msg_type;
  247. };
  248. inline uint8_t getCable(void) __attribute__((always_inline)) {
  249. return usb_midi_msg_cable;
  250. };
  251. inline uint8_t getChannel(void) __attribute__((always_inline)) {
  252. return usb_midi_msg_channel;
  253. };
  254. inline uint8_t getData1(void) __attribute__((always_inline)) {
  255. return usb_midi_msg_data1;
  256. };
  257. inline uint8_t getData2(void) __attribute__((always_inline)) {
  258. return usb_midi_msg_data2;
  259. };
  260. inline uint8_t * getSysExArray(void) __attribute__((always_inline)) {
  261. return usb_midi_msg_sysex;
  262. };
  263. inline void setHandleNoteOff(void (*fptr)(uint8_t channel, uint8_t note, uint8_t velocity)) {
  264. usb_midi_handleNoteOff = fptr;
  265. };
  266. inline void setHandleNoteOn(void (*fptr)(uint8_t channel, uint8_t note, uint8_t velocity)) {
  267. usb_midi_handleNoteOn = fptr;
  268. };
  269. inline void setHandleVelocityChange(void (*fptr)(uint8_t channel, uint8_t note, uint8_t velocity)) {
  270. usb_midi_handleVelocityChange = fptr;
  271. };
  272. inline void setHandleControlChange(void (*fptr)(uint8_t channel, uint8_t control, uint8_t value)) {
  273. usb_midi_handleControlChange = fptr;
  274. };
  275. inline void setHandleProgramChange(void (*fptr)(uint8_t channel, uint8_t program)) {
  276. usb_midi_handleProgramChange = fptr;
  277. };
  278. inline void setHandleAfterTouch(void (*fptr)(uint8_t channel, uint8_t pressure)) {
  279. usb_midi_handleAfterTouch = fptr;
  280. };
  281. inline void setHandlePitchChange(void (*fptr)(uint8_t channel, int pitch)) {
  282. usb_midi_handlePitchChange = fptr;
  283. };
  284. inline void setHandleSysEx(void (*fptr)(const uint8_t *data, uint16_t length, bool complete)) {
  285. usb_midi_handleSysEx = (void (*)(const uint8_t *, uint16_t, uint8_t))fptr;
  286. }
  287. inline void setHandleRealTimeSystem(void (*fptr)(uint8_t realtimebyte)) {
  288. usb_midi_handleRealTimeSystem = fptr;
  289. };
  290. inline void setHandleTimeCodeQuarterFrame(void (*fptr)(uint16_t data)) {
  291. usb_midi_handleTimeCodeQuarterFrame = fptr;
  292. };
  293. private:
  294. };
  295. extern usb_midi_class usbMIDI;
  296. #endif // __cplusplus
  297. #endif // MIDI_INTERFACE
  298. #endif // USBmidi_h_