via.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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. #include "quantum.h"
  23. #include "via.h"
  24. #include "raw_hid.h"
  25. #include "dynamic_keymap.h"
  26. #include "tmk_core/common/eeprom.h"
  27. #include "version.h" // for QMK_BUILDDATE used in EEPROM magic
  28. // Can be called in an overriding via_init_kb() to test if keyboard level code usage of
  29. // EEPROM is invalid and use/save defaults.
  30. bool via_eeprom_is_valid(void) {
  31. char * p = QMK_BUILDDATE; // e.g. "2019-11-05-11:29:54"
  32. uint8_t magic0 = ((p[2] & 0x0F) << 4) | (p[3] & 0x0F);
  33. uint8_t magic1 = ((p[5] & 0x0F) << 4) | (p[6] & 0x0F);
  34. uint8_t magic2 = ((p[8] & 0x0F) << 4) | (p[9] & 0x0F);
  35. 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);
  36. }
  37. // Sets VIA/keyboard level usage of EEPROM to valid/invalid
  38. // Keyboard level code (eg. via_init_kb()) should not call this
  39. void via_eeprom_set_valid(bool valid) {
  40. char * p = QMK_BUILDDATE; // e.g. "2019-11-05-11:29:54"
  41. uint8_t magic0 = ((p[2] & 0x0F) << 4) | (p[3] & 0x0F);
  42. uint8_t magic1 = ((p[5] & 0x0F) << 4) | (p[6] & 0x0F);
  43. uint8_t magic2 = ((p[8] & 0x0F) << 4) | (p[9] & 0x0F);
  44. eeprom_update_byte((void *)VIA_EEPROM_MAGIC_ADDR + 0, valid ? magic0 : 0xFF);
  45. eeprom_update_byte((void *)VIA_EEPROM_MAGIC_ADDR + 1, valid ? magic1 : 0xFF);
  46. eeprom_update_byte((void *)VIA_EEPROM_MAGIC_ADDR + 2, valid ? magic2 : 0xFF);
  47. }
  48. // Flag QMK and VIA/keyboard level EEPROM as invalid.
  49. // Used in bootmagic_lite() and VIA command handler.
  50. // Keyboard level code should not need to call this.
  51. void via_eeprom_reset(void) {
  52. // Set the VIA specific EEPROM state as invalid.
  53. via_eeprom_set_valid(false);
  54. // Set the TMK/QMK EEPROM state as invalid.
  55. eeconfig_disable();
  56. }
  57. // Override bootmagic_lite() so it can flag EEPROM as invalid
  58. // as well as jump to bootloader, thus performing a "factory reset"
  59. // of dynamic keymaps and optionally backlight/other settings.
  60. void bootmagic_lite(void) {
  61. // The lite version of TMK's bootmagic based on Wilba.
  62. // 100% less potential for accidentally making the
  63. // keyboard do stupid things.
  64. // We need multiple scans because debouncing can't be turned off.
  65. matrix_scan();
  66. #if defined(DEBOUNCE) && DEBOUNCE > 0
  67. wait_ms(DEBOUNCE * 2);
  68. #else
  69. wait_ms(30);
  70. #endif
  71. matrix_scan();
  72. // If the Esc and space bar are held down on power up,
  73. // reset the EEPROM valid state and jump to bootloader.
  74. // Assumes Esc is at [0,0].
  75. // This isn't very generalized, but we need something that doesn't
  76. // rely on user's keymaps in firmware or EEPROM.
  77. if (matrix_get_row(BOOTMAGIC_LITE_ROW) & (1 << BOOTMAGIC_LITE_COLUMN)) {
  78. // This is the only difference from the default implementation.
  79. via_eeprom_reset();
  80. // Jump to bootloader.
  81. bootloader_jump();
  82. }
  83. }
  84. // Override this at the keyboard code level to check
  85. // VIA's EEPROM valid state and reset to defaults as needed.
  86. // Used by keyboards that store their own state in EEPROM,
  87. // for backlight, rotary encoders, etc.
  88. // The override should not set via_eeprom_set_valid(true) as
  89. // the caller also needs to check the valid state.
  90. __attribute__((weak)) void via_init_kb(void) {}
  91. // Called by QMK core to initialize dynamic keymaps etc.
  92. void via_init(void) {
  93. // Let keyboard level test EEPROM valid state,
  94. // but not set it valid, it is done here.
  95. via_init_kb();
  96. // If the EEPROM has the magic, the data is good.
  97. // OK to load from EEPROM.
  98. if (via_eeprom_is_valid()) {
  99. } else {
  100. // This resets the layout options
  101. via_set_layout_options(0);
  102. // This resets the keymaps in EEPROM to what is in flash.
  103. dynamic_keymap_reset();
  104. // This resets the macros in EEPROM to nothing.
  105. dynamic_keymap_macro_reset();
  106. // Save the magic number last, in case saving was interrupted
  107. via_eeprom_set_valid(true);
  108. }
  109. }
  110. // This is generalized so the layout options EEPROM usage can be
  111. // variable, between 1 and 4 bytes.
  112. uint32_t via_get_layout_options(void) {
  113. uint32_t value = 0;
  114. // Start at the most significant byte
  115. void *source = (void *)(VIA_EEPROM_LAYOUT_OPTIONS_ADDR);
  116. for (uint8_t i = 0; i < VIA_EEPROM_LAYOUT_OPTIONS_SIZE; i++) {
  117. value = value << 8;
  118. value |= eeprom_read_byte(source);
  119. source++;
  120. }
  121. return value;
  122. }
  123. void via_set_layout_options(uint32_t value) {
  124. // Start at the least significant byte
  125. void *target = (void *)(VIA_EEPROM_LAYOUT_OPTIONS_ADDR + VIA_EEPROM_LAYOUT_OPTIONS_SIZE - 1);
  126. for (uint8_t i = 0; i < VIA_EEPROM_LAYOUT_OPTIONS_SIZE; i++) {
  127. eeprom_update_byte(target, value & 0xFF);
  128. value = value >> 8;
  129. target--;
  130. }
  131. }
  132. // Called by QMK core to process VIA-specific keycodes.
  133. bool process_record_via(uint16_t keycode, keyrecord_t *record) {
  134. // Handle macros
  135. if (record->event.pressed) {
  136. if (keycode >= MACRO00 && keycode <= MACRO15) {
  137. uint8_t id = keycode - MACRO00;
  138. dynamic_keymap_macro_send(id);
  139. return false;
  140. }
  141. }
  142. // TODO: ideally this would be generalized and refactored into
  143. // QMK core as advanced keycodes, until then, the simple case
  144. // can be available here to keyboards using VIA
  145. switch (keycode) {
  146. case FN_MO13:
  147. if (record->event.pressed) {
  148. layer_on(1);
  149. update_tri_layer(1, 2, 3);
  150. } else {
  151. layer_off(1);
  152. update_tri_layer(1, 2, 3);
  153. }
  154. return false;
  155. break;
  156. case FN_MO23:
  157. if (record->event.pressed) {
  158. layer_on(2);
  159. update_tri_layer(1, 2, 3);
  160. } else {
  161. layer_off(2);
  162. update_tri_layer(1, 2, 3);
  163. }
  164. return false;
  165. break;
  166. }
  167. return true;
  168. }
  169. // Keyboard level code can override this to handle custom messages from VIA.
  170. // See raw_hid_receive() implementation.
  171. // DO NOT call raw_hid_send() in the overide function.
  172. __attribute__((weak)) void raw_hid_receive_kb(uint8_t *data, uint8_t length) {
  173. uint8_t *command_id = &(data[0]);
  174. *command_id = id_unhandled;
  175. }
  176. // VIA handles received HID messages first, and will route to
  177. // raw_hid_receive_kb() for command IDs that are not handled here.
  178. // This gives the keyboard code level the ability to handle the command
  179. // specifically.
  180. //
  181. // raw_hid_send() is called at the end, with the same buffer, which was
  182. // possibly modified with returned values.
  183. void raw_hid_receive(uint8_t *data, uint8_t length) {
  184. uint8_t *command_id = &(data[0]);
  185. uint8_t *command_data = &(data[1]);
  186. switch (*command_id) {
  187. case id_get_protocol_version: {
  188. command_data[0] = VIA_PROTOCOL_VERSION >> 8;
  189. command_data[1] = VIA_PROTOCOL_VERSION & 0xFF;
  190. break;
  191. }
  192. case id_get_keyboard_value: {
  193. switch (command_data[0]) {
  194. case id_uptime: {
  195. uint32_t value = timer_read32();
  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_layout_options: {
  203. uint32_t value = via_get_layout_options();
  204. command_data[1] = (value >> 24) & 0xFF;
  205. command_data[2] = (value >> 16) & 0xFF;
  206. command_data[3] = (value >> 8) & 0xFF;
  207. command_data[4] = value & 0xFF;
  208. break;
  209. }
  210. case id_switch_matrix_state: {
  211. #if ((MATRIX_COLS / 8 + 1) * MATRIX_ROWS <= 28)
  212. uint8_t i = 1;
  213. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  214. matrix_row_t value = matrix_get_row(row);
  215. # if (MATRIX_COLS > 24)
  216. command_data[i++] = (value >> 24) & 0xFF;
  217. # endif
  218. # if (MATRIX_COLS > 16)
  219. command_data[i++] = (value >> 16) & 0xFF;
  220. # endif
  221. # if (MATRIX_COLS > 8)
  222. command_data[i++] = (value >> 8) & 0xFF;
  223. # endif
  224. command_data[i++] = value & 0xFF;
  225. }
  226. #endif
  227. break;
  228. }
  229. default: {
  230. raw_hid_receive_kb(data, length);
  231. break;
  232. }
  233. }
  234. break;
  235. }
  236. case id_set_keyboard_value: {
  237. switch (command_data[0]) {
  238. case id_layout_options: {
  239. 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];
  240. via_set_layout_options(value);
  241. break;
  242. }
  243. default: {
  244. raw_hid_receive_kb(data, length);
  245. break;
  246. }
  247. }
  248. break;
  249. }
  250. case id_dynamic_keymap_get_keycode: {
  251. uint16_t keycode = dynamic_keymap_get_keycode(command_data[0], command_data[1], command_data[2]);
  252. command_data[3] = keycode >> 8;
  253. command_data[4] = keycode & 0xFF;
  254. break;
  255. }
  256. case id_dynamic_keymap_set_keycode: {
  257. dynamic_keymap_set_keycode(command_data[0], command_data[1], command_data[2], (command_data[3] << 8) | command_data[4]);
  258. break;
  259. }
  260. case id_dynamic_keymap_reset: {
  261. dynamic_keymap_reset();
  262. break;
  263. }
  264. case id_backlight_config_set_value:
  265. case id_backlight_config_get_value:
  266. case id_backlight_config_save: {
  267. raw_hid_receive_kb(data, length);
  268. break;
  269. }
  270. case id_dynamic_keymap_macro_get_count: {
  271. command_data[0] = dynamic_keymap_macro_get_count();
  272. break;
  273. }
  274. case id_dynamic_keymap_macro_get_buffer_size: {
  275. uint16_t size = dynamic_keymap_macro_get_buffer_size();
  276. command_data[0] = size >> 8;
  277. command_data[1] = size & 0xFF;
  278. break;
  279. }
  280. case id_dynamic_keymap_macro_get_buffer: {
  281. uint16_t offset = (command_data[0] << 8) | command_data[1];
  282. uint16_t size = command_data[2]; // size <= 28
  283. dynamic_keymap_macro_get_buffer(offset, size, &command_data[3]);
  284. break;
  285. }
  286. case id_dynamic_keymap_macro_set_buffer: {
  287. uint16_t offset = (command_data[0] << 8) | command_data[1];
  288. uint16_t size = command_data[2]; // size <= 28
  289. dynamic_keymap_macro_set_buffer(offset, size, &command_data[3]);
  290. break;
  291. }
  292. case id_dynamic_keymap_macro_reset: {
  293. dynamic_keymap_macro_reset();
  294. break;
  295. }
  296. case id_dynamic_keymap_get_layer_count: {
  297. command_data[0] = dynamic_keymap_get_layer_count();
  298. break;
  299. }
  300. case id_dynamic_keymap_get_buffer: {
  301. uint16_t offset = (command_data[0] << 8) | command_data[1];
  302. uint16_t size = command_data[2]; // size <= 28
  303. dynamic_keymap_get_buffer(offset, size, &command_data[3]);
  304. break;
  305. }
  306. case id_dynamic_keymap_set_buffer: {
  307. uint16_t offset = (command_data[0] << 8) | command_data[1];
  308. uint16_t size = command_data[2]; // size <= 28
  309. dynamic_keymap_set_buffer(offset, size, &command_data[3]);
  310. break;
  311. }
  312. case id_eeprom_reset: {
  313. via_eeprom_reset();
  314. break;
  315. }
  316. case id_bootloader_jump: {
  317. // Need to send data back before the jump
  318. // Informs host that the command is handled
  319. raw_hid_send(data, length);
  320. // Give host time to read it
  321. wait_ms(100);
  322. bootloader_jump();
  323. break;
  324. }
  325. default: {
  326. // The command ID is not known
  327. // Return the unhandled state
  328. *command_id = id_unhandled;
  329. break;
  330. }
  331. }
  332. // Return the same buffer, optionally with values changed
  333. // (i.e. returning state to the host, or the unhandled state).
  334. raw_hid_send(data, length);
  335. }