led_matrix.c 11 KB

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