via.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788
  1. /* Copyright 2019 Jason Williams (Wilba)
  2. *
  3. * This program is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation, either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #ifndef RAW_ENABLE
  17. # error "RAW_ENABLE is not enabled"
  18. #endif
  19. #ifndef DYNAMIC_KEYMAP_ENABLE
  20. # error "DYNAMIC_KEYMAP_ENABLE is not enabled"
  21. #endif
  22. #include "quantum.h"
  23. #include "via.h"
  24. #include "raw_hid.h"
  25. #include "dynamic_keymap.h"
  26. #include "eeprom.h"
  27. #include "version.h" // for QMK_BUILDDATE used in EEPROM magic
  28. #if defined(RGB_MATRIX_ENABLE)
  29. # include <lib/lib8tion/lib8tion.h>
  30. #endif
  31. // Can be called in an overriding via_init_kb() to test if keyboard level code usage of
  32. // EEPROM is invalid and use/save defaults.
  33. bool via_eeprom_is_valid(void) {
  34. char * p = QMK_BUILDDATE; // e.g. "2019-11-05-11:29:54"
  35. uint8_t magic0 = ((p[2] & 0x0F) << 4) | (p[3] & 0x0F);
  36. uint8_t magic1 = ((p[5] & 0x0F) << 4) | (p[6] & 0x0F);
  37. uint8_t magic2 = ((p[8] & 0x0F) << 4) | (p[9] & 0x0F);
  38. return (eeprom_read_byte((void *)VIA_EEPROM_MAGIC_ADDR + 0) == magic0 && eeprom_read_byte((void *)VIA_EEPROM_MAGIC_ADDR + 1) == magic1 && eeprom_read_byte((void *)VIA_EEPROM_MAGIC_ADDR + 2) == magic2);
  39. }
  40. // Sets VIA/keyboard level usage of EEPROM to valid/invalid
  41. // Keyboard level code (eg. via_init_kb()) should not call this
  42. void via_eeprom_set_valid(bool valid) {
  43. char * p = QMK_BUILDDATE; // e.g. "2019-11-05-11:29:54"
  44. uint8_t magic0 = ((p[2] & 0x0F) << 4) | (p[3] & 0x0F);
  45. uint8_t magic1 = ((p[5] & 0x0F) << 4) | (p[6] & 0x0F);
  46. uint8_t magic2 = ((p[8] & 0x0F) << 4) | (p[9] & 0x0F);
  47. eeprom_update_byte((void *)VIA_EEPROM_MAGIC_ADDR + 0, valid ? magic0 : 0xFF);
  48. eeprom_update_byte((void *)VIA_EEPROM_MAGIC_ADDR + 1, valid ? magic1 : 0xFF);
  49. eeprom_update_byte((void *)VIA_EEPROM_MAGIC_ADDR + 2, valid ? magic2 : 0xFF);
  50. }
  51. // Override this at the keyboard code level to check
  52. // VIA's EEPROM valid state and reset to defaults as needed.
  53. // Used by keyboards that store their own state in EEPROM,
  54. // for backlight, rotary encoders, etc.
  55. // The override should not set via_eeprom_set_valid(true) as
  56. // the caller also needs to check the valid state.
  57. __attribute__((weak)) void via_init_kb(void) {}
  58. // Called by QMK core to initialize dynamic keymaps etc.
  59. void via_init(void) {
  60. // Let keyboard level test EEPROM valid state,
  61. // but not set it valid, it is done here.
  62. via_init_kb();
  63. via_set_layout_options_kb(via_get_layout_options());
  64. // If the EEPROM has the magic, the data is good.
  65. // OK to load from EEPROM.
  66. if (!via_eeprom_is_valid()) {
  67. eeconfig_init_via();
  68. }
  69. }
  70. void eeconfig_init_via(void) {
  71. // set the magic number to false, in case this gets interrupted
  72. via_eeprom_set_valid(false);
  73. // This resets the layout options
  74. via_set_layout_options(VIA_EEPROM_LAYOUT_OPTIONS_DEFAULT);
  75. // This resets the keymaps in EEPROM to what is in flash.
  76. dynamic_keymap_reset();
  77. // This resets the macros in EEPROM to nothing.
  78. dynamic_keymap_macro_reset();
  79. // Save the magic number last, in case saving was interrupted
  80. via_eeprom_set_valid(true);
  81. }
  82. // This is generalized so the layout options EEPROM usage can be
  83. // variable, between 1 and 4 bytes.
  84. uint32_t via_get_layout_options(void) {
  85. uint32_t value = 0;
  86. // Start at the most significant byte
  87. void *source = (void *)(VIA_EEPROM_LAYOUT_OPTIONS_ADDR);
  88. for (uint8_t i = 0; i < VIA_EEPROM_LAYOUT_OPTIONS_SIZE; i++) {
  89. value = value << 8;
  90. value |= eeprom_read_byte(source);
  91. source++;
  92. }
  93. return value;
  94. }
  95. __attribute__((weak)) void via_set_layout_options_kb(uint32_t value) {}
  96. void via_set_layout_options(uint32_t value) {
  97. via_set_layout_options_kb(value);
  98. // Start at the least significant byte
  99. void *target = (void *)(VIA_EEPROM_LAYOUT_OPTIONS_ADDR + VIA_EEPROM_LAYOUT_OPTIONS_SIZE - 1);
  100. for (uint8_t i = 0; i < VIA_EEPROM_LAYOUT_OPTIONS_SIZE; i++) {
  101. eeprom_update_byte(target, value & 0xFF);
  102. value = value >> 8;
  103. target--;
  104. }
  105. }
  106. #if defined(AUDIO_ENABLE)
  107. float via_device_indication_song[][2] = SONG(STARTUP_SOUND);
  108. #endif // AUDIO_ENABLE
  109. // Used by VIA to tell a device to flash LEDs (or do something else) when that
  110. // device becomes the active device being configured, on startup or switching
  111. // between devices. This function will be called six times, at 200ms interval,
  112. // with an incrementing value starting at zero. Since this function is called
  113. // an even number of times, it can call a toggle function and leave things in
  114. // the original state.
  115. __attribute__((weak)) void via_set_device_indication(uint8_t value) {
  116. #if defined(BACKLIGHT_ENABLE)
  117. backlight_toggle();
  118. #endif // BACKLIGHT_ENABLE
  119. #if defined(RGBLIGHT_ENABLE)
  120. rgblight_toggle_noeeprom();
  121. #endif // RGBLIGHT_ENABLE
  122. #if defined(RGB_MATRIX_ENABLE)
  123. rgb_matrix_toggle_noeeprom();
  124. #endif // RGB_MATRIX_ENABLE
  125. #if defined(AUDIO_ENABLE)
  126. if (value == 0) {
  127. wait_ms(10);
  128. PLAY_SONG(via_device_indication_song);
  129. }
  130. #endif // AUDIO_ENABLE
  131. }
  132. // Called by QMK core to process VIA-specific keycodes.
  133. bool process_record_via(uint16_t keycode, keyrecord_t *record) {
  134. // Handle macros
  135. if (record->event.pressed) {
  136. if (keycode >= MACRO00 && keycode <= MACRO15) {
  137. uint8_t id = keycode - MACRO00;
  138. dynamic_keymap_macro_send(id);
  139. return false;
  140. }
  141. }
  142. // TODO: ideally this would be generalized and refactored into
  143. // QMK core as advanced keycodes, until then, the simple case
  144. // can be available here to keyboards using VIA
  145. switch (keycode) {
  146. case FN_MO13:
  147. if (record->event.pressed) {
  148. layer_on(1);
  149. update_tri_layer(1, 2, 3);
  150. } else {
  151. layer_off(1);
  152. update_tri_layer(1, 2, 3);
  153. }
  154. return false;
  155. break;
  156. case FN_MO23:
  157. if (record->event.pressed) {
  158. layer_on(2);
  159. update_tri_layer(1, 2, 3);
  160. } else {
  161. layer_off(2);
  162. update_tri_layer(1, 2, 3);
  163. }
  164. return false;
  165. break;
  166. }
  167. return true;
  168. }
  169. //
  170. // via_custom_value_command() has the default handling of custom values for Core modules.
  171. // If a keyboard is using the default Core modules, it does not need to be overridden,
  172. // the VIA keyboard definition will have matching channel/IDs.
  173. //
  174. // If a keyboard has some extra custom values, then via_custom_value_command_kb() can be
  175. // overridden to handle the extra custom values, leaving via_custom_value_command() to
  176. // handle the custom values for Core modules.
  177. //
  178. // If a keyboard has custom values and code that are overlapping with Core modules,
  179. // then via_custom_value_command() can be overridden and call the same functions
  180. // as the default implementation, or do whatever else is required.
  181. //
  182. // DO NOT call raw_hid_send() in the override function.
  183. //
  184. // This is the default handler for "extra" custom values, i.e. keyboard-specific custom values
  185. // that are not handled by via_custom_value_command().
  186. __attribute__((weak)) void via_custom_value_command_kb(uint8_t *data, uint8_t length) {
  187. // data = [ command_id, channel_id, value_id, value_data ]
  188. uint8_t *command_id = &(data[0]);
  189. // Return the unhandled state
  190. *command_id = id_unhandled;
  191. }
  192. // This is the default handler for custom value commands.
  193. // It routes commands with channel IDs to command handlers as such:
  194. //
  195. // id_qmk_backlight_channel -> via_qmk_backlight_command()
  196. // id_qmk_rgblight_channel -> via_qmk_rgblight_command()
  197. // id_qmk_rgb_matrix_channel -> via_qmk_rgb_matrix_command()
  198. // id_qmk_audio_channel -> via_qmk_audio_command()
  199. //
  200. __attribute__((weak)) void via_custom_value_command(uint8_t *data, uint8_t length) {
  201. // data = [ command_id, channel_id, value_id, value_data ]
  202. uint8_t *channel_id = &(data[1]);
  203. #if defined(BACKLIGHT_ENABLE)
  204. if (*channel_id == id_qmk_backlight_channel) {
  205. via_qmk_backlight_command(data, length);
  206. return;
  207. }
  208. #endif // BACKLIGHT_ENABLE
  209. #if defined(RGBLIGHT_ENABLE)
  210. if (*channel_id == id_qmk_rgblight_channel) {
  211. via_qmk_rgblight_command(data, length);
  212. return;
  213. }
  214. #endif // RGBLIGHT_ENABLE
  215. #if defined(RGB_MATRIX_ENABLE)
  216. if (*channel_id == id_qmk_rgb_matrix_channel) {
  217. via_qmk_rgb_matrix_command(data, length);
  218. return;
  219. }
  220. #endif // RGBLIGHT_ENABLE
  221. #if defined(AUDIO_ENABLE)
  222. if (*channel_id == id_qmk_audio_channel) {
  223. via_qmk_audio_command(data, length);
  224. return;
  225. }
  226. #endif // AUDIO_ENABLE
  227. (void)channel_id; // force use of variable
  228. // If we haven't returned before here, then let the keyboard level code
  229. // handle this, if it is overridden, otherwise by default, this will
  230. // return the unhandled state.
  231. via_custom_value_command_kb(data, length);
  232. }
  233. // Keyboard level code can override this, but shouldn't need to.
  234. // Controlling custom features should be done by overriding
  235. // via_custom_value_command_kb() instead.
  236. __attribute__((weak)) bool via_command_kb(uint8_t *data, uint8_t length) {
  237. return false;
  238. }
  239. void raw_hid_receive(uint8_t *data, uint8_t length) {
  240. uint8_t *command_id = &(data[0]);
  241. uint8_t *command_data = &(data[1]);
  242. // If via_command_kb() returns true, the command was fully
  243. // handled, including calling raw_hid_send()
  244. if (via_command_kb(data, length)) {
  245. return;
  246. }
  247. switch (*command_id) {
  248. case id_get_protocol_version: {
  249. command_data[0] = VIA_PROTOCOL_VERSION >> 8;
  250. command_data[1] = VIA_PROTOCOL_VERSION & 0xFF;
  251. break;
  252. }
  253. case id_get_keyboard_value: {
  254. switch (command_data[0]) {
  255. case id_uptime: {
  256. uint32_t value = timer_read32();
  257. command_data[1] = (value >> 24) & 0xFF;
  258. command_data[2] = (value >> 16) & 0xFF;
  259. command_data[3] = (value >> 8) & 0xFF;
  260. command_data[4] = value & 0xFF;
  261. break;
  262. }
  263. case id_layout_options: {
  264. uint32_t value = via_get_layout_options();
  265. command_data[1] = (value >> 24) & 0xFF;
  266. command_data[2] = (value >> 16) & 0xFF;
  267. command_data[3] = (value >> 8) & 0xFF;
  268. command_data[4] = value & 0xFF;
  269. break;
  270. }
  271. case id_switch_matrix_state: {
  272. // Round up to the nearest number of bytes required to hold row state.
  273. // Multiply by number of rows to get the required size in bytes.
  274. // Guard against this being too big for the HID message.
  275. #if (((MATRIX_COLS + 7) / 8) * MATRIX_ROWS <= 28)
  276. uint8_t i = 1;
  277. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  278. matrix_row_t value = matrix_get_row(row);
  279. # if (MATRIX_COLS > 24)
  280. command_data[i++] = (value >> 24) & 0xFF;
  281. # endif
  282. # if (MATRIX_COLS > 16)
  283. command_data[i++] = (value >> 16) & 0xFF;
  284. # endif
  285. # if (MATRIX_COLS > 8)
  286. command_data[i++] = (value >> 8) & 0xFF;
  287. # endif
  288. command_data[i++] = value & 0xFF;
  289. }
  290. #endif
  291. break;
  292. }
  293. case id_firmware_version: {
  294. uint32_t value = VIA_FIRMWARE_VERSION;
  295. command_data[1] = (value >> 24) & 0xFF;
  296. command_data[2] = (value >> 16) & 0xFF;
  297. command_data[3] = (value >> 8) & 0xFF;
  298. command_data[4] = value & 0xFF;
  299. break;
  300. }
  301. default: {
  302. // The value ID is not known
  303. // Return the unhandled state
  304. *command_id = id_unhandled;
  305. break;
  306. }
  307. }
  308. break;
  309. }
  310. case id_set_keyboard_value: {
  311. switch (command_data[0]) {
  312. case id_layout_options: {
  313. uint32_t value = ((uint32_t)command_data[1] << 24) | ((uint32_t)command_data[2] << 16) | ((uint32_t)command_data[3] << 8) | (uint32_t)command_data[4];
  314. via_set_layout_options(value);
  315. break;
  316. }
  317. case id_device_indication: {
  318. uint8_t value = command_data[1];
  319. via_set_device_indication(value);
  320. break;
  321. }
  322. default: {
  323. // The value ID is not known
  324. // Return the unhandled state
  325. *command_id = id_unhandled;
  326. break;
  327. }
  328. }
  329. break;
  330. }
  331. case id_dynamic_keymap_get_keycode: {
  332. uint16_t keycode = dynamic_keymap_get_keycode(command_data[0], command_data[1], command_data[2]);
  333. command_data[3] = keycode >> 8;
  334. command_data[4] = keycode & 0xFF;
  335. break;
  336. }
  337. case id_dynamic_keymap_set_keycode: {
  338. dynamic_keymap_set_keycode(command_data[0], command_data[1], command_data[2], (command_data[3] << 8) | command_data[4]);
  339. break;
  340. }
  341. case id_dynamic_keymap_reset: {
  342. dynamic_keymap_reset();
  343. break;
  344. }
  345. case id_custom_set_value:
  346. case id_custom_get_value:
  347. case id_custom_save: {
  348. via_custom_value_command(data, length);
  349. break;
  350. }
  351. #ifdef VIA_EEPROM_ALLOW_RESET
  352. case id_eeprom_reset: {
  353. via_eeprom_set_valid(false);
  354. eeconfig_init_via();
  355. break;
  356. }
  357. #endif
  358. case id_dynamic_keymap_macro_get_count: {
  359. command_data[0] = dynamic_keymap_macro_get_count();
  360. break;
  361. }
  362. case id_dynamic_keymap_macro_get_buffer_size: {
  363. uint16_t size = dynamic_keymap_macro_get_buffer_size();
  364. command_data[0] = size >> 8;
  365. command_data[1] = size & 0xFF;
  366. break;
  367. }
  368. case id_dynamic_keymap_macro_get_buffer: {
  369. uint16_t offset = (command_data[0] << 8) | command_data[1];
  370. uint16_t size = command_data[2]; // size <= 28
  371. dynamic_keymap_macro_get_buffer(offset, size, &command_data[3]);
  372. break;
  373. }
  374. case id_dynamic_keymap_macro_set_buffer: {
  375. uint16_t offset = (command_data[0] << 8) | command_data[1];
  376. uint16_t size = command_data[2]; // size <= 28
  377. dynamic_keymap_macro_set_buffer(offset, size, &command_data[3]);
  378. break;
  379. }
  380. case id_dynamic_keymap_macro_reset: {
  381. dynamic_keymap_macro_reset();
  382. break;
  383. }
  384. case id_dynamic_keymap_get_layer_count: {
  385. command_data[0] = dynamic_keymap_get_layer_count();
  386. break;
  387. }
  388. case id_dynamic_keymap_get_buffer: {
  389. uint16_t offset = (command_data[0] << 8) | command_data[1];
  390. uint16_t size = command_data[2]; // size <= 28
  391. dynamic_keymap_get_buffer(offset, size, &command_data[3]);
  392. break;
  393. }
  394. case id_dynamic_keymap_set_buffer: {
  395. uint16_t offset = (command_data[0] << 8) | command_data[1];
  396. uint16_t size = command_data[2]; // size <= 28
  397. dynamic_keymap_set_buffer(offset, size, &command_data[3]);
  398. break;
  399. }
  400. #ifdef ENCODER_MAP_ENABLE
  401. case id_dynamic_keymap_get_encoder: {
  402. uint16_t keycode = dynamic_keymap_get_encoder(command_data[0], command_data[1], command_data[2] != 0);
  403. command_data[3] = keycode >> 8;
  404. command_data[4] = keycode & 0xFF;
  405. break;
  406. }
  407. case id_dynamic_keymap_set_encoder: {
  408. dynamic_keymap_set_encoder(command_data[0], command_data[1], command_data[2] != 0, (command_data[3] << 8) | command_data[4]);
  409. break;
  410. }
  411. #endif
  412. default: {
  413. // The command ID is not known
  414. // Return the unhandled state
  415. *command_id = id_unhandled;
  416. break;
  417. }
  418. }
  419. // Return the same buffer, optionally with values changed
  420. // (i.e. returning state to the host, or the unhandled state).
  421. raw_hid_send(data, length);
  422. }
  423. #if defined(BACKLIGHT_ENABLE)
  424. void via_qmk_backlight_command(uint8_t *data, uint8_t length) {
  425. // data = [ command_id, channel_id, value_id, value_data ]
  426. uint8_t *command_id = &(data[0]);
  427. uint8_t *value_id_and_data = &(data[2]);
  428. switch (*command_id) {
  429. case id_custom_set_value: {
  430. via_qmk_backlight_set_value(value_id_and_data);
  431. break;
  432. }
  433. case id_custom_get_value: {
  434. via_qmk_backlight_get_value(value_id_and_data);
  435. break;
  436. }
  437. case id_custom_save: {
  438. via_qmk_backlight_save();
  439. break;
  440. }
  441. default: {
  442. *command_id = id_unhandled;
  443. break;
  444. }
  445. }
  446. }
  447. # if BACKLIGHT_LEVELS == 0
  448. # error BACKLIGHT_LEVELS == 0
  449. # endif
  450. void via_qmk_backlight_get_value(uint8_t *data) {
  451. // data = [ value_id, value_data ]
  452. uint8_t *value_id = &(data[0]);
  453. uint8_t *value_data = &(data[1]);
  454. switch (*value_id) {
  455. case id_qmk_backlight_brightness: {
  456. // level / BACKLIGHT_LEVELS * 255
  457. value_data[0] = ((uint16_t)get_backlight_level() * UINT8_MAX) / BACKLIGHT_LEVELS;
  458. break;
  459. }
  460. case id_qmk_backlight_effect: {
  461. # ifdef BACKLIGHT_BREATHING
  462. value_data[0] = is_backlight_breathing() ? 1 : 0;
  463. # else
  464. value_data[0] = 0;
  465. # endif
  466. break;
  467. }
  468. }
  469. }
  470. void via_qmk_backlight_set_value(uint8_t *data) {
  471. // data = [ value_id, value_data ]
  472. uint8_t *value_id = &(data[0]);
  473. uint8_t *value_data = &(data[1]);
  474. switch (*value_id) {
  475. case id_qmk_backlight_brightness: {
  476. // level / 255 * BACKLIGHT_LEVELS
  477. backlight_level_noeeprom(((uint16_t)value_data[0] * BACKLIGHT_LEVELS) / UINT8_MAX);
  478. break;
  479. }
  480. case id_qmk_backlight_effect: {
  481. # ifdef BACKLIGHT_BREATHING
  482. if (value_data[0] == 0) {
  483. backlight_disable_breathing();
  484. } else {
  485. backlight_enable_breathing();
  486. }
  487. # endif
  488. break;
  489. }
  490. }
  491. }
  492. void via_qmk_backlight_save(void) {
  493. eeconfig_update_backlight_current();
  494. }
  495. #endif // BACKLIGHT_ENABLE
  496. #if defined(RGBLIGHT_ENABLE)
  497. # ifndef RGBLIGHT_LIMIT_VAL
  498. # define RGBLIGHT_LIMIT_VAL 255
  499. # endif
  500. void via_qmk_rgblight_command(uint8_t *data, uint8_t length) {
  501. // data = [ command_id, channel_id, value_id, value_data ]
  502. uint8_t *command_id = &(data[0]);
  503. uint8_t *value_id_and_data = &(data[2]);
  504. switch (*command_id) {
  505. case id_custom_set_value: {
  506. via_qmk_rgblight_set_value(value_id_and_data);
  507. break;
  508. }
  509. case id_custom_get_value: {
  510. via_qmk_rgblight_get_value(value_id_and_data);
  511. break;
  512. }
  513. case id_custom_save: {
  514. via_qmk_rgblight_save();
  515. break;
  516. }
  517. default: {
  518. *command_id = id_unhandled;
  519. break;
  520. }
  521. }
  522. }
  523. void via_qmk_rgblight_get_value(uint8_t *data) {
  524. // data = [ value_id, value_data ]
  525. uint8_t *value_id = &(data[0]);
  526. uint8_t *value_data = &(data[1]);
  527. switch (*value_id) {
  528. case id_qmk_rgblight_brightness: {
  529. value_data[0] = ((uint16_t)rgblight_get_val() * UINT8_MAX) / RGBLIGHT_LIMIT_VAL;
  530. break;
  531. }
  532. case id_qmk_rgblight_effect: {
  533. value_data[0] = rgblight_is_enabled() ? rgblight_get_mode() : 0;
  534. break;
  535. }
  536. case id_qmk_rgblight_effect_speed: {
  537. value_data[0] = rgblight_get_speed();
  538. break;
  539. }
  540. case id_qmk_rgblight_color: {
  541. value_data[0] = rgblight_get_hue();
  542. value_data[1] = rgblight_get_sat();
  543. break;
  544. }
  545. }
  546. }
  547. void via_qmk_rgblight_set_value(uint8_t *data) {
  548. // data = [ value_id, value_data ]
  549. uint8_t *value_id = &(data[0]);
  550. uint8_t *value_data = &(data[1]);
  551. switch (*value_id) {
  552. case id_qmk_rgblight_brightness: {
  553. rgblight_sethsv_noeeprom(rgblight_get_hue(), rgblight_get_sat(), ((uint16_t)value_data[0] * RGBLIGHT_LIMIT_VAL) / UINT8_MAX);
  554. break;
  555. }
  556. case id_qmk_rgblight_effect: {
  557. if (value_data[0] == 0) {
  558. rgblight_disable_noeeprom();
  559. } else {
  560. rgblight_enable_noeeprom();
  561. rgblight_mode_noeeprom(value_data[0]);
  562. }
  563. break;
  564. }
  565. case id_qmk_rgblight_effect_speed: {
  566. rgblight_set_speed_noeeprom(value_data[0]);
  567. break;
  568. }
  569. case id_qmk_rgblight_color: {
  570. rgblight_sethsv_noeeprom(value_data[0], value_data[1], rgblight_get_val());
  571. break;
  572. }
  573. }
  574. }
  575. void via_qmk_rgblight_save(void) {
  576. eeconfig_update_rgblight_current();
  577. }
  578. #endif // QMK_RGBLIGHT_ENABLE
  579. #if defined(RGB_MATRIX_ENABLE)
  580. # if !defined(RGB_MATRIX_MAXIMUM_BRIGHTNESS) || RGB_MATRIX_MAXIMUM_BRIGHTNESS > UINT8_MAX
  581. # undef RGB_MATRIX_MAXIMUM_BRIGHTNESS
  582. # define RGB_MATRIX_MAXIMUM_BRIGHTNESS UINT8_MAX
  583. # endif
  584. void via_qmk_rgb_matrix_command(uint8_t *data, uint8_t length) {
  585. // data = [ command_id, channel_id, value_id, value_data ]
  586. uint8_t *command_id = &(data[0]);
  587. uint8_t *value_id_and_data = &(data[2]);
  588. switch (*command_id) {
  589. case id_custom_set_value: {
  590. via_qmk_rgb_matrix_set_value(value_id_and_data);
  591. break;
  592. }
  593. case id_custom_get_value: {
  594. via_qmk_rgb_matrix_get_value(value_id_and_data);
  595. break;
  596. }
  597. case id_custom_save: {
  598. via_qmk_rgb_matrix_save();
  599. break;
  600. }
  601. default: {
  602. *command_id = id_unhandled;
  603. break;
  604. }
  605. }
  606. }
  607. void via_qmk_rgb_matrix_get_value(uint8_t *data) {
  608. // data = [ value_id, value_data ]
  609. uint8_t *value_id = &(data[0]);
  610. uint8_t *value_data = &(data[1]);
  611. switch (*value_id) {
  612. case id_qmk_rgb_matrix_brightness: {
  613. value_data[0] = ((uint16_t)rgb_matrix_get_val() * UINT8_MAX) / RGB_MATRIX_MAXIMUM_BRIGHTNESS;
  614. break;
  615. }
  616. case id_qmk_rgb_matrix_effect: {
  617. value_data[0] = rgb_matrix_is_enabled() ? rgb_matrix_get_mode() : 0;
  618. break;
  619. }
  620. case id_qmk_rgb_matrix_effect_speed: {
  621. value_data[0] = rgb_matrix_get_speed();
  622. break;
  623. }
  624. case id_qmk_rgb_matrix_color: {
  625. value_data[0] = rgb_matrix_get_hue();
  626. value_data[1] = rgb_matrix_get_sat();
  627. break;
  628. }
  629. }
  630. }
  631. void via_qmk_rgb_matrix_set_value(uint8_t *data) {
  632. // data = [ value_id, value_data ]
  633. uint8_t *value_id = &(data[0]);
  634. uint8_t *value_data = &(data[1]);
  635. switch (*value_id) {
  636. case id_qmk_rgb_matrix_brightness: {
  637. rgb_matrix_sethsv_noeeprom(rgb_matrix_get_hue(), rgb_matrix_get_sat(), scale8(value_data[0], RGB_MATRIX_MAXIMUM_BRIGHTNESS));
  638. break;
  639. }
  640. case id_qmk_rgb_matrix_effect: {
  641. if (value_data[0] == 0) {
  642. rgb_matrix_disable_noeeprom();
  643. } else {
  644. rgb_matrix_enable_noeeprom();
  645. rgb_matrix_mode_noeeprom(value_data[0]);
  646. }
  647. break;
  648. }
  649. case id_qmk_rgb_matrix_effect_speed: {
  650. rgblight_set_speed_noeeprom(value_data[0]);
  651. break;
  652. }
  653. case id_qmk_rgb_matrix_color: {
  654. rgblight_sethsv_noeeprom(value_data[0], value_data[1], rgb_matrix_get_val());
  655. break;
  656. }
  657. }
  658. }
  659. void via_qmk_rgb_matrix_save(void) {
  660. eeconfig_update_rgb_matrix();
  661. }
  662. #endif // RGB_MATRIX_ENABLE
  663. #if defined(AUDIO_ENABLE)
  664. extern audio_config_t audio_config;
  665. void via_qmk_audio_command(uint8_t *data, uint8_t length) {
  666. // data = [ command_id, channel_id, value_id, value_data ]
  667. uint8_t *command_id = &(data[0]);
  668. uint8_t *value_id_and_data = &(data[2]);
  669. switch (*command_id) {
  670. case id_custom_set_value: {
  671. via_qmk_audio_set_value(value_id_and_data);
  672. break;
  673. }
  674. case id_custom_get_value: {
  675. via_qmk_audio_get_value(value_id_and_data);
  676. break;
  677. }
  678. case id_custom_save: {
  679. via_qmk_audio_save();
  680. break;
  681. }
  682. default: {
  683. *command_id = id_unhandled;
  684. break;
  685. }
  686. }
  687. }
  688. void via_qmk_audio_get_value(uint8_t *data) {
  689. // data = [ value_id, value_data ]
  690. uint8_t *value_id = &(data[0]);
  691. uint8_t *value_data = &(data[1]);
  692. switch (*value_id) {
  693. case id_qmk_audio_enable: {
  694. value_data[0] = audio_config.enable ? 1 : 0;
  695. break;
  696. }
  697. case id_qmk_audio_clicky_enable: {
  698. value_data[0] = audio_config.clicky_enable ? 1 : 0;
  699. break;
  700. }
  701. }
  702. }
  703. void via_qmk_audio_set_value(uint8_t *data) {
  704. // data = [ value_id, value_data ]
  705. uint8_t *value_id = &(data[0]);
  706. uint8_t *value_data = &(data[1]);
  707. switch (*value_id) {
  708. case id_qmk_audio_enable: {
  709. audio_config.enable = value_data[0] ? 1 : 0;
  710. break;
  711. }
  712. case id_qmk_audio_clicky_enable: {
  713. audio_config.clicky_enable = value_data[0] ? 1 : 0;
  714. break;
  715. }
  716. }
  717. }
  718. void via_qmk_audio_save(void) {
  719. eeconfig_update_audio(audio_config.raw);
  720. }
  721. #endif // QMK_AUDIO_ENABLE