led_matrix.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /* Copyright 2017 Jason Williams
  2. * Copyright 2017 Jack Humbert
  3. * Copyright 2018 Yiancar
  4. * Copyright 2019 Clueboard
  5. *
  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.
  10. *
  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.
  15. *
  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/>.
  18. */
  19. #include <stdint.h>
  20. #include <stdbool.h>
  21. #include "quantum.h"
  22. #include "led_matrix.h"
  23. #include "progmem.h"
  24. #include "config.h"
  25. #include "eeprom.h"
  26. #include <string.h>
  27. #include <math.h>
  28. led_eeconfig_t led_matrix_eeconfig;
  29. #ifndef MAX
  30. # define MAX(X, Y) ((X) > (Y) ? (X) : (Y))
  31. #endif
  32. #ifndef MIN
  33. # define MIN(a, b) ((a) < (b) ? (a) : (b))
  34. #endif
  35. #ifndef LED_DISABLE_AFTER_TIMEOUT
  36. # define LED_DISABLE_AFTER_TIMEOUT 0
  37. #endif
  38. #ifndef LED_DISABLE_WHEN_USB_SUSPENDED
  39. # define LED_DISABLE_WHEN_USB_SUSPENDED false
  40. #endif
  41. #ifndef EECONFIG_LED_MATRIX
  42. # define EECONFIG_LED_MATRIX EECONFIG_RGBLIGHT
  43. #endif
  44. #if !defined(LED_MATRIX_MAXIMUM_BRIGHTNESS) || LED_MATRIX_MAXIMUM_BRIGHTNESS > 255
  45. # define LED_MATRIX_MAXIMUM_BRIGHTNESS 255
  46. #endif
  47. bool g_suspend_state = false;
  48. // Global tick at 20 Hz
  49. uint32_t g_tick = 0;
  50. // Ticks since this key was last hit.
  51. uint8_t g_key_hit[DRIVER_LED_TOTAL];
  52. // Ticks since any key was last hit.
  53. uint32_t g_any_key_hit = 0;
  54. uint32_t eeconfig_read_led_matrix(void) { return eeprom_read_dword(EECONFIG_LED_MATRIX); }
  55. void eeconfig_update_led_matrix(uint32_t config_value) { eeprom_update_dword(EECONFIG_LED_MATRIX, config_value); }
  56. void eeconfig_update_led_matrix_default(void) {
  57. dprintf("eeconfig_update_led_matrix_default\n");
  58. led_matrix_eeconfig.enable = 1;
  59. led_matrix_eeconfig.mode = LED_MATRIX_UNIFORM_BRIGHTNESS;
  60. led_matrix_eeconfig.val = 128;
  61. led_matrix_eeconfig.speed = 0;
  62. eeconfig_update_led_matrix(led_matrix_eeconfig.raw);
  63. }
  64. void eeconfig_debug_led_matrix(void) {
  65. dprintf("led_matrix_eeconfig eeprom\n");
  66. dprintf("led_matrix_eeconfig.enable = %d\n", led_matrix_eeconfig.enable);
  67. dprintf("led_matrix_eeconfig.mode = %d\n", led_matrix_eeconfig.mode);
  68. dprintf("led_matrix_eeconfig.val = %d\n", led_matrix_eeconfig.val);
  69. dprintf("led_matrix_eeconfig.speed = %d\n", led_matrix_eeconfig.speed);
  70. }
  71. uint8_t g_last_led_hit[LED_HITS_TO_REMEMBER] = {255};
  72. uint8_t g_last_led_count = 0;
  73. uint8_t map_row_column_to_led(uint8_t row, uint8_t column, uint8_t *led_i) {
  74. uint8_t led_count = 0;
  75. uint8_t led_index = g_led_config.matrix_co[row][column];
  76. if (led_index != NO_LED) {
  77. led_i[led_count] = led_index;
  78. led_count++;
  79. }
  80. return led_count;
  81. }
  82. void led_matrix_update_pwm_buffers(void) { led_matrix_driver.flush(); }
  83. void led_matrix_set_index_value(int index, uint8_t value) { led_matrix_driver.set_value(index, value); }
  84. void led_matrix_set_index_value_all(uint8_t value) { led_matrix_driver.set_value_all(value); }
  85. bool process_led_matrix(uint16_t keycode, keyrecord_t *record) {
  86. if (record->event.pressed) {
  87. uint8_t led[8];
  88. uint8_t led_count = map_row_column_to_led(record->event.key.row, record->event.key.col, led);
  89. if (led_count > 0) {
  90. for (uint8_t i = LED_HITS_TO_REMEMBER; i > 1; i--) {
  91. g_last_led_hit[i - 1] = g_last_led_hit[i - 2];
  92. }
  93. g_last_led_hit[0] = led[0];
  94. g_last_led_count = MIN(LED_HITS_TO_REMEMBER, g_last_led_count + 1);
  95. }
  96. for (uint8_t i = 0; i < led_count; i++) g_key_hit[led[i]] = 0;
  97. g_any_key_hit = 0;
  98. } else {
  99. #ifdef LED_MATRIX_KEYRELEASES
  100. uint8_t led[8];
  101. uint8_t led_count = map_row_column_to_led(record->event.key.row, record->event.key.col, led);
  102. for (uint8_t i = 0; i < led_count; i++) g_key_hit[led[i]] = 255;
  103. g_any_key_hit = 255;
  104. #endif
  105. }
  106. return true;
  107. }
  108. void led_matrix_set_suspend_state(bool state) { g_suspend_state = state; }
  109. // All LEDs off
  110. void led_matrix_all_off(void) { led_matrix_set_index_value_all(0); }
  111. // Uniform brightness
  112. void led_matrix_uniform_brightness(void) { led_matrix_set_index_value_all(LED_MATRIX_MAXIMUM_BRIGHTNESS / BACKLIGHT_LEVELS * led_matrix_eeconfig.val); }
  113. void led_matrix_custom(void) {}
  114. void led_matrix_task(void) {
  115. if (!led_matrix_eeconfig.enable) {
  116. led_matrix_all_off();
  117. led_matrix_indicators();
  118. return;
  119. }
  120. g_tick++;
  121. if (g_any_key_hit < 0xFFFFFFFF) {
  122. g_any_key_hit++;
  123. }
  124. for (int led = 0; led < DRIVER_LED_TOTAL; led++) {
  125. if (g_key_hit[led] < 255) {
  126. if (g_key_hit[led] == 254) g_last_led_count = MAX(g_last_led_count - 1, 0);
  127. g_key_hit[led]++;
  128. }
  129. }
  130. // Ideally we would also stop sending zeros to the LED driver PWM buffers
  131. // while suspended and just do a software shutdown. This is a cheap hack for now.
  132. 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));
  133. uint8_t effect = suspend_backlight ? 0 : led_matrix_eeconfig.mode;
  134. // this gets ticked at 20 Hz.
  135. // each effect can opt to do calculations
  136. // and/or request PWM buffer updates.
  137. switch (effect) {
  138. case LED_MATRIX_UNIFORM_BRIGHTNESS:
  139. led_matrix_uniform_brightness();
  140. break;
  141. default:
  142. led_matrix_custom();
  143. break;
  144. }
  145. if (!suspend_backlight) {
  146. led_matrix_indicators();
  147. }
  148. // Tell the LED driver to update its state
  149. led_matrix_driver.flush();
  150. }
  151. void led_matrix_indicators(void) {
  152. led_matrix_indicators_kb();
  153. led_matrix_indicators_user();
  154. }
  155. __attribute__((weak)) void led_matrix_indicators_kb(void) {}
  156. __attribute__((weak)) void led_matrix_indicators_user(void) {}
  157. // void led_matrix_set_indicator_index(uint8_t *index, uint8_t row, uint8_t column)
  158. // {
  159. // if (row >= MATRIX_ROWS)
  160. // {
  161. // // Special value, 255=none, 254=all
  162. // *index = row;
  163. // }
  164. // else
  165. // {
  166. // // This needs updated to something like
  167. // // uint8_t led[8];
  168. // // uint8_t led_count = map_row_column_to_led(row, column, led);
  169. // // for(uint8_t i = 0; i < led_count; i++)
  170. // map_row_column_to_led(row, column, index);
  171. // }
  172. // }
  173. void led_matrix_init(void) {
  174. led_matrix_driver.init();
  175. // Wait half a second for the driver to finish initializing
  176. wait_ms(500);
  177. // clear the key hits
  178. for (int led = 0; led < DRIVER_LED_TOTAL; led++) {
  179. g_key_hit[led] = 255;
  180. }
  181. if (!eeconfig_is_enabled()) {
  182. dprintf("led_matrix_init_drivers eeconfig is not enabled.\n");
  183. eeconfig_init();
  184. eeconfig_update_led_matrix_default();
  185. }
  186. led_matrix_eeconfig.raw = eeconfig_read_led_matrix();
  187. if (!led_matrix_eeconfig.mode) {
  188. dprintf("led_matrix_init_drivers led_matrix_eeconfig.mode = 0. Write default values to EEPROM.\n");
  189. eeconfig_update_led_matrix_default();
  190. led_matrix_eeconfig.raw = eeconfig_read_led_matrix();
  191. }
  192. eeconfig_debug_led_matrix(); // display current eeprom values
  193. }
  194. // Deals with the messy details of incrementing an integer
  195. static uint8_t increment(uint8_t value, uint8_t step, uint8_t min, uint8_t max) {
  196. int16_t new_value = value;
  197. new_value += step;
  198. return MIN(MAX(new_value, min), max);
  199. }
  200. static uint8_t decrement(uint8_t value, uint8_t step, uint8_t min, uint8_t max) {
  201. int16_t new_value = value;
  202. new_value -= step;
  203. return MIN(MAX(new_value, min), max);
  204. }
  205. // void *backlight_get_custom_key_value_eeprom_address(uint8_t led) {
  206. // // 3 bytes per value
  207. // return EECONFIG_LED_MATRIX + (led * 3);
  208. // }
  209. // void backlight_get_key_value(uint8_t led, uint8_t *value) {
  210. // void *address = backlight_get_custom_key_value_eeprom_address(led);
  211. // value = eeprom_read_byte(address);
  212. // }
  213. // void backlight_set_key_value(uint8_t row, uint8_t column, uint8_t value) {
  214. // uint8_t led[8];
  215. // uint8_t led_count = map_row_column_to_led(row, column, led);
  216. // for(uint8_t i = 0; i < led_count; i++) {
  217. // if (led[i] < DRIVER_LED_TOTAL) {
  218. // void *address = backlight_get_custom_key_value_eeprom_address(led[i]);
  219. // eeprom_update_byte(address, value);
  220. // }
  221. // }
  222. // }
  223. uint32_t led_matrix_get_tick(void) { return g_tick; }
  224. void led_matrix_toggle(void) {
  225. led_matrix_eeconfig.enable ^= 1;
  226. eeconfig_update_led_matrix(led_matrix_eeconfig.raw);
  227. }
  228. void led_matrix_enable(void) {
  229. led_matrix_eeconfig.enable = 1;
  230. eeconfig_update_led_matrix(led_matrix_eeconfig.raw);
  231. }
  232. void led_matrix_enable_noeeprom(void) { led_matrix_eeconfig.enable = 1; }
  233. void led_matrix_disable(void) {
  234. led_matrix_eeconfig.enable = 0;
  235. eeconfig_update_led_matrix(led_matrix_eeconfig.raw);
  236. }
  237. void led_matrix_disable_noeeprom(void) { led_matrix_eeconfig.enable = 0; }
  238. void led_matrix_step(void) {
  239. led_matrix_eeconfig.mode++;
  240. if (led_matrix_eeconfig.mode >= LED_MATRIX_EFFECT_MAX) {
  241. led_matrix_eeconfig.mode = 1;
  242. }
  243. eeconfig_update_led_matrix(led_matrix_eeconfig.raw);
  244. }
  245. void led_matrix_step_reverse(void) {
  246. led_matrix_eeconfig.mode--;
  247. if (led_matrix_eeconfig.mode < 1) {
  248. led_matrix_eeconfig.mode = LED_MATRIX_EFFECT_MAX - 1;
  249. }
  250. eeconfig_update_led_matrix(led_matrix_eeconfig.raw);
  251. }
  252. void led_matrix_increase_val(void) {
  253. led_matrix_eeconfig.val = increment(led_matrix_eeconfig.val, 8, 0, LED_MATRIX_MAXIMUM_BRIGHTNESS);
  254. eeconfig_update_led_matrix(led_matrix_eeconfig.raw);
  255. }
  256. void led_matrix_decrease_val(void) {
  257. led_matrix_eeconfig.val = decrement(led_matrix_eeconfig.val, 8, 0, LED_MATRIX_MAXIMUM_BRIGHTNESS);
  258. eeconfig_update_led_matrix(led_matrix_eeconfig.raw);
  259. }
  260. void led_matrix_increase_speed(void) {
  261. led_matrix_eeconfig.speed = increment(led_matrix_eeconfig.speed, 1, 0, 3);
  262. eeconfig_update_led_matrix(led_matrix_eeconfig.raw); // EECONFIG needs to be increased to support this
  263. }
  264. void led_matrix_decrease_speed(void) {
  265. led_matrix_eeconfig.speed = decrement(led_matrix_eeconfig.speed, 1, 0, 3);
  266. eeconfig_update_led_matrix(led_matrix_eeconfig.raw); // EECONFIG needs to be increased to support this
  267. }
  268. void led_matrix_mode(uint8_t mode, bool eeprom_write) {
  269. led_matrix_eeconfig.mode = mode;
  270. if (eeprom_write) {
  271. eeconfig_update_led_matrix(led_matrix_eeconfig.raw);
  272. }
  273. }
  274. uint8_t led_matrix_get_mode(void) { return led_matrix_eeconfig.mode; }
  275. void led_matrix_set_value_noeeprom(uint8_t val) { led_matrix_eeconfig.val = val; }
  276. void led_matrix_set_value(uint8_t val) {
  277. led_matrix_set_value_noeeprom(val);
  278. eeconfig_update_led_matrix(led_matrix_eeconfig.raw);
  279. }
  280. void backlight_set(uint8_t val) { led_matrix_set_value(val); }