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.

1295 lines
35 KiB

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