lufa.c 36 KB

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