lufa.c 36 KB

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