lufa.c 37 KB

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