lufa.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277
  1. /*
  2. * Copyright 2012 Jun Wako <wakojun@gmail.com>
  3. * This file is based on:
  4. * LUFA-120219/Demos/Device/Lowlevel/KeyboardMouse
  5. * LUFA-120219/Demos/Device/Lowlevel/GenericHID
  6. */
  7. /*
  8. LUFA Library
  9. Copyright (C) Dean Camera, 2012.
  10. dean [at] fourwalledcubicle [dot] com
  11. www.lufa-lib.org
  12. */
  13. /*
  14. Copyright 2012 Dean Camera (dean [at] fourwalledcubicle [dot] com)
  15. Copyright 2010 Denver Gingerich (denver [at] ossguy [dot] com)
  16. Permission to use, copy, modify, distribute, and sell this
  17. software and its documentation for any purpose is hereby granted
  18. without fee, provided that the above copyright notice appear in
  19. all copies and that both that the copyright notice and this
  20. permission notice and warranty disclaimer appear in supporting
  21. documentation, and that the name of the author not be used in
  22. advertising or publicity pertaining to distribution of the
  23. software without specific, written prior permission.
  24. The author disclaim all warranties with regard to this
  25. software, including all implied warranties of merchantability
  26. and fitness. In no event shall the author be liable for any
  27. special, indirect or consequential damages or any damages
  28. whatsoever resulting from loss of use, data or profits, whether
  29. in an action of contract, negligence or other tortious action,
  30. arising out of or in connection with the use or performance of
  31. this software.
  32. */
  33. #include "report.h"
  34. #include "host.h"
  35. #include "host_driver.h"
  36. #include "keyboard.h"
  37. #include "action.h"
  38. #include "led.h"
  39. #include "sendchar.h"
  40. #include "debug.h"
  41. #ifdef SLEEP_LED_ENABLE
  42. #include "sleep_led.h"
  43. #endif
  44. #include "suspend.h"
  45. #include "descriptor.h"
  46. #include "lufa.h"
  47. #include "quantum.h"
  48. #include <util/atomic.h>
  49. #include "outputselect.h"
  50. #ifdef NKRO_ENABLE
  51. #include "keycode_config.h"
  52. extern keymap_config_t keymap_config;
  53. #endif
  54. #ifdef AUDIO_ENABLE
  55. #include <audio.h>
  56. #endif
  57. #ifdef BLUETOOTH_ENABLE
  58. #include "bluetooth.h"
  59. #endif
  60. #ifdef ADAFRUIT_BLE_ENABLE
  61. #include "adafruit_ble.h"
  62. #endif
  63. #ifdef VIRTSER_ENABLE
  64. #include "virtser.h"
  65. #endif
  66. #if (defined(RGB_MIDI) | defined(RGBLIGHT_ANIMATIONS)) & defined(RGBLIGHT_ENABLE)
  67. #include "rgblight.h"
  68. #endif
  69. #ifdef MIDI_ENABLE
  70. #include "sysex_tools.h"
  71. #endif
  72. #ifdef RAW_ENABLE
  73. #include "raw_hid.h"
  74. #endif
  75. uint8_t keyboard_idle = 0;
  76. /* 0: Boot Protocol, 1: Report Protocol(default) */
  77. uint8_t keyboard_protocol = 1;
  78. static uint8_t keyboard_led_stats = 0;
  79. static report_keyboard_t keyboard_report_sent;
  80. #ifdef MIDI_ENABLE
  81. static void usb_send_func(MidiDevice * device, uint16_t cnt, uint8_t byte0, uint8_t byte1, uint8_t byte2);
  82. static void usb_get_midi(MidiDevice * device);
  83. static void midi_usb_init(MidiDevice * device);
  84. #endif
  85. /* Host driver */
  86. static uint8_t keyboard_leds(void);
  87. static void send_keyboard(report_keyboard_t *report);
  88. static void send_mouse(report_mouse_t *report);
  89. static void send_system(uint16_t data);
  90. static void send_consumer(uint16_t data);
  91. host_driver_t lufa_driver = {
  92. keyboard_leds,
  93. send_keyboard,
  94. send_mouse,
  95. send_system,
  96. send_consumer,
  97. #ifdef MIDI_ENABLE
  98. usb_send_func,
  99. usb_get_midi,
  100. midi_usb_init
  101. #endif
  102. };
  103. /*******************************************************************************
  104. * MIDI
  105. ******************************************************************************/
  106. #ifdef MIDI_ENABLE
  107. USB_ClassInfo_MIDI_Device_t USB_MIDI_Interface =
  108. {
  109. .Config =
  110. {
  111. .StreamingInterfaceNumber = AS_INTERFACE,
  112. .DataINEndpoint =
  113. {
  114. .Address = MIDI_STREAM_IN_EPADDR,
  115. .Size = MIDI_STREAM_EPSIZE,
  116. .Banks = 1,
  117. },
  118. .DataOUTEndpoint =
  119. {
  120. .Address = MIDI_STREAM_OUT_EPADDR,
  121. .Size = MIDI_STREAM_EPSIZE,
  122. .Banks = 1,
  123. },
  124. },
  125. };
  126. #define SYSEX_START_OR_CONT 0x40
  127. #define SYSEX_ENDS_IN_1 0x50
  128. #define SYSEX_ENDS_IN_2 0x60
  129. #define SYSEX_ENDS_IN_3 0x70
  130. #define SYS_COMMON_1 0x50
  131. #define SYS_COMMON_2 0x20
  132. #define SYS_COMMON_3 0x30
  133. #endif
  134. #ifdef VIRTSER_ENABLE
  135. USB_ClassInfo_CDC_Device_t cdc_device =
  136. {
  137. .Config =
  138. {
  139. .ControlInterfaceNumber = CCI_INTERFACE,
  140. .DataINEndpoint =
  141. {
  142. .Address = CDC_IN_EPADDR,
  143. .Size = CDC_EPSIZE,
  144. .Banks = 1,
  145. },
  146. .DataOUTEndpoint =
  147. {
  148. .Address = CDC_OUT_EPADDR,
  149. .Size = CDC_EPSIZE,
  150. .Banks = 1,
  151. },
  152. .NotificationEndpoint =
  153. {
  154. .Address = CDC_NOTIFICATION_EPADDR,
  155. .Size = CDC_NOTIFICATION_EPSIZE,
  156. .Banks = 1,
  157. },
  158. },
  159. };
  160. #endif
  161. #ifdef RAW_ENABLE
  162. void raw_hid_send( uint8_t *data, uint8_t length )
  163. {
  164. // TODO: implement variable size packet
  165. if ( length != RAW_EPSIZE )
  166. {
  167. return;
  168. }
  169. if (USB_DeviceState != DEVICE_STATE_Configured)
  170. {
  171. return;
  172. }
  173. // TODO: decide if we allow calls to raw_hid_send() in the middle
  174. // of other endpoint usage.
  175. uint8_t ep = Endpoint_GetCurrentEndpoint();
  176. Endpoint_SelectEndpoint(RAW_IN_EPNUM);
  177. // Check to see if the host is ready to accept another packet
  178. if (Endpoint_IsINReady())
  179. {
  180. // Write data
  181. Endpoint_Write_Stream_LE(data, RAW_EPSIZE, NULL);
  182. // Finalize the stream transfer to send the last packet
  183. Endpoint_ClearIN();
  184. }
  185. Endpoint_SelectEndpoint(ep);
  186. }
  187. __attribute__ ((weak))
  188. void raw_hid_receive( uint8_t *data, uint8_t length )
  189. {
  190. // Users should #include "raw_hid.h" in their own code
  191. // and implement this function there. Leave this as weak linkage
  192. // so users can opt to not handle data coming in.
  193. }
  194. static void raw_hid_task(void)
  195. {
  196. // Create a temporary buffer to hold the read in data from the host
  197. uint8_t data[RAW_EPSIZE];
  198. bool data_read = false;
  199. // Device must be connected and configured for the task to run
  200. if (USB_DeviceState != DEVICE_STATE_Configured)
  201. return;
  202. Endpoint_SelectEndpoint(RAW_OUT_EPNUM);
  203. // Check to see if a packet has been sent from the host
  204. if (Endpoint_IsOUTReceived())
  205. {
  206. // Check to see if the packet contains data
  207. if (Endpoint_IsReadWriteAllowed())
  208. {
  209. /* Read data */
  210. Endpoint_Read_Stream_LE(data, sizeof(data), NULL);
  211. data_read = true;
  212. }
  213. // Finalize the stream transfer to receive the last packet
  214. Endpoint_ClearOUT();
  215. if ( data_read )
  216. {
  217. raw_hid_receive( data, sizeof(data) );
  218. }
  219. }
  220. }
  221. #endif
  222. /*******************************************************************************
  223. * Console
  224. ******************************************************************************/
  225. #ifdef CONSOLE_ENABLE
  226. static void Console_Task(void)
  227. {
  228. /* Device must be connected and configured for the task to run */
  229. if (USB_DeviceState != DEVICE_STATE_Configured)
  230. return;
  231. uint8_t ep = Endpoint_GetCurrentEndpoint();
  232. #if 0
  233. // TODO: impl receivechar()/recvchar()
  234. Endpoint_SelectEndpoint(CONSOLE_OUT_EPNUM);
  235. /* Check to see if a packet has been sent from the host */
  236. if (Endpoint_IsOUTReceived())
  237. {
  238. /* Check to see if the packet contains data */
  239. if (Endpoint_IsReadWriteAllowed())
  240. {
  241. /* Create a temporary buffer to hold the read in report from the host */
  242. uint8_t ConsoleData[CONSOLE_EPSIZE];
  243. /* Read Console Report Data */
  244. Endpoint_Read_Stream_LE(&ConsoleData, sizeof(ConsoleData), NULL);
  245. /* Process Console Report Data */
  246. //ProcessConsoleHIDReport(ConsoleData);
  247. }
  248. /* Finalize the stream transfer to send the last packet */
  249. Endpoint_ClearOUT();
  250. }
  251. #endif
  252. /* IN packet */
  253. Endpoint_SelectEndpoint(CONSOLE_IN_EPNUM);
  254. if (!Endpoint_IsEnabled() || !Endpoint_IsConfigured()) {
  255. Endpoint_SelectEndpoint(ep);
  256. return;
  257. }
  258. // fill empty bank
  259. while (Endpoint_IsReadWriteAllowed())
  260. Endpoint_Write_8(0);
  261. // flash senchar packet
  262. if (Endpoint_IsINReady()) {
  263. Endpoint_ClearIN();
  264. }
  265. Endpoint_SelectEndpoint(ep);
  266. }
  267. #endif
  268. /*******************************************************************************
  269. * USB Events
  270. ******************************************************************************/
  271. /*
  272. * Event Order of Plug in:
  273. * 0) EVENT_USB_Device_Connect
  274. * 1) EVENT_USB_Device_Suspend
  275. * 2) EVENT_USB_Device_Reset
  276. * 3) EVENT_USB_Device_Wake
  277. */
  278. void EVENT_USB_Device_Connect(void)
  279. {
  280. print("[C]");
  281. /* For battery powered device */
  282. if (!USB_IsInitialized) {
  283. USB_Disable();
  284. USB_Init();
  285. USB_Device_EnableSOFEvents();
  286. }
  287. }
  288. void EVENT_USB_Device_Disconnect(void)
  289. {
  290. print("[D]");
  291. /* For battery powered device */
  292. USB_IsInitialized = false;
  293. /* TODO: This doesn't work. After several plug in/outs can not be enumerated.
  294. if (USB_IsInitialized) {
  295. USB_Disable(); // Disable all interrupts
  296. USB_Controller_Enable();
  297. USB_INT_Enable(USB_INT_VBUSTI);
  298. }
  299. */
  300. }
  301. void EVENT_USB_Device_Reset(void)
  302. {
  303. print("[R]");
  304. }
  305. void EVENT_USB_Device_Suspend()
  306. {
  307. print("[S]");
  308. #ifdef SLEEP_LED_ENABLE
  309. sleep_led_enable();
  310. #endif
  311. }
  312. void EVENT_USB_Device_WakeUp()
  313. {
  314. print("[W]");
  315. suspend_wakeup_init();
  316. #ifdef SLEEP_LED_ENABLE
  317. sleep_led_disable();
  318. // NOTE: converters may not accept this
  319. led_set(host_keyboard_leds());
  320. #endif
  321. }
  322. #ifdef CONSOLE_ENABLE
  323. static bool console_flush = false;
  324. #define CONSOLE_FLUSH_SET(b) do { \
  325. ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {\
  326. console_flush = b; \
  327. } \
  328. } while (0)
  329. // called every 1ms
  330. void EVENT_USB_Device_StartOfFrame(void)
  331. {
  332. static uint8_t count;
  333. if (++count % 50) return;
  334. count = 0;
  335. if (!console_flush) return;
  336. Console_Task();
  337. console_flush = false;
  338. }
  339. #endif
  340. /** Event handler for the USB_ConfigurationChanged event.
  341. * This is fired when the host sets the current configuration of the USB device after enumeration.
  342. *
  343. * ATMega32u2 supports dual bank(ping-pong mode) only on endpoint 3 and 4,
  344. * it is safe to use singl bank for all endpoints.
  345. */
  346. void EVENT_USB_Device_ConfigurationChanged(void)
  347. {
  348. bool ConfigSuccess = true;
  349. /* Setup Keyboard HID Report Endpoints */
  350. ConfigSuccess &= ENDPOINT_CONFIG(KEYBOARD_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
  351. KEYBOARD_EPSIZE, ENDPOINT_BANK_SINGLE);
  352. #ifdef MOUSE_ENABLE
  353. /* Setup Mouse HID Report Endpoint */
  354. ConfigSuccess &= ENDPOINT_CONFIG(MOUSE_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
  355. MOUSE_EPSIZE, ENDPOINT_BANK_SINGLE);
  356. #endif
  357. #ifdef EXTRAKEY_ENABLE
  358. /* Setup Extra HID Report Endpoint */
  359. ConfigSuccess &= ENDPOINT_CONFIG(EXTRAKEY_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
  360. EXTRAKEY_EPSIZE, ENDPOINT_BANK_SINGLE);
  361. #endif
  362. #ifdef RAW_ENABLE
  363. /* Setup Raw HID Report Endpoints */
  364. ConfigSuccess &= ENDPOINT_CONFIG(RAW_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
  365. RAW_EPSIZE, ENDPOINT_BANK_SINGLE);
  366. ConfigSuccess &= ENDPOINT_CONFIG(RAW_OUT_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_OUT,
  367. RAW_EPSIZE, ENDPOINT_BANK_SINGLE);
  368. #endif
  369. #ifdef CONSOLE_ENABLE
  370. /* Setup Console HID Report Endpoints */
  371. ConfigSuccess &= ENDPOINT_CONFIG(CONSOLE_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
  372. CONSOLE_EPSIZE, ENDPOINT_BANK_SINGLE);
  373. #if 0
  374. ConfigSuccess &= ENDPOINT_CONFIG(CONSOLE_OUT_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_OUT,
  375. CONSOLE_EPSIZE, ENDPOINT_BANK_SINGLE);
  376. #endif
  377. #endif
  378. #ifdef NKRO_ENABLE
  379. /* Setup NKRO HID Report Endpoints */
  380. ConfigSuccess &= ENDPOINT_CONFIG(NKRO_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
  381. NKRO_EPSIZE, ENDPOINT_BANK_SINGLE);
  382. #endif
  383. #ifdef MIDI_ENABLE
  384. ConfigSuccess &= Endpoint_ConfigureEndpoint(MIDI_STREAM_IN_EPADDR, EP_TYPE_BULK, MIDI_STREAM_EPSIZE, ENDPOINT_BANK_SINGLE);
  385. ConfigSuccess &= Endpoint_ConfigureEndpoint(MIDI_STREAM_OUT_EPADDR, EP_TYPE_BULK, MIDI_STREAM_EPSIZE, ENDPOINT_BANK_SINGLE);
  386. #endif
  387. #ifdef VIRTSER_ENABLE
  388. ConfigSuccess &= Endpoint_ConfigureEndpoint(CDC_NOTIFICATION_EPADDR, EP_TYPE_INTERRUPT, CDC_NOTIFICATION_EPSIZE, ENDPOINT_BANK_SINGLE);
  389. ConfigSuccess &= Endpoint_ConfigureEndpoint(CDC_OUT_EPADDR, EP_TYPE_BULK, CDC_EPSIZE, ENDPOINT_BANK_SINGLE);
  390. ConfigSuccess &= Endpoint_ConfigureEndpoint(CDC_IN_EPADDR, EP_TYPE_BULK, CDC_EPSIZE, ENDPOINT_BANK_SINGLE);
  391. #endif
  392. }
  393. /*
  394. Appendix G: HID Request Support Requirements
  395. The following table enumerates the requests that need to be supported by various types of HID class devices.
  396. Device type GetReport SetReport GetIdle SetIdle GetProtocol SetProtocol
  397. ------------------------------------------------------------------------------------------
  398. Boot Mouse Required Optional Optional Optional Required Required
  399. Non-Boot Mouse Required Optional Optional Optional Optional Optional
  400. Boot Keyboard Required Optional Required Required Required Required
  401. Non-Boot Keybrd Required Optional Required Required Optional Optional
  402. Other Device Required Optional Optional Optional Optional Optional
  403. */
  404. /** Event handler for the USB_ControlRequest event.
  405. * This is fired before passing along unhandled control requests to the library for processing internally.
  406. */
  407. void EVENT_USB_Device_ControlRequest(void)
  408. {
  409. uint8_t* ReportData = NULL;
  410. uint8_t ReportSize = 0;
  411. /* Handle HID Class specific requests */
  412. switch (USB_ControlRequest.bRequest)
  413. {
  414. case HID_REQ_GetReport:
  415. if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
  416. {
  417. Endpoint_ClearSETUP();
  418. // Interface
  419. switch (USB_ControlRequest.wIndex) {
  420. case KEYBOARD_INTERFACE:
  421. // TODO: test/check
  422. ReportData = (uint8_t*)&keyboard_report_sent;
  423. ReportSize = sizeof(keyboard_report_sent);
  424. break;
  425. }
  426. /* Write the report data to the control endpoint */
  427. Endpoint_Write_Control_Stream_LE(ReportData, ReportSize);
  428. Endpoint_ClearOUT();
  429. }
  430. break;
  431. case HID_REQ_SetReport:
  432. if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
  433. {
  434. // Interface
  435. switch (USB_ControlRequest.wIndex) {
  436. case KEYBOARD_INTERFACE:
  437. #ifdef NKRO_ENABLE
  438. case NKRO_INTERFACE:
  439. #endif
  440. Endpoint_ClearSETUP();
  441. while (!(Endpoint_IsOUTReceived())) {
  442. if (USB_DeviceState == DEVICE_STATE_Unattached)
  443. return;
  444. }
  445. keyboard_led_stats = Endpoint_Read_8();
  446. Endpoint_ClearOUT();
  447. Endpoint_ClearStatusStage();
  448. break;
  449. }
  450. }
  451. break;
  452. case HID_REQ_GetProtocol:
  453. if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
  454. {
  455. if (USB_ControlRequest.wIndex == KEYBOARD_INTERFACE) {
  456. Endpoint_ClearSETUP();
  457. while (!(Endpoint_IsINReady()));
  458. Endpoint_Write_8(keyboard_protocol);
  459. Endpoint_ClearIN();
  460. Endpoint_ClearStatusStage();
  461. }
  462. }
  463. break;
  464. case HID_REQ_SetProtocol:
  465. if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
  466. {
  467. if (USB_ControlRequest.wIndex == KEYBOARD_INTERFACE) {
  468. Endpoint_ClearSETUP();
  469. Endpoint_ClearStatusStage();
  470. keyboard_protocol = (USB_ControlRequest.wValue & 0xFF);
  471. clear_keyboard();
  472. }
  473. }
  474. break;
  475. case HID_REQ_SetIdle:
  476. if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
  477. {
  478. Endpoint_ClearSETUP();
  479. Endpoint_ClearStatusStage();
  480. keyboard_idle = ((USB_ControlRequest.wValue & 0xFF00) >> 8);
  481. }
  482. break;
  483. case HID_REQ_GetIdle:
  484. if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
  485. {
  486. Endpoint_ClearSETUP();
  487. while (!(Endpoint_IsINReady()));
  488. Endpoint_Write_8(keyboard_idle);
  489. Endpoint_ClearIN();
  490. Endpoint_ClearStatusStage();
  491. }
  492. break;
  493. }
  494. #ifdef VIRTSER_ENABLE
  495. CDC_Device_ProcessControlRequest(&cdc_device);
  496. #endif
  497. }
  498. /*******************************************************************************
  499. * Host driver
  500. ******************************************************************************/
  501. static uint8_t keyboard_leds(void)
  502. {
  503. return keyboard_led_stats;
  504. }
  505. static void send_keyboard(report_keyboard_t *report)
  506. {
  507. uint8_t timeout = 255;
  508. uint8_t where = where_to_send();
  509. #ifdef BLUETOOTH_ENABLE
  510. if (where == OUTPUT_BLUETOOTH || where == OUTPUT_USB_AND_BT) {
  511. bluefruit_serial_send(0xFD);
  512. for (uint8_t i = 0; i < KEYBOARD_EPSIZE; i++) {
  513. bluefruit_serial_send(report->raw[i]);
  514. }
  515. }
  516. #endif
  517. #ifdef ADAFRUIT_BLE_ENABLE
  518. if (where == OUTPUT_ADAFRUIT_BLE) {
  519. adafruit_ble_send_keys(report->mods, report->keys, sizeof(report->keys));
  520. }
  521. #endif
  522. if (where != OUTPUT_USB && where != OUTPUT_USB_AND_BT) {
  523. return;
  524. }
  525. /* Select the Keyboard Report Endpoint */
  526. #ifdef NKRO_ENABLE
  527. if (keyboard_protocol && keymap_config.nkro) {
  528. /* Report protocol - NKRO */
  529. Endpoint_SelectEndpoint(NKRO_IN_EPNUM);
  530. /* Check if write ready for a polling interval around 1ms */
  531. while (timeout-- && !Endpoint_IsReadWriteAllowed()) _delay_us(4);
  532. if (!Endpoint_IsReadWriteAllowed()) return;
  533. /* Write Keyboard Report Data */
  534. Endpoint_Write_Stream_LE(report, NKRO_EPSIZE, NULL);
  535. }
  536. else
  537. #endif
  538. {
  539. /* Boot protocol */
  540. Endpoint_SelectEndpoint(KEYBOARD_IN_EPNUM);
  541. /* Check if write ready for a polling interval around 10ms */
  542. while (timeout-- && !Endpoint_IsReadWriteAllowed()) _delay_us(40);
  543. if (!Endpoint_IsReadWriteAllowed()) return;
  544. /* Write Keyboard Report Data */
  545. Endpoint_Write_Stream_LE(report, KEYBOARD_EPSIZE, NULL);
  546. }
  547. /* Finalize the stream transfer to send the last packet */
  548. Endpoint_ClearIN();
  549. keyboard_report_sent = *report;
  550. }
  551. static void send_mouse(report_mouse_t *report)
  552. {
  553. #ifdef MOUSE_ENABLE
  554. uint8_t timeout = 255;
  555. uint8_t where = where_to_send();
  556. #ifdef BLUETOOTH_ENABLE
  557. if (where == OUTPUT_BLUETOOTH || where == OUTPUT_USB_AND_BT) {
  558. bluefruit_serial_send(0xFD);
  559. bluefruit_serial_send(0x00);
  560. bluefruit_serial_send(0x03);
  561. bluefruit_serial_send(report->buttons);
  562. bluefruit_serial_send(report->x);
  563. bluefruit_serial_send(report->y);
  564. bluefruit_serial_send(report->v); // should try sending the wheel v here
  565. bluefruit_serial_send(report->h); // should try sending the wheel h here
  566. bluefruit_serial_send(0x00);
  567. }
  568. #endif
  569. #ifdef ADAFRUIT_BLE_ENABLE
  570. if (where == OUTPUT_ADAFRUIT_BLE) {
  571. // FIXME: mouse buttons
  572. adafruit_ble_send_mouse_move(report->x, report->y, report->v, report->h);
  573. }
  574. #endif
  575. if (where != OUTPUT_USB && where != OUTPUT_USB_AND_BT) {
  576. return;
  577. }
  578. /* Select the Mouse Report Endpoint */
  579. Endpoint_SelectEndpoint(MOUSE_IN_EPNUM);
  580. /* Check if write ready for a polling interval around 10ms */
  581. while (timeout-- && !Endpoint_IsReadWriteAllowed()) _delay_us(40);
  582. if (!Endpoint_IsReadWriteAllowed()) return;
  583. /* Write Mouse Report Data */
  584. Endpoint_Write_Stream_LE(report, sizeof(report_mouse_t), NULL);
  585. /* Finalize the stream transfer to send the last packet */
  586. Endpoint_ClearIN();
  587. #endif
  588. }
  589. static void send_system(uint16_t data)
  590. {
  591. uint8_t timeout = 255;
  592. if (USB_DeviceState != DEVICE_STATE_Configured)
  593. return;
  594. report_extra_t r = {
  595. .report_id = REPORT_ID_SYSTEM,
  596. .usage = data - SYSTEM_POWER_DOWN + 1
  597. };
  598. Endpoint_SelectEndpoint(EXTRAKEY_IN_EPNUM);
  599. /* Check if write ready for a polling interval around 10ms */
  600. while (timeout-- && !Endpoint_IsReadWriteAllowed()) _delay_us(40);
  601. if (!Endpoint_IsReadWriteAllowed()) return;
  602. Endpoint_Write_Stream_LE(&r, sizeof(report_extra_t), NULL);
  603. Endpoint_ClearIN();
  604. }
  605. static void send_consumer(uint16_t data)
  606. {
  607. uint8_t timeout = 255;
  608. uint8_t where = where_to_send();
  609. #ifdef BLUETOOTH_ENABLE
  610. if (where == OUTPUT_BLUETOOTH || where == OUTPUT_USB_AND_BT) {
  611. static uint16_t last_data = 0;
  612. if (data == last_data) return;
  613. last_data = data;
  614. uint16_t bitmap = CONSUMER2BLUEFRUIT(data);
  615. bluefruit_serial_send(0xFD);
  616. bluefruit_serial_send(0x00);
  617. bluefruit_serial_send(0x02);
  618. bluefruit_serial_send((bitmap>>8)&0xFF);
  619. bluefruit_serial_send(bitmap&0xFF);
  620. bluefruit_serial_send(0x00);
  621. bluefruit_serial_send(0x00);
  622. bluefruit_serial_send(0x00);
  623. bluefruit_serial_send(0x00);
  624. }
  625. #endif
  626. #ifdef ADAFRUIT_BLE_ENABLE
  627. if (where == OUTPUT_ADAFRUIT_BLE) {
  628. adafruit_ble_send_consumer_key(data, 0);
  629. }
  630. #endif
  631. if (where != OUTPUT_USB && where != OUTPUT_USB_AND_BT) {
  632. return;
  633. }
  634. report_extra_t r = {
  635. .report_id = REPORT_ID_CONSUMER,
  636. .usage = data
  637. };
  638. Endpoint_SelectEndpoint(EXTRAKEY_IN_EPNUM);
  639. /* Check if write ready for a polling interval around 10ms */
  640. while (timeout-- && !Endpoint_IsReadWriteAllowed()) _delay_us(40);
  641. if (!Endpoint_IsReadWriteAllowed()) return;
  642. Endpoint_Write_Stream_LE(&r, sizeof(report_extra_t), NULL);
  643. Endpoint_ClearIN();
  644. }
  645. /*******************************************************************************
  646. * sendchar
  647. ******************************************************************************/
  648. #ifdef CONSOLE_ENABLE
  649. #define SEND_TIMEOUT 5
  650. int8_t sendchar(uint8_t c)
  651. {
  652. // Not wait once timeouted.
  653. // Because sendchar() is called so many times, waiting each call causes big lag.
  654. static bool timeouted = false;
  655. // prevents Console_Task() from running during sendchar() runs.
  656. // or char will be lost. These two function is mutually exclusive.
  657. CONSOLE_FLUSH_SET(false);
  658. if (USB_DeviceState != DEVICE_STATE_Configured)
  659. return -1;
  660. uint8_t ep = Endpoint_GetCurrentEndpoint();
  661. Endpoint_SelectEndpoint(CONSOLE_IN_EPNUM);
  662. if (!Endpoint_IsEnabled() || !Endpoint_IsConfigured()) {
  663. goto ERROR_EXIT;
  664. }
  665. if (timeouted && !Endpoint_IsReadWriteAllowed()) {
  666. goto ERROR_EXIT;
  667. }
  668. timeouted = false;
  669. uint8_t timeout = SEND_TIMEOUT;
  670. while (!Endpoint_IsReadWriteAllowed()) {
  671. if (USB_DeviceState != DEVICE_STATE_Configured) {
  672. goto ERROR_EXIT;
  673. }
  674. if (Endpoint_IsStalled()) {
  675. goto ERROR_EXIT;
  676. }
  677. if (!(timeout--)) {
  678. timeouted = true;
  679. goto ERROR_EXIT;
  680. }
  681. _delay_ms(1);
  682. }
  683. Endpoint_Write_8(c);
  684. // send when bank is full
  685. if (!Endpoint_IsReadWriteAllowed()) {
  686. while (!(Endpoint_IsINReady()));
  687. Endpoint_ClearIN();
  688. } else {
  689. CONSOLE_FLUSH_SET(true);
  690. }
  691. Endpoint_SelectEndpoint(ep);
  692. return 0;
  693. ERROR_EXIT:
  694. Endpoint_SelectEndpoint(ep);
  695. return -1;
  696. }
  697. #else
  698. int8_t sendchar(uint8_t c)
  699. {
  700. return 0;
  701. }
  702. #endif
  703. /*******************************************************************************
  704. * MIDI
  705. ******************************************************************************/
  706. #ifdef MIDI_ENABLE
  707. static void usb_send_func(MidiDevice * device, uint16_t cnt, uint8_t byte0, uint8_t byte1, uint8_t byte2) {
  708. MIDI_EventPacket_t event;
  709. event.Data1 = byte0;
  710. event.Data2 = byte1;
  711. event.Data3 = byte2;
  712. uint8_t cable = 0;
  713. // Endpoint_SelectEndpoint(MIDI_STREAM_IN_EPNUM);
  714. //if the length is undefined we assume it is a SYSEX message
  715. if (midi_packet_length(byte0) == UNDEFINED) {
  716. switch(cnt) {
  717. case 3:
  718. if (byte2 == SYSEX_END)
  719. event.Event = MIDI_EVENT(cable, SYSEX_ENDS_IN_3);
  720. else
  721. event.Event = MIDI_EVENT(cable, SYSEX_START_OR_CONT);
  722. break;
  723. case 2:
  724. if (byte1 == SYSEX_END)
  725. event.Event = MIDI_EVENT(cable, SYSEX_ENDS_IN_2);
  726. else
  727. event.Event = MIDI_EVENT(cable, SYSEX_START_OR_CONT);
  728. break;
  729. case 1:
  730. if (byte0 == SYSEX_END)
  731. event.Event = MIDI_EVENT(cable, SYSEX_ENDS_IN_1);
  732. else
  733. event.Event = MIDI_EVENT(cable, SYSEX_START_OR_CONT);
  734. break;
  735. default:
  736. return; //invalid cnt
  737. }
  738. } else {
  739. //deal with 'system common' messages
  740. //TODO are there any more?
  741. switch(byte0 & 0xF0){
  742. case MIDI_SONGPOSITION:
  743. event.Event = MIDI_EVENT(cable, SYS_COMMON_3);
  744. break;
  745. case MIDI_SONGSELECT:
  746. case MIDI_TC_QUARTERFRAME:
  747. event.Event = MIDI_EVENT(cable, SYS_COMMON_2);
  748. break;
  749. default:
  750. event.Event = MIDI_EVENT(cable, byte0);
  751. break;
  752. }
  753. }
  754. // Endpoint_Write_Stream_LE(&event, sizeof(event), NULL);
  755. // Endpoint_ClearIN();
  756. MIDI_Device_SendEventPacket(&USB_MIDI_Interface, &event);
  757. MIDI_Device_Flush(&USB_MIDI_Interface);
  758. MIDI_Device_USBTask(&USB_MIDI_Interface);
  759. USB_USBTask();
  760. }
  761. static void usb_get_midi(MidiDevice * device) {
  762. MIDI_EventPacket_t event;
  763. while (MIDI_Device_ReceiveEventPacket(&USB_MIDI_Interface, &event)) {
  764. midi_packet_length_t length = midi_packet_length(event.Data1);
  765. uint8_t input[3];
  766. input[0] = event.Data1;
  767. input[1] = event.Data2;
  768. input[2] = event.Data3;
  769. if (length == UNDEFINED) {
  770. //sysex
  771. if (event.Event == MIDI_EVENT(0, SYSEX_START_OR_CONT) || event.Event == MIDI_EVENT(0, SYSEX_ENDS_IN_3)) {
  772. length = 3;
  773. } else if (event.Event == MIDI_EVENT(0, SYSEX_ENDS_IN_2)) {
  774. length = 2;
  775. } else if(event.Event == MIDI_EVENT(0, SYSEX_ENDS_IN_1)) {
  776. length = 1;
  777. } else {
  778. //XXX what to do?
  779. }
  780. }
  781. //pass the data to the device input function
  782. if (length != UNDEFINED)
  783. midi_device_input(device, length, input);
  784. }
  785. MIDI_Device_USBTask(&USB_MIDI_Interface);
  786. USB_USBTask();
  787. }
  788. static void midi_usb_init(MidiDevice * device){
  789. midi_device_init(device);
  790. midi_device_set_send_func(device, usb_send_func);
  791. midi_device_set_pre_input_process_func(device, usb_get_midi);
  792. // SetupHardware();
  793. sei();
  794. }
  795. void MIDI_Task(void)
  796. {
  797. /* Device must be connected and configured for the task to run */
  798. dprint("in MIDI_TASK\n");
  799. if (USB_DeviceState != DEVICE_STATE_Configured)
  800. return;
  801. dprint("continuing in MIDI_TASK\n");
  802. Endpoint_SelectEndpoint(MIDI_STREAM_IN_EPADDR);
  803. if (Endpoint_IsINReady())
  804. {
  805. dprint("Endpoint is ready\n");
  806. uint8_t MIDICommand = 0;
  807. uint8_t MIDIPitch;
  808. /* Get board button status - if pressed use channel 10 (percussion), otherwise use channel 1 */
  809. uint8_t Channel = MIDI_CHANNEL(1);
  810. MIDICommand = MIDI_COMMAND_NOTE_ON;
  811. MIDIPitch = 0x3E;
  812. /* Check if a MIDI command is to be sent */
  813. if (MIDICommand)
  814. {
  815. dprint("Command exists\n");
  816. MIDI_EventPacket_t MIDIEvent = (MIDI_EventPacket_t)
  817. {
  818. .Event = MIDI_EVENT(0, MIDICommand),
  819. .Data1 = MIDICommand | Channel,
  820. .Data2 = MIDIPitch,
  821. .Data3 = MIDI_STANDARD_VELOCITY,
  822. };
  823. /* Write the MIDI event packet to the endpoint */
  824. Endpoint_Write_Stream_LE(&MIDIEvent, sizeof(MIDIEvent), NULL);
  825. /* Send the data in the endpoint to the host */
  826. Endpoint_ClearIN();
  827. }
  828. }
  829. /* Select the MIDI OUT stream */
  830. Endpoint_SelectEndpoint(MIDI_STREAM_OUT_EPADDR);
  831. /* Check if a MIDI command has been received */
  832. if (Endpoint_IsOUTReceived())
  833. {
  834. MIDI_EventPacket_t MIDIEvent;
  835. /* Read the MIDI event packet from the endpoint */
  836. Endpoint_Read_Stream_LE(&MIDIEvent, sizeof(MIDIEvent), NULL);
  837. /* If the endpoint is now empty, clear the bank */
  838. if (!(Endpoint_BytesInEndpoint()))
  839. {
  840. /* Clear the endpoint ready for new packet */
  841. Endpoint_ClearOUT();
  842. }
  843. }
  844. }
  845. #endif
  846. /*******************************************************************************
  847. * VIRTUAL SERIAL
  848. ******************************************************************************/
  849. #ifdef VIRTSER_ENABLE
  850. void virtser_init(void)
  851. {
  852. cdc_device.State.ControlLineStates.DeviceToHost = CDC_CONTROL_LINE_IN_DSR ;
  853. CDC_Device_SendControlLineStateChange(&cdc_device);
  854. }
  855. void virtser_recv(uint8_t c) __attribute__ ((weak));
  856. void virtser_recv(uint8_t c)
  857. {
  858. // Ignore by default
  859. }
  860. void virtser_task(void)
  861. {
  862. uint16_t count = CDC_Device_BytesReceived(&cdc_device);
  863. uint8_t ch;
  864. if (count)
  865. {
  866. ch = CDC_Device_ReceiveByte(&cdc_device);
  867. virtser_recv(ch);
  868. }
  869. }
  870. void virtser_send(const uint8_t byte)
  871. {
  872. uint8_t timeout = 255;
  873. uint8_t ep = Endpoint_GetCurrentEndpoint();
  874. if (cdc_device.State.ControlLineStates.HostToDevice & CDC_CONTROL_LINE_OUT_DTR)
  875. {
  876. /* IN packet */
  877. Endpoint_SelectEndpoint(cdc_device.Config.DataINEndpoint.Address);
  878. if (!Endpoint_IsEnabled() || !Endpoint_IsConfigured()) {
  879. Endpoint_SelectEndpoint(ep);
  880. return;
  881. }
  882. while (timeout-- && !Endpoint_IsReadWriteAllowed()) _delay_us(40);
  883. Endpoint_Write_8(byte);
  884. CDC_Device_Flush(&cdc_device);
  885. if (Endpoint_IsINReady()) {
  886. Endpoint_ClearIN();
  887. }
  888. Endpoint_SelectEndpoint(ep);
  889. }
  890. }
  891. #endif
  892. /*******************************************************************************
  893. * main
  894. ******************************************************************************/
  895. static void setup_mcu(void)
  896. {
  897. /* Disable watchdog if enabled by bootloader/fuses */
  898. MCUSR &= ~(1 << WDRF);
  899. wdt_disable();
  900. /* Disable clock division */
  901. // clock_prescale_set(clock_div_1);
  902. CLKPR = (1 << CLKPCE);
  903. CLKPR = (0 << CLKPS3) | (0 << CLKPS2) | (0 << CLKPS1) | (0 << CLKPS0);
  904. }
  905. static void setup_usb(void)
  906. {
  907. // Leonardo needs. Without this USB device is not recognized.
  908. USB_Disable();
  909. USB_Init();
  910. // for Console_Task
  911. USB_Device_EnableSOFEvents();
  912. print_set_sendchar(sendchar);
  913. }
  914. #ifdef MIDI_ENABLE
  915. void fallthrough_callback(MidiDevice * device,
  916. uint16_t cnt, uint8_t byte0, uint8_t byte1, uint8_t byte2);
  917. void cc_callback(MidiDevice * device,
  918. uint8_t chan, uint8_t num, uint8_t val);
  919. void sysex_callback(MidiDevice * device,
  920. uint16_t start, uint8_t length, uint8_t * data);
  921. void setup_midi(void)
  922. {
  923. midi_init();
  924. midi_device_init(&midi_device);
  925. midi_device_set_send_func(&midi_device, usb_send_func);
  926. midi_device_set_pre_input_process_func(&midi_device, usb_get_midi);
  927. }
  928. #endif
  929. int main(void) __attribute__ ((weak));
  930. int main(void)
  931. {
  932. #ifdef MIDI_ENABLE
  933. setup_midi();
  934. #endif
  935. setup_mcu();
  936. keyboard_setup();
  937. setup_usb();
  938. sei();
  939. #ifdef MIDI_ENABLE
  940. midi_register_fallthrough_callback(&midi_device, fallthrough_callback);
  941. midi_register_cc_callback(&midi_device, cc_callback);
  942. midi_register_sysex_callback(&midi_device, sysex_callback);
  943. // init_notes();
  944. // midi_send_cc(&midi_device, 0, 1, 2);
  945. // midi_send_cc(&midi_device, 15, 1, 0);
  946. // midi_send_noteon(&midi_device, 0, 64, 127);
  947. // midi_send_noteoff(&midi_device, 0, 64, 127);
  948. #endif
  949. #ifdef BLUETOOTH_ENABLE
  950. serial_init();
  951. #endif
  952. /* wait for USB startup & debug output */
  953. #ifdef WAIT_FOR_USB
  954. while (USB_DeviceState != DEVICE_STATE_Configured) {
  955. #if defined(INTERRUPT_CONTROL_ENDPOINT)
  956. ;
  957. #else
  958. USB_USBTask();
  959. #endif
  960. }
  961. print("USB configured.\n");
  962. #else
  963. USB_USBTask();
  964. #endif
  965. /* init modules */
  966. keyboard_init();
  967. host_set_driver(&lufa_driver);
  968. #ifdef SLEEP_LED_ENABLE
  969. sleep_led_init();
  970. #endif
  971. #ifdef VIRTSER_ENABLE
  972. virtser_init();
  973. #endif
  974. print("Keyboard start.\n");
  975. while (1) {
  976. #if !defined(BLUETOOTH_ENABLE) && !defined(ADAFRUIT_BLE_ENABLE)
  977. while (USB_DeviceState == DEVICE_STATE_Suspended) {
  978. print("[s]");
  979. suspend_power_down();
  980. if (USB_Device_RemoteWakeupEnabled && suspend_wakeup_condition()) {
  981. USB_Device_SendRemoteWakeup();
  982. }
  983. }
  984. #endif
  985. keyboard_task();
  986. #ifdef MIDI_ENABLE
  987. midi_device_process(&midi_device);
  988. // MIDI_Task();
  989. #endif
  990. #if defined(RGBLIGHT_ANIMATIONS) & defined(RGBLIGHT_ENABLE)
  991. rgblight_task();
  992. #endif
  993. #ifdef ADAFRUIT_BLE_ENABLE
  994. adafruit_ble_task();
  995. #endif
  996. #ifdef VIRTSER_ENABLE
  997. virtser_task();
  998. CDC_Device_USBTask(&cdc_device);
  999. #endif
  1000. #ifdef RAW_ENABLE
  1001. raw_hid_task();
  1002. #endif
  1003. #if !defined(INTERRUPT_CONTROL_ENDPOINT)
  1004. USB_USBTask();
  1005. #endif
  1006. }
  1007. }
  1008. #ifdef MIDI_ENABLE
  1009. void fallthrough_callback(MidiDevice * device,
  1010. uint16_t cnt, uint8_t byte0, uint8_t byte1, uint8_t byte2){
  1011. #ifdef AUDIO_ENABLE
  1012. if (cnt == 3) {
  1013. switch (byte0 & 0xF0) {
  1014. case MIDI_NOTEON:
  1015. play_note(((double)261.6)*pow(2.0, -4.0)*pow(2.0,(byte1 & 0x7F)/12.0), (byte2 & 0x7F) / 8);
  1016. break;
  1017. case MIDI_NOTEOFF:
  1018. stop_note(((double)261.6)*pow(2.0, -4.0)*pow(2.0,(byte1 & 0x7F)/12.0));
  1019. break;
  1020. }
  1021. }
  1022. if (byte0 == MIDI_STOP) {
  1023. stop_all_notes();
  1024. }
  1025. #endif
  1026. }
  1027. void cc_callback(MidiDevice * device,
  1028. uint8_t chan, uint8_t num, uint8_t val) {
  1029. //sending it back on the next channel
  1030. // midi_send_cc(device, (chan + 1) % 16, num, val);
  1031. }
  1032. #ifdef API_SYSEX_ENABLE
  1033. uint8_t midi_buffer[MIDI_SYSEX_BUFFER] = {0};
  1034. #endif
  1035. void sysex_callback(MidiDevice * device, uint16_t start, uint8_t length, uint8_t * data) {
  1036. #ifdef API_SYSEX_ENABLE
  1037. // SEND_STRING("\n");
  1038. // send_word(start);
  1039. // SEND_STRING(": ");
  1040. // Don't store the header
  1041. int16_t pos = start - 4;
  1042. for (uint8_t place = 0; place < length; place++) {
  1043. // send_byte(*data);
  1044. if (pos >= 0) {
  1045. if (*data == 0xF7) {
  1046. // SEND_STRING("\nRD: ");
  1047. // for (uint8_t i = 0; i < start + place + 1; i++){
  1048. // send_byte(midi_buffer[i]);
  1049. // SEND_STRING(" ");
  1050. // }
  1051. const unsigned decoded_length = sysex_decoded_length(pos);
  1052. uint8_t decoded[API_SYSEX_MAX_SIZE];
  1053. sysex_decode(decoded, midi_buffer, pos);
  1054. process_api(decoded_length, decoded);
  1055. return;
  1056. }
  1057. else if (pos >= MIDI_SYSEX_BUFFER) {
  1058. return;
  1059. }
  1060. midi_buffer[pos] = *data;
  1061. }
  1062. // SEND_STRING(" ");
  1063. data++;
  1064. pos++;
  1065. }
  1066. #endif
  1067. }
  1068. #endif