1 /* Copyright 2017 Jason Williams
2 * Copyright 2017 Jack Humbert
3 * Copyright 2018 Yiancar
4 * Copyright 2019 Clueboard
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "led_matrix.h"
30 led_config_t led_matrix_config;
33 #define MAX(X, Y) ((X) > (Y) ? (X) : (Y))
37 #define MIN(a,b) ((a) < (b)? (a): (b))
40 #ifndef LED_DISABLE_AFTER_TIMEOUT
41 #define LED_DISABLE_AFTER_TIMEOUT 0
44 #ifndef LED_DISABLE_WHEN_USB_SUSPENDED
45 #define LED_DISABLE_WHEN_USB_SUSPENDED false
48 #ifndef EECONFIG_LED_MATRIX
49 #define EECONFIG_LED_MATRIX EECONFIG_RGBLIGHT
52 #if !defined(LED_MATRIX_MAXIMUM_BRIGHTNESS) || LED_MATRIX_MAXIMUM_BRIGHTNESS > 255
53 #define LED_MATRIX_MAXIMUM_BRIGHTNESS 255
56 bool g_suspend_state = false;
58 // Last uniform brightness level.
59 uint8_t g_uniform_brightness = 0;
61 // Global tick at 20 Hz
64 // Ticks since this key was last hit.
65 uint8_t g_key_hit[LED_DRIVER_LED_COUNT];
67 // Ticks since any key was last hit.
68 uint32_t g_any_key_hit = 0;
70 uint32_t eeconfig_read_led_matrix(void) {
71 return eeprom_read_dword(EECONFIG_LED_MATRIX);
73 void eeconfig_update_led_matrix(uint32_t config_value) {
74 eeprom_update_dword(EECONFIG_LED_MATRIX, config_value);
76 void eeconfig_update_led_matrix_default(void) {
77 dprintf("eeconfig_update_led_matrix_default\n");
78 led_matrix_config.enable = 1;
79 led_matrix_config.mode = LED_MATRIX_UNIFORM_BRIGHTNESS;
80 led_matrix_config.val = 128;
81 led_matrix_config.speed = 0;
82 eeconfig_update_led_matrix(led_matrix_config.raw);
84 void eeconfig_debug_led_matrix(void) {
85 dprintf("led_matrix_config eeprom\n");
86 dprintf("led_matrix_config.enable = %d\n", led_matrix_config.enable);
87 dprintf("led_matrix_config.mode = %d\n", led_matrix_config.mode);
88 dprintf("led_matrix_config.val = %d\n", led_matrix_config.val);
89 dprintf("led_matrix_config.speed = %d\n", led_matrix_config.speed);
93 #define LED_HITS_TO_REMEMBER 8
94 uint8_t g_last_led_hit[LED_HITS_TO_REMEMBER] = {255};
95 uint8_t g_last_led_count = 0;
97 void map_row_column_to_led(uint8_t row, uint8_t column, uint8_t *led_i, uint8_t *led_count) {
101 for (uint8_t i = 0; i < LED_DRIVER_LED_COUNT; i++) {
102 // map_index_to_led(i, &led);
104 if (row == led.matrix_co.row && column == led.matrix_co.col) {
105 led_i[*led_count] = i;
111 void led_matrix_update_pwm_buffers(void) {
112 led_matrix_driver.flush();
115 void led_matrix_set_index_value(int index, uint8_t value) {
116 led_matrix_driver.set_value(index, value);
119 void led_matrix_set_index_value_all(uint8_t value) {
120 led_matrix_driver.set_value_all(value);
123 bool process_led_matrix(uint16_t keycode, keyrecord_t *record) {
124 /* FIXME: Why you comment out skully?
125 if (record->event.pressed) {
126 uint8_t led[8], led_count;
127 map_row_column_to_led(record->event.key.row, record->event.key.col, led, &led_count);
129 for (uint8_t i = LED_HITS_TO_REMEMBER; i > 1; i--) {
130 g_last_led_hit[i - 1] = g_last_led_hit[i - 2];
132 g_last_led_hit[0] = led[0];
133 g_last_led_count = MIN(LED_HITS_TO_REMEMBER, g_last_led_count + 1);
135 for(uint8_t i = 0; i < led_count; i++)
136 g_key_hit[led[i]] = 0;
139 #ifdef LED_MATRIX_KEYRELEASES
140 uint8_t led[8], led_count;
141 map_row_column_to_led(record->event.key.row, record->event.key.col, led, &led_count);
142 for(uint8_t i = 0; i < led_count; i++)
143 g_key_hit[led[i]] = 255;
152 void led_matrix_set_suspend_state(bool state) {
153 g_suspend_state = state;
157 void led_matrix_all_off(void) {
158 led_matrix_set_index_value_all(0);
161 // Uniform brightness
162 void led_matrix_uniform_brightness(void) {
163 uint8_t current_brightness = (LED_MATRIX_MAXIMUM_BRIGHTNESS / BACKLIGHT_LEVELS) * led_matrix_config.val;
164 if (current_brightness != g_uniform_brightness) {
165 g_uniform_brightness = current_brightness;
166 led_matrix_set_index_value_all(current_brightness);
170 void led_matrix_custom(void) {}
172 void led_matrix_task(void) {
173 if (!led_matrix_config.enable) {
174 led_matrix_all_off();
175 led_matrix_indicators();
181 if (g_any_key_hit < 0xFFFFFFFF) {
185 for (int led = 0; led < LED_DRIVER_LED_COUNT; led++) {
186 if (g_key_hit[led] < 255) {
187 if (g_key_hit[led] == 254)
188 g_last_led_count = MAX(g_last_led_count - 1, 0);
193 // Ideally we would also stop sending zeros to the LED driver PWM buffers
194 // while suspended and just do a software shutdown. This is a cheap hack for now.
195 bool suspend_backlight = ((g_suspend_state && LED_DISABLE_WHEN_USB_SUSPENDED) ||
196 (LED_DISABLE_AFTER_TIMEOUT > 0 && g_any_key_hit > LED_DISABLE_AFTER_TIMEOUT * 60 * 20));
197 uint8_t effect = suspend_backlight ? 0 : led_matrix_config.mode;
199 // this gets ticked at 20 Hz.
200 // each effect can opt to do calculations
201 // and/or request PWM buffer updates.
203 case LED_MATRIX_UNIFORM_BRIGHTNESS:
204 led_matrix_uniform_brightness();
211 if (!suspend_backlight) {
212 led_matrix_indicators();
215 // Tell the LED driver to update its state
216 led_matrix_driver.flush();
219 void led_matrix_indicators(void) {
220 led_matrix_indicators_kb();
221 led_matrix_indicators_user();
224 __attribute__((weak))
225 void led_matrix_indicators_kb(void) {}
227 __attribute__((weak))
228 void led_matrix_indicators_user(void) {}
231 // void led_matrix_set_indicator_index(uint8_t *index, uint8_t row, uint8_t column)
233 // if (row >= MATRIX_ROWS)
235 // // Special value, 255=none, 254=all
240 // // This needs updated to something like
241 // // uint8_t led[8], led_count;
242 // // map_row_column_to_led(row,column,led,&led_count);
243 // // for(uint8_t i = 0; i < led_count; i++)
244 // map_row_column_to_led(row, column, index);
248 void led_matrix_init(void) {
249 led_matrix_driver.init();
251 // Wait a second for the driver to finish initializing
254 // clear the key hits
255 for (int led=0; led<LED_DRIVER_LED_COUNT; led++) {
256 g_key_hit[led] = 255;
259 if (!eeconfig_is_enabled()) {
260 dprintf("led_matrix_init_drivers eeconfig is not enabled.\n");
262 eeconfig_update_led_matrix_default();
265 led_matrix_config.raw = eeconfig_read_led_matrix();
267 if (!led_matrix_config.mode) {
268 dprintf("led_matrix_init_drivers led_matrix_config.mode = 0. Write default values to EEPROM.\n");
269 eeconfig_update_led_matrix_default();
270 led_matrix_config.raw = eeconfig_read_led_matrix();
273 eeconfig_debug_led_matrix(); // display current eeprom values
276 // Deals with the messy details of incrementing an integer
277 static uint8_t increment(uint8_t value, uint8_t step, uint8_t min, uint8_t max) {
278 int16_t new_value = value;
280 return MIN(MAX( new_value, min), max );
283 static uint8_t decrement(uint8_t value, uint8_t step, uint8_t min, uint8_t max) {
284 int16_t new_value = value;
286 return MIN(MAX( new_value, min), max );
289 // void *backlight_get_custom_key_value_eeprom_address(uint8_t led) {
290 // // 3 bytes per value
291 // return EECONFIG_LED_MATRIX + (led * 3);
294 // void backlight_get_key_value(uint8_t led, uint8_t *value) {
295 // void *address = backlight_get_custom_key_value_eeprom_address(led);
296 // value = eeprom_read_byte(address);
299 // void backlight_set_key_value(uint8_t row, uint8_t column, uint8_t value) {
300 // uint8_t led[8], led_count;
301 // map_row_column_to_led(row,column,led,&led_count);
302 // for(uint8_t i = 0; i < led_count; i++) {
303 // if (led[i] < LED_DRIVER_LED_COUNT) {
304 // void *address = backlight_get_custom_key_value_eeprom_address(led[i]);
305 // eeprom_update_byte(address, value);
310 uint32_t led_matrix_get_tick(void) {
314 void led_matrix_toggle(void) {
315 led_matrix_config.enable ^= 1;
316 eeconfig_update_led_matrix(led_matrix_config.raw);
319 void led_matrix_enable(void) {
320 led_matrix_config.enable = 1;
321 eeconfig_update_led_matrix(led_matrix_config.raw);
324 void led_matrix_enable_noeeprom(void) {
325 led_matrix_config.enable = 1;
328 void led_matrix_disable(void) {
329 led_matrix_config.enable = 0;
330 eeconfig_update_led_matrix(led_matrix_config.raw);
333 void led_matrix_disable_noeeprom(void) {
334 led_matrix_config.enable = 0;
337 void led_matrix_step(void) {
338 led_matrix_config.mode++;
339 if (led_matrix_config.mode >= LED_MATRIX_EFFECT_MAX)
340 led_matrix_config.mode = 1;
341 eeconfig_update_led_matrix(led_matrix_config.raw);
344 void led_matrix_step_reverse(void) {
345 led_matrix_config.mode--;
346 if (led_matrix_config.mode < 1)
347 led_matrix_config.mode = LED_MATRIX_EFFECT_MAX - 1;
348 eeconfig_update_led_matrix(led_matrix_config.raw);
351 void led_matrix_increase_val(void) {
352 led_matrix_config.val = increment(led_matrix_config.val, 8, 0, LED_MATRIX_MAXIMUM_BRIGHTNESS);
353 eeconfig_update_led_matrix(led_matrix_config.raw);
356 void led_matrix_decrease_val(void) {
357 led_matrix_config.val = decrement(led_matrix_config.val, 8, 0, LED_MATRIX_MAXIMUM_BRIGHTNESS);
358 eeconfig_update_led_matrix(led_matrix_config.raw);
361 void led_matrix_increase_speed(void) {
362 led_matrix_config.speed = increment(led_matrix_config.speed, 1, 0, 3);
363 eeconfig_update_led_matrix(led_matrix_config.raw);//EECONFIG needs to be increased to support this
366 void led_matrix_decrease_speed(void) {
367 led_matrix_config.speed = decrement(led_matrix_config.speed, 1, 0, 3);
368 eeconfig_update_led_matrix(led_matrix_config.raw);//EECONFIG needs to be increased to support this
371 void led_matrix_mode(uint8_t mode, bool eeprom_write) {
372 led_matrix_config.mode = mode;
374 eeconfig_update_led_matrix(led_matrix_config.raw);
378 uint8_t led_matrix_get_mode(void) {
379 return led_matrix_config.mode;
382 void led_matrix_set_value_noeeprom(uint8_t val) {
383 led_matrix_config.val = val;
386 void led_matrix_set_value(uint8_t val) {
387 led_matrix_set_value_noeeprom(val);
388 eeconfig_update_led_matrix(led_matrix_config.raw);
391 void backlight_set(uint8_t val) {
392 led_matrix_set_value(val);