From 1b0e83b1d65cbe3e313d71f4deccfa2b991fec6b Mon Sep 17 00:00:00 2001 From: rootiest Date: Mon, 13 Apr 2026 13:19:00 -0400 Subject: [PATCH] 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 --- .../k17_max/ansi_encoder/rgb/config.h | 8 +++++ .../keychron/k17_max/ansi_encoder/rgb/rgb.c | 34 +++++++++++++++++++ keyboards/keychron/k17_max/k17_max.c | 31 +++++++++++++++++ keyboards/keychron/k17_max/rules.mk | 2 ++ 4 files changed, 75 insertions(+) diff --git a/keyboards/keychron/k17_max/ansi_encoder/rgb/config.h b/keyboards/keychron/k17_max/ansi_encoder/rgb/config.h index 8f36bcb0f3..40df0dd19d 100644 --- a/keyboards/keychron/k17_max/ansi_encoder/rgb/config.h +++ b/keyboards/keychron/k17_max/ansi_encoder/rgb/config.h @@ -16,6 +16,14 @@ #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 /* RGB Matrix driver configuration */ # define RGB_MATRIX_LED_COUNT 103 diff --git a/keyboards/keychron/k17_max/ansi_encoder/rgb/rgb.c b/keyboards/keychron/k17_max/ansi_encoder/rgb/rgb.c index 3c7599fa29..da3ecebef2 100644 --- a/keyboards/keychron/k17_max/ansi_encoder/rgb/rgb.c +++ b/keyboards/keychron/k17_max/ansi_encoder/rgb/rgb.c @@ -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 diff --git a/keyboards/keychron/k17_max/k17_max.c b/keyboards/keychron/k17_max/k17_max.c index 6ce92125cc..b792984ebe 100644 --- a/keyboards/keychron/k17_max/k17_max.c +++ b/keyboards/keychron/k17_max/k17_max.c @@ -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(); } diff --git a/keyboards/keychron/k17_max/rules.mk b/keyboards/keychron/k17_max/rules.mk index 4eaf6820bc..e33052f4dd 100644 --- a/keyboards/keychron/k17_max/rules.mk +++ b/keyboards/keychron/k17_max/rules.mk @@ -1,3 +1,5 @@ +KEYCHRON_RGB_ENABLE = yes + include keyboards/keychron/common/wireless/wireless.mk include keyboards/keychron/common/keychron_common.mk -- 2.52.0