via.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. #pragma once
  17. #include <tmk_core/common/eeconfig.h> // for EECONFIG_SIZE
  18. // Keyboard level code can change where VIA stores the magic.
  19. // The magic is the build date YYMMDD encoded as BCD in 3 bytes,
  20. // thus installing firmware built on a different date to the one
  21. // already installed can be detected and the EEPROM data is reset.
  22. // The only reason this is important is in case EEPROM usage changes
  23. // and the EEPROM was not explicitly reset by bootmagic lite.
  24. #ifndef VIA_EEPROM_MAGIC_ADDR
  25. # define VIA_EEPROM_MAGIC_ADDR (EECONFIG_SIZE)
  26. #endif
  27. #define VIA_EEPROM_LAYOUT_OPTIONS_ADDR (VIA_EEPROM_MAGIC_ADDR + 3)
  28. // Changing the layout options size after release will invalidate EEPROM,
  29. // but this is something that should be set correctly on initial implementation.
  30. // 1 byte is enough for most uses (i.e. 8 binary states, or 6 binary + 1 ternary/quaternary )
  31. #ifndef VIA_EEPROM_LAYOUT_OPTIONS_SIZE
  32. # define VIA_EEPROM_LAYOUT_OPTIONS_SIZE 1
  33. #endif
  34. // The end of the EEPROM memory used by VIA
  35. // By default, dynamic keymaps will start at this if there is no
  36. // custom config
  37. #define VIA_EEPROM_CUSTOM_CONFIG_ADDR (VIA_EEPROM_LAYOUT_OPTIONS_ADDR + VIA_EEPROM_LAYOUT_OPTIONS_SIZE)
  38. #ifndef VIA_EEPROM_CUSTOM_CONFIG_SIZE
  39. # define VIA_EEPROM_CUSTOM_CONFIG_SIZE 0
  40. #endif
  41. // This is changed only when the command IDs change,
  42. // so VIA Configurator can detect compatible firmware.
  43. #define VIA_PROTOCOL_VERSION 0x0009
  44. enum via_command_id {
  45. id_get_protocol_version = 0x01, // always 0x01
  46. id_get_keyboard_value,
  47. id_set_keyboard_value,
  48. id_dynamic_keymap_get_keycode,
  49. id_dynamic_keymap_set_keycode,
  50. id_dynamic_keymap_reset,
  51. id_backlight_config_set_value,
  52. id_backlight_config_get_value,
  53. id_backlight_config_save,
  54. id_eeprom_reset,
  55. id_bootloader_jump,
  56. id_dynamic_keymap_macro_get_count,
  57. id_dynamic_keymap_macro_get_buffer_size,
  58. id_dynamic_keymap_macro_get_buffer,
  59. id_dynamic_keymap_macro_set_buffer,
  60. id_dynamic_keymap_macro_reset,
  61. id_dynamic_keymap_get_layer_count,
  62. id_dynamic_keymap_get_buffer,
  63. id_dynamic_keymap_set_buffer,
  64. id_unhandled = 0xFF,
  65. };
  66. enum via_keyboard_value_id { id_uptime = 0x01, id_layout_options, id_switch_matrix_state };
  67. // Can't use SAFE_RANGE here, it might change if someone adds
  68. // new values to enum quantum_keycodes.
  69. // Need to keep checking 0x5F10 is still in the safe range.
  70. // TODO: merge this into quantum_keycodes
  71. // Backlight keycodes are in range 0x5F00-0x5F0F
  72. enum via_keycodes {
  73. FN_MO13 = 0x5F10,
  74. FN_MO23,
  75. MACRO00,
  76. MACRO01,
  77. MACRO02,
  78. MACRO03,
  79. MACRO04,
  80. MACRO05,
  81. MACRO06,
  82. MACRO07,
  83. MACRO08,
  84. MACRO09,
  85. MACRO10,
  86. MACRO11,
  87. MACRO12,
  88. MACRO13,
  89. MACRO14,
  90. MACRO15,
  91. };
  92. enum user_keycodes {
  93. USER00 = 0x5F80,
  94. USER01,
  95. USER02,
  96. USER03,
  97. USER04,
  98. USER05,
  99. USER06,
  100. USER07,
  101. USER08,
  102. USER09,
  103. USER10,
  104. USER11,
  105. USER12,
  106. USER13,
  107. USER14,
  108. USER15,
  109. };
  110. // Can be called in an overriding via_init_kb() to test if keyboard level code usage of
  111. // EEPROM is invalid and use/save defaults.
  112. bool via_eeprom_is_valid(void);
  113. // Sets VIA/keyboard level usage of EEPROM to valid/invalid
  114. // Keyboard level code (eg. via_init_kb()) should not call this
  115. void via_eeprom_set_valid(bool valid);
  116. // Flag QMK and VIA/keyboard level EEPROM as invalid.
  117. // Used in bootmagic_lite() and VIA command handler.
  118. // Keyboard level code should not need to call this.
  119. void via_eeprom_reset(void);
  120. // Called by QMK core to initialize dynamic keymaps etc.
  121. void via_init(void);
  122. // Used by VIA to store and retrieve the layout options.
  123. uint32_t via_get_layout_options(void);
  124. void via_set_layout_options(uint32_t value);
  125. // Called by QMK core to process VIA-specific keycodes.
  126. bool process_record_via(uint16_t keycode, keyrecord_t *record);