diff options
Diffstat (limited to 'quantum/led_matrix.c')
| -rw-r--r-- | quantum/led_matrix.c | 554 |
1 files changed, 363 insertions, 191 deletions
diff --git a/quantum/led_matrix.c b/quantum/led_matrix.c index 4f1f06c7a..69600c498 100644 --- a/quantum/led_matrix.c +++ b/quantum/led_matrix.c | |||
| @@ -17,79 +17,106 @@ | |||
| 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | */ | 18 | */ |
| 19 | 19 | ||
| 20 | #include <stdint.h> | ||
| 21 | #include <stdbool.h> | ||
| 22 | #include "quantum.h" | ||
| 23 | #include "led_matrix.h" | 20 | #include "led_matrix.h" |
| 24 | #include "progmem.h" | 21 | #include "progmem.h" |
| 25 | #include "config.h" | 22 | #include "config.h" |
| 26 | #include "eeprom.h" | 23 | #include "eeprom.h" |
| 27 | #include <string.h> | 24 | #include <string.h> |
| 28 | #include <math.h> | 25 | #include <math.h> |
| 26 | #include "led_tables.h" | ||
| 29 | 27 | ||
| 30 | led_eeconfig_t led_matrix_eeconfig; | 28 | #include <lib/lib8tion/lib8tion.h> |
| 31 | 29 | ||
| 32 | #ifndef MAX | 30 | #if defined(LED_DISABLE_AFTER_TIMEOUT) && !defined(LED_DISABLE_TIMEOUT) |
| 33 | # define MAX(X, Y) ((X) > (Y) ? (X) : (Y)) | 31 | # define LED_DISABLE_TIMEOUT (LED_DISABLE_AFTER_TIMEOUT * 1200UL) |
| 34 | #endif | 32 | #endif |
| 35 | 33 | ||
| 36 | #ifndef MIN | 34 | #ifndef LED_DISABLE_TIMEOUT |
| 37 | # define MIN(a, b) ((a) < (b) ? (a) : (b)) | 35 | # define LED_DISABLE_TIMEOUT 0 |
| 38 | #endif | ||
| 39 | |||
| 40 | #ifndef LED_DISABLE_AFTER_TIMEOUT | ||
| 41 | # define LED_DISABLE_AFTER_TIMEOUT 0 | ||
| 42 | #endif | 36 | #endif |
| 43 | 37 | ||
| 44 | #ifndef LED_DISABLE_WHEN_USB_SUSPENDED | 38 | #ifndef LED_DISABLE_WHEN_USB_SUSPENDED |
| 45 | # define LED_DISABLE_WHEN_USB_SUSPENDED false | 39 | # define LED_DISABLE_WHEN_USB_SUSPENDED false |
| 46 | #endif | 40 | #endif |
| 47 | 41 | ||
| 48 | #ifndef EECONFIG_LED_MATRIX | 42 | #if !defined(LED_MATRIX_MAXIMUM_BRIGHTNESS) || LED_MATRIX_MAXIMUM_BRIGHTNESS > UINT8_MAX |
| 49 | # define EECONFIG_LED_MATRIX EECONFIG_RGBLIGHT | 43 | # undef LED_MATRIX_MAXIMUM_BRIGHTNESS |
| 44 | # define LED_MATRIX_MAXIMUM_BRIGHTNESS UINT8_MAX | ||
| 50 | #endif | 45 | #endif |
| 51 | 46 | ||
| 52 | #if !defined(LED_MATRIX_MAXIMUM_BRIGHTNESS) || LED_MATRIX_MAXIMUM_BRIGHTNESS > 255 | 47 | #if !defined(LED_MATRIX_VAL_STEP) |
| 53 | # define LED_MATRIX_MAXIMUM_BRIGHTNESS 255 | 48 | # define LED_MATRIX_VAL_STEP 8 |
| 54 | #endif | 49 | #endif |
| 55 | 50 | ||
| 56 | bool g_suspend_state = false; | 51 | #if !defined(LED_MATRIX_SPD_STEP) |
| 57 | 52 | # define LED_MATRIX_SPD_STEP 16 | |
| 58 | // Global tick at 20 Hz | 53 | #endif |
| 59 | uint32_t g_tick = 0; | ||
| 60 | 54 | ||
| 61 | // Ticks since this key was last hit. | 55 | #if !defined(LED_MATRIX_STARTUP_MODE) |
| 62 | uint8_t g_key_hit[DRIVER_LED_TOTAL]; | 56 | # define LED_MATRIX_STARTUP_MODE LED_MATRIX_UNIFORM_BRIGHTNESS |
| 57 | #endif | ||
| 63 | 58 | ||
| 64 | // Ticks since any key was last hit. | 59 | #if !defined(LED_MATRIX_STARTUP_VAL) |
| 65 | uint32_t g_any_key_hit = 0; | 60 | # define LED_MATRIX_STARTUP_VAL LED_MATRIX_MAXIMUM_BRIGHTNESS |
| 61 | #endif | ||
| 66 | 62 | ||
| 67 | uint32_t eeconfig_read_led_matrix(void) { return eeprom_read_dword(EECONFIG_LED_MATRIX); } | 63 | #if !defined(LED_MATRIX_STARTUP_SPD) |
| 64 | # define LED_MATRIX_STARTUP_SPD UINT8_MAX / 2 | ||
| 65 | #endif | ||
| 68 | 66 | ||
| 69 | void eeconfig_update_led_matrix(uint32_t config_value) { eeprom_update_dword(EECONFIG_LED_MATRIX, config_value); } | 67 | // globals |
| 68 | bool g_suspend_state = false; | ||
| 69 | led_eeconfig_t led_matrix_eeconfig; // TODO: would like to prefix this with g_ for global consistancy, do this in another pr | ||
| 70 | uint32_t g_led_timer; | ||
| 71 | #ifdef LED_MATRIX_FRAMEBUFFER_EFFECTS | ||
| 72 | uint8_t g_led_frame_buffer[MATRIX_ROWS][MATRIX_COLS] = {{0}}; | ||
| 73 | #endif // LED_MATRIX_FRAMEBUFFER_EFFECTS | ||
| 74 | #ifdef LED_MATRIX_KEYREACTIVE_ENABLED | ||
| 75 | last_hit_t g_last_hit_tracker; | ||
| 76 | #endif // LED_MATRIX_KEYREACTIVE_ENABLED | ||
| 77 | |||
| 78 | // internals | ||
| 79 | static uint8_t led_last_enable = UINT8_MAX; | ||
| 80 | static uint8_t led_last_effect = UINT8_MAX; | ||
| 81 | static effect_params_t led_effect_params = {0, LED_FLAG_ALL, false}; | ||
| 82 | static led_task_states led_task_state = SYNCING; | ||
| 83 | #if LED_DISABLE_TIMEOUT > 0 | ||
| 84 | static uint32_t led_anykey_timer; | ||
| 85 | #endif // LED_DISABLE_TIMEOUT > 0 | ||
| 86 | |||
| 87 | // double buffers | ||
| 88 | static uint32_t led_timer_buffer; | ||
| 89 | #ifdef LED_MATRIX_KEYREACTIVE_ENABLED | ||
| 90 | static last_hit_t last_hit_buffer; | ||
| 91 | #endif // LED_MATRIX_KEYREACTIVE_ENABLED | ||
| 92 | |||
| 93 | void eeconfig_read_led_matrix(void) { eeprom_read_block(&led_matrix_eeconfig, EECONFIG_LED_MATRIX, sizeof(led_matrix_eeconfig)); } | ||
| 94 | |||
| 95 | void eeconfig_update_led_matrix(void) { eeprom_update_block(&led_matrix_eeconfig, EECONFIG_LED_MATRIX, sizeof(led_matrix_eeconfig)); } | ||
| 70 | 96 | ||
| 71 | void eeconfig_update_led_matrix_default(void) { | 97 | void eeconfig_update_led_matrix_default(void) { |
| 72 | dprintf("eeconfig_update_led_matrix_default\n"); | 98 | dprintf("eeconfig_update_led_matrix_default\n"); |
| 73 | led_matrix_eeconfig.enable = 1; | 99 | led_matrix_eeconfig.enable = 1; |
| 74 | led_matrix_eeconfig.mode = LED_MATRIX_UNIFORM_BRIGHTNESS; | 100 | led_matrix_eeconfig.mode = LED_MATRIX_STARTUP_MODE; |
| 75 | led_matrix_eeconfig.val = 128; | 101 | led_matrix_eeconfig.val = LED_MATRIX_STARTUP_VAL; |
| 76 | led_matrix_eeconfig.speed = 0; | 102 | led_matrix_eeconfig.speed = LED_MATRIX_STARTUP_SPD; |
| 77 | eeconfig_update_led_matrix(led_matrix_eeconfig.raw); | 103 | led_matrix_eeconfig.flags = LED_FLAG_ALL; |
| 104 | eeconfig_update_led_matrix(); | ||
| 78 | } | 105 | } |
| 79 | 106 | ||
| 80 | void eeconfig_debug_led_matrix(void) { | 107 | void eeconfig_debug_led_matrix(void) { |
| 81 | dprintf("led_matrix_eeconfig eeprom\n"); | 108 | dprintf("led_matrix_eeconfig EEPROM\n"); |
| 82 | dprintf("led_matrix_eeconfig.enable = %d\n", led_matrix_eeconfig.enable); | 109 | dprintf("led_matrix_eeconfig.enable = %d\n", led_matrix_eeconfig.enable); |
| 83 | dprintf("led_matrix_eeconfig.mode = %d\n", led_matrix_eeconfig.mode); | 110 | dprintf("led_matrix_eeconfig.mode = %d\n", led_matrix_eeconfig.mode); |
| 84 | dprintf("led_matrix_eeconfig.val = %d\n", led_matrix_eeconfig.val); | 111 | dprintf("led_matrix_eeconfig.val = %d\n", led_matrix_eeconfig.val); |
| 85 | dprintf("led_matrix_eeconfig.speed = %d\n", led_matrix_eeconfig.speed); | 112 | dprintf("led_matrix_eeconfig.speed = %d\n", led_matrix_eeconfig.speed); |
| 113 | dprintf("led_matrix_eeconfig.flags = %d\n", led_matrix_eeconfig.flags); | ||
| 86 | } | 114 | } |
| 87 | 115 | ||
| 88 | uint8_t g_last_led_hit[LED_HITS_TO_REMEMBER] = {255}; | 116 | __attribute__((weak)) uint8_t led_matrix_map_row_column_to_led_kb(uint8_t row, uint8_t column, uint8_t *led_i) { return 0; } |
| 89 | uint8_t g_last_led_count = 0; | ||
| 90 | 117 | ||
| 91 | uint8_t map_row_column_to_led(uint8_t row, uint8_t column, uint8_t *led_i) { | 118 | uint8_t led_matrix_map_row_column_to_led(uint8_t row, uint8_t column, uint8_t *led_i) { |
| 92 | uint8_t led_count = 0; | 119 | uint8_t led_count = led_matrix_map_row_column_to_led_kb(row, column, led_i); |
| 93 | uint8_t led_index = g_led_config.matrix_co[row][column]; | 120 | uint8_t led_index = g_led_config.matrix_co[row][column]; |
| 94 | if (led_index != NO_LED) { | 121 | if (led_index != NO_LED) { |
| 95 | led_i[led_count] = led_index; | 122 | led_i[led_count] = led_index; |
| @@ -100,88 +127,211 @@ uint8_t map_row_column_to_led(uint8_t row, uint8_t column, uint8_t *led_i) { | |||
| 100 | 127 | ||
| 101 | void led_matrix_update_pwm_buffers(void) { led_matrix_driver.flush(); } | 128 | void led_matrix_update_pwm_buffers(void) { led_matrix_driver.flush(); } |
| 102 | 129 | ||
| 103 | void led_matrix_set_index_value(int index, uint8_t value) { led_matrix_driver.set_value(index, value); } | 130 | void led_matrix_set_value(int index, uint8_t value) { |
| 104 | 131 | #ifdef USE_CIE1931_CURVE | |
| 105 | void led_matrix_set_index_value_all(uint8_t value) { led_matrix_driver.set_value_all(value); } | 132 | led_matrix_driver.set_value(index, pgm_read_byte(&CIE1931_CURVE[value])); |
| 106 | 133 | #else | |
| 107 | bool process_led_matrix(uint16_t keycode, keyrecord_t *record) { | 134 | led_matrix_driver.set_value(index, value); |
| 108 | if (record->event.pressed) { | 135 | #endif |
| 109 | uint8_t led[8]; | 136 | } |
| 110 | uint8_t led_count = map_row_column_to_led(record->event.key.row, record->event.key.col, led); | ||
| 111 | if (led_count > 0) { | ||
| 112 | for (uint8_t i = LED_HITS_TO_REMEMBER; i > 1; i--) { | ||
| 113 | g_last_led_hit[i - 1] = g_last_led_hit[i - 2]; | ||
| 114 | } | ||
| 115 | g_last_led_hit[0] = led[0]; | ||
| 116 | g_last_led_count = MIN(LED_HITS_TO_REMEMBER, g_last_led_count + 1); | ||
| 117 | } | ||
| 118 | for (uint8_t i = 0; i < led_count; i++) g_key_hit[led[i]] = 0; | ||
| 119 | g_any_key_hit = 0; | ||
| 120 | } else { | ||
| 121 | #ifdef LED_MATRIX_KEYRELEASES | ||
| 122 | uint8_t led[8]; | ||
| 123 | uint8_t led_count = map_row_column_to_led(record->event.key.row, record->event.key.col, led); | ||
| 124 | for (uint8_t i = 0; i < led_count; i++) g_key_hit[led[i]] = 255; | ||
| 125 | 137 | ||
| 126 | g_any_key_hit = 255; | 138 | void led_matrix_set_value_all(uint8_t value) { |
| 139 | #ifdef USE_CIE1931_CURVE | ||
| 140 | led_matrix_driver.set_value_all(pgm_read_byte(&CIE1931_CURVE[value])); | ||
| 141 | #else | ||
| 142 | led_matrix_driver.set_value_all(value); | ||
| 127 | #endif | 143 | #endif |
| 128 | } | ||
| 129 | return true; | ||
| 130 | } | 144 | } |
| 131 | 145 | ||
| 132 | void led_matrix_set_suspend_state(bool state) { g_suspend_state = state; } | 146 | void process_led_matrix(uint8_t row, uint8_t col, bool pressed) { |
| 147 | #if LED_DISABLE_TIMEOUT > 0 | ||
| 148 | led_anykey_timer = 0; | ||
| 149 | #endif // LED_DISABLE_TIMEOUT > 0 | ||
| 150 | |||
| 151 | #ifdef LED_MATRIX_KEYREACTIVE_ENABLED | ||
| 152 | uint8_t led[LED_HITS_TO_REMEMBER]; | ||
| 153 | uint8_t led_count = 0; | ||
| 133 | 154 | ||
| 134 | // All LEDs off | 155 | # if defined(LED_MATRIX_KEYRELEASES) |
| 135 | void led_matrix_all_off(void) { led_matrix_set_index_value_all(0); } | 156 | if (!pressed) |
| 157 | # elif defined(LED_MATRIX_KEYPRESSES) | ||
| 158 | if (pressed) | ||
| 159 | # endif // defined(LED_MATRIX_KEYRELEASES) | ||
| 160 | { | ||
| 161 | led_count = led_matrix_map_row_column_to_led(row, col, led); | ||
| 162 | } | ||
| 136 | 163 | ||
| 137 | // Uniform brightness | 164 | if (last_hit_buffer.count + led_count > LED_HITS_TO_REMEMBER) { |
| 138 | void led_matrix_uniform_brightness(void) { led_matrix_set_index_value_all(LED_MATRIX_MAXIMUM_BRIGHTNESS / BACKLIGHT_LEVELS * led_matrix_eeconfig.val); } | 165 | memcpy(&last_hit_buffer.x[0], &last_hit_buffer.x[led_count], LED_HITS_TO_REMEMBER - led_count); |
| 166 | memcpy(&last_hit_buffer.y[0], &last_hit_buffer.y[led_count], LED_HITS_TO_REMEMBER - led_count); | ||
| 167 | memcpy(&last_hit_buffer.tick[0], &last_hit_buffer.tick[led_count], (LED_HITS_TO_REMEMBER - led_count) * 2); // 16 bit | ||
| 168 | memcpy(&last_hit_buffer.index[0], &last_hit_buffer.index[led_count], LED_HITS_TO_REMEMBER - led_count); | ||
| 169 | last_hit_buffer.count--; | ||
| 170 | } | ||
| 139 | 171 | ||
| 140 | void led_matrix_custom(void) {} | 172 | for (uint8_t i = 0; i < led_count; i++) { |
| 173 | uint8_t index = last_hit_buffer.count; | ||
| 174 | last_hit_buffer.x[index] = g_led_config.point[led[i]].x; | ||
| 175 | last_hit_buffer.y[index] = g_led_config.point[led[i]].y; | ||
| 176 | last_hit_buffer.index[index] = led[i]; | ||
| 177 | last_hit_buffer.tick[index] = 0; | ||
| 178 | last_hit_buffer.count++; | ||
| 179 | } | ||
| 180 | #endif // LED_MATRIX_KEYREACTIVE_ENABLED | ||
| 141 | 181 | ||
| 142 | void led_matrix_task(void) { | 182 | #if defined(LED_MATRIX_FRAMEBUFFER_EFFECTS) && !defined(DISABLE_LED_MATRIX_TYPING_HEATMAP) |
| 143 | if (!led_matrix_eeconfig.enable) { | 183 | if (led_matrix_eeconfig.mode == LED_MATRIX_TYPING_HEATMAP) { |
| 144 | led_matrix_all_off(); | 184 | process_led_matrix_typing_heatmap(row, col); |
| 145 | led_matrix_indicators(); | 185 | } |
| 146 | return; | 186 | #endif // defined(LED_MATRIX_FRAMEBUFFER_EFFECTS) && !defined(DISABLE_LED_MATRIX_TYPING_HEATMAP) |
| 187 | } | ||
| 188 | |||
| 189 | static bool led_matrix_none(effect_params_t *params) { | ||
| 190 | if (!params->init) { | ||
| 191 | return false; | ||
| 147 | } | 192 | } |
| 148 | 193 | ||
| 149 | g_tick++; | 194 | led_matrix_set_value_all(0); |
| 195 | return false; | ||
| 196 | } | ||
| 197 | |||
| 198 | static bool led_matrix_uniform_brightness(effect_params_t *params) { | ||
| 199 | LED_MATRIX_USE_LIMITS(led_min, led_max); | ||
| 150 | 200 | ||
| 151 | if (g_any_key_hit < 0xFFFFFFFF) { | 201 | uint8_t val = led_matrix_eeconfig.val; |
| 152 | g_any_key_hit++; | 202 | for (uint8_t i = led_min; i < led_max; i++) { |
| 203 | LED_MATRIX_TEST_LED_FLAGS(); | ||
| 204 | led_matrix_set_value(i, val); | ||
| 153 | } | 205 | } |
| 206 | return led_max < DRIVER_LED_TOTAL; | ||
| 207 | } | ||
| 154 | 208 | ||
| 155 | for (int led = 0; led < DRIVER_LED_TOTAL; led++) { | 209 | static void led_task_timers(void) { |
| 156 | if (g_key_hit[led] < 255) { | 210 | #if defined(LED_MATRIX_KEYREACTIVE_ENABLED) || LED_DISABLE_TIMEOUT > 0 |
| 157 | if (g_key_hit[led] == 254) g_last_led_count = MAX(g_last_led_count - 1, 0); | 211 | uint32_t deltaTime = sync_timer_elapsed32(led_timer_buffer); |
| 158 | g_key_hit[led]++; | 212 | #endif // defined(LED_MATRIX_KEYREACTIVE_ENABLED) || LED_DISABLE_TIMEOUT > 0 |
| 213 | led_timer_buffer = sync_timer_read32(); | ||
| 214 | |||
| 215 | // Update double buffer timers | ||
| 216 | #if LED_DISABLE_TIMEOUT > 0 | ||
| 217 | if (led_anykey_timer < UINT32_MAX) { | ||
| 218 | if (UINT32_MAX - deltaTime < led_anykey_timer) { | ||
| 219 | led_anykey_timer = UINT32_MAX; | ||
| 220 | } else { | ||
| 221 | led_anykey_timer += deltaTime; | ||
| 159 | } | 222 | } |
| 160 | } | 223 | } |
| 224 | #endif // LED_DISABLE_TIMEOUT > 0 | ||
| 225 | |||
| 226 | // Update double buffer last hit timers | ||
| 227 | #ifdef LED_MATRIX_KEYREACTIVE_ENABLED | ||
| 228 | uint8_t count = last_hit_buffer.count; | ||
| 229 | for (uint8_t i = 0; i < count; ++i) { | ||
| 230 | if (UINT16_MAX - deltaTime < last_hit_buffer.tick[i]) { | ||
| 231 | last_hit_buffer.count--; | ||
| 232 | continue; | ||
| 233 | } | ||
| 234 | last_hit_buffer.tick[i] += deltaTime; | ||
| 235 | } | ||
| 236 | #endif // LED_MATRIX_KEYREACTIVE_ENABLED | ||
| 237 | } | ||
| 161 | 238 | ||
| 162 | // Ideally we would also stop sending zeros to the LED driver PWM buffers | 239 | static void led_task_sync(void) { |
| 163 | // while suspended and just do a software shutdown. This is a cheap hack for now. | 240 | // next task |
| 164 | bool suspend_backlight = ((g_suspend_state && LED_DISABLE_WHEN_USB_SUSPENDED) || (LED_DISABLE_AFTER_TIMEOUT > 0 && g_any_key_hit > LED_DISABLE_AFTER_TIMEOUT * 60 * 20)); | 241 | if (sync_timer_elapsed32(g_led_timer) >= LED_MATRIX_LED_FLUSH_LIMIT) led_task_state = STARTING; |
| 165 | uint8_t effect = suspend_backlight ? 0 : led_matrix_eeconfig.mode; | 242 | } |
| 243 | |||
| 244 | static void led_task_start(void) { | ||
| 245 | // reset iter | ||
| 246 | led_effect_params.iter = 0; | ||
| 247 | |||
| 248 | // update double buffers | ||
| 249 | g_led_timer = led_timer_buffer; | ||
| 250 | #ifdef LED_MATRIX_KEYREACTIVE_ENABLED | ||
| 251 | g_last_hit_tracker = last_hit_buffer; | ||
| 252 | #endif // LED_MATRIX_KEYREACTIVE_ENABLED | ||
| 253 | |||
| 254 | // next task | ||
| 255 | led_task_state = RENDERING; | ||
| 256 | } | ||
| 257 | |||
| 258 | static void led_task_render(uint8_t effect) { | ||
| 259 | bool rendering = false; | ||
| 260 | led_effect_params.init = (effect != led_last_effect) || (led_matrix_eeconfig.enable != led_last_enable); | ||
| 261 | if (led_effect_params.flags != led_matrix_eeconfig.flags) { | ||
| 262 | led_effect_params.flags = led_matrix_eeconfig.flags; | ||
| 263 | led_matrix_set_value_all(0); | ||
| 264 | } | ||
| 166 | 265 | ||
| 167 | // this gets ticked at 20 Hz. | ||
| 168 | // each effect can opt to do calculations | 266 | // each effect can opt to do calculations |
| 169 | // and/or request PWM buffer updates. | 267 | // and/or request PWM buffer updates. |
| 170 | switch (effect) { | 268 | switch (effect) { |
| 171 | case LED_MATRIX_UNIFORM_BRIGHTNESS: | 269 | case LED_MATRIX_NONE: |
| 172 | led_matrix_uniform_brightness(); | 270 | rendering = led_matrix_none(&led_effect_params); |
| 173 | break; | 271 | break; |
| 174 | default: | 272 | case LED_MATRIX_UNIFORM_BRIGHTNESS: |
| 175 | led_matrix_custom(); | 273 | rendering = led_matrix_uniform_brightness(&led_effect_params); |
| 176 | break; | 274 | break; |
| 177 | } | 275 | } |
| 178 | 276 | ||
| 179 | if (!suspend_backlight) { | 277 | led_effect_params.iter++; |
| 180 | led_matrix_indicators(); | 278 | |
| 279 | // next task | ||
| 280 | if (!rendering) { | ||
| 281 | led_task_state = FLUSHING; | ||
| 282 | if (!led_effect_params.init && effect == LED_MATRIX_NONE) { | ||
| 283 | // We only need to flush once if we are LED_MATRIX_NONE | ||
| 284 | led_task_state = SYNCING; | ||
| 285 | } | ||
| 181 | } | 286 | } |
| 287 | } | ||
| 288 | |||
| 289 | static void led_task_flush(uint8_t effect) { | ||
| 290 | // update last trackers after the first full render so we can init over several frames | ||
| 291 | led_last_effect = effect; | ||
| 292 | led_last_enable = led_matrix_eeconfig.enable; | ||
| 182 | 293 | ||
| 183 | // Tell the LED driver to update its state | 294 | // update pwm buffers |
| 184 | led_matrix_driver.flush(); | 295 | led_matrix_update_pwm_buffers(); |
| 296 | |||
| 297 | // next task | ||
| 298 | led_task_state = SYNCING; | ||
| 299 | } | ||
| 300 | |||
| 301 | void led_matrix_task(void) { | ||
| 302 | led_task_timers(); | ||
| 303 | |||
| 304 | // Ideally we would also stop sending zeros to the LED driver PWM buffers | ||
| 305 | // while suspended and just do a software shutdown. This is a cheap hack for now. | ||
| 306 | bool suspend_backlight = | ||
| 307 | #if LED_DISABLE_WHEN_USB_SUSPENDED == true | ||
| 308 | g_suspend_state || | ||
| 309 | #endif // LED_DISABLE_WHEN_USB_SUSPENDED == true | ||
| 310 | #if LED_DISABLE_TIMEOUT > 0 | ||
| 311 | (led_anykey_timer > (uint32_t)LED_DISABLE_TIMEOUT) || | ||
| 312 | #endif // LED_DISABLE_TIMEOUT > 0 | ||
| 313 | false; | ||
| 314 | |||
| 315 | uint8_t effect = suspend_backlight || !led_matrix_eeconfig.enable ? 0 : led_matrix_eeconfig.mode; | ||
| 316 | |||
| 317 | switch (led_task_state) { | ||
| 318 | case STARTING: | ||
| 319 | led_task_start(); | ||
| 320 | break; | ||
| 321 | case RENDERING: | ||
| 322 | led_task_render(effect); | ||
| 323 | if (effect) { | ||
| 324 | led_matrix_indicators(); | ||
| 325 | led_matrix_indicators_advanced(&led_effect_params); | ||
| 326 | } | ||
| 327 | break; | ||
| 328 | case FLUSHING: | ||
| 329 | led_task_flush(effect); | ||
| 330 | break; | ||
| 331 | case SYNCING: | ||
| 332 | led_task_sync(); | ||
| 333 | break; | ||
| 334 | } | ||
| 185 | } | 335 | } |
| 186 | 336 | ||
| 187 | void led_matrix_indicators(void) { | 337 | void led_matrix_indicators(void) { |
| @@ -193,33 +343,42 @@ __attribute__((weak)) void led_matrix_indicators_kb(void) {} | |||
| 193 | 343 | ||
| 194 | __attribute__((weak)) void led_matrix_indicators_user(void) {} | 344 | __attribute__((weak)) void led_matrix_indicators_user(void) {} |
| 195 | 345 | ||
| 196 | // void led_matrix_set_indicator_index(uint8_t *index, uint8_t row, uint8_t column) | 346 | void led_matrix_indicators_advanced(effect_params_t *params) { |
| 197 | // { | 347 | /* special handling is needed for "params->iter", since it's already been incremented. |
| 198 | // if (row >= MATRIX_ROWS) | 348 | * Could move the invocations to led_task_render, but then it's missing a few checks |
| 199 | // { | 349 | * and not sure which would be better. Otherwise, this should be called from |
| 200 | // // Special value, 255=none, 254=all | 350 | * led_task_render, right before the iter++ line. |
| 201 | // *index = row; | 351 | */ |
| 202 | // } | 352 | #if defined(LED_MATRIX_LED_PROCESS_LIMIT) && LED_MATRIX_LED_PROCESS_LIMIT > 0 && LED_MATRIX_LED_PROCESS_LIMIT < DRIVER_LED_TOTAL |
| 203 | // else | 353 | uint8_t min = LED_MATRIX_LED_PROCESS_LIMIT * (params->iter - 1); |
| 204 | // { | 354 | uint8_t max = min + LED_MATRIX_LED_PROCESS_LIMIT; |
| 205 | // // This needs updated to something like | 355 | if (max > DRIVER_LED_TOTAL) max = DRIVER_LED_TOTAL; |
| 206 | // // uint8_t led[8]; | 356 | #else |
| 207 | // // uint8_t led_count = map_row_column_to_led(row, column, led); | 357 | uint8_t min = 0; |
| 208 | // // for(uint8_t i = 0; i < led_count; i++) | 358 | uint8_t max = DRIVER_LED_TOTAL; |
| 209 | // map_row_column_to_led(row, column, index); | 359 | #endif |
| 210 | // } | 360 | led_matrix_indicators_advanced_kb(min, max); |
| 211 | // } | 361 | led_matrix_indicators_advanced_user(min, max); |
| 362 | } | ||
| 363 | |||
| 364 | __attribute__((weak)) void led_matrix_indicators_advanced_kb(uint8_t led_min, uint8_t led_max) {} | ||
| 365 | |||
| 366 | __attribute__((weak)) void led_matrix_indicators_advanced_user(uint8_t led_min, uint8_t led_max) {} | ||
| 212 | 367 | ||
| 213 | void led_matrix_init(void) { | 368 | void led_matrix_init(void) { |
| 214 | led_matrix_driver.init(); | 369 | led_matrix_driver.init(); |
| 215 | 370 | ||
| 216 | // Wait half a second for the driver to finish initializing | 371 | #ifdef LED_MATRIX_KEYREACTIVE_ENABLED |
| 217 | wait_ms(500); | 372 | g_last_hit_tracker.count = 0; |
| 373 | for (uint8_t i = 0; i < LED_HITS_TO_REMEMBER; ++i) { | ||
| 374 | g_last_hit_tracker.tick[i] = UINT16_MAX; | ||
| 375 | } | ||
| 218 | 376 | ||
| 219 | // clear the key hits | 377 | last_hit_buffer.count = 0; |
| 220 | for (int led = 0; led < DRIVER_LED_TOTAL; led++) { | 378 | for (uint8_t i = 0; i < LED_HITS_TO_REMEMBER; ++i) { |
| 221 | g_key_hit[led] = 255; | 379 | last_hit_buffer.tick[i] = UINT16_MAX; |
| 222 | } | 380 | } |
| 381 | #endif // LED_MATRIX_KEYREACTIVE_ENABLED | ||
| 223 | 382 | ||
| 224 | if (!eeconfig_is_enabled()) { | 383 | if (!eeconfig_is_enabled()) { |
| 225 | dprintf("led_matrix_init_drivers eeconfig is not enabled.\n"); | 384 | dprintf("led_matrix_init_drivers eeconfig is not enabled.\n"); |
| @@ -227,122 +386,135 @@ void led_matrix_init(void) { | |||
| 227 | eeconfig_update_led_matrix_default(); | 386 | eeconfig_update_led_matrix_default(); |
| 228 | } | 387 | } |
| 229 | 388 | ||
| 230 | led_matrix_eeconfig.raw = eeconfig_read_led_matrix(); | 389 | eeconfig_read_led_matrix(); |
| 231 | |||
| 232 | if (!led_matrix_eeconfig.mode) { | 390 | if (!led_matrix_eeconfig.mode) { |
| 233 | dprintf("led_matrix_init_drivers led_matrix_eeconfig.mode = 0. Write default values to EEPROM.\n"); | 391 | dprintf("led_matrix_init_drivers led_matrix_eeconfig.mode = 0. Write default values to EEPROM.\n"); |
| 234 | eeconfig_update_led_matrix_default(); | 392 | eeconfig_update_led_matrix_default(); |
| 235 | led_matrix_eeconfig.raw = eeconfig_read_led_matrix(); | ||
| 236 | } | 393 | } |
| 237 | |||
| 238 | eeconfig_debug_led_matrix(); // display current eeprom values | 394 | eeconfig_debug_led_matrix(); // display current eeprom values |
| 239 | } | 395 | } |
| 240 | 396 | ||
| 241 | // Deals with the messy details of incrementing an integer | 397 | void led_matrix_set_suspend_state(bool state) { |
| 242 | static uint8_t increment(uint8_t value, uint8_t step, uint8_t min, uint8_t max) { | 398 | if (LED_DISABLE_WHEN_USB_SUSPENDED && state) { |
| 243 | int16_t new_value = value; | 399 | led_matrix_set_value_all(0); // turn off all LEDs when suspending |
| 244 | new_value += step; | 400 | } |
| 245 | return MIN(MAX(new_value, min), max); | 401 | g_suspend_state = state; |
| 246 | } | 402 | } |
| 247 | 403 | ||
| 248 | static uint8_t decrement(uint8_t value, uint8_t step, uint8_t min, uint8_t max) { | 404 | bool led_matrix_get_suspend_state(void) { return g_suspend_state; } |
| 249 | int16_t new_value = value; | ||
| 250 | new_value -= step; | ||
| 251 | return MIN(MAX(new_value, min), max); | ||
| 252 | } | ||
| 253 | 405 | ||
| 254 | // void *backlight_get_custom_key_value_eeprom_address(uint8_t led) { | 406 | void led_matrix_toggle_eeprom_helper(bool write_to_eeprom) { |
| 255 | // // 3 bytes per value | ||
| 256 | // return EECONFIG_LED_MATRIX + (led * 3); | ||
| 257 | // } | ||
| 258 | |||
| 259 | // void backlight_get_key_value(uint8_t led, uint8_t *value) { | ||
| 260 | // void *address = backlight_get_custom_key_value_eeprom_address(led); | ||
| 261 | // value = eeprom_read_byte(address); | ||
| 262 | // } | ||
| 263 | |||
| 264 | // void backlight_set_key_value(uint8_t row, uint8_t column, uint8_t value) { | ||
| 265 | // uint8_t led[8]; | ||
| 266 | // uint8_t led_count = map_row_column_to_led(row, column, led); | ||
| 267 | // for(uint8_t i = 0; i < led_count; i++) { | ||
| 268 | // if (led[i] < DRIVER_LED_TOTAL) { | ||
| 269 | // void *address = backlight_get_custom_key_value_eeprom_address(led[i]); | ||
| 270 | // eeprom_update_byte(address, value); | ||
| 271 | // } | ||
| 272 | // } | ||
| 273 | // } | ||
| 274 | |||
| 275 | uint32_t led_matrix_get_tick(void) { return g_tick; } | ||
| 276 | |||
| 277 | void led_matrix_toggle(void) { | ||
| 278 | led_matrix_eeconfig.enable ^= 1; | 407 | led_matrix_eeconfig.enable ^= 1; |
| 279 | eeconfig_update_led_matrix(led_matrix_eeconfig.raw); | 408 | led_task_state = STARTING; |
| 409 | if (write_to_eeprom) { | ||
| 410 | eeconfig_update_led_matrix(); | ||
| 411 | } | ||
| 412 | dprintf("led matrix toggle [%s]: led_matrix_eeconfig.enable = %u\n", (write_to_eeprom) ? "EEPROM" : "NOEEPROM", led_matrix_eeconfig.enable); | ||
| 280 | } | 413 | } |
| 414 | void led_matrix_toggle_noeeprom(void) { led_matrix_toggle_eeprom_helper(false); } | ||
| 415 | void led_matrix_toggle(void) { led_matrix_toggle_eeprom_helper(true); } | ||
| 281 | 416 | ||
| 282 | void led_matrix_enable(void) { | 417 | void led_matrix_enable(void) { |
| 283 | led_matrix_eeconfig.enable = 1; | 418 | led_matrix_enable_noeeprom(); |
| 284 | eeconfig_update_led_matrix(led_matrix_eeconfig.raw); | 419 | eeconfig_update_led_matrix(); |
| 285 | } | 420 | } |
| 286 | 421 | ||
| 287 | void led_matrix_enable_noeeprom(void) { led_matrix_eeconfig.enable = 1; } | 422 | void led_matrix_enable_noeeprom(void) { |
| 423 | if (!led_matrix_eeconfig.enable) led_task_state = STARTING; | ||
| 424 | led_matrix_eeconfig.enable = 1; | ||
| 425 | } | ||
| 288 | 426 | ||
| 289 | void led_matrix_disable(void) { | 427 | void led_matrix_disable(void) { |
| 428 | led_matrix_disable_noeeprom(); | ||
| 429 | eeconfig_update_led_matrix(); | ||
| 430 | } | ||
| 431 | |||
| 432 | void led_matrix_disable_noeeprom(void) { | ||
| 433 | if (led_matrix_eeconfig.enable) led_task_state = STARTING; | ||
| 290 | led_matrix_eeconfig.enable = 0; | 434 | led_matrix_eeconfig.enable = 0; |
| 291 | eeconfig_update_led_matrix(led_matrix_eeconfig.raw); | ||
| 292 | } | 435 | } |
| 293 | 436 | ||
| 294 | void led_matrix_disable_noeeprom(void) { led_matrix_eeconfig.enable = 0; } | 437 | uint8_t led_matrix_is_enabled(void) { return led_matrix_eeconfig.enable; } |
| 295 | 438 | ||
| 296 | void led_matrix_step(void) { | 439 | void led_matrix_mode_eeprom_helper(uint8_t mode, bool write_to_eeprom) { |
| 297 | led_matrix_eeconfig.mode++; | 440 | if (!led_matrix_eeconfig.enable) { |
| 298 | if (led_matrix_eeconfig.mode >= LED_MATRIX_EFFECT_MAX) { | 441 | return; |
| 299 | led_matrix_eeconfig.mode = 1; | ||
| 300 | } | 442 | } |
| 301 | eeconfig_update_led_matrix(led_matrix_eeconfig.raw); | 443 | if (mode < 1) { |
| 302 | } | 444 | led_matrix_eeconfig.mode = 1; |
| 303 | 445 | } else if (mode >= LED_MATRIX_EFFECT_MAX) { | |
| 304 | void led_matrix_step_reverse(void) { | ||
| 305 | led_matrix_eeconfig.mode--; | ||
| 306 | if (led_matrix_eeconfig.mode < 1) { | ||
| 307 | led_matrix_eeconfig.mode = LED_MATRIX_EFFECT_MAX - 1; | 446 | led_matrix_eeconfig.mode = LED_MATRIX_EFFECT_MAX - 1; |
| 447 | } else { | ||
| 448 | led_matrix_eeconfig.mode = mode; | ||
| 449 | } | ||
| 450 | led_task_state = STARTING; | ||
| 451 | if (write_to_eeprom) { | ||
| 452 | eeconfig_update_led_matrix(); | ||
| 308 | } | 453 | } |
| 309 | eeconfig_update_led_matrix(led_matrix_eeconfig.raw); | 454 | dprintf("led matrix mode [%s]: %u\n", (write_to_eeprom) ? "EEPROM" : "NOEEPROM", led_matrix_eeconfig.mode); |
| 310 | } | 455 | } |
| 456 | void led_matrix_mode_noeeprom(uint8_t mode) { led_matrix_mode_eeprom_helper(mode, false); } | ||
| 457 | void led_matrix_mode(uint8_t mode) { led_matrix_mode_eeprom_helper(mode, true); } | ||
| 311 | 458 | ||
| 312 | void led_matrix_increase_val(void) { | 459 | uint8_t led_matrix_get_mode(void) { return led_matrix_eeconfig.mode; } |
| 313 | led_matrix_eeconfig.val = increment(led_matrix_eeconfig.val, 8, 0, LED_MATRIX_MAXIMUM_BRIGHTNESS); | ||
| 314 | eeconfig_update_led_matrix(led_matrix_eeconfig.raw); | ||
| 315 | } | ||
| 316 | 460 | ||
| 317 | void led_matrix_decrease_val(void) { | 461 | void led_matrix_step_helper(bool write_to_eeprom) { |
| 318 | led_matrix_eeconfig.val = decrement(led_matrix_eeconfig.val, 8, 0, LED_MATRIX_MAXIMUM_BRIGHTNESS); | 462 | uint8_t mode = led_matrix_eeconfig.mode + 1; |
| 319 | eeconfig_update_led_matrix(led_matrix_eeconfig.raw); | 463 | led_matrix_mode_eeprom_helper((mode < LED_MATRIX_EFFECT_MAX) ? mode : 1, write_to_eeprom); |
| 320 | } | 464 | } |
| 465 | void led_matrix_step_noeeprom(void) { led_matrix_step_helper(false); } | ||
| 466 | void led_matrix_step(void) { led_matrix_step_helper(true); } | ||
| 321 | 467 | ||
| 322 | void led_matrix_increase_speed(void) { | 468 | void led_matrix_step_reverse_helper(bool write_to_eeprom) { |
| 323 | led_matrix_eeconfig.speed = increment(led_matrix_eeconfig.speed, 1, 0, 3); | 469 | uint8_t mode = led_matrix_eeconfig.mode - 1; |
| 324 | eeconfig_update_led_matrix(led_matrix_eeconfig.raw); // EECONFIG needs to be increased to support this | 470 | led_matrix_mode_eeprom_helper((mode < 1) ? LED_MATRIX_EFFECT_MAX - 1 : mode, write_to_eeprom); |
| 325 | } | 471 | } |
| 472 | void led_matrix_step_reverse_noeeprom(void) { led_matrix_step_reverse_helper(false); } | ||
| 473 | void led_matrix_step_reverse(void) { led_matrix_step_reverse_helper(true); } | ||
| 326 | 474 | ||
| 327 | void led_matrix_decrease_speed(void) { | 475 | void led_matrix_set_val_eeprom_helper(uint8_t val, bool write_to_eeprom) { |
| 328 | led_matrix_eeconfig.speed = decrement(led_matrix_eeconfig.speed, 1, 0, 3); | 476 | if (!led_matrix_eeconfig.enable) { |
| 329 | eeconfig_update_led_matrix(led_matrix_eeconfig.raw); // EECONFIG needs to be increased to support this | 477 | return; |
| 478 | } | ||
| 479 | led_matrix_eeconfig.val = (val > LED_MATRIX_MAXIMUM_BRIGHTNESS) ? LED_MATRIX_MAXIMUM_BRIGHTNESS : val; | ||
| 480 | if (write_to_eeprom) { | ||
| 481 | eeconfig_update_led_matrix(); | ||
| 482 | } | ||
| 483 | dprintf("led matrix set val [%s]: %u\n", (write_to_eeprom) ? "EEPROM" : "NOEEPROM", led_matrix_eeconfig.val); | ||
| 330 | } | 484 | } |
| 485 | void led_matrix_set_val_noeeprom(uint8_t val) { led_matrix_set_val_eeprom_helper(val, false); } | ||
| 486 | void led_matrix_set_val(uint8_t val) { led_matrix_set_val_eeprom_helper(val, true); } | ||
| 331 | 487 | ||
| 332 | void led_matrix_mode(uint8_t mode, bool eeprom_write) { | 488 | uint8_t led_matrix_get_val(void) { return led_matrix_eeconfig.val; } |
| 333 | led_matrix_eeconfig.mode = mode; | 489 | |
| 334 | if (eeprom_write) { | 490 | void led_matrix_increase_val_helper(bool write_to_eeprom) { led_matrix_set_val_eeprom_helper(qadd8(led_matrix_eeconfig.val, LED_MATRIX_VAL_STEP), write_to_eeprom); } |
| 335 | eeconfig_update_led_matrix(led_matrix_eeconfig.raw); | 491 | void led_matrix_increase_val_noeeprom(void) { led_matrix_increase_val_helper(false); } |
| 492 | void led_matrix_increase_val(void) { led_matrix_increase_val_helper(true); } | ||
| 493 | |||
| 494 | void led_matrix_decrease_val_helper(bool write_to_eeprom) { led_matrix_set_val_eeprom_helper(qsub8(led_matrix_eeconfig.val, LED_MATRIX_VAL_STEP), write_to_eeprom); } | ||
| 495 | void led_matrix_decrease_val_noeeprom(void) { led_matrix_decrease_val_helper(false); } | ||
| 496 | void led_matrix_decrease_val(void) { led_matrix_decrease_val_helper(true); } | ||
| 497 | |||
| 498 | void led_matrix_set_speed_eeprom_helper(uint8_t speed, bool write_to_eeprom) { | ||
| 499 | led_matrix_eeconfig.speed = speed; | ||
| 500 | if (write_to_eeprom) { | ||
| 501 | eeconfig_update_led_matrix(); | ||
| 336 | } | 502 | } |
| 503 | dprintf("led matrix set speed [%s]: %u\n", (write_to_eeprom) ? "EEPROM" : "NOEEPROM", led_matrix_eeconfig.speed); | ||
| 337 | } | 504 | } |
| 505 | void led_matrix_set_speed_noeeprom(uint8_t speed) { led_matrix_set_speed_eeprom_helper(speed, false); } | ||
| 506 | void led_matrix_set_speed(uint8_t speed) { led_matrix_set_speed_eeprom_helper(speed, true); } | ||
| 338 | 507 | ||
| 339 | uint8_t led_matrix_get_mode(void) { return led_matrix_eeconfig.mode; } | 508 | uint8_t led_matrix_get_speed(void) { return led_matrix_eeconfig.speed; } |
| 340 | 509 | ||
| 341 | void led_matrix_set_value_noeeprom(uint8_t val) { led_matrix_eeconfig.val = val; } | 510 | void led_matrix_increase_speed_helper(bool write_to_eeprom) { led_matrix_set_speed_eeprom_helper(qadd8(led_matrix_eeconfig.speed, LED_MATRIX_SPD_STEP), write_to_eeprom); } |
| 511 | void led_matrix_increase_speed_noeeprom(void) { led_matrix_increase_speed_helper(false); } | ||
| 512 | void led_matrix_increase_speed(void) { led_matrix_increase_speed_helper(true); } | ||
| 342 | 513 | ||
| 343 | void led_matrix_set_value(uint8_t val) { | 514 | void led_matrix_decrease_speed_helper(bool write_to_eeprom) { led_matrix_set_speed_eeprom_helper(qsub8(led_matrix_eeconfig.speed, LED_MATRIX_SPD_STEP), write_to_eeprom); } |
| 344 | led_matrix_set_value_noeeprom(val); | 515 | void led_matrix_decrease_speed_noeeprom(void) { led_matrix_decrease_speed_helper(false); } |
| 345 | eeconfig_update_led_matrix(led_matrix_eeconfig.raw); | 516 | void led_matrix_decrease_speed(void) { led_matrix_decrease_speed_helper(true); } |
| 346 | } | 517 | |
| 518 | led_flags_t led_matrix_get_flags(void) { return led_matrix_eeconfig.flags; } | ||
| 347 | 519 | ||
| 348 | void backlight_set(uint8_t val) { led_matrix_set_value(val); } | 520 | void led_matrix_set_flags(led_flags_t flags) { led_matrix_eeconfig.flags = flags; } |
