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.

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