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.

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