led_matrix.c 12 KB

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