via.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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. default: {
  351. // The command ID is not known
  352. // Return the unhandled state
  353. *command_id = id_unhandled;
  354. break;
  355. }
  356. }
  357. // Return the same buffer, optionally with values changed
  358. // (i.e. returning state to the host, or the unhandled state).
  359. raw_hid_send(data, length);
  360. }
  361. #if defined(VIA_QMK_BACKLIGHT_ENABLE)
  362. # if BACKLIGHT_LEVELS == 0
  363. # error BACKLIGHT_LEVELS == 0
  364. # endif
  365. void via_qmk_backlight_get_value(uint8_t *data) {
  366. uint8_t *value_id = &(data[0]);
  367. uint8_t *value_data = &(data[1]);
  368. switch (*value_id) {
  369. case id_qmk_backlight_brightness: {
  370. // level / BACKLIGHT_LEVELS * 255
  371. value_data[0] = ((uint16_t)get_backlight_level()) * 255 / BACKLIGHT_LEVELS;
  372. break;
  373. }
  374. case id_qmk_backlight_effect: {
  375. # ifdef BACKLIGHT_BREATHING
  376. value_data[0] = is_backlight_breathing() ? 1 : 0;
  377. # else
  378. value_data[0] = 0;
  379. # endif
  380. break;
  381. }
  382. }
  383. }
  384. void via_qmk_backlight_set_value(uint8_t *data) {
  385. uint8_t *value_id = &(data[0]);
  386. uint8_t *value_data = &(data[1]);
  387. switch (*value_id) {
  388. case id_qmk_backlight_brightness: {
  389. // level / 255 * BACKLIGHT_LEVELS
  390. backlight_level_noeeprom(((uint16_t)value_data[0]) * BACKLIGHT_LEVELS / 255);
  391. break;
  392. }
  393. case id_qmk_backlight_effect: {
  394. # ifdef BACKLIGHT_BREATHING
  395. if (value_data[0] == 0) {
  396. backlight_disable_breathing();
  397. } else {
  398. backlight_enable_breathing();
  399. }
  400. # endif
  401. break;
  402. }
  403. }
  404. }
  405. #endif // #if defined(VIA_QMK_BACKLIGHT_ENABLE)
  406. #if defined(VIA_QMK_RGBLIGHT_ENABLE)
  407. void via_qmk_rgblight_get_value(uint8_t *data) {
  408. uint8_t *value_id = &(data[0]);
  409. uint8_t *value_data = &(data[1]);
  410. switch (*value_id) {
  411. case id_qmk_rgblight_brightness: {
  412. value_data[0] = rgblight_get_val();
  413. break;
  414. }
  415. case id_qmk_rgblight_effect: {
  416. value_data[0] = rgblight_get_mode();
  417. break;
  418. }
  419. case id_qmk_rgblight_effect_speed: {
  420. value_data[0] = rgblight_get_speed();
  421. break;
  422. }
  423. case id_qmk_rgblight_color: {
  424. value_data[0] = rgblight_get_hue();
  425. value_data[1] = rgblight_get_sat();
  426. break;
  427. }
  428. }
  429. }
  430. void via_qmk_rgblight_set_value(uint8_t *data) {
  431. uint8_t *value_id = &(data[0]);
  432. uint8_t *value_data = &(data[1]);
  433. switch (*value_id) {
  434. case id_qmk_rgblight_brightness: {
  435. rgblight_sethsv_noeeprom(rgblight_get_hue(), rgblight_get_sat(), value_data[0]);
  436. break;
  437. }
  438. case id_qmk_rgblight_effect: {
  439. rgblight_mode_noeeprom(value_data[0]);
  440. if (value_data[0] == 0) {
  441. rgblight_disable_noeeprom();
  442. } else {
  443. rgblight_enable_noeeprom();
  444. }
  445. break;
  446. }
  447. case id_qmk_rgblight_effect_speed: {
  448. rgblight_set_speed_noeeprom(value_data[0]);
  449. break;
  450. }
  451. case id_qmk_rgblight_color: {
  452. rgblight_sethsv_noeeprom(value_data[0], value_data[1], rgblight_get_val());
  453. break;
  454. }
  455. }
  456. }
  457. #endif // #if defined(VIA_QMK_RGBLIGHT_ENABLE)