Parent directory

via.h

7643 bytes
  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
 17#pragma once
 18
 19#include "eeconfig.h" // for EECONFIG_SIZE
 20#include "action.h"
 21
 22// Keyboard level code can change where VIA stores the magic.
 23// The magic is the build date YYMMDD encoded as BCD in 3 bytes,
 24// thus installing firmware built on a different date to the one
 25// already installed can be detected and the EEPROM data is reset.
 26// The only reason this is important is in case EEPROM usage changes
 27// and the EEPROM was not explicitly reset by bootmagic lite.
 28#ifndef VIA_EEPROM_MAGIC_ADDR
 29#    define VIA_EEPROM_MAGIC_ADDR (EECONFIG_SIZE)
 30#endif
 31
 32#define VIA_EEPROM_LAYOUT_OPTIONS_ADDR (VIA_EEPROM_MAGIC_ADDR + 3)
 33
 34// Changing the layout options size after release will invalidate EEPROM,
 35// but this is something that should be set correctly on initial implementation.
 36// 1 byte is enough for most uses (i.e. 8 binary states, or 6 binary + 1 ternary/quaternary )
 37#ifndef VIA_EEPROM_LAYOUT_OPTIONS_SIZE
 38#    define VIA_EEPROM_LAYOUT_OPTIONS_SIZE 1
 39#endif
 40
 41// Allow override of the layout options default value.
 42// This requires advanced knowledge of how VIA stores layout options
 43// and is only really useful for setting a boolean layout option
 44// state to true by default.
 45#ifndef VIA_EEPROM_LAYOUT_OPTIONS_DEFAULT
 46#    define VIA_EEPROM_LAYOUT_OPTIONS_DEFAULT 0x00000000
 47#endif
 48
 49// The end of the EEPROM memory used by VIA
 50// By default, dynamic keymaps will start at this if there is no
 51// custom config
 52#define VIA_EEPROM_CUSTOM_CONFIG_ADDR (VIA_EEPROM_LAYOUT_OPTIONS_ADDR + VIA_EEPROM_LAYOUT_OPTIONS_SIZE)
 53
 54#ifndef VIA_EEPROM_CUSTOM_CONFIG_SIZE
 55#    define VIA_EEPROM_CUSTOM_CONFIG_SIZE 0
 56#endif
 57
 58#define VIA_EEPROM_CONFIG_END (VIA_EEPROM_CUSTOM_CONFIG_ADDR + VIA_EEPROM_CUSTOM_CONFIG_SIZE)
 59
 60// This is changed only when the command IDs change,
 61// so VIA Configurator can detect compatible firmware.
 62#define VIA_PROTOCOL_VERSION 0x000C
 63
 64// This is a version number for the firmware for the keyboard.
 65// It can be used to ensure the VIA keyboard definition and the firmware
 66// have the same version, especially if there are changes to custom values.
 67// Define this in config.h to override and bump this number.
 68// This is *not* required if the keyboard is only using basic functionality
 69// and not using custom values for lighting, rotary encoders, etc.
 70#ifndef VIA_FIRMWARE_VERSION
 71#    define VIA_FIRMWARE_VERSION 0x00000000
 72#endif
 73
 74enum via_command_id {
 75    id_get_protocol_version                 = 0x01, // always 0x01
 76    id_get_keyboard_value                   = 0x02,
 77    id_set_keyboard_value                   = 0x03,
 78    id_dynamic_keymap_get_keycode           = 0x04,
 79    id_dynamic_keymap_set_keycode           = 0x05,
 80    id_dynamic_keymap_reset                 = 0x06,
 81    id_custom_set_value                     = 0x07,
 82    id_custom_get_value                     = 0x08,
 83    id_custom_save                          = 0x09,
 84    id_eeprom_reset                         = 0x0A,
 85    id_bootloader_jump                      = 0x0B,
 86    id_dynamic_keymap_macro_get_count       = 0x0C,
 87    id_dynamic_keymap_macro_get_buffer_size = 0x0D,
 88    id_dynamic_keymap_macro_get_buffer      = 0x0E,
 89    id_dynamic_keymap_macro_set_buffer      = 0x0F,
 90    id_dynamic_keymap_macro_reset           = 0x10,
 91    id_dynamic_keymap_get_layer_count       = 0x11,
 92    id_dynamic_keymap_get_buffer            = 0x12,
 93    id_dynamic_keymap_set_buffer            = 0x13,
 94    id_dynamic_keymap_get_encoder           = 0x14,
 95    id_dynamic_keymap_set_encoder           = 0x15,
 96    id_unhandled                            = 0xFF,
 97};
 98
 99enum via_keyboard_value_id {
100    id_uptime              = 0x01,
101    id_layout_options      = 0x02,
102    id_switch_matrix_state = 0x03,
103    id_firmware_version    = 0x04,
104    id_device_indication   = 0x05,
105};
106
107enum via_channel_id {
108    id_custom_channel         = 0,
109    id_qmk_backlight_channel  = 1,
110    id_qmk_rgblight_channel   = 2,
111    id_qmk_rgb_matrix_channel = 3,
112    id_qmk_audio_channel      = 4,
113    id_qmk_led_matrix_channel = 5,
114};
115
116enum via_qmk_backlight_value {
117    id_qmk_backlight_brightness = 1,
118    id_qmk_backlight_effect     = 2,
119};
120
121enum via_qmk_rgblight_value {
122    id_qmk_rgblight_brightness   = 1,
123    id_qmk_rgblight_effect       = 2,
124    id_qmk_rgblight_effect_speed = 3,
125    id_qmk_rgblight_color        = 4,
126};
127
128enum via_qmk_rgb_matrix_value {
129    id_qmk_rgb_matrix_brightness   = 1,
130    id_qmk_rgb_matrix_effect       = 2,
131    id_qmk_rgb_matrix_effect_speed = 3,
132    id_qmk_rgb_matrix_color        = 4,
133};
134
135enum via_qmk_led_matrix_value {
136    id_qmk_led_matrix_brightness   = 1,
137    id_qmk_led_matrix_effect       = 2,
138    id_qmk_led_matrix_effect_speed = 3,
139};
140
141enum via_qmk_audio_value {
142    id_qmk_audio_enable        = 1,
143    id_qmk_audio_clicky_enable = 2,
144};
145
146// Can be called in an overriding via_init_kb() to test if keyboard level code usage of
147// EEPROM is invalid and use/save defaults.
148bool via_eeprom_is_valid(void);
149
150// Sets VIA/keyboard level usage of EEPROM to valid/invalid
151// Keyboard level code (eg. via_init_kb()) should not call this
152void via_eeprom_set_valid(bool valid);
153
154// Called by QMK core to initialize dynamic keymaps etc.
155void eeconfig_init_via(void);
156void via_init(void);
157
158// Used by VIA to store and retrieve the layout options.
159uint32_t via_get_layout_options(void);
160void     via_set_layout_options(uint32_t value);
161void     via_set_layout_options_kb(uint32_t value);
162
163// Used by VIA to tell a device to flash LEDs (or do something else) when that
164// device becomes the active device being configured, on startup or switching
165// between devices.
166void via_set_device_indication(uint8_t value);
167
168// Called by QMK core to process VIA-specific keycodes.
169bool process_record_via(uint16_t keycode, keyrecord_t *record);
170
171// These are made external so that keyboard level custom value handlers can use them.
172#if defined(BACKLIGHT_ENABLE)
173void via_qmk_backlight_command(uint8_t *data, uint8_t length);
174void via_qmk_backlight_set_value(uint8_t *data);
175void via_qmk_backlight_get_value(uint8_t *data);
176void via_qmk_backlight_save(void);
177#endif
178
179#if defined(RGBLIGHT_ENABLE)
180void via_qmk_rgblight_command(uint8_t *data, uint8_t length);
181void via_qmk_rgblight_set_value(uint8_t *data);
182void via_qmk_rgblight_get_value(uint8_t *data);
183void via_qmk_rgblight_save(void);
184#endif
185
186#if defined(RGB_MATRIX_ENABLE)
187void via_qmk_rgb_matrix_command(uint8_t *data, uint8_t length);
188void via_qmk_rgb_matrix_set_value(uint8_t *data);
189void via_qmk_rgb_matrix_get_value(uint8_t *data);
190void via_qmk_rgb_matrix_save(void);
191#endif
192
193#if defined(LED_MATRIX_ENABLE)
194void via_qmk_led_matrix_command(uint8_t *data, uint8_t length);
195void via_qmk_led_matrix_set_value(uint8_t *data);
196void via_qmk_led_matrix_get_value(uint8_t *data);
197void via_qmk_led_matrix_save(void);
198#endif
199
200#if defined(AUDIO_ENABLE)
201void via_qmk_audio_command(uint8_t *data, uint8_t length);
202void via_qmk_audio_set_value(uint8_t *data);
203void via_qmk_audio_get_value(uint8_t *data);
204void via_qmk_audio_save(void);
205#endif