Hash dynamic keymap at runtime

This commit is contained in:
zvecr
2022-07-15 03:33:34 +01:00
parent 8874c664a8
commit 28855f658a
7 changed files with 67 additions and 62 deletions
+43
View File
@@ -19,6 +19,7 @@
#include "progmem.h" // to read default from flash
#include "quantum.h" // for send_string()
#include "dynamic_keymap.h"
#include "fnv.h"
#ifdef VIA_ENABLE
# include "via.h" // for VIA_EEPROM_CONFIG_END
@@ -147,6 +148,47 @@ void dynamic_keymap_set_encoder(uint8_t layer, uint8_t encoder_id, bool clockwis
}
#endif // ENCODER_MAP_ENABLE
static uint32_t dynamic_keymap_compute_hash(void) {
Fnv32_t hash = FNV1_32A_INIT;
uint16_t keycode;
for (int layer = 0; layer < DYNAMIC_KEYMAP_LAYER_COUNT; layer++) {
for (int row = 0; row < MATRIX_ROWS; row++) {
for (int column = 0; column < MATRIX_COLS; column++) {
keycode = pgm_read_word(&keymaps[layer][row][column]);
hash = fnv_32a_buf(&keycode, sizeof(keycode), hash);
}
}
#ifdef ENCODER_MAP_ENABLE
for (int encoder = 0; encoder < NUM_ENCODERS; encoder++) {
keycode = pgm_read_word(&encoder_map[layer][encoder][0]);
hash = fnv_32a_buf(&keycode, sizeof(keycode), hash);
keycode = pgm_read_word(&encoder_map[layer][encoder][1]);
hash = fnv_32a_buf(&keycode, sizeof(keycode), hash);
}
#endif // ENCODER_MAP_ENABLE
}
return hash;
}
static uint32_t dynamic_keymap_hash(void) {
static uint32_t hash = 0;
static uint8_t s_init = 0;
if (!s_init) {
s_init = 1;
hash = dynamic_keymap_compute_hash();
}
return hash;
}
bool dynamic_keymap_is_valid(void) {
return eeprom_read_dword(EECONFIG_KEYMAP_HASH) == dynamic_keymap_hash();
}
void dynamic_keymap_reset(void) {
// Reset the keymaps in EEPROM to what is in flash.
// All keyboards using dynamic keymaps should define a layout
@@ -164,6 +206,7 @@ void dynamic_keymap_reset(void) {
}
#endif // ENCODER_MAP_ENABLE
}
eeprom_update_dword(EECONFIG_KEYMAP_HASH, dynamic_keymap_hash());
}
void dynamic_keymap_get_buffer(uint16_t offset, uint16_t size, uint8_t *data) {