qmk_firmware

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

led_matrix.h (7064B)


      1 /* Copyright 2017 Jason Williams
      2  * Copyright 2017 Jack Humbert
      3  * Copyright 2018 Yiancar
      4  * Copyright 2019 Clueboard
      5  * Copyright 2021 Leo Deng
      6  *
      7  * This program is free software: you can redistribute it and/or modify
      8  * it under the terms of the GNU General Public License as published by
      9  * the Free Software Foundation, either version 2 of the License, or
     10  * (at your option) any later version.
     11  *
     12  * This program is distributed in the hope that it will be useful,
     13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15  * GNU General Public License for more details.
     16  *
     17  * You should have received a copy of the GNU General Public License
     18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
     19  */
     20 
     21 #pragma once
     22 
     23 #include <stdint.h>
     24 #include <stdbool.h>
     25 #include "led_matrix_types.h"
     26 #include "led_matrix_drivers.h"
     27 #include "keyboard.h"
     28 
     29 #ifndef LED_MATRIX_TIMEOUT
     30 #    define LED_MATRIX_TIMEOUT 0
     31 #endif
     32 
     33 #ifndef LED_MATRIX_MAXIMUM_BRIGHTNESS
     34 #    define LED_MATRIX_MAXIMUM_BRIGHTNESS UINT8_MAX
     35 #endif
     36 
     37 #ifndef LED_MATRIX_VAL_STEP
     38 #    define LED_MATRIX_VAL_STEP 8
     39 #endif
     40 
     41 #ifndef LED_MATRIX_SPD_STEP
     42 #    define LED_MATRIX_SPD_STEP 16
     43 #endif
     44 
     45 #ifndef LED_MATRIX_DEFAULT_ON
     46 #    define LED_MATRIX_DEFAULT_ON true
     47 #endif
     48 
     49 #ifndef LED_MATRIX_DEFAULT_MODE
     50 #    define LED_MATRIX_DEFAULT_MODE LED_MATRIX_SOLID
     51 #endif
     52 
     53 #ifndef LED_MATRIX_DEFAULT_VAL
     54 #    define LED_MATRIX_DEFAULT_VAL LED_MATRIX_MAXIMUM_BRIGHTNESS
     55 #endif
     56 
     57 #ifndef LED_MATRIX_DEFAULT_SPD
     58 #    define LED_MATRIX_DEFAULT_SPD UINT8_MAX / 2
     59 #endif
     60 
     61 #ifndef LED_MATRIX_DEFAULT_FLAGS
     62 #    define LED_MATRIX_DEFAULT_FLAGS LED_FLAG_ALL
     63 #endif
     64 
     65 #ifndef LED_MATRIX_LED_FLUSH_LIMIT
     66 #    define LED_MATRIX_LED_FLUSH_LIMIT 16
     67 #endif
     68 
     69 #ifndef LED_MATRIX_LED_PROCESS_LIMIT
     70 #    define LED_MATRIX_LED_PROCESS_LIMIT ((LED_MATRIX_LED_COUNT + 4) / 5)
     71 #endif
     72 
     73 struct led_matrix_limits_t {
     74     uint8_t led_min_index;
     75     uint8_t led_max_index;
     76 };
     77 
     78 struct led_matrix_limits_t led_matrix_get_limits(uint8_t iter);
     79 
     80 #define LED_MATRIX_USE_LIMITS_ITER(min, max, iter)                   \
     81     struct led_matrix_limits_t limits = led_matrix_get_limits(iter); \
     82     uint8_t                    min    = limits.led_min_index;        \
     83     uint8_t                    max    = limits.led_max_index;        \
     84     (void)min;                                                       \
     85     (void)max;
     86 
     87 #define LED_MATRIX_USE_LIMITS(min, max) LED_MATRIX_USE_LIMITS_ITER(min, max, params->iter)
     88 
     89 #define LED_MATRIX_TEST_LED_FLAGS() \
     90     if (!HAS_ANY_FLAGS(g_led_config.flags[i], params->flags)) continue
     91 
     92 enum led_matrix_effects {
     93     LED_MATRIX_NONE = 0,
     94 
     95 // --------------------------------------
     96 // -----Begin led effect enum macros-----
     97 #define LED_MATRIX_EFFECT(name, ...) LED_MATRIX_##name,
     98 #include "led_matrix_effects.inc"
     99 #undef LED_MATRIX_EFFECT
    100 
    101 #ifdef COMMUNITY_MODULES_ENABLE
    102 #    define LED_MATRIX_EFFECT(name, ...) LED_MATRIX_COMMUNITY_MODULE_##name,
    103 #    include "led_matrix_community_modules.inc"
    104 #    undef LED_MATRIX_EFFECT
    105 #endif
    106 
    107 #if defined(LED_MATRIX_CUSTOM_KB) || defined(LED_MATRIX_CUSTOM_USER)
    108 #    define LED_MATRIX_EFFECT(name, ...) LED_MATRIX_CUSTOM_##name,
    109 #    ifdef LED_MATRIX_CUSTOM_KB
    110 #        include "led_matrix_kb.inc"
    111 #    endif
    112 #    ifdef LED_MATRIX_CUSTOM_USER
    113 #        include "led_matrix_user.inc"
    114 #    endif
    115 #    undef LED_MATRIX_EFFECT
    116 #endif
    117     // --------------------------------------
    118     // -----End led effect enum macros-------
    119 
    120     LED_MATRIX_EFFECT_MAX
    121 };
    122 
    123 void eeconfig_update_led_matrix_default(void);
    124 void eeconfig_force_flush_led_matrix(void);
    125 void eeconfig_debug_led_matrix(void);
    126 
    127 uint8_t led_matrix_map_row_column_to_led_kb(uint8_t row, uint8_t column, uint8_t *led_i);
    128 uint8_t led_matrix_map_row_column_to_led(uint8_t row, uint8_t column, uint8_t *led_i);
    129 
    130 int led_matrix_led_index(int index);
    131 
    132 void led_matrix_set_value(int index, uint8_t value);
    133 void led_matrix_set_value_all(uint8_t value);
    134 
    135 void led_matrix_handle_key_event(uint8_t row, uint8_t col, bool pressed);
    136 
    137 void led_matrix_task(void);
    138 
    139 // This runs after another backlight effect and replaces
    140 // values already set
    141 void led_matrix_indicators(void);
    142 bool led_matrix_indicators_kb(void);
    143 bool led_matrix_indicators_user(void);
    144 
    145 void led_matrix_indicators_advanced(effect_params_t *params);
    146 bool led_matrix_indicators_advanced_kb(uint8_t led_min, uint8_t led_max);
    147 bool led_matrix_indicators_advanced_user(uint8_t led_min, uint8_t led_max);
    148 
    149 void led_matrix_init(void);
    150 
    151 void led_matrix_reload_from_eeprom(void);
    152 
    153 void        led_matrix_set_suspend_state(bool state);
    154 bool        led_matrix_get_suspend_state(void);
    155 void        led_matrix_toggle(void);
    156 void        led_matrix_toggle_noeeprom(void);
    157 void        led_matrix_enable(void);
    158 void        led_matrix_enable_noeeprom(void);
    159 void        led_matrix_disable(void);
    160 void        led_matrix_disable_noeeprom(void);
    161 uint8_t     led_matrix_is_enabled(void);
    162 void        led_matrix_mode(uint8_t mode);
    163 void        led_matrix_mode_noeeprom(uint8_t mode);
    164 uint8_t     led_matrix_get_mode(void);
    165 void        led_matrix_step(void);
    166 void        led_matrix_step_noeeprom(void);
    167 void        led_matrix_step_reverse(void);
    168 void        led_matrix_step_reverse_noeeprom(void);
    169 void        led_matrix_set_val(uint8_t val);
    170 void        led_matrix_set_val_noeeprom(uint8_t val);
    171 uint8_t     led_matrix_get_val(void);
    172 void        led_matrix_increase_val(void);
    173 void        led_matrix_increase_val_noeeprom(void);
    174 void        led_matrix_decrease_val(void);
    175 void        led_matrix_decrease_val_noeeprom(void);
    176 void        led_matrix_set_speed(uint8_t speed);
    177 void        led_matrix_set_speed_noeeprom(uint8_t speed);
    178 uint8_t     led_matrix_get_speed(void);
    179 void        led_matrix_increase_speed(void);
    180 void        led_matrix_increase_speed_noeeprom(void);
    181 void        led_matrix_decrease_speed(void);
    182 void        led_matrix_decrease_speed_noeeprom(void);
    183 led_flags_t led_matrix_get_flags(void);
    184 void        led_matrix_set_flags(led_flags_t flags);
    185 void        led_matrix_set_flags_noeeprom(led_flags_t flags);
    186 void        led_matrix_flags_step_noeeprom(void);
    187 void        led_matrix_flags_step(void);
    188 void        led_matrix_flags_step_reverse_noeeprom(void);
    189 void        led_matrix_flags_step_reverse(void);
    190 
    191 #ifdef LED_MATRIX_MODE_NAME_ENABLE
    192 const char *led_matrix_get_mode_name(uint8_t mode);
    193 #endif // LED_MATRIX_MODE_NAME_ENABLE
    194 
    195 static inline bool led_matrix_check_finished_leds(uint8_t led_idx) {
    196 #if defined(LED_MATRIX_SPLIT)
    197     if (is_keyboard_left()) {
    198         uint8_t k_led_matrix_split[2] = LED_MATRIX_SPLIT;
    199         return led_idx < k_led_matrix_split[0];
    200     } else
    201         return led_idx < LED_MATRIX_LED_COUNT;
    202 #else
    203     return led_idx < LED_MATRIX_LED_COUNT;
    204 #endif
    205 }
    206 
    207 extern led_eeconfig_t led_matrix_eeconfig;
    208 
    209 extern uint32_t     g_led_timer;
    210 extern led_config_t g_led_config;
    211 #ifdef LED_MATRIX_KEYREACTIVE_ENABLED
    212 extern last_hit_t g_last_hit_tracker;
    213 #endif
    214 #ifdef LED_MATRIX_FRAMEBUFFER_EFFECTS
    215 extern uint8_t g_led_frame_buffer[MATRIX_ROWS][MATRIX_COLS];
    216 #endif