PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

390 lines
16KB

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