You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

391 lines
11 KiB

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