You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

599 lines
21 KiB

  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. // If VIA_CUSTOM_LIGHTING_ENABLE is not defined, then VIA_QMK_BACKLIGHT_ENABLE is set
  23. // if BACKLIGHT_ENABLE is set, so handling of QMK Backlight values happens here by default.
  24. // if VIA_CUSTOM_LIGHTING_ENABLE is defined, then VIA_QMK_BACKLIGHT_ENABLE must be explicitly
  25. // set in keyboard-level config.h, so handling of QMK Backlight values happens here
  26. #if defined(BACKLIGHT_ENABLE) && !defined(VIA_CUSTOM_LIGHTING_ENABLE)
  27. # define VIA_QMK_BACKLIGHT_ENABLE
  28. #endif
  29. // If VIA_CUSTOM_LIGHTING_ENABLE is not defined, then VIA_QMK_RGBLIGHT_ENABLE is set
  30. // if RGBLIGHT_ENABLE is set, so handling of QMK RGBLIGHT values happens here by default.
  31. // If VIA_CUSTOM_LIGHTING_ENABLE is defined, then VIA_QMK_RGBLIGHT_ENABLE must be explicitly
  32. // set in keyboard-level config.h, so handling of QMK RGBLIGHT values happens here
  33. #if defined(RGBLIGHT_ENABLE) && !defined(VIA_CUSTOM_LIGHTING_ENABLE)
  34. # define VIA_QMK_RGBLIGHT_ENABLE
  35. #endif
  36. #if defined(RGB_MATRIX_ENABLE) && !defined(VIA_QMK_RGBLIGHT_ENABLE) && !defined(VIA_CUSTOM_LIGHTING_ENABLE)
  37. # define VIA_QMK_RGB_MATRIX_ENABLE
  38. #endif
  39. #include "quantum.h"
  40. #include "via.h"
  41. #include "raw_hid.h"
  42. #include "dynamic_keymap.h"
  43. #include "eeprom.h"
  44. #include "version.h" // for QMK_BUILDDATE used in EEPROM magic
  45. #include "via_ensure_keycode.h"
  46. // Forward declare some helpers.
  47. #if defined(VIA_QMK_BACKLIGHT_ENABLE)
  48. void via_qmk_backlight_set_value(uint8_t *data);
  49. void via_qmk_backlight_get_value(uint8_t *data);
  50. #endif
  51. #if defined(VIA_QMK_RGBLIGHT_ENABLE)
  52. void via_qmk_rgblight_set_value(uint8_t *data);
  53. void via_qmk_rgblight_get_value(uint8_t *data);
  54. #endif
  55. #if defined(VIA_QMK_RGB_MATRIX_ENABLE)
  56. void via_qmk_rgb_matrix_set_value(uint8_t *data);
  57. void via_qmk_rgb_matrix_get_value(uint8_t *data);
  58. void eeconfig_update_rgb_matrix(void);
  59. #endif
  60. // Can be called in an overriding via_init_kb() to test if keyboard level code usage of
  61. // EEPROM is invalid and use/save defaults.
  62. bool via_eeprom_is_valid(void) {
  63. char * p = QMK_BUILDDATE; // e.g. "2019-11-05-11:29:54"
  64. uint8_t magic0 = ((p[2] & 0x0F) << 4) | (p[3] & 0x0F);
  65. uint8_t magic1 = ((p[5] & 0x0F) << 4) | (p[6] & 0x0F);
  66. uint8_t magic2 = ((p[8] & 0x0F) << 4) | (p[9] & 0x0F);
  67. 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);
  68. }
  69. // Sets VIA/keyboard level usage of EEPROM to valid/invalid
  70. // Keyboard level code (eg. via_init_kb()) should not call this
  71. void via_eeprom_set_valid(bool valid) {
  72. char * p = QMK_BUILDDATE; // e.g. "2019-11-05-11:29:54"
  73. uint8_t magic0 = ((p[2] & 0x0F) << 4) | (p[3] & 0x0F);
  74. uint8_t magic1 = ((p[5] & 0x0F) << 4) | (p[6] & 0x0F);
  75. uint8_t magic2 = ((p[8] & 0x0F) << 4) | (p[9] & 0x0F);
  76. eeprom_update_byte((void *)VIA_EEPROM_MAGIC_ADDR + 0, valid ? magic0 : 0xFF);
  77. eeprom_update_byte((void *)VIA_EEPROM_MAGIC_ADDR + 1, valid ? magic1 : 0xFF);
  78. eeprom_update_byte((void *)VIA_EEPROM_MAGIC_ADDR + 2, valid ? magic2 : 0xFF);
  79. }
  80. // Override this at the keyboard code level to check
  81. // VIA's EEPROM valid state and reset to defaults as needed.
  82. // Used by keyboards that store their own state in EEPROM,
  83. // for backlight, rotary encoders, etc.
  84. // The override should not set via_eeprom_set_valid(true) as
  85. // the caller also needs to check the valid state.
  86. __attribute__((weak)) void via_init_kb(void) {}
  87. // Called by QMK core to initialize dynamic keymaps etc.
  88. void via_init(void) {
  89. // Let keyboard level test EEPROM valid state,
  90. // but not set it valid, it is done here.
  91. via_init_kb();
  92. via_set_layout_options_kb(via_get_layout_options());
  93. // If the EEPROM has the magic, the data is good.
  94. // OK to load from EEPROM.
  95. if (!via_eeprom_is_valid()) {
  96. eeconfig_init_via();
  97. }
  98. }
  99. void eeconfig_init_via(void) {
  100. // set the magic number to false, in case this gets interrupted
  101. via_eeprom_set_valid(false);
  102. // This resets the layout options
  103. via_set_layout_options(VIA_EEPROM_LAYOUT_OPTIONS_DEFAULT);
  104. // This resets the keymaps in EEPROM to what is in flash.
  105. dynamic_keymap_reset();
  106. // This resets the macros in EEPROM to nothing.
  107. dynamic_keymap_macro_reset();
  108. // Save the magic number last, in case saving was interrupted
  109. via_eeprom_set_valid(true);
  110. }
  111. // This is generalized so the layout options EEPROM usage can be
  112. // variable, between 1 and 4 bytes.
  113. uint32_t via_get_layout_options(void) {
  114. uint32_t value = 0;
  115. // Start at the most significant byte
  116. void *source = (void *)(VIA_EEPROM_LAYOUT_OPTIONS_ADDR);
  117. for (uint8_t i = 0; i < VIA_EEPROM_LAYOUT_OPTIONS_SIZE; i++) {
  118. value = value << 8;
  119. value |= eeprom_read_byte(source);
  120. source++;
  121. }
  122. return value;
  123. }
  124. __attribute__((weak)) void via_set_layout_options_kb(uint32_t value) {}
  125. void via_set_layout_options(uint32_t value) {
  126. via_set_layout_options_kb(value);
  127. // Start at the least significant byte
  128. void *target = (void *)(VIA_EEPROM_LAYOUT_OPTIONS_ADDR + VIA_EEPROM_LAYOUT_OPTIONS_SIZE - 1);
  129. for (uint8_t i = 0; i < VIA_EEPROM_LAYOUT_OPTIONS_SIZE; i++) {
  130. eeprom_update_byte(target, value & 0xFF);
  131. value = value >> 8;
  132. target--;
  133. }
  134. }
  135. // Called by QMK core to process VIA-specific keycodes.
  136. bool process_record_via(uint16_t keycode, keyrecord_t *record) {
  137. // Handle macros
  138. if (record->event.pressed) {
  139. if (keycode >= MACRO00 && keycode <= MACRO15) {
  140. uint8_t id = keycode - MACRO00;
  141. dynamic_keymap_macro_send(id);
  142. return false;
  143. }
  144. }
  145. // TODO: ideally this would be generalized and refactored into
  146. // QMK core as advanced keycodes, until then, the simple case
  147. // can be available here to keyboards using VIA
  148. switch (keycode) {
  149. case FN_MO13:
  150. if (record->event.pressed) {
  151. layer_on(1);
  152. update_tri_layer(1, 2, 3);
  153. } else {
  154. layer_off(1);
  155. update_tri_layer(1, 2, 3);
  156. }
  157. return false;
  158. break;
  159. case FN_MO23:
  160. if (record->event.pressed) {
  161. layer_on(2);
  162. update_tri_layer(1, 2, 3);
  163. } else {
  164. layer_off(2);
  165. update_tri_layer(1, 2, 3);
  166. }
  167. return false;
  168. break;
  169. }
  170. return true;
  171. }
  172. // Keyboard level code can override this to handle custom messages from VIA.
  173. // See raw_hid_receive() implementation.
  174. // DO NOT call raw_hid_send() in the override function.
  175. __attribute__((weak)) void raw_hid_receive_kb(uint8_t *data, uint8_t length) {
  176. uint8_t *command_id = &(data[0]);
  177. *command_id = id_unhandled;
  178. }
  179. // VIA handles received HID messages first, and will route to
  180. // raw_hid_receive_kb() for command IDs that are not handled here.
  181. // This gives the keyboard code level the ability to handle the command
  182. // specifically.
  183. //
  184. // raw_hid_send() is called at the end, with the same buffer, which was
  185. // possibly modified with returned values.
  186. void raw_hid_receive(uint8_t *data, uint8_t length) {
  187. uint8_t *command_id = &(data[0]);
  188. uint8_t *command_data = &(data[1]);
  189. switch (*command_id) {
  190. case id_get_protocol_version: {
  191. command_data[0] = VIA_PROTOCOL_VERSION >> 8;
  192. command_data[1] = VIA_PROTOCOL_VERSION & 0xFF;
  193. break;
  194. }
  195. case id_get_keyboard_value: {
  196. switch (command_data[0]) {
  197. case id_uptime: {
  198. uint32_t value = timer_read32();
  199. command_data[1] = (value >> 24) & 0xFF;
  200. command_data[2] = (value >> 16) & 0xFF;
  201. command_data[3] = (value >> 8) & 0xFF;
  202. command_data[4] = value & 0xFF;
  203. break;
  204. }
  205. case id_layout_options: {
  206. uint32_t value = via_get_layout_options();
  207. command_data[1] = (value >> 24) & 0xFF;
  208. command_data[2] = (value >> 16) & 0xFF;
  209. command_data[3] = (value >> 8) & 0xFF;
  210. command_data[4] = value & 0xFF;
  211. break;
  212. }
  213. case id_switch_matrix_state: {
  214. #if ((MATRIX_COLS / 8 + 1) * MATRIX_ROWS <= 28)
  215. uint8_t i = 1;
  216. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  217. matrix_row_t value = matrix_get_row(row);
  218. # if (MATRIX_COLS > 24)
  219. command_data[i++] = (value >> 24) & 0xFF;
  220. # endif
  221. # if (MATRIX_COLS > 16)
  222. command_data[i++] = (value >> 16) & 0xFF;
  223. # endif
  224. # if (MATRIX_COLS > 8)
  225. command_data[i++] = (value >> 8) & 0xFF;
  226. # endif
  227. command_data[i++] = value & 0xFF;
  228. }
  229. #endif
  230. break;
  231. }
  232. default: {
  233. raw_hid_receive_kb(data, length);
  234. break;
  235. }
  236. }
  237. break;
  238. }
  239. case id_set_keyboard_value: {
  240. switch (command_data[0]) {
  241. case id_layout_options: {
  242. 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];
  243. via_set_layout_options(value);
  244. break;
  245. }
  246. default: {
  247. raw_hid_receive_kb(data, length);
  248. break;
  249. }
  250. }
  251. break;
  252. }
  253. case id_dynamic_keymap_get_keycode: {
  254. uint16_t keycode = dynamic_keymap_get_keycode(command_data[0], command_data[1], command_data[2]);
  255. command_data[3] = keycode >> 8;
  256. command_data[4] = keycode & 0xFF;
  257. break;
  258. }
  259. case id_dynamic_keymap_set_keycode: {
  260. dynamic_keymap_set_keycode(command_data[0], command_data[1], command_data[2], (command_data[3] << 8) | command_data[4]);
  261. break;
  262. }
  263. case id_dynamic_keymap_reset: {
  264. dynamic_keymap_reset();
  265. break;
  266. }
  267. case id_lighting_set_value: {
  268. #if defined(VIA_QMK_BACKLIGHT_ENABLE)
  269. via_qmk_backlight_set_value(command_data);
  270. #endif
  271. #if defined(VIA_QMK_RGBLIGHT_ENABLE)
  272. via_qmk_rgblight_set_value(command_data);
  273. #endif
  274. #if defined(VIA_QMK_RGB_MATRIX_ENABLE)
  275. via_qmk_rgb_matrix_set_value(command_data);
  276. #endif
  277. #if defined(VIA_CUSTOM_LIGHTING_ENABLE)
  278. raw_hid_receive_kb(data, length);
  279. #endif
  280. #if !defined(VIA_QMK_BACKLIGHT_ENABLE) && !defined(VIA_QMK_RGBLIGHT_ENABLE) && !defined(VIA_CUSTOM_LIGHTING_ENABLE) && !defined(VIA_QMK_RGB_MATRIX_ENABLE)
  281. // Return the unhandled state
  282. *command_id = id_unhandled;
  283. #endif
  284. break;
  285. }
  286. case id_lighting_get_value: {
  287. #if defined(VIA_QMK_BACKLIGHT_ENABLE)
  288. via_qmk_backlight_get_value(command_data);
  289. #endif
  290. #if defined(VIA_QMK_RGBLIGHT_ENABLE)
  291. via_qmk_rgblight_get_value(command_data);
  292. #endif
  293. #if defined(VIA_QMK_RGB_MATRIX_ENABLE)
  294. via_qmk_rgb_matrix_get_value(command_data);
  295. #endif
  296. #if defined(VIA_CUSTOM_LIGHTING_ENABLE)
  297. raw_hid_receive_kb(data, length);
  298. #endif
  299. #if !defined(VIA_QMK_BACKLIGHT_ENABLE) && !defined(VIA_QMK_RGBLIGHT_ENABLE) && !defined(VIA_CUSTOM_LIGHTING_ENABLE) && !defined(VIA_QMK_RGB_MATRIX_ENABLE)
  300. // Return the unhandled state
  301. *command_id = id_unhandled;
  302. #endif
  303. break;
  304. }
  305. case id_lighting_save: {
  306. #if defined(VIA_QMK_BACKLIGHT_ENABLE)
  307. eeconfig_update_backlight_current();
  308. #endif
  309. #if defined(VIA_QMK_RGBLIGHT_ENABLE)
  310. eeconfig_update_rgblight_current();
  311. #endif
  312. #if defined(VIA_QMK_RGB_MATRIX_ENABLE)
  313. eeconfig_update_rgb_matrix();
  314. #endif
  315. #if defined(VIA_CUSTOM_LIGHTING_ENABLE)
  316. raw_hid_receive_kb(data, length);
  317. #endif
  318. #if !defined(VIA_QMK_BACKLIGHT_ENABLE) && !defined(VIA_QMK_RGBLIGHT_ENABLE) && !defined(VIA_CUSTOM_LIGHTING_ENABLE) && !defined(VIA_QMK_RGB_MATRIX_ENABLE)
  319. // Return the unhandled state
  320. *command_id = id_unhandled;
  321. #endif
  322. break;
  323. }
  324. #ifdef VIA_EEPROM_ALLOW_RESET
  325. case id_eeprom_reset: {
  326. via_eeprom_set_valid(false);
  327. eeconfig_init_via();
  328. break;
  329. }
  330. #endif
  331. case id_dynamic_keymap_macro_get_count: {
  332. command_data[0] = dynamic_keymap_macro_get_count();
  333. break;
  334. }
  335. case id_dynamic_keymap_macro_get_buffer_size: {
  336. uint16_t size = dynamic_keymap_macro_get_buffer_size();
  337. command_data[0] = size >> 8;
  338. command_data[1] = size & 0xFF;
  339. break;
  340. }
  341. case id_dynamic_keymap_macro_get_buffer: {
  342. uint16_t offset = (command_data[0] << 8) | command_data[1];
  343. uint16_t size = command_data[2]; // size <= 28
  344. dynamic_keymap_macro_get_buffer(offset, size, &command_data[3]);
  345. break;
  346. }
  347. case id_dynamic_keymap_macro_set_buffer: {
  348. uint16_t offset = (command_data[0] << 8) | command_data[1];
  349. uint16_t size = command_data[2]; // size <= 28
  350. dynamic_keymap_macro_set_buffer(offset, size, &command_data[3]);
  351. break;
  352. }
  353. case id_dynamic_keymap_macro_reset: {
  354. dynamic_keymap_macro_reset();
  355. break;
  356. }
  357. case id_dynamic_keymap_get_layer_count: {
  358. command_data[0] = dynamic_keymap_get_layer_count();
  359. break;
  360. }
  361. case id_dynamic_keymap_get_buffer: {
  362. uint16_t offset = (command_data[0] << 8) | command_data[1];
  363. uint16_t size = command_data[2]; // size <= 28
  364. dynamic_keymap_get_buffer(offset, size, &command_data[3]);
  365. break;
  366. }
  367. case id_dynamic_keymap_set_buffer: {
  368. uint16_t offset = (command_data[0] << 8) | command_data[1];
  369. uint16_t size = command_data[2]; // size <= 28
  370. dynamic_keymap_set_buffer(offset, size, &command_data[3]);
  371. break;
  372. }
  373. default: {
  374. // The command ID is not known
  375. // Return the unhandled state
  376. *command_id = id_unhandled;
  377. break;
  378. }
  379. }
  380. // Return the same buffer, optionally with values changed
  381. // (i.e. returning state to the host, or the unhandled state).
  382. raw_hid_send(data, length);
  383. }
  384. #if defined(VIA_QMK_BACKLIGHT_ENABLE)
  385. # if BACKLIGHT_LEVELS == 0
  386. # error BACKLIGHT_LEVELS == 0
  387. # endif
  388. void via_qmk_backlight_get_value(uint8_t *data) {
  389. uint8_t *value_id = &(data[0]);
  390. uint8_t *value_data = &(data[1]);
  391. switch (*value_id) {
  392. case id_qmk_backlight_brightness: {
  393. // level / BACKLIGHT_LEVELS * 255
  394. value_data[0] = ((uint16_t)get_backlight_level()) * 255 / BACKLIGHT_LEVELS;
  395. break;
  396. }
  397. case id_qmk_backlight_effect: {
  398. # ifdef BACKLIGHT_BREATHING
  399. value_data[0] = is_backlight_breathing() ? 1 : 0;
  400. # else
  401. value_data[0] = 0;
  402. # endif
  403. break;
  404. }
  405. }
  406. }
  407. void via_qmk_backlight_set_value(uint8_t *data) {
  408. uint8_t *value_id = &(data[0]);
  409. uint8_t *value_data = &(data[1]);
  410. switch (*value_id) {
  411. case id_qmk_backlight_brightness: {
  412. // level / 255 * BACKLIGHT_LEVELS
  413. backlight_level_noeeprom(((uint16_t)value_data[0]) * BACKLIGHT_LEVELS / 255);
  414. break;
  415. }
  416. case id_qmk_backlight_effect: {
  417. # ifdef BACKLIGHT_BREATHING
  418. if (value_data[0] == 0) {
  419. backlight_disable_breathing();
  420. } else {
  421. backlight_enable_breathing();
  422. }
  423. # endif
  424. break;
  425. }
  426. }
  427. }
  428. #endif // #if defined(VIA_QMK_BACKLIGHT_ENABLE)
  429. #if defined(VIA_QMK_RGBLIGHT_ENABLE)
  430. void via_qmk_rgblight_get_value(uint8_t *data) {
  431. uint8_t *value_id = &(data[0]);
  432. uint8_t *value_data = &(data[1]);
  433. switch (*value_id) {
  434. case id_qmk_rgblight_brightness: {
  435. value_data[0] = rgblight_get_val();
  436. break;
  437. }
  438. case id_qmk_rgblight_effect: {
  439. value_data[0] = rgblight_get_mode();
  440. break;
  441. }
  442. case id_qmk_rgblight_effect_speed: {
  443. value_data[0] = rgblight_get_speed();
  444. break;
  445. }
  446. case id_qmk_rgblight_color: {
  447. value_data[0] = rgblight_get_hue();
  448. value_data[1] = rgblight_get_sat();
  449. break;
  450. }
  451. }
  452. }
  453. void via_qmk_rgblight_set_value(uint8_t *data) {
  454. uint8_t *value_id = &(data[0]);
  455. uint8_t *value_data = &(data[1]);
  456. switch (*value_id) {
  457. case id_qmk_rgblight_brightness: {
  458. rgblight_sethsv_noeeprom(rgblight_get_hue(), rgblight_get_sat(), value_data[0]);
  459. break;
  460. }
  461. case id_qmk_rgblight_effect: {
  462. rgblight_mode_noeeprom(value_data[0]);
  463. if (value_data[0] == 0) {
  464. rgblight_disable_noeeprom();
  465. } else {
  466. rgblight_enable_noeeprom();
  467. }
  468. break;
  469. }
  470. case id_qmk_rgblight_effect_speed: {
  471. rgblight_set_speed_noeeprom(value_data[0]);
  472. break;
  473. }
  474. case id_qmk_rgblight_color: {
  475. rgblight_sethsv_noeeprom(value_data[0], value_data[1], rgblight_get_val());
  476. break;
  477. }
  478. }
  479. }
  480. #endif // #if defined(VIA_QMK_RGBLIGHT_ENABLE)
  481. #if defined(VIA_QMK_RGB_MATRIX_ENABLE)
  482. // VIA supports only 4 discrete values for effect speed; map these to some
  483. // useful speed values for RGB Matrix.
  484. enum speed_values {
  485. RGBLIGHT_SPEED_0 = UINT8_MAX / 16, // not 0 to avoid really slow effects
  486. RGBLIGHT_SPEED_1 = UINT8_MAX / 4,
  487. RGBLIGHT_SPEED_2 = UINT8_MAX / 2, // matches the default value
  488. RGBLIGHT_SPEED_3 = UINT8_MAX / 4 * 3, // UINT8_MAX is really fast
  489. };
  490. static uint8_t speed_from_rgblight(uint8_t rgblight_speed) {
  491. switch (rgblight_speed) {
  492. case 0:
  493. return RGBLIGHT_SPEED_0;
  494. case 1:
  495. return RGBLIGHT_SPEED_1;
  496. case 2:
  497. default:
  498. return RGBLIGHT_SPEED_2;
  499. case 3:
  500. return RGBLIGHT_SPEED_3;
  501. }
  502. }
  503. static uint8_t speed_to_rgblight(uint8_t rgb_matrix_speed) {
  504. if (rgb_matrix_speed < ((RGBLIGHT_SPEED_0 + RGBLIGHT_SPEED_1) / 2)) {
  505. return 0;
  506. } else if (rgb_matrix_speed < ((RGBLIGHT_SPEED_1 + RGBLIGHT_SPEED_2) / 2)) {
  507. return 1;
  508. } else if (rgb_matrix_speed < ((RGBLIGHT_SPEED_2 + RGBLIGHT_SPEED_3) / 2)) {
  509. return 2;
  510. } else {
  511. return 3;
  512. }
  513. }
  514. void via_qmk_rgb_matrix_get_value(uint8_t *data) {
  515. uint8_t *value_id = &(data[0]);
  516. uint8_t *value_data = &(data[1]);
  517. switch (*value_id) {
  518. case id_qmk_rgblight_brightness:
  519. value_data[0] = rgb_matrix_get_val();
  520. break;
  521. case id_qmk_rgblight_effect:
  522. value_data[0] = rgb_matrix_get_mode();
  523. break;
  524. case id_qmk_rgblight_effect_speed:
  525. value_data[0] = speed_to_rgblight(rgb_matrix_get_speed());
  526. break;
  527. case id_qmk_rgblight_color:
  528. value_data[0] = rgb_matrix_get_hue();
  529. value_data[1] = rgb_matrix_get_sat();
  530. break;
  531. }
  532. }
  533. void via_qmk_rgb_matrix_set_value(uint8_t *data) {
  534. uint8_t *value_id = &(data[0]);
  535. uint8_t *value_data = &(data[1]);
  536. switch (*value_id) {
  537. case id_qmk_rgblight_brightness:
  538. rgb_matrix_sethsv_noeeprom(rgb_matrix_get_hue(), rgb_matrix_get_sat(), value_data[0]);
  539. break;
  540. case id_qmk_rgblight_effect:
  541. rgb_matrix_mode_noeeprom(value_data[0]);
  542. if (value_data[0] == 0) {
  543. rgb_matrix_disable_noeeprom();
  544. } else {
  545. rgb_matrix_enable_noeeprom();
  546. }
  547. break;
  548. case id_qmk_rgblight_effect_speed:
  549. rgb_matrix_set_speed_noeeprom(speed_from_rgblight(value_data[0]));
  550. break;
  551. case id_qmk_rgblight_color:
  552. rgb_matrix_sethsv_noeeprom(value_data[0], value_data[1], rgb_matrix_get_val());
  553. break;
  554. }
  555. }
  556. #endif // #if defined(VIA_QMK_RGB_MATRIX_ENABLE)