Files
qmk_firmware/keyboards/keychron/q5_max/ansi_encoder/ansi_encoder.c
T
rootiest c93249f1b2 feat(q5_max): enable Keychron RGB and fix EEPROM persistence
Enable KEYCHRON_RGB_ENABLE for the Q5 Max, wiring up PER_KEY_RGB and
MIXED_RGB effects, and fix a cascade of EEPROM bugs that caused the
Launcher-configured RGB mode to revert to the default heatmap on every
power cycle and wireless transport change.

Keychron RGB enablement:
- Add KEYCHRON_RGB_ENABLE = yes to rules.mk
- Define default_per_key_led[] and default_region[] for the ANSI Encoder
  layout in ansi_encoder.c (extern'd by keychron_rgb.c)
- Fix missing #include "eeconfig_custom_rgb.h" in mixed_rgb.c and
  rgb_matrix_kb.inc so EECONFIG_SIZE_CUSTOM_RGB is in scope for the
  compile guards that gate the custom effects
- Add #undef EECONFIG_KB_DATA_SIZE before the Keychron redefinition in
  eeconfig_kb.h to suppress redefinition of QMK's default-zero value

EEPROM persistence fixes (keychron_rgb.c):
- Fix retail_demo_enable never being written to EEPROM in
  eeconfig_reset_custom_rgb(): original code used eeprom_read_block
  instead of eeprom_update_block, leaving 0xFF on freshly-flashed
  boards; retail_demo_task() treats any non-zero value as "demo active"
  and forces the mode to CUSTOM_MIXED_RGB every scan
- Clamp retail_demo_enable > 1 to 0 on load to recover boards already
  affected by the above bug
- Move EECONFIG_KEYBOARD version stamp from eeconfig_init_custom_rgb()
  (load path) to eeconfig_reset_custom_rgb() (reset/write path) so the
  version is only stamped when valid defaults are actually written
- Call eeconfig_update_rgb_matrix() in kc_rgb_save() so the current QMK
  RGB mode is persisted alongside Keychron data; without this,
  rgb_matrix_init() (called on every transport change) reloads the
  compile-time default RGB_MATRIX_TYPING_HEATMAP from EEPROM

Transport-change persistence (q5_max.c):
- Call eeconfig_init_custom_rgb() in keyboard_post_init_kb() so Keychron
  RGB arrays are loaded from EEPROM on every boot instead of being
  zero-initialised
- Add wireless_enter_connected_kb() hook: re-applies the EEPROM-saved
  QMK RGB mode after BT/2.4G reconnect in case the reconnect sequence
  resets the in-RAM mode before the display settles

DIP switch Win-side override (keymap.c):
- Replace rgb_matrix_mode() / rgb_matrix_sethsv() calls (which write to
  EEPROM and permanently overwrite the Launcher mode) with a
  dip_win_active flag; rgb_matrix_indicators_advanced_user() paints all
  LEDs white each frame when the flag is set, leaving the active effect
  and EEPROM untouched

VIA keymap address pinning (config.h):
- Define VIA_EEPROM_MAGIC_ADDR 544 to anchor VIA keymap storage at a
  fixed EEPROM offset; without this, growth in EECONFIG_KB_DATA_SIZE
  silently shifts the keymap block, corrupting stored layouts (observed
  as layer-0 keys reverting to KC_TRNS / KC_NONE on boot)
2026-04-13 12:32:10 -04:00

203 lines
8.0 KiB
C

/* Copyright 2024 @ Keychron (https://www.keychron.com)
*
* This program is free software : you can redistribute it and /or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.If not, see < http://www.gnu.org/licenses/>.
*/
#include "quantum.h"
// clang-format off
#ifdef RGB_MATRIX_ENABLE
const snled27351_led_t g_snled27351_leds[RGB_MATRIX_LED_COUNT] = {
/* Refer to SNLED27351 manual for these locations
* driver
* | R location
* | | G location
* | | | B location
* | | | | */
{0, G_15, I_15, H_15},
{0, G_14, I_14, H_14},
{0, G_13, I_13, H_13},
{0, G_12, I_12, H_12},
{0, G_11, I_11, H_11},
{0, G_10, I_10, H_10},
{0, G_9, I_9, H_9},
{0, G_8, I_8, H_8},
{0, G_7, I_7, H_7},
{0, G_6, I_6, H_6},
{0, G_5, I_5, H_5},
{0, G_4, I_4, H_4},
{0, G_3, I_3, H_3},
{0, G_2, I_2, H_2},
{0, D_4, F_4, E_4},
{0, D_3, F_3, E_3},
{0, D_2, F_2, E_2},
{0, A_15, C_15, B_15},
{0, A_14, C_14, B_14},
{0, A_13, C_13, B_13},
{0, A_12, C_12, B_12},
{0, A_11, C_11, B_11},
{0, A_10, C_10, B_10},
{0, A_9, C_9, B_9},
{0, A_8, C_8, B_8},
{0, A_7, C_7, B_7},
{0, A_6, C_6, B_6},
{0, A_5, C_5, B_5},
{0, A_4, C_4, B_4},
{0, A_3, C_3, B_3},
{0, A_2, C_2, B_2},
{0, A_1, C_1, B_1},
{0, D_8, F_8, E_8},
{0, D_7, F_7, E_7},
{0, D_6, F_6, E_6},
{0, D_5, F_5, E_5},
{0, J_15, L_15, K_15},
{0, J_14, L_14, K_14},
{0, J_13, L_13, K_13},
{0, J_12, L_12, K_12},
{0, J_11, L_11, K_11},
{0, J_10, L_10, K_10},
{0, J_9, L_9, K_9},
{0, J_8, L_8, K_8},
{0, J_7, L_7, K_7},
{0, J_6, L_6, K_6},
{0, J_5, L_5, K_5},
{0, J_4, L_4, K_4},
{0, J_3, L_3, K_3},
{0, J_2, L_2, K_2},
{0, J_1, L_1, K_1},
{0, D_12, F_12, E_12},
{0, D_11, F_11, E_11},
{0, D_10, F_10, E_10},
{0, D_9, F_9, E_9},
{1, A_15, C_15, B_15},
{1, A_14, C_14, B_14},
{1, A_13, C_13, B_13},
{1, A_12, C_12, B_12},
{1, A_11, C_11, B_11},
{1, A_10, C_10, B_10},
{1, A_9, C_9, B_9},
{1, A_8, C_8, B_8},
{1, A_7, C_7, B_7},
{1, A_6, C_6, B_6},
{1, A_5, C_5, B_5},
{1, A_4, C_4, B_4},
{1, A_3, C_3, B_3},
{1, A_2, C_2, B_2},
{1, J_9, L_9, K_9},
{1, J_8, L_8, K_8},
{1, J_7, L_7, K_7},
{1, G_15, I_15, H_15},
{1, G_13, I_13, H_13},
{1, G_12, I_12, H_12},
{1, G_11, I_11, H_11},
{1, G_10, I_10, H_10},
{1, G_9, I_9, H_9},
{1, G_8, I_8, H_8},
{1, G_7, I_7, H_7},
{1, G_6, I_6, H_6},
{1, G_5, I_5, H_5},
{1, G_4, I_4, H_4},
{1, G_3, I_3, H_3},
{1, G_2, I_2, H_2},
{1, J_6, L_6, K_6},
{1, J_5, L_5, K_5},
{1, J_4, L_4, K_4},
{1, J_3, L_3, K_3},
{1, D_15, F_15, E_15},
{1, D_14, F_14, E_14},
{1, D_13, F_13, E_13},
{1, D_9, F_9, E_9},
{1, D_6, F_6, E_6},
{1, D_5, F_5, E_5},
{1, D_4, F_4, E_4},
{1, D_3, F_3, E_3},
{1, D_2, F_2, E_2},
{1, D_1, F_1, E_1},
{1, J_2, L_2, K_2},
{1, J_1, L_1, K_1},
};
#define __ NO_LED
led_config_t g_led_config = {
{
// Key Matrix to LED Index
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, __, 14, 15, 16, __ },
{ 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35 },
{ 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54 },
{ 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, __, 69, 70, 71, __ },
{ 72, __, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, __, 85, 86, 87, 88 },
{ 89, 90, 91, __, __, __, 92, __, __, 93, 94, 95, 96, 97, 98, __, 99, 100, __ },
},
{
// LED Index to Physical Position
{0, 0}, {23, 0}, {34, 0}, {46, 0}, {57, 0}, {75, 0}, {86, 0}, {98, 0}, {109, 0}, {127, 0}, {138, 0}, {150, 0}, {161, 0}, {175, 0}, {189, 0}, {201, 0}, {212, 0},
{0,15}, {12,15}, {23,15}, {35,15}, {46,15}, {58,15}, {69,15}, {81,15}, { 92,15}, {104,15}, {115,15}, {127,15}, {138,15}, {155,15}, {175,15}, {189,15}, {201,15}, {212,15}, {224,15},
{3,26}, {17,26}, {29,26}, {40,26}, {52,26}, {63,26}, {75,26}, {86,26}, { 98,26}, {109,26}, {121,26}, {132,26}, {144,26}, {158,26}, {175,26}, {189,26}, {201,26}, {212,26}, {224,32},
{4,38}, {20,38}, {32,38}, {43,38}, {55,38}, {66,38}, {78,38}, {89,38}, {101,38}, {112,38}, {124,38}, {135,38}, {154,38}, {175,38}, {189,38}, {201,38}, {212,38},
{7,50}, {26,50}, {37,50}, {49,50}, {60,50}, {72,50}, {83,50}, { 95,50}, {106,50}, {118,50}, {129,50}, {145,50}, {164,52}, {189,50}, {201,50}, {212,50}, {224,55},
{1,61}, {16,61}, {30,61}, {73,61}, {115,61}, {126,61}, {138,61}, {152,64}, {164,64}, {175,64}, {195,61}, {212,61},
},
{
// RGB LED Index to Flag
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
}
};
#ifdef KEYCHRON_RGB_ENABLE
// Default Color of Per Key RGB
#define DC_RED {HSV_RED}
#define DC_BLU {HSV_BLUE}
#define DC_YLW {HSV_YELLOW}
// 101 LEDs: rows match g_led_config above
// Row 0 (0-16): Fn row (Esc, F1-F12, Del, PrtSc, PgUp, PgDn)
// Row 1 (17-35): Number row + numpad cluster top
// Row 2 (36-54): QWERTY row + numpad cluster mid
// Row 3 (55-71): ASDF row + numpad cluster
// Row 4 (72-88): ZXCV row + numpad arrows
// Row 5 (89-100): 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_BLU, DC_BLU, 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_BLU, DC_BLU, DC_BLU, 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_RED, DC_BLU, DC_BLU, 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_BLU, 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_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
};
#endif
#endif