via.c 19 KB

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