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.

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