mirror of
https://github.com/qmk/qmk_firmware.git
synced 2025-03-01 02:33:53 +00:00
184 lines
7.8 KiB
C
184 lines
7.8 KiB
C
// Copyright 2024 Nick Brassel (@tzarc)
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
#include "eeprom.h"
|
|
#include "dynamic_keymap.h"
|
|
#include "nvm_dynamic_keymap.h"
|
|
#include "nvm_eeprom_eeconfig_internal.h"
|
|
#include "nvm_eeprom_via_internal.h"
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
#ifdef VIA_ENABLE
|
|
# include "via.h"
|
|
# define DYNAMIC_KEYMAP_EEPROM_START (VIA_EEPROM_CONFIG_END)
|
|
#else
|
|
# define DYNAMIC_KEYMAP_EEPROM_START (EECONFIG_SIZE)
|
|
#endif
|
|
|
|
#ifndef DYNAMIC_KEYMAP_EEPROM_MAX_ADDR
|
|
# define DYNAMIC_KEYMAP_EEPROM_MAX_ADDR (TOTAL_EEPROM_BYTE_COUNT - 1)
|
|
#endif
|
|
|
|
#if DYNAMIC_KEYMAP_EEPROM_MAX_ADDR > (TOTAL_EEPROM_BYTE_COUNT - 1)
|
|
# pragma message STR(DYNAMIC_KEYMAP_EEPROM_MAX_ADDR) " > " STR((TOTAL_EEPROM_BYTE_COUNT - 1))
|
|
# error DYNAMIC_KEYMAP_EEPROM_MAX_ADDR is configured to use more space than what is available for the selected EEPROM driver
|
|
#endif
|
|
|
|
// Due to usage of uint16_t check for max 65535
|
|
#if DYNAMIC_KEYMAP_EEPROM_MAX_ADDR > 65535
|
|
# pragma message STR(DYNAMIC_KEYMAP_EEPROM_MAX_ADDR) " > 65535"
|
|
# error DYNAMIC_KEYMAP_EEPROM_MAX_ADDR must be less than 65536
|
|
#endif
|
|
|
|
// If DYNAMIC_KEYMAP_EEPROM_ADDR not explicitly defined in config.h,
|
|
#ifndef DYNAMIC_KEYMAP_EEPROM_ADDR
|
|
# define DYNAMIC_KEYMAP_EEPROM_ADDR DYNAMIC_KEYMAP_EEPROM_START
|
|
#endif
|
|
|
|
// Dynamic encoders starts after dynamic keymaps
|
|
#ifndef DYNAMIC_KEYMAP_ENCODER_EEPROM_ADDR
|
|
# define DYNAMIC_KEYMAP_ENCODER_EEPROM_ADDR (DYNAMIC_KEYMAP_EEPROM_ADDR + (DYNAMIC_KEYMAP_LAYER_COUNT * MATRIX_ROWS * MATRIX_COLS * 2))
|
|
#endif
|
|
|
|
// Dynamic macro starts after dynamic encoders, but only when using ENCODER_MAP
|
|
#ifdef ENCODER_MAP_ENABLE
|
|
# ifndef DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR
|
|
# define DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR (DYNAMIC_KEYMAP_ENCODER_EEPROM_ADDR + (DYNAMIC_KEYMAP_LAYER_COUNT * NUM_ENCODERS * 2 * 2))
|
|
# endif // DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR
|
|
#else // ENCODER_MAP_ENABLE
|
|
# ifndef DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR
|
|
# define DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR (DYNAMIC_KEYMAP_ENCODER_EEPROM_ADDR)
|
|
# endif // DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR
|
|
#endif // ENCODER_MAP_ENABLE
|
|
|
|
// Sanity check that dynamic keymaps fit in available EEPROM
|
|
// If there's not 100 bytes available for macros, then something is wrong.
|
|
// The keyboard should override DYNAMIC_KEYMAP_LAYER_COUNT to reduce it,
|
|
// or DYNAMIC_KEYMAP_EEPROM_MAX_ADDR to increase it, *only if* the microcontroller has
|
|
// more than the default.
|
|
_Static_assert((DYNAMIC_KEYMAP_EEPROM_MAX_ADDR) - (DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR) >= 100, "Dynamic keymaps are configured to use more EEPROM than is available.");
|
|
|
|
#ifndef TOTAL_EEPROM_BYTE_COUNT
|
|
# error Unknown total EEPROM size. Cannot derive maximum for dynamic keymaps.
|
|
#endif
|
|
// Dynamic macros are stored after the keymaps and use what is available
|
|
// up to and including DYNAMIC_KEYMAP_EEPROM_MAX_ADDR.
|
|
#ifndef DYNAMIC_KEYMAP_MACRO_EEPROM_SIZE
|
|
# define DYNAMIC_KEYMAP_MACRO_EEPROM_SIZE (DYNAMIC_KEYMAP_EEPROM_MAX_ADDR - DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR + 1)
|
|
#endif
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
static inline void *dynamic_keymap_key_to_eeprom_address(uint8_t layer, uint8_t row, uint8_t column) {
|
|
return ((void *)DYNAMIC_KEYMAP_EEPROM_ADDR) + (layer * MATRIX_ROWS * MATRIX_COLS * 2) + (row * MATRIX_COLS * 2) + (column * 2);
|
|
}
|
|
|
|
uint16_t nvm_dynamic_keymap_read_keycode(uint8_t layer, uint8_t row, uint8_t column) {
|
|
if (layer >= DYNAMIC_KEYMAP_LAYER_COUNT || row >= MATRIX_ROWS || column >= MATRIX_COLS) return KC_NO;
|
|
void *address = dynamic_keymap_key_to_eeprom_address(layer, row, column);
|
|
// Big endian, so we can read/write EEPROM directly from host if we want
|
|
uint16_t keycode = eeprom_read_byte(address) << 8;
|
|
keycode |= eeprom_read_byte(address + 1);
|
|
return keycode;
|
|
}
|
|
|
|
void nvm_dynamic_keymap_update_keycode(uint8_t layer, uint8_t row, uint8_t column, uint16_t keycode) {
|
|
if (layer >= DYNAMIC_KEYMAP_LAYER_COUNT || row >= MATRIX_ROWS || column >= MATRIX_COLS) return;
|
|
void *address = dynamic_keymap_key_to_eeprom_address(layer, row, column);
|
|
// Big endian, so we can read/write EEPROM directly from host if we want
|
|
eeprom_update_byte(address, (uint8_t)(keycode >> 8));
|
|
eeprom_update_byte(address + 1, (uint8_t)(keycode & 0xFF));
|
|
}
|
|
|
|
#ifdef ENCODER_MAP_ENABLE
|
|
static void *dynamic_keymap_encoder_to_eeprom_address(uint8_t layer, uint8_t encoder_id) {
|
|
return ((void *)DYNAMIC_KEYMAP_ENCODER_EEPROM_ADDR) + (layer * NUM_ENCODERS * 2 * 2) + (encoder_id * 2 * 2);
|
|
}
|
|
|
|
uint16_t nvm_dynamic_keymap_read_encoder(uint8_t layer, uint8_t encoder_id, bool clockwise) {
|
|
if (layer >= DYNAMIC_KEYMAP_LAYER_COUNT || encoder_id >= NUM_ENCODERS) return KC_NO;
|
|
void *address = dynamic_keymap_encoder_to_eeprom_address(layer, encoder_id);
|
|
// Big endian, so we can read/write EEPROM directly from host if we want
|
|
uint16_t keycode = ((uint16_t)eeprom_read_byte(address + (clockwise ? 0 : 2))) << 8;
|
|
keycode |= eeprom_read_byte(address + (clockwise ? 0 : 2) + 1);
|
|
return keycode;
|
|
}
|
|
|
|
void nvm_dynamic_keymap_update_encoder(uint8_t layer, uint8_t encoder_id, bool clockwise, uint16_t keycode) {
|
|
if (layer >= DYNAMIC_KEYMAP_LAYER_COUNT || encoder_id >= NUM_ENCODERS) return;
|
|
void *address = dynamic_keymap_encoder_to_eeprom_address(layer, encoder_id);
|
|
// Big endian, so we can read/write EEPROM directly from host if we want
|
|
eeprom_update_byte(address + (clockwise ? 0 : 2), (uint8_t)(keycode >> 8));
|
|
eeprom_update_byte(address + (clockwise ? 0 : 2) + 1, (uint8_t)(keycode & 0xFF));
|
|
}
|
|
#endif // ENCODER_MAP_ENABLE
|
|
|
|
void nvm_dynamic_keymap_read_buffer(uint32_t offset, uint32_t size, uint8_t *data) {
|
|
uint16_t dynamic_keymap_eeprom_size = DYNAMIC_KEYMAP_LAYER_COUNT * MATRIX_ROWS * MATRIX_COLS * 2;
|
|
void * source = (void *)(DYNAMIC_KEYMAP_EEPROM_ADDR + offset);
|
|
uint8_t *target = data;
|
|
for (uint16_t i = 0; i < size; i++) {
|
|
if (offset + i < dynamic_keymap_eeprom_size) {
|
|
*target = eeprom_read_byte(source);
|
|
} else {
|
|
*target = 0x00;
|
|
}
|
|
source++;
|
|
target++;
|
|
}
|
|
}
|
|
|
|
void nvm_dynamic_keymap_update_buffer(uint32_t offset, uint32_t size, uint8_t *data) {
|
|
uint16_t dynamic_keymap_eeprom_size = DYNAMIC_KEYMAP_LAYER_COUNT * MATRIX_ROWS * MATRIX_COLS * 2;
|
|
void * target = (void *)(DYNAMIC_KEYMAP_EEPROM_ADDR + offset);
|
|
uint8_t *source = data;
|
|
for (uint16_t i = 0; i < size; i++) {
|
|
if (offset + i < dynamic_keymap_eeprom_size) {
|
|
eeprom_update_byte(target, *source);
|
|
}
|
|
source++;
|
|
target++;
|
|
}
|
|
}
|
|
|
|
uint32_t nvm_dynamic_keymap_macro_size(void) {
|
|
return DYNAMIC_KEYMAP_MACRO_EEPROM_SIZE;
|
|
}
|
|
|
|
void nvm_dynamic_keymap_macro_read_buffer(uint32_t offset, uint32_t size, uint8_t *data) {
|
|
void * source = (void *)(DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR + offset);
|
|
uint8_t *target = data;
|
|
for (uint16_t i = 0; i < size; i++) {
|
|
if (offset + i < DYNAMIC_KEYMAP_MACRO_EEPROM_SIZE) {
|
|
*target = eeprom_read_byte(source);
|
|
} else {
|
|
*target = 0x00;
|
|
}
|
|
source++;
|
|
target++;
|
|
}
|
|
}
|
|
|
|
void nvm_dynamic_keymap_macro_update_buffer(uint32_t offset, uint32_t size, uint8_t *data) {
|
|
void * target = (void *)(DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR + offset);
|
|
uint8_t *source = data;
|
|
for (uint16_t i = 0; i < size; i++) {
|
|
if (offset + i < DYNAMIC_KEYMAP_MACRO_EEPROM_SIZE) {
|
|
eeprom_update_byte(target, *source);
|
|
}
|
|
source++;
|
|
target++;
|
|
}
|
|
}
|
|
|
|
void nvm_dynamic_keymap_macro_reset(void) {
|
|
void *p = (void *)(DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR);
|
|
void *end = (void *)(DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR + DYNAMIC_KEYMAP_MACRO_EEPROM_SIZE);
|
|
while (p != end) {
|
|
eeprom_update_byte(p, 0);
|
|
++p;
|
|
}
|
|
}
|
|
|