feat(k17_max): enable Keychron RGB and fix EEPROM persistence

Port the Keychron RGB EEPROM persistence work from the Q5 Max (PR #18)
to the K17 Max ANSI Encoder RGB variant.

- Add KEYCHRON_RGB_ENABLE = yes to rules.mk
- Define default_per_key_led[] (red Esc/CapsLock, yellow modifiers and
  numpad, blue alpha keys) and default_region[] for the 103-LED layout
  in ansi_encoder/rgb/rgb.c; both are extern'd by keychron_rgb.c
- Add wireless_enter_connected_kb() hook and eeconfig_init_custom_rgb()
  call in keyboard_post_init_kb() in k17_max.c, matching the Q5 Max
  pattern for boot-time and transport-change RGB persistence
- Define VIA_EEPROM_MAGIC_ADDR 552 in ansi_encoder/rgb/config.h to pin
  VIA keymap storage past the Keychron EEPROM data region end (540 for
  K17 with 103 LEDs); prevents keymap corruption if the KB data block
  grows in future
- Define RGB_MATRIX_DEFAULT_MODE RGB_MATRIX_TYPING_HEATMAP to set the
  compile-time fallback mode consistently with the Q5 Max
This commit is contained in:
2026-04-13 13:19:00 -04:00
parent ad41de2f54
commit 41195cbdde
4 changed files with 79 additions and 0 deletions
@@ -16,7 +16,19 @@
#pragma once
// Pin VIA keymap storage to a fixed EEPROM address. By default VIA places its
// magic/keymap block immediately after EECONFIG_KB_DATA_SIZE, so any growth in
// the Keychron custom-RGB EEPROM region shifts the keymap silently and corrupts
// the stored layout (observed as layer 0 keys reverting to KC_TRNS on boot).
// 552 is past the current Keychron data region end (540) and leaves headroom
// for further EEPROM additions without requiring another VIA reset.
#define VIA_EEPROM_MAGIC_ADDR 552
#ifdef RGB_MATRIX_ENABLE
/* Default lighting effect */
# define RGB_MATRIX_DEFAULT_MODE RGB_MATRIX_TYPING_HEATMAP
/* RGB Matrix driver configuration */
# define RGB_MATRIX_LED_COUNT 103
# define DRIVER_COUNT 2
@@ -166,4 +166,38 @@ led_config_t g_led_config = {
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
}
};
#ifdef KEYCHRON_RGB_ENABLE
// Default colour of Per Key RGB
#define DC_RED {HSV_RED}
#define DC_BLU {HSV_BLUE}
#define DC_YLW {HSV_YELLOW}
// 103 LEDs: rows match g_led_config above
// Row 0 (0-18): Fn/media row + numpad cluster top
// Row 1 (19-37): Number row + numpad cluster
// Row 2 (38-56): QWERTY row + numpad cluster
// Row 3 (57-73): ASDF row + numpad cluster
// Row 4 (74-90): ZXCV row + numpad arrows
// Row 5 (91-102): Modifier/bottom row + numpad
HSV default_per_key_led[RGB_MATRIX_LED_COUNT] = {
DC_RED, DC_YLW, DC_YLW, DC_YLW, DC_YLW, DC_YLW, DC_YLW, DC_YLW, DC_YLW, DC_YLW, DC_YLW, DC_YLW, DC_YLW, DC_YLW, DC_YLW, DC_YLW, DC_YLW, DC_YLW, DC_YLW,
DC_YLW, DC_YLW, DC_YLW, DC_YLW, DC_YLW, DC_YLW, DC_YLW, DC_YLW, DC_YLW, DC_YLW, DC_YLW, DC_YLW, DC_YLW, DC_YLW, DC_YLW, DC_YLW, DC_YLW, DC_YLW, DC_YLW,
DC_YLW, DC_BLU, DC_BLU, DC_BLU, DC_BLU, DC_BLU, DC_BLU, DC_BLU, DC_BLU, DC_BLU, DC_BLU, DC_YLW, DC_YLW, DC_YLW, DC_YLW, DC_YLW, DC_YLW, DC_YLW, DC_YLW,
DC_RED, DC_BLU, DC_BLU, DC_BLU, DC_BLU, DC_BLU, DC_BLU, DC_BLU, DC_BLU, DC_BLU, DC_YLW, DC_YLW, DC_YLW, DC_YLW, DC_YLW, DC_YLW, DC_YLW,
DC_YLW, DC_BLU, DC_BLU, DC_BLU, DC_BLU, DC_BLU, DC_BLU, DC_BLU, DC_YLW, DC_YLW, DC_YLW, DC_YLW, DC_YLW, DC_YLW, DC_YLW, DC_YLW, DC_YLW,
DC_YLW, DC_YLW, DC_YLW, DC_BLU, DC_YLW, DC_YLW, DC_YLW, DC_YLW, DC_YLW, DC_YLW, DC_YLW, DC_YLW
};
// Default mixed RGB region (all keys in region 0)
uint8_t default_region[RGB_MATRIX_LED_COUNT] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};
#endif
#endif
+31
View File
@@ -23,6 +23,8 @@
#ifdef LK_WIRELESS_ENABLE
# include "lkbt51.h"
# include "wireless.h"
# include "keychron_wireless_common.h"
# include "battery.h"
#endif
bool dip_switch_update_kb(uint8_t index, bool active) {
@@ -34,6 +36,27 @@ bool dip_switch_update_kb(uint8_t index, bool active) {
return true;
}
#ifdef LK_WIRELESS_ENABLE
// Re-apply the saved QMK RGB mode every time wireless connects.
// During transport changes, rgb_matrix_init() reads the correct mode from
// EEPROM, but something in the BT/2.4G reconnect sequence can reset it
// before the display settles. This hook fires after connection is fully
// established, ensuring the Launcher-configured mode persists.
void wireless_enter_connected_kb(uint8_t host_idx) {
# if defined(RGB_MATRIX_ENABLE) && defined(KEYCHRON_RGB_ENABLE)
extern void eeconfig_init_custom_rgb(void);
eeconfig_init_custom_rgb();
// Re-read the QMK RGB mode from EEPROM and apply if it drifted.
rgb_config_t saved_rgb;
eeprom_read_block(&saved_rgb, EECONFIG_RGB_MATRIX, sizeof(saved_rgb));
if (saved_rgb.mode && saved_rgb.mode != rgb_matrix_get_mode()) {
rgb_matrix_mode_noeeprom(saved_rgb.mode);
}
# endif
}
#endif
void keyboard_post_init_kb(void) {
#ifdef LK_WIRELESS_ENABLE
palSetLineMode(P2P4_MODE_SELECT_PIN, PAL_MODE_INPUT);
@@ -47,6 +70,14 @@ void keyboard_post_init_kb(void) {
encoder_cb_init();
#endif
#if defined(RGB_MATRIX_ENABLE) && defined(KEYCHRON_RGB_ENABLE)
// Load Keychron custom RGB data (effect list, regions, per-key colours)
// from EEPROM into RAM. Without this call the arrays are zero-initialised
// and Launcher settings are lost on every power cycle or transport change.
extern void eeconfig_init_custom_rgb(void);
eeconfig_init_custom_rgb();
#endif
keyboard_post_init_user();
}
+2
View File
@@ -1,3 +1,5 @@
KEYCHRON_RGB_ENABLE = yes
include keyboards/keychron/common/wireless/wireless.mk
include keyboards/keychron/common/keychron_common.mk