led_matrix.c 11 KB

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