led_matrix.c 11 KB

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