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.

348 lines
11 KiB

2020 November 28 Breaking Changes Update (#11053) * Branch point for 2020 November 28 Breaking Change * Remove matrix_col_t to allow MATRIX_ROWS > 32 (#10183) * Add support for soft serial to ATmega32U2 (#10204) * Change MIDI velocity implementation to allow direct control of velocity value (#9940) * Add ability to build a subset of all keyboards based on platform. * Actually use eeprom_driver_init(). * Make bootloader_jump weak for ChibiOS. (#10417) * Joystick 16-bit support (#10439) * Per-encoder resolutions (#10259) * Share button state from mousekey to pointing_device (#10179) * Add hotfix for chibios keyboards not wake (#10088) * Add advanced/efficient RGB Matrix Indicators (#8564) * Naming change. * Support for STM32 GPIOF,G,H,I,J,K (#10206) * Add milc as a dependency and remove the installed milc (#10563) * ChibiOS upgrade: early init conversions (#10214) * ChibiOS upgrade: configuration file migrator (#9952) * Haptic and solenoid cleanup (#9700) * XD75 cleanup (#10524) * OLED display update interval support (#10388) * Add definition based on currently-selected serial driver. (#10716) * New feature: Retro Tapping per key (#10622) * Allow for modification of output RGB values when using rgblight/rgb_matrix. (#10638) * Add housekeeping task callbacks so that keyboards/keymaps are capable of executing code for each main loop iteration. (#10530) * Rescale both ChibiOS and AVR backlighting. * Reduce Helix keyboard build variation (#8669) * Minor change to behavior allowing display updates to continue between task ticks (#10750) * Some GPIO manipulations in matrix.c change to atomic. (#10491) * qmk cformat (#10767) * [Keyboard] Update the Speedo firmware for v3.0 (#10657) * Maartenwut/Maarten namechange to evyd13/Evy (#10274) * [quantum] combine repeated lines of code (#10837) * Add step sequencer feature (#9703) * aeboards/ext65 refactor (#10820) * Refactor xelus/dawn60 for Rev2 later (#10584) * add DEBUG_MATRIX_SCAN_RATE_ENABLE to common_features.mk (#10824) * [Core] Added `add_oneshot_mods` & `del_oneshot_mods` (#10549) * update chibios os usb for the otg driver (#8893) * Remove HD44780 References, Part 4 (#10735) * [Keyboard] Add Valor FRL TKL (+refactor) (#10512) * Fix cursor position bug in oled_write_raw functions (#10800) * Fixup version.h writing when using SKIP_VERSION=yes (#10972) * Allow for certain code in the codebase assuming length of string. (#10974) * Add AT90USB support for serial.c (#10706) * Auto shift: support repeats and early registration (#9826) * Rename ledmatrix.h to match .c file (#7949) * Split RGB_MATRIX_ENABLE into _ENABLE and _DRIVER (#10231) * Split LED_MATRIX_ENABLE into _ENABLE and _DRIVER (#10840) * Merge point for 2020 Nov 28 Breaking Change
3 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 "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_eeconfig_t led_matrix_eeconfig;
  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) { 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_eeconfig.enable = 1;
  59. led_matrix_eeconfig.mode = LED_MATRIX_UNIFORM_BRIGHTNESS;
  60. led_matrix_eeconfig.val = 128;
  61. led_matrix_eeconfig.speed = 0;
  62. eeconfig_update_led_matrix(led_matrix_eeconfig.raw);
  63. }
  64. void eeconfig_debug_led_matrix(void) {
  65. dprintf("led_matrix_eeconfig eeprom\n");
  66. dprintf("led_matrix_eeconfig.enable = %d\n", led_matrix_eeconfig.enable);
  67. dprintf("led_matrix_eeconfig.mode = %d\n", led_matrix_eeconfig.mode);
  68. dprintf("led_matrix_eeconfig.val = %d\n", led_matrix_eeconfig.val);
  69. dprintf("led_matrix_eeconfig.speed = %d\n", led_matrix_eeconfig.speed);
  70. }
  71. uint8_t g_last_led_hit[LED_HITS_TO_REMEMBER] = {255};
  72. uint8_t g_last_led_count = 0;
  73. uint8_t map_row_column_to_led(uint8_t row, uint8_t column, uint8_t *led_i) {
  74. uint8_t led_count = 0;
  75. uint8_t led_index = g_led_config.matrix_co[row][column];
  76. if (led_index != NO_LED) {
  77. led_i[led_count] = led_index;
  78. led_count++;
  79. }
  80. return led_count;
  81. }
  82. void led_matrix_update_pwm_buffers(void) { led_matrix_driver.flush(); }
  83. void led_matrix_set_index_value(int index, uint8_t value) { led_matrix_driver.set_value(index, value); }
  84. void led_matrix_set_index_value_all(uint8_t value) { led_matrix_driver.set_value_all(value); }
  85. bool process_led_matrix(uint16_t keycode, keyrecord_t *record) {
  86. if (record->event.pressed) {
  87. uint8_t led[8];
  88. uint8_t led_count = map_row_column_to_led(record->event.key.row, record->event.key.col, led);
  89. if (led_count > 0) {
  90. for (uint8_t i = LED_HITS_TO_REMEMBER; i > 1; i--) {
  91. g_last_led_hit[i - 1] = g_last_led_hit[i - 2];
  92. }
  93. g_last_led_hit[0] = led[0];
  94. g_last_led_count = MIN(LED_HITS_TO_REMEMBER, g_last_led_count + 1);
  95. }
  96. for (uint8_t i = 0; i < led_count; i++) g_key_hit[led[i]] = 0;
  97. g_any_key_hit = 0;
  98. } else {
  99. #ifdef LED_MATRIX_KEYRELEASES
  100. uint8_t led[8];
  101. uint8_t led_count = map_row_column_to_led(record->event.key.row, record->event.key.col, led);
  102. for (uint8_t i = 0; i < led_count; i++) g_key_hit[led[i]] = 255;
  103. g_any_key_hit = 255;
  104. #endif
  105. }
  106. return true;
  107. }
  108. void led_matrix_set_suspend_state(bool state) { g_suspend_state = state; }
  109. // All LEDs off
  110. void led_matrix_all_off(void) { led_matrix_set_index_value_all(0); }
  111. // Uniform brightness
  112. void led_matrix_uniform_brightness(void) { led_matrix_set_index_value_all(LED_MATRIX_MAXIMUM_BRIGHTNESS / BACKLIGHT_LEVELS * led_matrix_eeconfig.val); }
  113. void led_matrix_custom(void) {}
  114. void led_matrix_task(void) {
  115. if (!led_matrix_eeconfig.enable) {
  116. led_matrix_all_off();
  117. led_matrix_indicators();
  118. return;
  119. }
  120. g_tick++;
  121. if (g_any_key_hit < 0xFFFFFFFF) {
  122. g_any_key_hit++;
  123. }
  124. for (int led = 0; led < DRIVER_LED_TOTAL; led++) {
  125. if (g_key_hit[led] < 255) {
  126. if (g_key_hit[led] == 254) g_last_led_count = MAX(g_last_led_count - 1, 0);
  127. g_key_hit[led]++;
  128. }
  129. }
  130. // Ideally we would also stop sending zeros to the LED driver PWM buffers
  131. // while suspended and just do a software shutdown. This is a cheap hack for now.
  132. 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));
  133. uint8_t effect = suspend_backlight ? 0 : led_matrix_eeconfig.mode;
  134. // this gets ticked at 20 Hz.
  135. // each effect can opt to do calculations
  136. // and/or request PWM buffer updates.
  137. switch (effect) {
  138. case LED_MATRIX_UNIFORM_BRIGHTNESS:
  139. led_matrix_uniform_brightness();
  140. break;
  141. default:
  142. led_matrix_custom();
  143. break;
  144. }
  145. if (!suspend_backlight) {
  146. led_matrix_indicators();
  147. }
  148. // Tell the LED driver to update its state
  149. led_matrix_driver.flush();
  150. }
  151. void led_matrix_indicators(void) {
  152. led_matrix_indicators_kb();
  153. led_matrix_indicators_user();
  154. }
  155. __attribute__((weak)) void led_matrix_indicators_kb(void) {}
  156. __attribute__((weak)) void led_matrix_indicators_user(void) {}
  157. // void led_matrix_set_indicator_index(uint8_t *index, uint8_t row, uint8_t column)
  158. // {
  159. // if (row >= MATRIX_ROWS)
  160. // {
  161. // // Special value, 255=none, 254=all
  162. // *index = row;
  163. // }
  164. // else
  165. // {
  166. // // This needs updated to something like
  167. // // uint8_t led[8];
  168. // // uint8_t led_count = map_row_column_to_led(row, column, led);
  169. // // for(uint8_t i = 0; i < led_count; i++)
  170. // map_row_column_to_led(row, column, index);
  171. // }
  172. // }
  173. void led_matrix_init(void) {
  174. led_matrix_driver.init();
  175. // Wait half a second for the driver to finish initializing
  176. wait_ms(500);
  177. // clear the key hits
  178. for (int led = 0; led < DRIVER_LED_TOTAL; led++) {
  179. g_key_hit[led] = 255;
  180. }
  181. if (!eeconfig_is_enabled()) {
  182. dprintf("led_matrix_init_drivers eeconfig is not enabled.\n");
  183. eeconfig_init();
  184. eeconfig_update_led_matrix_default();
  185. }
  186. led_matrix_eeconfig.raw = eeconfig_read_led_matrix();
  187. if (!led_matrix_eeconfig.mode) {
  188. dprintf("led_matrix_init_drivers led_matrix_eeconfig.mode = 0. Write default values to EEPROM.\n");
  189. eeconfig_update_led_matrix_default();
  190. led_matrix_eeconfig.raw = eeconfig_read_led_matrix();
  191. }
  192. eeconfig_debug_led_matrix(); // display current eeprom values
  193. }
  194. // Deals with the messy details of incrementing an integer
  195. static uint8_t increment(uint8_t value, uint8_t step, uint8_t min, uint8_t max) {
  196. int16_t new_value = value;
  197. new_value += step;
  198. return MIN(MAX(new_value, min), max);
  199. }
  200. static uint8_t decrement(uint8_t value, uint8_t step, uint8_t min, uint8_t max) {
  201. int16_t new_value = value;
  202. new_value -= step;
  203. return MIN(MAX(new_value, min), max);
  204. }
  205. // void *backlight_get_custom_key_value_eeprom_address(uint8_t led) {
  206. // // 3 bytes per value
  207. // return EECONFIG_LED_MATRIX + (led * 3);
  208. // }
  209. // void backlight_get_key_value(uint8_t led, uint8_t *value) {
  210. // void *address = backlight_get_custom_key_value_eeprom_address(led);
  211. // value = eeprom_read_byte(address);
  212. // }
  213. // void backlight_set_key_value(uint8_t row, uint8_t column, uint8_t value) {
  214. // uint8_t led[8];
  215. // uint8_t led_count = map_row_column_to_led(row, column, led);
  216. // for(uint8_t i = 0; i < led_count; i++) {
  217. // if (led[i] < DRIVER_LED_TOTAL) {
  218. // void *address = backlight_get_custom_key_value_eeprom_address(led[i]);
  219. // eeprom_update_byte(address, value);
  220. // }
  221. // }
  222. // }
  223. uint32_t led_matrix_get_tick(void) { return g_tick; }
  224. void led_matrix_toggle(void) {
  225. led_matrix_eeconfig.enable ^= 1;
  226. eeconfig_update_led_matrix(led_matrix_eeconfig.raw);
  227. }
  228. void led_matrix_enable(void) {
  229. led_matrix_eeconfig.enable = 1;
  230. eeconfig_update_led_matrix(led_matrix_eeconfig.raw);
  231. }
  232. void led_matrix_enable_noeeprom(void) { led_matrix_eeconfig.enable = 1; }
  233. void led_matrix_disable(void) {
  234. led_matrix_eeconfig.enable = 0;
  235. eeconfig_update_led_matrix(led_matrix_eeconfig.raw);
  236. }
  237. void led_matrix_disable_noeeprom(void) { led_matrix_eeconfig.enable = 0; }
  238. void led_matrix_step(void) {
  239. led_matrix_eeconfig.mode++;
  240. if (led_matrix_eeconfig.mode >= LED_MATRIX_EFFECT_MAX) {
  241. led_matrix_eeconfig.mode = 1;
  242. }
  243. eeconfig_update_led_matrix(led_matrix_eeconfig.raw);
  244. }
  245. void led_matrix_step_reverse(void) {
  246. led_matrix_eeconfig.mode--;
  247. if (led_matrix_eeconfig.mode < 1) {
  248. led_matrix_eeconfig.mode = LED_MATRIX_EFFECT_MAX - 1;
  249. }
  250. eeconfig_update_led_matrix(led_matrix_eeconfig.raw);
  251. }
  252. void led_matrix_increase_val(void) {
  253. led_matrix_eeconfig.val = increment(led_matrix_eeconfig.val, 8, 0, LED_MATRIX_MAXIMUM_BRIGHTNESS);
  254. eeconfig_update_led_matrix(led_matrix_eeconfig.raw);
  255. }
  256. void led_matrix_decrease_val(void) {
  257. led_matrix_eeconfig.val = decrement(led_matrix_eeconfig.val, 8, 0, LED_MATRIX_MAXIMUM_BRIGHTNESS);
  258. eeconfig_update_led_matrix(led_matrix_eeconfig.raw);
  259. }
  260. void led_matrix_increase_speed(void) {
  261. led_matrix_eeconfig.speed = increment(led_matrix_eeconfig.speed, 1, 0, 3);
  262. eeconfig_update_led_matrix(led_matrix_eeconfig.raw); // EECONFIG needs to be increased to support this
  263. }
  264. void led_matrix_decrease_speed(void) {
  265. led_matrix_eeconfig.speed = decrement(led_matrix_eeconfig.speed, 1, 0, 3);
  266. eeconfig_update_led_matrix(led_matrix_eeconfig.raw); // EECONFIG needs to be increased to support this
  267. }
  268. void led_matrix_mode(uint8_t mode, bool eeprom_write) {
  269. led_matrix_eeconfig.mode = mode;
  270. if (eeprom_write) {
  271. eeconfig_update_led_matrix(led_matrix_eeconfig.raw);
  272. }
  273. }
  274. uint8_t led_matrix_get_mode(void) { return led_matrix_eeconfig.mode; }
  275. void led_matrix_set_value_noeeprom(uint8_t val) { led_matrix_eeconfig.val = val; }
  276. void led_matrix_set_value(uint8_t val) {
  277. led_matrix_set_value_noeeprom(val);
  278. eeconfig_update_led_matrix(led_matrix_eeconfig.raw);
  279. }
  280. void backlight_set(uint8_t val) { led_matrix_set_value(val); }