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.

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