qmk_firmware

QMK firmware for my keyboards (Corne, Sweep Ferris) and trackball (Ploopy Adept)
Log | Files | Refs | Submodules | LICENSE

rgb_matrix_kb.inc (4605B)


      1 /*
      2  *  Copyright (C) 2021  System76
      3  *
      4  *  This program is free software: you can redistribute it and/or modify
      5  *  it under the terms of the GNU General Public License as published by
      6  *  the Free Software Foundation, either version 3 of the License, or
      7  *  (at your option) any later version.
      8  *
      9  *  This program is distributed in the hope that it will be useful,
     10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
     11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     12  *  GNU General Public License for more details.
     13  *
     14  *  You should have received a copy of the GNU General Public License
     15  *  along with this program.  If not, see <https://www.gnu.org/licenses/>.
     16  */
     17 
     18 RGB_MATRIX_EFFECT(active_keys)
     19 RGB_MATRIX_EFFECT(raw_rgb)
     20 RGB_MATRIX_EFFECT(unlocked)
     21 
     22 #ifdef RGB_MATRIX_CUSTOM_EFFECT_IMPLS
     23 
     24 #include "dynamic_keymap.h"
     25 #include "action_layer.h"
     26 
     27 static bool active_keys_initialized = false;
     28 static uint8_t active_keys_table[RGB_MATRIX_LED_COUNT] = {0};
     29 
     30 static void active_keys_initialize(void) {
     31     for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
     32         for (uint8_t col = 0; col < MATRIX_COLS; col++) {
     33             uint8_t led = g_led_config.matrix_co[row][col];
     34             if (led < RGB_MATRIX_LED_COUNT && row < 16 && col < 16) {
     35                 active_keys_table[led] = (row << 4) | col;
     36             }
     37         }
     38     }
     39     active_keys_initialized = true;
     40 }
     41 
     42 static bool active_keys(effect_params_t* params) {
     43     if (!active_keys_initialized) {
     44         active_keys_initialize();
     45     }
     46 
     47     RGB_MATRIX_USE_LIMITS(led_min, led_max);
     48     uint8_t layer = get_highest_layer(layer_state);
     49     rgb_t rgb = hsv_to_rgb(rgb_matrix_config.hsv);
     50 
     51     for (uint8_t i = led_min; i < led_max; i++) {
     52         RGB_MATRIX_TEST_LED_FLAGS();
     53 
     54         uint8_t rowcol = active_keys_table[i];
     55         uint8_t row = rowcol >> 4;
     56         uint8_t col = rowcol & 0xF;
     57         uint16_t keycode = dynamic_keymap_get_keycode(layer, row, col);
     58         switch (keycode) {
     59             case KC_NO:
     60             case KC_TRNS:
     61                 rgb_matrix_set_color(i, 0, 0, 0);
     62                 break;
     63             default:
     64                 rgb_matrix_set_color(i, rgb.r, rgb.g, rgb.b);
     65                 break;
     66         }
     67     }
     68 
     69     return led_max < RGB_MATRIX_LED_COUNT;
     70 }
     71 
     72 extern rgb_t raw_rgb_data[RGB_MATRIX_LED_COUNT];
     73 
     74 static uint8_t normalize_component(uint8_t component) {
     75     uint16_t x = (uint16_t)component;
     76     x *= rgb_matrix_config.hsv.v;  // Multiply by current brightness
     77     x /= 255;                      // Divide by maximum brightness
     78     return (uint8_t)x;
     79 }
     80 
     81 static rgb_t normalize_index(uint8_t i) {
     82     rgb_t raw = raw_rgb_data[i];
     83     rgb_t rgb = {
     84         .r = normalize_component(raw.r),
     85         .g = normalize_component(raw.g),
     86         .b = normalize_component(raw.b),
     87     };
     88     return rgb;
     89 }
     90 
     91 static bool raw_rgb(effect_params_t* params) {
     92     RGB_MATRIX_USE_LIMITS(led_min, led_max);
     93     for (uint8_t i = led_min; i < led_max; i++) {
     94         RGB_MATRIX_TEST_LED_FLAGS();
     95         rgb_t rgb = normalize_index(i);
     96         rgb_matrix_set_color(i, rgb.r, rgb.g, rgb.b);
     97     }
     98     return led_max < RGB_MATRIX_LED_COUNT;
     99 }
    100 
    101 static uint8_t unlocked_keys[8][2] = {
    102     {2, 7},  // U
    103     {4, 6},  // N
    104     {3, 9},  // L
    105     {2, 9},  // O
    106     {4, 3},  // C
    107     {3, 8},  // K
    108     {2, 3},  // E
    109     {3, 3},  // D
    110 };
    111 
    112 static uint8_t unlocked_ticks      = 0;
    113 static uint8_t unlocked_i          = 0;
    114 static uint8_t unlocked_leds_count = 0;
    115 static uint8_t unlocked_leds[2]    = {0, 0};
    116 
    117 static bool unlocked(effect_params_t* params) {
    118     RGB_MATRIX_USE_LIMITS(led_min, led_max);
    119 
    120     unlocked_ticks++;
    121 
    122     if (params->init) {
    123         unlocked_ticks = 0;
    124         unlocked_i = 0;
    125     }
    126 
    127     if (unlocked_ticks == 0) {
    128         if (unlocked_i == 8) {
    129             unlocked_leds_count = 0;
    130             unlocked_i = 0;
    131         } else {
    132             unlocked_leds_count = rgb_matrix_map_row_column_to_led(unlocked_keys[unlocked_i][0], unlocked_keys[unlocked_i][1], unlocked_leds);
    133             unlocked_i++;
    134         }
    135     }
    136 
    137     for (uint8_t i = led_min; i < led_max; i++) {
    138         RGB_MATRIX_TEST_LED_FLAGS();
    139 
    140         hsv_t hsv = {
    141             .h = i + unlocked_ticks,
    142             .s = 0xFF,
    143             .v = 0x70,
    144         };
    145         for (uint8_t j = 0; j < unlocked_leds_count; j++) {
    146             if (i == unlocked_leds[j]) {
    147                 hsv.s = 0;
    148                 hsv.v = 0xFF;
    149             }
    150         }
    151 
    152         rgb_t rgb = hsv_to_rgb(hsv);
    153         rgb_matrix_set_color(i, rgb.r, rgb.g, rgb.b);
    154     }
    155     return led_max < RGB_MATRIX_LED_COUNT;
    156 }
    157 
    158 #endif  // RGB_MATRIX_CUSTOM_EFFECT_IMPLS