Contains all the directives required to activate QMK's rgb matrix features.
Sets a constant required to read the rgb matrix config from the EEPROM.
Custom keymap based on my layout. This is where the magic happens.
First let's declare a rgb_matrix_config
variable of type rgb_config_t :
rgb_config_t rgb_matrix_config;
Then declare two maps which will serve two purposes, layercolors
to illuminate all the leds of a layer with the same color, and ledcolors
to illuminate individual keys on a layer.
const uint8_t PROGMEM layercolors[][2] = {
...
}
const uint8_t PROGMEM ledcolors[][DRIVER_LED_TOTAL][3] = {
...
}
All colors are described using the HSV format.
Note that the layercolor contains data for the hue and saturation only, since value (or brightness) will be overriden with the value from the eeprom to take into account the user's brightness settings.
Next step is to read the rgb_matrix config from the eeprom. We will use the enable
boolean to turn off the leds if the user toggles them off, as well as use the val
value to adjust the leds brightness based on user control:
void matrix_init_user(void) {
rgb_matrix_config.raw = eeprom_read_dword(EECONFIG_RGB_MATRIX);
}
Then based on which layer is active, the led colors gets assigned:
void rgb_matrix_indicators_user(void) {
uint32_t mode = rgblight_get_mode();
// assign colors if the matrix is on and the current mode
// is SOLID COLORS => No animations running
if(rgb_matrix_config.enable == 1 && mode == 1) {
uint8_t layer = biton32(layer_state);
switch (layer) {
case 0:
set_layer_color(0);
break;
case 1:
set_leds_color(1);
break;
}
}
}
The set_layer_color
function will set all the color of all the leds uniformally by merging the hue and saturation values from the layercolors
map with the brightness value from the eeprom.
void set_layer_color( uint8_t layer ) {
HSV hsv = { .h = pgm_read_byte(&layercolors[layer][0]), .s = pgm_read_byte(&layercolors[layer][1]), .v = rgb_matrix_config.val};
RGB rgb = hsv_to_rgb( hsv );
for (int i = 0; i < DRIVER_LED_TOTAL; i++) {
rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
}
}
The set_leds_color
function will set the leds color individually by merging the hue, saturation values from the ledcolors
map with the brightness value from the eeprom. If the brightness of the key was set to 0 in the ledcolors
map, it will override the brightness value from the eeprom allowing to have individal leds turned off.
void set_leds_color( int layer) {
for (int i = 0; i < DRIVER_LED_TOTAL; i++) {
uint8_t val = pgm_read_byte(&ledcolors[layer][i][2]);
// if the brightness of the led is set to 0 in the map,
// the value is not overriden with global controls, allowing the led
// to appear turned off
HSV hsv = { .h = pgm_read_byte(&ledcolors[layer][i][0]), .s = pgm_read_byte(&ledcolors[layer][i][1]), .v = val == 0 ? 0 : rgb_matrix_config.val};
RGB rgb = hsv_to_rgb( hsv );
rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
}
}
thank you fdidron for sharing this information.
i'm hand-wiring a keyboard that will have several layers dedicated to different musical layouts, and am looking to implement discretionary lighting to distinguish "natural" notes (light on) from "accented" (sharp or flat) notes (light off).
but i'm not versed in circuitry, and am unsure how the wiring of the leds needs to be done to enable the lighting matrix. could you point me somewhere where i could find an example of how the leds are wired to enable the matrix? i'm working with a Teensy 2.0 microcontroller, and need to plan accordingly.
thanks for any information you might provide, and apologies if the question is misplaced.
cheers,
ralf