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.

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