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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  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 MIDIDeviceBase::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 MIDIDeviceBase::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. bool ismidi = false;
  93. if (p[5] == 1 && p[6] == 3) {
  94. println(" Interface is MIDI"); // p[5] is bInterfaceClass: 1 = Audio class
  95. ismidi = true; // p[6] is bInterfaceSubClass: 3 = MIDI
  96. } else {
  97. if (p[5] >= 2 && p[5] <= 18) return false; // definitely not MIDI
  98. // Yamaha uses vendor specific class, but can be
  99. // identified as MIDI from CS_INTERFACE descriptors.
  100. // https://forum.pjrc.com/threads/55142?p=199162&viewfull=1#post199162
  101. println(" Interface is unknown (might be Yahama)");
  102. }
  103. p += 9;
  104. rx_ep = 0;
  105. tx_ep = 0;
  106. while (p < end) {
  107. len = *p;
  108. if (len < 4) return false; // all audio desc are at least 4 bytes
  109. if (p + len > end) return false; // reject if beyond end of data
  110. uint32_t type = p[1];
  111. print("type: ", type);
  112. println(", len: ", len);
  113. if (type == 4 || type == 11) break; // interface or IAD, not for us
  114. if (type == 0x24) { // 0x24 = Audio CS_INTERFACE, audio 1.0, page 99
  115. uint32_t subtype = p[2];
  116. //println("subtype: ", subtype);
  117. if (subtype == 1) {
  118. // Interface Header, midi 1.0, page 21
  119. println(" MIDI Header (ignored)");
  120. ismidi = true;
  121. } else if (subtype == 2) {
  122. // MIDI IN Jack, midi 1.0, page 22
  123. println(" MIDI IN Jack (ignored)");
  124. ismidi = true;
  125. } else if (subtype == 3) {
  126. // MIDI OUT Jack, midi 1.0, page 22
  127. println(" MIDI OUT Jack (ignored)");
  128. ismidi = true;
  129. } else if (subtype == 4) {
  130. // Element Descriptor, midi 1.0, page 23-24
  131. println(" MIDI Element (ignored)");
  132. ismidi = true;
  133. } else if (subtype == 0xF1 && p[3] == 2) {
  134. // see Linux sound/usb/quirks.c create_roland_midi_quirk()
  135. println(" Roland vendor-specific (ignored)");
  136. ismidi = true;
  137. } else {
  138. println(" Unknown MIDI CS_INTERFACE descriptor!");
  139. return false; // unknown
  140. }
  141. } else if (type == 5) {
  142. // endpoint descriptor
  143. if (p[0] < 7) return false; // at least 7 bytes
  144. if (p[3] != 2 && p[3] != 3) return false; // must be bulk or interrupt type
  145. println(" MIDI Endpoint: ", p[2], HEX);
  146. switch (p[2] & 0xF0) {
  147. case 0x80:
  148. // IN endpoint
  149. if (rx_ep == 0) {
  150. rx_ep = p[2] & 0x0F;
  151. rx_ep_type = p[3];
  152. rx_size = p[4] | (p[5] << 8);
  153. println(" rx_size = ", rx_size);
  154. }
  155. break;
  156. case 0x00:
  157. // OUT endpoint
  158. if (tx_ep == 0) {
  159. tx_ep = p[2];
  160. tx_ep_type = p[3];
  161. tx_size = p[4] | (p[5] << 8);
  162. println(" tx_size = ", tx_size);
  163. }
  164. break;
  165. default:
  166. return false;
  167. }
  168. } else if (type == 37) {
  169. // MIDI endpoint info, midi 1.0: 6.2.2, page 26
  170. println(" MIDI Endpoint Jack Association (ignored)");
  171. } else {
  172. println(" Unknown descriptor, type=", type);
  173. return false; // unknown
  174. }
  175. p += len;
  176. }
  177. if (!ismidi) {
  178. println("This interface is not MIDI");
  179. return false;
  180. }
  181. // if an IN endpoint was found, create its pipe
  182. if (rx_ep && rx_size <= max_packet_size) {
  183. rxpipe = new_Pipe(dev, rx_ep_type, rx_ep, 1, rx_size);
  184. if (rxpipe) {
  185. rxpipe->callback_function = rx_callback;
  186. queue_Data_Transfer(rxpipe, rx_buffer, rx_size, this);
  187. rx_packet_queued = true;
  188. }
  189. } else {
  190. rxpipe = NULL;
  191. }
  192. // if an OUT endpoint was found, create its pipe
  193. if (tx_ep && tx_size <= max_packet_size) {
  194. txpipe = new_Pipe(dev, tx_ep_type, tx_ep, 0, tx_size);
  195. if (txpipe) {
  196. txpipe->callback_function = tx_callback;
  197. tx1_count = 0;
  198. tx2_count = 0;
  199. }
  200. } else {
  201. txpipe = NULL;
  202. }
  203. rx_head = 0;
  204. rx_tail = 0;
  205. msg_channel = 0;
  206. msg_type = 0;
  207. msg_data1 = 0;
  208. msg_data2 = 0;
  209. msg_sysex_len = 0;
  210. // claim if either pipe created
  211. return (rxpipe || txpipe);
  212. }
  213. void MIDIDeviceBase::rx_callback(const Transfer_t *transfer)
  214. {
  215. if (transfer->driver) {
  216. ((MIDIDevice *)(transfer->driver))->rx_data(transfer);
  217. }
  218. }
  219. void MIDIDeviceBase::tx_callback(const Transfer_t *transfer)
  220. {
  221. if (transfer->driver) {
  222. ((MIDIDevice *)(transfer->driver))->tx_data(transfer);
  223. }
  224. }
  225. void MIDIDeviceBase::rx_data(const Transfer_t *transfer)
  226. {
  227. println("MIDIDevice Receive");
  228. print(" MIDI Data: ");
  229. uint32_t len = (transfer->length - ((transfer->qtd.token >> 16) & 0x7FFF)) >> 2;
  230. print_hexbytes(transfer->buffer, len * 4);
  231. uint32_t head = rx_head;
  232. uint32_t tail = rx_tail;
  233. for (uint32_t i=0; i < len; i++) {
  234. uint32_t msg = rx_buffer[i];
  235. if (msg) {
  236. if (++head >= rx_queue_size) head = 0;
  237. rx_queue[head] = msg;
  238. }
  239. }
  240. rx_head = head;
  241. rx_tail = tail;
  242. uint32_t avail = (head < tail) ? tail - head - 1 : rx_queue_size - 1 - head + tail;
  243. //println("rx_size = ", rx_size);
  244. println("avail = ", avail);
  245. if (avail >= (uint32_t)(rx_size>>2)) {
  246. // enough space to accept another full packet
  247. println("queue another receive packet");
  248. queue_Data_Transfer(rxpipe, rx_buffer, rx_size, this);
  249. rx_packet_queued = true;
  250. } else {
  251. // queue can't accept another packet's data, so leave
  252. // the data waiting on the device until we can accept it
  253. println("wait to receive more packets");
  254. rx_packet_queued = false;
  255. }
  256. }
  257. void MIDIDeviceBase::tx_data(const Transfer_t *transfer)
  258. {
  259. println("MIDIDevice transmit complete");
  260. print(" MIDI Data: ");
  261. print_hexbytes(transfer->buffer, tx_size);
  262. if (transfer->buffer == tx_buffer1) {
  263. tx1_count = 0;
  264. } else if (transfer->buffer == tx_buffer2) {
  265. tx2_count = 0;
  266. }
  267. }
  268. void MIDIDeviceBase::disconnect()
  269. {
  270. // should rx_queue be cleared?
  271. // as-is, the user can still read MIDI messages
  272. // which arrived before the device disconnected.
  273. rxpipe = NULL;
  274. txpipe = NULL;
  275. }
  276. void MIDIDeviceBase::write_packed(uint32_t data)
  277. {
  278. if (!txpipe) return;
  279. uint32_t tx_max = tx_size / 4;
  280. while (1) {
  281. __disable_irq();
  282. uint32_t tx1 = tx1_count;
  283. uint32_t tx2 = tx2_count;
  284. if (tx1 < tx_max && (tx2 == 0 || tx2 >= tx_max)) {
  285. // use tx_buffer1
  286. tx_buffer1[tx1++] = data;
  287. tx1_count = tx1;
  288. txtimer.stop();
  289. if (tx1 >= tx_max) {
  290. queue_Data_Transfer(txpipe, tx_buffer1, tx_max*4, this);
  291. } else {
  292. txtimer.start(tx_max >= 128 ? 200 : 1500);
  293. }
  294. __enable_irq();
  295. return;
  296. }
  297. if (tx2 < tx_max) {
  298. // use tx_buffer2
  299. tx_buffer2[tx2++] = data;
  300. tx2_count = tx2;
  301. txtimer.stop();
  302. if (tx2 >= tx_max) {
  303. queue_Data_Transfer(txpipe, tx_buffer2, tx_max*4, this);
  304. } else {
  305. txtimer.start(tx_max >= 128 ? 200 : 1500);
  306. }
  307. __enable_irq();
  308. return;
  309. }
  310. __enable_irq();
  311. // TODO: call yield() ??
  312. }
  313. }
  314. void MIDIDeviceBase::timer_event(USBDriverTimer *timer)
  315. {
  316. const uint32_t tx_max = tx_size / 4;
  317. uint32_t tx1 = tx1_count;
  318. if (tx1 > 0) {
  319. tx1_count = tx_max;
  320. queue_Data_Transfer(txpipe, tx_buffer1, tx1*4, this);
  321. }
  322. uint32_t tx2 = tx2_count;
  323. if (tx2 > 0) {
  324. tx2_count = tx_max;
  325. queue_Data_Transfer(txpipe, tx_buffer2, tx2*4, this);
  326. }
  327. }
  328. void MIDIDeviceBase::send_sysex_buffer_has_term(const uint8_t *data, uint32_t length, uint8_t cable)
  329. {
  330. cable = (cable & 0x0F) << 4;
  331. while (length > 3) {
  332. write_packed(0x04 | cable | (data[0] << 8) | (data[1] << 16) | (data[2] << 24));
  333. data += 3;
  334. length -= 3;
  335. }
  336. if (length == 3) {
  337. write_packed(0x07 | cable | (data[0] << 8) | (data[1] << 16) | (data[2] << 24));
  338. } else if (length == 2) {
  339. write_packed(0x06 | cable | (data[0] << 8) | (data[1] << 16));
  340. } else if (length == 1) {
  341. write_packed(0x05 | cable | (data[0] << 8));
  342. }
  343. }
  344. void MIDIDeviceBase::send_sysex_add_term_bytes(const uint8_t *data, uint32_t length, uint8_t cable)
  345. {
  346. cable = (cable & 0x0F) << 4;
  347. if (length == 0) {
  348. write_packed(0x06 | cable | (0xF0 << 8) | (0xF7 << 16));
  349. return;
  350. } else if (length == 1) {
  351. write_packed(0x07 | cable | (0xF0 << 8) | (data[0] << 16) | (0xF7 << 24));
  352. return;
  353. } else {
  354. write_packed(0x04 | cable | (0xF0 << 8) | (data[0] << 16) | (data[1] << 24));
  355. data += 2;
  356. length -= 2;
  357. }
  358. while (length >= 3) {
  359. write_packed(0x04 | cable | (data[0] << 8) | (data[1] << 16) | (data[2] << 24));
  360. data += 3;
  361. length -= 3;
  362. }
  363. if (length == 2) {
  364. write_packed(0x07 | cable | (data[0] << 8) | (data[1] << 16) | (0xF7 << 24));
  365. } else if (length == 1) {
  366. write_packed(0x06 | cable | (data[0] << 8) | (0xF7 << 16));
  367. } else {
  368. write_packed(0x05 | cable | (0xF7 << 8));
  369. }
  370. }
  371. bool MIDIDeviceBase::read(uint8_t channel)
  372. {
  373. uint32_t n, head, tail, avail, ch, type1, type2, b1;
  374. head = rx_head;
  375. tail = rx_tail;
  376. if (head == tail) return false;
  377. if (++tail >= rx_queue_size) tail = 0;
  378. n = rx_queue[tail];
  379. rx_tail = tail;
  380. if (!rx_packet_queued && rxpipe) {
  381. avail = (head < tail) ? tail - head - 1 : rx_queue_size - 1 - head + tail;
  382. if (avail >= (uint32_t)(rx_size>>2)) {
  383. __disable_irq();
  384. queue_Data_Transfer(rxpipe, rx_buffer, rx_size, this);
  385. __enable_irq();
  386. }
  387. }
  388. println("read: ", n, HEX);
  389. type1 = n & 15;
  390. type2 = (n >> 12) & 15;
  391. b1 = (n >> 8) & 0xFF;
  392. ch = (b1 & 15) + 1;
  393. msg_cable = (n >> 4) & 15;
  394. if (type1 >= 0x08 && type1 <= 0x0E) {
  395. if (channel && channel != ch) {
  396. // ignore other channels when user wants single channel read
  397. return false;
  398. }
  399. if (type1 == 0x08 && type2 == 0x08) {
  400. msg_type = 0x80; // 0x80 = Note off
  401. if (handleNoteOff) {
  402. (*handleNoteOff)(ch, (n >> 16), (n >> 24));
  403. }
  404. } else
  405. if (type1 == 0x09 && type2 == 0x09) {
  406. if ((n >> 24) > 0) {
  407. msg_type = 0x90; // 0x90 = Note on
  408. if (handleNoteOn) {
  409. (*handleNoteOn)(ch, (n >> 16), (n >> 24));
  410. }
  411. } else {
  412. msg_type = 0x80; // 0x80 = Note off
  413. if (handleNoteOff) {
  414. (*handleNoteOff)(ch, (n >> 16), (n >> 24));
  415. }
  416. }
  417. } else
  418. if (type1 == 0x0A && type2 == 0x0A) {
  419. msg_type = 0xA0; // 0xA0 = AfterTouchPoly
  420. if (handleVelocityChange) {
  421. (*handleVelocityChange)(ch, (n >> 16), (n >> 24));
  422. }
  423. } else
  424. if (type1 == 0x0B && type2 == 0x0B) {
  425. msg_type = 0xB0; // 0xB0 = Control Change
  426. if (handleControlChange) {
  427. (*handleControlChange)(ch, (n >> 16), (n >> 24));
  428. }
  429. } else
  430. if (type1 == 0x0C && type2 == 0x0C) {
  431. msg_type = 0xC0; // 0xC0 = Program Change
  432. if (handleProgramChange) {
  433. (*handleProgramChange)(ch, (n >> 16));
  434. }
  435. } else
  436. if (type1 == 0x0D && type2 == 0x0D) {
  437. msg_type = 0xD0; // 0xD0 = After Touch
  438. if (handleAfterTouch) {
  439. (*handleAfterTouch)(ch, (n >> 16));
  440. }
  441. } else
  442. if (type1 == 0x0E && type2 == 0x0E) {
  443. msg_type = 0xE0; // 0xE0 = Pitch Bend
  444. if (handlePitchChange) {
  445. int value = ((n >> 16) & 0x7F) | ((n >> 17) & 0x3F80);
  446. value -= 8192; // 0 to 16383 --> -8192 to +8191
  447. (*handlePitchChange)(ch, value);
  448. }
  449. } else {
  450. return false;
  451. }
  452. return_message:
  453. msg_channel = ch;
  454. msg_data1 = (n >> 16);
  455. msg_data2 = (n >> 24);
  456. return true;
  457. }
  458. if (type1 == 0x02 || type1 == 0x03 || (type1 == 0x05 && b1 >= 0xF1 && b1 != 0xF7)) {
  459. // system common or system realtime message
  460. system_common_or_realtime:
  461. switch (b1) {
  462. case 0xF1: // usbMIDI.TimeCodeQuarterFrame
  463. if (handleTimeCodeQuarterFrame) {
  464. (*handleTimeCodeQuarterFrame)(n >> 16);
  465. }
  466. break;
  467. case 0xF2: // usbMIDI.SongPosition
  468. if (handleSongPosition) {
  469. (*handleSongPosition)(((n >> 16) & 0x7F) | ((n >> 17) & 0x3F80));
  470. }
  471. break;
  472. case 0xF3: // usbMIDI.SongSelect
  473. if (handleSongSelect) {
  474. (*handleSongSelect)(n >> 16);
  475. }
  476. break;
  477. case 0xF6: // usbMIDI.TuneRequest
  478. if (handleTuneRequest) {
  479. (*handleTuneRequest)();
  480. }
  481. break;
  482. case 0xF8: // usbMIDI.Clock
  483. if (handleClock) {
  484. (*handleClock)();
  485. } else if (handleRealTimeSystem) {
  486. (*handleRealTimeSystem)(0xF8);
  487. }
  488. break;
  489. case 0xFA: // usbMIDI.Start
  490. if (handleStart) {
  491. (*handleStart)();
  492. } else if (handleRealTimeSystem) {
  493. (*handleRealTimeSystem)(0xFA);
  494. }
  495. break;
  496. case 0xFB: // usbMIDI.Continue
  497. if (handleContinue) {
  498. (*handleContinue)();
  499. } else if (handleRealTimeSystem) {
  500. (*handleRealTimeSystem)(0xFB);
  501. }
  502. break;
  503. case 0xFC: // usbMIDI.Stop
  504. if (handleStop) {
  505. (*handleStop)();
  506. } else if (handleRealTimeSystem) {
  507. (*handleRealTimeSystem)(0xFC);
  508. }
  509. break;
  510. case 0xFE: // usbMIDI.ActiveSensing
  511. if (handleActiveSensing) {
  512. (*handleActiveSensing)();
  513. } else if (handleRealTimeSystem) {
  514. (*handleRealTimeSystem)(0xFE);
  515. }
  516. break;
  517. case 0xFF: // usbMIDI.SystemReset
  518. if (handleSystemReset) {
  519. (*handleSystemReset)();
  520. } else if (handleRealTimeSystem) {
  521. (*handleRealTimeSystem)(0xFF);
  522. }
  523. break;
  524. default:
  525. return false; // unknown message, ignore it
  526. }
  527. msg_type = b1;
  528. goto return_message;
  529. }
  530. if (type1 == 0x04) {
  531. sysex_byte(n >> 8);
  532. sysex_byte(n >> 16);
  533. sysex_byte(n >> 24);
  534. return false;
  535. }
  536. if (type1 >= 0x05 && type1 <= 0x07) {
  537. sysex_byte(b1);
  538. // allow for buggy devices which use code 5 to transmit 1 byte at a time
  539. // https://forum.pjrc.com/threads/43450?p=164596&viewfull=1#post164596
  540. if (type1 == 0x05 && b1 != 0xF7) return false;
  541. if (type1 >= 0x06) sysex_byte(n >> 16);
  542. if (type1 == 0x07) sysex_byte(n >> 24);
  543. uint16_t len = msg_sysex_len;
  544. msg_data1 = len;
  545. msg_data2 = len >> 8;
  546. msg_sysex_len = 0;
  547. msg_type = 0xF0; // 0xF0 = SystemExclusive
  548. if (handleSysExPartial) {
  549. (*handleSysExPartial)(msg_sysex, len, 1);
  550. } else if (handleSysExComplete) {
  551. (*handleSysExComplete)(msg_sysex, len);
  552. }
  553. return true;
  554. }
  555. if (type1 == 0x0F) {
  556. if (b1 >= 0xF8) {
  557. goto system_common_or_realtime;
  558. }
  559. if (b1 == 0xF0 || msg_sysex_len > 0) {
  560. // Is this really needed? Mac OS-X does this, but do any devices?
  561. sysex_byte(b1);
  562. }
  563. }
  564. return false;
  565. }
  566. void MIDIDeviceBase::sysex_byte(uint8_t b)
  567. {
  568. if (handleSysExPartial && msg_sysex_len >= SYSEX_MAX_LEN) {
  569. // when buffer is full, send another chunk to partial handler.
  570. (*handleSysExPartial)(msg_sysex, msg_sysex_len, 0);
  571. msg_sysex_len = 0;
  572. }
  573. if (msg_sysex_len < SYSEX_MAX_LEN) {
  574. msg_sysex[msg_sysex_len++] = b;
  575. }
  576. }