led_matrix.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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 eprom\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. // delay 1 second before driving LEDs or doing anything else
  152. // FIXME: Can't we use wait_ms() here?
  153. static uint8_t startup_tick = 0;
  154. if (startup_tick < 20) {
  155. startup_tick++;
  156. return;
  157. }
  158. g_tick++;
  159. if (g_any_key_hit < 0xFFFFFFFF) {
  160. g_any_key_hit++;
  161. }
  162. /* FIXME: WHY YOU COMMENT OUT?!
  163. for (int led = 0; led < LED_DRIVER_LED_COUNT; led++) {
  164. if (g_key_hit[led] < 255) {
  165. if (g_key_hit[led] == 254)
  166. g_last_led_count = MAX(g_last_led_count - 1, 0);
  167. g_key_hit[led]++;
  168. }
  169. }
  170. // Factory default magic value
  171. if (led_matrix_config.mode == 255) {
  172. led_matrix_uniform_brightness();
  173. return;
  174. }
  175. */
  176. // Ideally we would also stop sending zeros to the LED driver PWM buffers
  177. // while suspended and just do a software shutdown. This is a cheap hack for now.
  178. bool suspend_backlight = ((g_suspend_state && LED_DISABLE_WHEN_USB_SUSPENDED) ||
  179. (LED_DISABLE_AFTER_TIMEOUT > 0 && g_any_key_hit > LED_DISABLE_AFTER_TIMEOUT * 60 * 20));
  180. uint8_t effect = suspend_backlight ? 0 : led_matrix_config.mode;
  181. // this gets ticked at 20 Hz.
  182. // each effect can opt to do calculations
  183. // and/or request PWM buffer updates.
  184. switch (effect) {
  185. case LED_MATRIX_UNIFORM_BRIGHTNESS:
  186. led_matrix_uniform_brightness();
  187. break;
  188. default:
  189. led_matrix_custom();
  190. break;
  191. }
  192. if (!suspend_backlight) {
  193. led_matrix_indicators();
  194. }
  195. // Tell the LED driver to update its state
  196. led_matrix_driver.flush();
  197. }
  198. void led_matrix_indicators(void) {
  199. led_matrix_indicators_kb();
  200. led_matrix_indicators_user();
  201. }
  202. __attribute__((weak))
  203. void led_matrix_indicators_kb(void) {}
  204. __attribute__((weak))
  205. void led_matrix_indicators_user(void) {}
  206. // void led_matrix_set_indicator_index(uint8_t *index, uint8_t row, uint8_t column)
  207. // {
  208. // if (row >= MATRIX_ROWS)
  209. // {
  210. // // Special value, 255=none, 254=all
  211. // *index = row;
  212. // }
  213. // else
  214. // {
  215. // // This needs updated to something like
  216. // // uint8_t led[8], led_count;
  217. // // map_row_column_to_led(row,column,led,&led_count);
  218. // // for(uint8_t i = 0; i < led_count; i++)
  219. // map_row_column_to_led(row, column, index);
  220. // }
  221. // }
  222. void led_matrix_init(void) {
  223. led_matrix_driver.init();
  224. // TODO: put the 1 second startup delay here?
  225. // clear the key hits
  226. for (int led=0; led<LED_DRIVER_LED_COUNT; led++) {
  227. g_key_hit[led] = 255;
  228. }
  229. if (!eeconfig_is_enabled()) {
  230. dprintf("led_matrix_init_drivers eeconfig is not enabled.\n");
  231. eeconfig_init();
  232. eeconfig_update_led_matrix_default();
  233. }
  234. led_matrix_config.raw = eeconfig_read_led_matrix();
  235. if (!led_matrix_config.mode) {
  236. dprintf("led_matrix_init_drivers led_matrix_config.mode = 0. Write default values to EEPROM.\n");
  237. eeconfig_update_led_matrix_default();
  238. led_matrix_config.raw = eeconfig_read_led_matrix();
  239. }
  240. eeconfig_debug_led_matrix(); // display current eeprom values
  241. }
  242. // Deals with the messy details of incrementing an integer
  243. static uint8_t increment(uint8_t value, uint8_t step, uint8_t min, uint8_t max) {
  244. int16_t new_value = value;
  245. new_value += step;
  246. return MIN(MAX( new_value, min), max );
  247. }
  248. static uint8_t decrement(uint8_t value, uint8_t step, uint8_t min, uint8_t max) {
  249. int16_t new_value = value;
  250. new_value -= step;
  251. return MIN(MAX( new_value, min), max );
  252. }
  253. // void *backlight_get_custom_key_value_eeprom_address(uint8_t led) {
  254. // // 3 bytes per value
  255. // return EECONFIG_LED_MATRIX + (led * 3);
  256. // }
  257. // void backlight_get_key_value(uint8_t led, uint8_t *value) {
  258. // void *address = backlight_get_custom_key_value_eeprom_address(led);
  259. // value = eeprom_read_byte(address);
  260. // }
  261. // void backlight_set_key_value(uint8_t row, uint8_t column, uint8_t value) {
  262. // uint8_t led[8], led_count;
  263. // map_row_column_to_led(row,column,led,&led_count);
  264. // for(uint8_t i = 0; i < led_count; i++) {
  265. // if (led[i] < LED_DRIVER_LED_COUNT) {
  266. // void *address = backlight_get_custom_key_value_eeprom_address(led[i]);
  267. // eeprom_update_byte(address, value);
  268. // }
  269. // }
  270. // }
  271. uint32_t led_matrix_get_tick(void) {
  272. return g_tick;
  273. }
  274. void led_matrix_toggle(void) {
  275. led_matrix_config.enable ^= 1;
  276. eeconfig_update_led_matrix(led_matrix_config.raw);
  277. }
  278. void led_matrix_enable(void) {
  279. led_matrix_config.enable = 1;
  280. eeconfig_update_led_matrix(led_matrix_config.raw);
  281. }
  282. void led_matrix_enable_noeeprom(void) {
  283. led_matrix_config.enable = 1;
  284. }
  285. void led_matrix_disable(void) {
  286. led_matrix_config.enable = 0;
  287. eeconfig_update_led_matrix(led_matrix_config.raw);
  288. }
  289. void led_matrix_disable_noeeprom(void) {
  290. led_matrix_config.enable = 0;
  291. }
  292. void led_matrix_step(void) {
  293. led_matrix_config.mode++;
  294. if (led_matrix_config.mode >= LED_MATRIX_EFFECT_MAX)
  295. led_matrix_config.mode = 1;
  296. eeconfig_update_led_matrix(led_matrix_config.raw);
  297. }
  298. void led_matrix_step_reverse(void) {
  299. led_matrix_config.mode--;
  300. if (led_matrix_config.mode < 1)
  301. led_matrix_config.mode = LED_MATRIX_EFFECT_MAX - 1;
  302. eeconfig_update_led_matrix(led_matrix_config.raw);
  303. }
  304. void led_matrix_increase_val(void) {
  305. led_matrix_config.val = increment(led_matrix_config.val, 8, 0, LED_MATRIX_MAXIMUM_BRIGHTNESS);
  306. eeconfig_update_led_matrix(led_matrix_config.raw);
  307. }
  308. void led_matrix_decrease_val(void) {
  309. led_matrix_config.val = decrement(led_matrix_config.val, 8, 0, LED_MATRIX_MAXIMUM_BRIGHTNESS);
  310. eeconfig_update_led_matrix(led_matrix_config.raw);
  311. }
  312. void led_matrix_increase_speed(void) {
  313. led_matrix_config.speed = increment(led_matrix_config.speed, 1, 0, 3);
  314. eeconfig_update_led_matrix(led_matrix_config.raw);//EECONFIG needs to be increased to support this
  315. }
  316. void led_matrix_decrease_speed(void) {
  317. led_matrix_config.speed = decrement(led_matrix_config.speed, 1, 0, 3);
  318. eeconfig_update_led_matrix(led_matrix_config.raw);//EECONFIG needs to be increased to support this
  319. }
  320. void led_matrix_mode(uint8_t mode, bool eeprom_write) {
  321. led_matrix_config.mode = mode;
  322. if (eeprom_write) {
  323. eeconfig_update_led_matrix(led_matrix_config.raw);
  324. }
  325. }
  326. uint8_t led_matrix_get_mode(void) {
  327. return led_matrix_config.mode;
  328. }
  329. void led_matrix_set_value_noeeprom(uint8_t val) {
  330. led_matrix_config.val = val;
  331. }
  332. void led_matrix_set_value(uint8_t val) {
  333. led_matrix_set_value_noeeprom(val);
  334. eeconfig_update_led_matrix(led_matrix_config.raw);
  335. }
  336. void backlight_set(uint8_t val) {
  337. led_matrix_set_value(val);
  338. }