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.

преди 7 години
преди 7 години
преди 7 години
преди 7 години
преди 7 години
преди 7 години
преди 7 години
преди 7 години
преди 7 години
преди 7 години
преди 7 години
преди 7 години
преди 7 години
преди 7 години
преди 7 години
преди 7 години
преди 7 години
преди 7 години
преди 7 години
преди 7 години
преди 7 години
преди 7 години
преди 7 години
преди 7 години
преди 7 години
преди 7 години
преди 7 години
преди 7 години
преди 7 години
преди 7 години
преди 7 години
преди 7 години
преди 7 години
преди 7 години
преди 7 години
преди 7 години
преди 7 години
преди 7 години
преди 7 години
преди 7 години
преди 7 години
преди 7 години
преди 7 години
преди 7 години
преди 7 години
преди 7 години
преди 7 години
преди 7 години
преди 7 години
преди 7 години
преди 7 години
преди 7 години
преди 7 години
преди 7 години
преди 7 години
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. /* USB EHCI Host for Teensy 3.6
  2. * Copyright 2017 Paul Stoffregen (paul@pjrc.com)
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the
  6. * "Software"), to deal in the Software without restriction, including
  7. * without limitation the rights to use, copy, modify, merge, publish,
  8. * distribute, sublicense, and/or sell copies of the Software, and to
  9. * permit persons to whom the Software is furnished to do so, subject to
  10. * the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included
  13. * in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  16. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  18. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  19. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  20. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  21. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #include <Arduino.h>
  24. #include "USBHost_t36.h" // Read this header first for key info
  25. #define print USBHost::print_
  26. #define println USBHost::println_
  27. void MIDIDevice::init()
  28. {
  29. contribute_Pipes(mypipes, sizeof(mypipes)/sizeof(Pipe_t));
  30. contribute_Transfers(mytransfers, sizeof(mytransfers)/sizeof(Transfer_t));
  31. contribute_String_Buffers(mystring_bufs, sizeof(mystring_bufs)/sizeof(strbuf_t));
  32. handleNoteOff = NULL;
  33. handleNoteOn = NULL;
  34. handleVelocityChange = NULL;
  35. handleControlChange = NULL;
  36. handleProgramChange = NULL;
  37. handleAfterTouch = NULL;
  38. handlePitchChange = NULL;
  39. handleSysExPartial = NULL;
  40. handleSysExComplete = NULL;
  41. handleTimeCodeQuarterFrame = NULL;
  42. handleSongPosition = NULL;
  43. handleSongSelect = NULL;
  44. handleTuneRequest = NULL;
  45. handleClock = NULL;
  46. handleStart = NULL;
  47. handleContinue = NULL;
  48. handleStop = NULL;
  49. handleActiveSensing = NULL;
  50. handleSystemReset = NULL;
  51. handleRealTimeSystem = NULL;
  52. rx_head = 0;
  53. rx_tail = 0;
  54. rxpipe = NULL;
  55. txpipe = NULL;
  56. driver_ready_for_device(this);
  57. }
  58. // Audio Class-Specific Descriptor Types (audio 1.0, page 99)
  59. // CS_UNDEFINED 0x20
  60. // CS_DEVICE 0x21
  61. // CS_CONFIGURATION 0x22
  62. // CS_STRING 0x23
  63. // CS_INTERFACE 0x24
  64. // CS_ENDPOINT 0x25
  65. // MS Class-Specific Interface Descriptor Subtypes (midi 1.0, page 36)
  66. // MS_DESCRIPTOR_UNDEFINED 0x00
  67. // MS_HEADER 0x01
  68. // MIDI_IN_JACK 0x02
  69. // MIDI_OUT_JACK 0x03
  70. // ELEMENT 0x04
  71. // MS Class-Specific Endpoint Descriptor Subtypes (midi 1.0, page 36)
  72. // DESCRIPTOR_UNDEFINED 0x00
  73. // MS_GENERAL 0x01
  74. // MS MIDI IN and OUT Jack types (midi 1.0, page 36)
  75. // JACK_TYPE_UNDEFINED 0x00
  76. // EMBEDDED 0x01
  77. // EXTERNAL 0x02
  78. // Endpoint Control Selectors (midi 1.0, page 36)
  79. // EP_CONTROL_UNDEFINED 0x00
  80. // ASSOCIATION_CONTROL 0x01
  81. bool MIDIDevice::claim(Device_t *dev, int type, const uint8_t *descriptors, uint32_t len)
  82. {
  83. // only claim at interface level
  84. if (type != 1) return false;
  85. println("MIDIDevice claim this=", (uint32_t)this, HEX);
  86. println("len = ", len);
  87. const uint8_t *p = descriptors;
  88. const uint8_t *end = p + len;
  89. if (p[0] != 9 || p[1] != 4) return false; // interface descriptor
  90. //println(" bInterfaceClass=", p[5]);
  91. //println(" bInterfaceSubClass=", p[6]);
  92. if (p[5] != 1) return false; // bInterfaceClass: 1 = Audio class
  93. if (p[6] != 3) return false; // bInterfaceSubClass: 3 = MIDI
  94. p += 9;
  95. println(" Interface is MIDI");
  96. rx_ep = 0;
  97. tx_ep = 0;
  98. while (p < end) {
  99. len = *p;
  100. if (len < 4) return false; // all audio desc are at least 4 bytes
  101. if (p + len > end) return false; // reject if beyond end of data
  102. uint32_t type = p[1];
  103. print("type: ", type);
  104. println(", len: ", len);
  105. if (type == 4 || type == 11) break; // interface or IAD, not for us
  106. if (type == 0x24) { // 0x24 = Audio CS_INTERFACE, audio 1.0, page 99
  107. uint32_t subtype = p[2];
  108. //println("subtype: ", subtype);
  109. if (subtype == 1) {
  110. // Interface Header, midi 1.0, page 21
  111. println(" MIDI Header (ignored)");
  112. } else if (subtype == 2) {
  113. // MIDI IN Jack, midi 1.0, page 22
  114. println(" MIDI IN Jack (ignored)");
  115. } else if (subtype == 3) {
  116. // MIDI OUT Jack, midi 1.0, page 22
  117. println(" MIDI OUT Jack (ignored)");
  118. } else if (subtype == 4) {
  119. // Element Descriptor, midi 1.0, page 23-24
  120. println(" MIDI Element (ignored)");
  121. } else {
  122. return false; // unknown
  123. }
  124. } else if (type == 5) {
  125. // endpoint descriptor
  126. if (p[0] < 7) return false; // at least 7 bytes
  127. if (p[3] != 2 && p[3] != 3) return false; // must be bulk or interrupt type
  128. println(" MIDI Endpoint: ", p[2], HEX);
  129. switch (p[2] & 0xF0) {
  130. case 0x80:
  131. // IN endpoint
  132. if (rx_ep == 0) {
  133. rx_ep = p[2] & 0x0F;
  134. rx_ep_type = p[3];
  135. rx_size = p[4] | (p[5] << 8);
  136. println(" rx_size = ", rx_size);
  137. }
  138. break;
  139. case 0x00:
  140. // OUT endpoint
  141. if (tx_ep == 0) {
  142. tx_ep = p[2];
  143. tx_ep_type = p[3];
  144. tx_size = p[4] | (p[5] << 8);
  145. println(" tx_size = ", tx_size);
  146. }
  147. break;
  148. default:
  149. return false;
  150. }
  151. } else if (type == 37) {
  152. // MIDI endpoint info, midi 1.0: 6.2.2, page 26
  153. println(" MIDI Endpoint Jack Association (ignored)");
  154. } else {
  155. return false; // unknown
  156. }
  157. p += len;
  158. }
  159. // if an IN endpoint was found, create its pipe
  160. if (rx_ep && rx_size <= MAX_PACKET_SIZE) {
  161. rxpipe = new_Pipe(dev, rx_ep_type, rx_ep, 1, rx_size);
  162. if (rxpipe) {
  163. rxpipe->callback_function = rx_callback;
  164. queue_Data_Transfer(rxpipe, rx_buffer, rx_size, this);
  165. rx_packet_queued = true;
  166. }
  167. } else {
  168. rxpipe = NULL;
  169. }
  170. // if an OUT endpoint was found, create its pipe
  171. if (tx_ep && tx_size <= MAX_PACKET_SIZE) {
  172. txpipe = new_Pipe(dev, tx_ep_type, tx_ep, 0, tx_size);
  173. if (txpipe) {
  174. txpipe->callback_function = tx_callback;
  175. tx1_count = 0;
  176. tx2_count = 0;
  177. }
  178. } else {
  179. txpipe = NULL;
  180. }
  181. rx_head = 0;
  182. rx_tail = 0;
  183. msg_channel = 0;
  184. msg_type = 0;
  185. msg_data1 = 0;
  186. msg_data2 = 0;
  187. msg_sysex_len = 0;
  188. // claim if either pipe created
  189. return (rxpipe || txpipe);
  190. }
  191. void MIDIDevice::rx_callback(const Transfer_t *transfer)
  192. {
  193. if (transfer->driver) {
  194. ((MIDIDevice *)(transfer->driver))->rx_data(transfer);
  195. }
  196. }
  197. void MIDIDevice::tx_callback(const Transfer_t *transfer)
  198. {
  199. if (transfer->driver) {
  200. ((MIDIDevice *)(transfer->driver))->tx_data(transfer);
  201. }
  202. }
  203. void MIDIDevice::rx_data(const Transfer_t *transfer)
  204. {
  205. println("MIDIDevice Receive");
  206. print(" MIDI Data: ");
  207. uint32_t len = (transfer->length - ((transfer->qtd.token >> 16) & 0x7FFF)) >> 2;
  208. print_hexbytes(transfer->buffer, len * 4);
  209. uint32_t head = rx_head;
  210. uint32_t tail = rx_tail;
  211. for (uint32_t i=0; i < len; i++) {
  212. uint32_t msg = rx_buffer[i];
  213. if (msg) {
  214. if (++head >= RX_QUEUE_SIZE) head = 0;
  215. rx_queue[head] = msg;
  216. }
  217. }
  218. rx_head = head;
  219. rx_tail = tail;
  220. uint32_t avail = (head < tail) ? tail - head - 1 : RX_QUEUE_SIZE - 1 - head + tail;
  221. //println("rx_size = ", rx_size);
  222. println("avail = ", avail);
  223. if (avail >= (uint32_t)(rx_size>>2)) {
  224. // enough space to accept another full packet
  225. println("queue another receive packet");
  226. queue_Data_Transfer(rxpipe, rx_buffer, rx_size, this);
  227. rx_packet_queued = true;
  228. } else {
  229. // queue can't accept another packet's data, so leave
  230. // the data waiting on the device until we can accept it
  231. println("wait to receive more packets");
  232. rx_packet_queued = false;
  233. }
  234. }
  235. void MIDIDevice::tx_data(const Transfer_t *transfer)
  236. {
  237. println("MIDIDevice transmit complete");
  238. print(" MIDI Data: ");
  239. print_hexbytes(transfer->buffer, tx_size);
  240. if (transfer->buffer == tx_buffer1) {
  241. tx1_count = 0;
  242. } else if (transfer->buffer == tx_buffer2) {
  243. tx2_count = 0;
  244. }
  245. }
  246. void MIDIDevice::disconnect()
  247. {
  248. // should rx_queue be cleared?
  249. // as-is, the user can still read MIDI messages
  250. // which arrived before the device disconnected.
  251. rxpipe = NULL;
  252. txpipe = NULL;
  253. }
  254. void MIDIDevice::write_packed(uint32_t data)
  255. {
  256. if (!txpipe) return;
  257. uint32_t tx_max = tx_size / 4;
  258. while (1) {
  259. uint32_t tx1 = tx1_count;
  260. uint32_t tx2 = tx2_count;
  261. if (tx1 < tx_max && (tx2 == 0 || tx2 >= tx_max)) {
  262. // use tx_buffer1
  263. tx_buffer1[tx1++] = data;
  264. tx1_count = tx1;
  265. if (tx1 >= tx_max) {
  266. queue_Data_Transfer(txpipe, tx_buffer1, tx_max*4, this);
  267. } else {
  268. // TODO: start a timer, rather than sending the buffer
  269. // before it's full, to make best use of bandwidth
  270. tx1_count = tx_max;
  271. queue_Data_Transfer(txpipe, tx_buffer1, tx_max*4, this);
  272. }
  273. return;
  274. }
  275. if (tx2 < tx_max) {
  276. // use tx_buffer2
  277. tx_buffer2[tx2++] = data;
  278. tx2_count = tx2;
  279. if (tx2 >= tx_max) {
  280. queue_Data_Transfer(txpipe, tx_buffer2, tx_max*4, this);
  281. } else {
  282. // TODO: start a timer, rather than sending the buffer
  283. // before it's full, to make best use of bandwidth
  284. tx2_count = tx_max;
  285. queue_Data_Transfer(txpipe, tx_buffer2, tx_max*4, this);
  286. }
  287. return;
  288. }
  289. }
  290. }
  291. void MIDIDevice::send_sysex_buffer_has_term(const uint8_t *data, uint32_t length, uint8_t cable)
  292. {
  293. cable = (cable & 0x0F) << 4;
  294. while (length > 3) {
  295. write_packed(0x04 | cable | (data[0] << 8) | (data[1] << 16) | (data[2] << 24));
  296. data += 3;
  297. length -= 3;
  298. }
  299. if (length == 3) {
  300. write_packed(0x07 | cable | (data[0] << 8) | (data[1] << 16) | (data[2] << 24));
  301. } else if (length == 2) {
  302. write_packed(0x06 | cable | (data[0] << 8) | (data[1] << 16));
  303. } else if (length == 1) {
  304. write_packed(0x05 | cable | (data[0] << 8));
  305. }
  306. }
  307. void MIDIDevice::send_sysex_add_term_bytes(const uint8_t *data, uint32_t length, uint8_t cable)
  308. {
  309. cable = (cable & 0x0F) << 4;
  310. if (length == 0) {
  311. write_packed(0x06 | cable | (0xF0 << 8) | (0xF7 << 16));
  312. return;
  313. } else if (length == 1) {
  314. write_packed(0x07 | cable | (0xF0 << 8) | (data[0] << 16) | (0xF7 << 24));
  315. return;
  316. } else {
  317. write_packed(0x04 | cable | (0xF0 << 8) | (data[0] << 16) | (data[1] << 24));
  318. data += 2;
  319. length -= 2;
  320. }
  321. while (length >= 3) {
  322. write_packed(0x04 | cable | (data[0] << 8) | (data[1] << 16) | (data[2] << 24));
  323. data += 3;
  324. length -= 3;
  325. }
  326. if (length == 2) {
  327. write_packed(0x07 | cable | (data[0] << 8) | (data[1] << 16) | (0xF7 << 24));
  328. } else if (length == 1) {
  329. write_packed(0x06 | cable | (data[0] << 8) | (0xF7 << 16));
  330. } else {
  331. write_packed(0x05 | cable | (0xF7 << 8));
  332. }
  333. }
  334. bool MIDIDevice::read(uint8_t channel)
  335. {
  336. uint32_t n, head, tail, avail, ch, type1, type2, b1;
  337. head = rx_head;
  338. tail = rx_tail;
  339. if (head == tail) return false;
  340. if (++tail >= RX_QUEUE_SIZE) tail = 0;
  341. n = rx_queue[tail];
  342. rx_tail = tail;
  343. if (!rx_packet_queued && rxpipe) {
  344. avail = (head < tail) ? tail - head - 1 : RX_QUEUE_SIZE - 1 - head + tail;
  345. if (avail >= (uint32_t)(rx_size>>2)) {
  346. __disable_irq();
  347. queue_Data_Transfer(rxpipe, rx_buffer, rx_size, this);
  348. __enable_irq();
  349. }
  350. }
  351. println("read: ", n, HEX);
  352. type1 = n & 15;
  353. type2 = (n >> 12) & 15;
  354. b1 = (n >> 8) & 0xFF;
  355. ch = (b1 & 15) + 1;
  356. msg_cable = (n >> 4) & 15;
  357. if (type1 >= 0x08 && type1 <= 0x0E) {
  358. if (channel && channel != ch) {
  359. // ignore other channels when user wants single channel read
  360. return false;
  361. }
  362. if (type1 == 0x08 && type2 == 0x08) {
  363. msg_type = 0x80; // 0x80 = Note off
  364. if (handleNoteOff) {
  365. (*handleNoteOff)(ch, (n >> 16), (n >> 24));
  366. }
  367. } else
  368. if (type1 == 0x09 && type2 == 0x09) {
  369. if ((n >> 24) > 0) {
  370. msg_type = 0x90; // 0x90 = Note on
  371. if (handleNoteOn) {
  372. (*handleNoteOn)(ch, (n >> 16), (n >> 24));
  373. }
  374. } else {
  375. msg_type = 0x80; // 0x80 = Note off
  376. if (handleNoteOff) {
  377. (*handleNoteOff)(ch, (n >> 16), (n >> 24));
  378. }
  379. }
  380. } else
  381. if (type1 == 0x0A && type2 == 0x0A) {
  382. msg_type = 0xA0; // 0xA0 = AfterTouchPoly
  383. if (handleVelocityChange) {
  384. (*handleVelocityChange)(ch, (n >> 16), (n >> 24));
  385. }
  386. } else
  387. if (type1 == 0x0B && type2 == 0x0B) {
  388. msg_type = 0xB0; // 0xB0 = Control Change
  389. if (handleControlChange) {
  390. (*handleControlChange)(ch, (n >> 16), (n >> 24));
  391. }
  392. } else
  393. if (type1 == 0x0C && type2 == 0x0C) {
  394. msg_type = 0xC0; // 0xC0 = Program Change
  395. if (handleProgramChange) {
  396. (*handleProgramChange)(ch, (n >> 16));
  397. }
  398. } else
  399. if (type1 == 0x0D && type2 == 0x0D) {
  400. msg_type = 0xD0; // 0xD0 = After Touch
  401. if (handleAfterTouch) {
  402. (*handleAfterTouch)(ch, (n >> 16));
  403. }
  404. } else
  405. if (type1 == 0x0E && type2 == 0x0E) {
  406. msg_type = 0xE0; // 0xE0 = Pitch Bend
  407. if (handlePitchChange) {
  408. int value = ((n >> 16) & 0x7F) | ((n >> 17) & 0x3F80);
  409. value -= 8192; // 0 to 16383 --> -8192 to +8191
  410. (*handlePitchChange)(ch, value);
  411. }
  412. } else {
  413. return false;
  414. }
  415. return_message:
  416. msg_channel = ch;
  417. msg_data1 = (n >> 16);
  418. msg_data2 = (n >> 24);
  419. return true;
  420. }
  421. if (type1 == 0x02 || type1 == 0x03 || (type1 == 0x05 && b1 >= 0xF1 && b1 != 0xF7)) {
  422. // system common or system realtime message
  423. system_common_or_realtime:
  424. switch (b1) {
  425. case 0xF1: // usbMIDI.TimeCodeQuarterFrame
  426. if (handleTimeCodeQuarterFrame) {
  427. (*handleTimeCodeQuarterFrame)(n >> 16);
  428. }
  429. break;
  430. case 0xF2: // usbMIDI.SongPosition
  431. if (handleSongPosition) {
  432. (*handleSongPosition)(((n >> 16) & 0x7F) | ((n >> 17) & 0x3F80));
  433. }
  434. break;
  435. case 0xF3: // usbMIDI.SongSelect
  436. if (handleSongSelect) {
  437. (*handleSongSelect)(n >> 16);
  438. }
  439. break;
  440. case 0xF6: // usbMIDI.TuneRequest
  441. if (handleTuneRequest) {
  442. (*handleTuneRequest)();
  443. }
  444. break;
  445. case 0xF8: // usbMIDI.Clock
  446. if (handleClock) {
  447. (*handleClock)();
  448. } else if (handleRealTimeSystem) {
  449. (*handleRealTimeSystem)(0xF8);
  450. }
  451. break;
  452. case 0xFA: // usbMIDI.Start
  453. if (handleStart) {
  454. (*handleStart)();
  455. } else if (handleRealTimeSystem) {
  456. (*handleRealTimeSystem)(0xFA);
  457. }
  458. break;
  459. case 0xFB: // usbMIDI.Continue
  460. if (handleContinue) {
  461. (*handleContinue)();
  462. } else if (handleRealTimeSystem) {
  463. (*handleRealTimeSystem)(0xFB);
  464. }
  465. break;
  466. case 0xFC: // usbMIDI.Stop
  467. if (handleStop) {
  468. (*handleStop)();
  469. } else if (handleRealTimeSystem) {
  470. (*handleRealTimeSystem)(0xFC);
  471. }
  472. break;
  473. case 0xFE: // usbMIDI.ActiveSensing
  474. if (handleActiveSensing) {
  475. (*handleActiveSensing)();
  476. } else if (handleRealTimeSystem) {
  477. (*handleRealTimeSystem)(0xFE);
  478. }
  479. break;
  480. case 0xFF: // usbMIDI.SystemReset
  481. if (handleSystemReset) {
  482. (*handleSystemReset)();
  483. } else if (handleRealTimeSystem) {
  484. (*handleRealTimeSystem)(0xFF);
  485. }
  486. break;
  487. default:
  488. return false; // unknown message, ignore it
  489. }
  490. msg_type = b1;
  491. goto return_message;
  492. }
  493. if (type1 == 0x04) {
  494. sysex_byte(n >> 8);
  495. sysex_byte(n >> 16);
  496. sysex_byte(n >> 24);
  497. return false;
  498. }
  499. if (type1 >= 0x05 && type1 <= 0x07) {
  500. sysex_byte(b1);
  501. // allow for buggy devices which use code 5 to transmit 1 byte at a time
  502. // https://forum.pjrc.com/threads/43450?p=164596&viewfull=1#post164596
  503. if (type1 == 0x05 && b1 != 0xF7) return false;
  504. if (type1 >= 0x06) sysex_byte(n >> 16);
  505. if (type1 == 0x07) sysex_byte(n >> 24);
  506. uint16_t len = msg_sysex_len;
  507. msg_data1 = len;
  508. msg_data2 = len >> 8;
  509. msg_sysex_len = 0;
  510. msg_type = 0xF0; // 0xF0 = SystemExclusive
  511. if (handleSysExPartial) {
  512. (*handleSysExPartial)(msg_sysex, len, 1);
  513. } else if (handleSysExComplete) {
  514. (*handleSysExComplete)(msg_sysex, len);
  515. }
  516. return true;
  517. }
  518. if (type1 == 0x0F) {
  519. if (b1 >= 0xF8) {
  520. goto system_common_or_realtime;
  521. }
  522. if (b1 == 0xF0 || msg_sysex_len > 0) {
  523. // Is this really needed? Mac OS-X does this, but do any devices?
  524. sysex_byte(b1);
  525. }
  526. }
  527. return false;
  528. }
  529. void MIDIDevice::sysex_byte(uint8_t b)
  530. {
  531. if (handleSysExPartial && msg_sysex_len >= SYSEX_MAX_LEN) {
  532. // when buffer is full, send another chunk to partial handler.
  533. (*handleSysExPartial)(msg_sysex, msg_sysex_len, 0);
  534. msg_sysex_len = 0;
  535. }
  536. if (msg_sysex_len < SYSEX_MAX_LEN) {
  537. msg_sysex[msg_sysex_len++] = b;
  538. }
  539. }