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.

527 lines
17 KiB

  1. /* Copyright 2017 Jason Williams
  2. * Copyright 2017 Jack Humbert
  3. * Copyright 2018 Yiancar
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "rgb_matrix.h"
  19. #include "progmem.h"
  20. #include "config.h"
  21. #include "eeprom.h"
  22. #include <string.h>
  23. #include <math.h>
  24. #include "lib/lib8tion/lib8tion.h"
  25. #ifndef RGB_MATRIX_CENTER
  26. const point_t k_rgb_matrix_center = {112, 32};
  27. #else
  28. const point_t k_rgb_matrix_center = RGB_MATRIX_CENTER;
  29. #endif
  30. // Generic effect runners
  31. #include "rgb_matrix_runners/effect_runner_dx_dy_dist.h"
  32. #include "rgb_matrix_runners/effect_runner_dx_dy.h"
  33. #include "rgb_matrix_runners/effect_runner_i.h"
  34. #include "rgb_matrix_runners/effect_runner_sin_cos_i.h"
  35. #include "rgb_matrix_runners/effect_runner_reactive.h"
  36. #include "rgb_matrix_runners/effect_runner_reactive_splash.h"
  37. // ------------------------------------------
  38. // -----Begin rgb effect includes macros-----
  39. #define RGB_MATRIX_EFFECT(name)
  40. #define RGB_MATRIX_CUSTOM_EFFECT_IMPLS
  41. #include "rgb_matrix_animations/rgb_matrix_effects.inc"
  42. #ifdef RGB_MATRIX_CUSTOM_KB
  43. # include "rgb_matrix_kb.inc"
  44. #endif
  45. #ifdef RGB_MATRIX_CUSTOM_USER
  46. # include "rgb_matrix_user.inc"
  47. #endif
  48. #undef RGB_MATRIX_CUSTOM_EFFECT_IMPLS
  49. #undef RGB_MATRIX_EFFECT
  50. // -----End rgb effect includes macros-------
  51. // ------------------------------------------
  52. #ifndef RGB_DISABLE_AFTER_TIMEOUT
  53. # define RGB_DISABLE_AFTER_TIMEOUT 0
  54. #endif
  55. #ifndef RGB_DISABLE_WHEN_USB_SUSPENDED
  56. # define RGB_DISABLE_WHEN_USB_SUSPENDED false
  57. #endif
  58. #if !defined(RGB_MATRIX_MAXIMUM_BRIGHTNESS) || RGB_MATRIX_MAXIMUM_BRIGHTNESS > UINT8_MAX
  59. # undef RGB_MATRIX_MAXIMUM_BRIGHTNESS
  60. # define RGB_MATRIX_MAXIMUM_BRIGHTNESS UINT8_MAX
  61. #endif
  62. #if !defined(RGB_MATRIX_HUE_STEP)
  63. # define RGB_MATRIX_HUE_STEP 8
  64. #endif
  65. #if !defined(RGB_MATRIX_SAT_STEP)
  66. # define RGB_MATRIX_SAT_STEP 16
  67. #endif
  68. #if !defined(RGB_MATRIX_VAL_STEP)
  69. # define RGB_MATRIX_VAL_STEP 16
  70. #endif
  71. #if !defined(RGB_MATRIX_SPD_STEP)
  72. # define RGB_MATRIX_SPD_STEP 16
  73. #endif
  74. #if !defined(RGB_MATRIX_STARTUP_MODE)
  75. # ifndef DISABLE_RGB_MATRIX_CYCLE_LEFT_RIGHT
  76. # define RGB_MATRIX_STARTUP_MODE RGB_MATRIX_CYCLE_LEFT_RIGHT
  77. # else
  78. // fallback to solid colors if RGB_MATRIX_CYCLE_LEFT_RIGHT is disabled in userspace
  79. # define RGB_MATRIX_STARTUP_MODE RGB_MATRIX_SOLID_COLOR
  80. # endif
  81. #endif
  82. bool g_suspend_state = false;
  83. rgb_config_t rgb_matrix_config;
  84. rgb_counters_t g_rgb_counters;
  85. static uint32_t rgb_counters_buffer;
  86. #ifdef RGB_MATRIX_FRAMEBUFFER_EFFECTS
  87. uint8_t rgb_frame_buffer[MATRIX_ROWS][MATRIX_COLS] = {{0}};
  88. #endif
  89. #ifdef RGB_MATRIX_KEYREACTIVE_ENABLED
  90. last_hit_t g_last_hit_tracker;
  91. static last_hit_t last_hit_buffer;
  92. #endif // RGB_MATRIX_KEYREACTIVE_ENABLED
  93. void eeconfig_read_rgb_matrix(void) { eeprom_read_block(&rgb_matrix_config, EECONFIG_RGB_MATRIX, sizeof(rgb_matrix_config)); }
  94. void eeconfig_update_rgb_matrix(void) { eeprom_update_block(&rgb_matrix_config, EECONFIG_RGB_MATRIX, sizeof(rgb_matrix_config)); }
  95. void eeconfig_update_rgb_matrix_default(void) {
  96. dprintf("eeconfig_update_rgb_matrix_default\n");
  97. rgb_matrix_config.enable = 1;
  98. rgb_matrix_config.mode = RGB_MATRIX_STARTUP_MODE;
  99. rgb_matrix_config.hsv = (HSV){0, UINT8_MAX, RGB_MATRIX_MAXIMUM_BRIGHTNESS};
  100. rgb_matrix_config.speed = UINT8_MAX / 2;
  101. eeconfig_update_rgb_matrix();
  102. }
  103. void eeconfig_debug_rgb_matrix(void) {
  104. dprintf("rgb_matrix_config eprom\n");
  105. dprintf("rgb_matrix_config.enable = %d\n", rgb_matrix_config.enable);
  106. dprintf("rgb_matrix_config.mode = %d\n", rgb_matrix_config.mode);
  107. dprintf("rgb_matrix_config.hsv.h = %d\n", rgb_matrix_config.hsv.h);
  108. dprintf("rgb_matrix_config.hsv.s = %d\n", rgb_matrix_config.hsv.s);
  109. dprintf("rgb_matrix_config.hsv.v = %d\n", rgb_matrix_config.hsv.v);
  110. dprintf("rgb_matrix_config.speed = %d\n", rgb_matrix_config.speed);
  111. }
  112. __attribute__((weak)) uint8_t rgb_matrix_map_row_column_to_led_kb(uint8_t row, uint8_t column, uint8_t *led_i) { return 0; }
  113. uint8_t rgb_matrix_map_row_column_to_led(uint8_t row, uint8_t column, uint8_t *led_i) {
  114. uint8_t led_count = rgb_matrix_map_row_column_to_led_kb(row, column, led_i);
  115. uint8_t led_index = g_led_config.matrix_co[row][column];
  116. if (led_index != NO_LED) {
  117. led_i[led_count] = led_index;
  118. led_count++;
  119. }
  120. return led_count;
  121. }
  122. void rgb_matrix_update_pwm_buffers(void) { rgb_matrix_driver.flush(); }
  123. void rgb_matrix_set_color(int index, uint8_t red, uint8_t green, uint8_t blue) { rgb_matrix_driver.set_color(index, red, green, blue); }
  124. void rgb_matrix_set_color_all(uint8_t red, uint8_t green, uint8_t blue) { rgb_matrix_driver.set_color_all(red, green, blue); }
  125. bool process_rgb_matrix(uint16_t keycode, keyrecord_t *record) {
  126. #ifdef RGB_MATRIX_KEYREACTIVE_ENABLED
  127. uint8_t led[LED_HITS_TO_REMEMBER];
  128. uint8_t led_count = 0;
  129. # if defined(RGB_MATRIX_KEYRELEASES)
  130. if (!record->event.pressed) {
  131. led_count = rgb_matrix_map_row_column_to_led(record->event.key.row, record->event.key.col, led);
  132. g_rgb_counters.any_key_hit = 0;
  133. }
  134. # elif defined(RGB_MATRIX_KEYPRESSES)
  135. if (record->event.pressed) {
  136. led_count = rgb_matrix_map_row_column_to_led(record->event.key.row, record->event.key.col, led);
  137. g_rgb_counters.any_key_hit = 0;
  138. }
  139. # endif // defined(RGB_MATRIX_KEYRELEASES)
  140. if (last_hit_buffer.count + led_count > LED_HITS_TO_REMEMBER) {
  141. memcpy(&last_hit_buffer.x[0], &last_hit_buffer.x[led_count], LED_HITS_TO_REMEMBER - led_count);
  142. memcpy(&last_hit_buffer.y[0], &last_hit_buffer.y[led_count], LED_HITS_TO_REMEMBER - led_count);
  143. memcpy(&last_hit_buffer.tick[0], &last_hit_buffer.tick[led_count], (LED_HITS_TO_REMEMBER - led_count) * 2); // 16 bit
  144. memcpy(&last_hit_buffer.index[0], &last_hit_buffer.index[led_count], LED_HITS_TO_REMEMBER - led_count);
  145. last_hit_buffer.count--;
  146. }
  147. for (uint8_t i = 0; i < led_count; i++) {
  148. uint8_t index = last_hit_buffer.count;
  149. last_hit_buffer.x[index] = g_led_config.point[led[i]].x;
  150. last_hit_buffer.y[index] = g_led_config.point[led[i]].y;
  151. last_hit_buffer.index[index] = led[i];
  152. last_hit_buffer.tick[index] = 0;
  153. last_hit_buffer.count++;
  154. }
  155. #endif // RGB_MATRIX_KEYREACTIVE_ENABLED
  156. #if defined(RGB_MATRIX_FRAMEBUFFER_EFFECTS) && !defined(DISABLE_RGB_MATRIX_TYPING_HEATMAP)
  157. if (rgb_matrix_config.mode == RGB_MATRIX_TYPING_HEATMAP) {
  158. process_rgb_matrix_typing_heatmap(record);
  159. }
  160. #endif // defined(RGB_MATRIX_FRAMEBUFFER_EFFECTS) && !defined(DISABLE_RGB_MATRIX_TYPING_HEATMAP)
  161. return true;
  162. }
  163. void rgb_matrix_test(void) {
  164. // Mask out bits 4 and 5
  165. // Increase the factor to make the test animation slower (and reduce to make it faster)
  166. uint8_t factor = 10;
  167. switch ((g_rgb_counters.tick & (0b11 << factor)) >> factor) {
  168. case 0: {
  169. rgb_matrix_set_color_all(20, 0, 0);
  170. break;
  171. }
  172. case 1: {
  173. rgb_matrix_set_color_all(0, 20, 0);
  174. break;
  175. }
  176. case 2: {
  177. rgb_matrix_set_color_all(0, 0, 20);
  178. break;
  179. }
  180. case 3: {
  181. rgb_matrix_set_color_all(20, 20, 20);
  182. break;
  183. }
  184. }
  185. }
  186. static bool rgb_matrix_none(effect_params_t *params) {
  187. if (!params->init) {
  188. return false;
  189. }
  190. RGB_MATRIX_USE_LIMITS(led_min, led_max);
  191. for (uint8_t i = led_min; i < led_max; i++) {
  192. rgb_matrix_set_color(i, 0, 0, 0);
  193. }
  194. return led_max < DRIVER_LED_TOTAL;
  195. }
  196. static uint8_t rgb_last_enable = UINT8_MAX;
  197. static uint8_t rgb_last_effect = UINT8_MAX;
  198. static effect_params_t rgb_effect_params = {0, 0xFF};
  199. static rgb_task_states rgb_task_state = SYNCING;
  200. static void rgb_task_timers(void) {
  201. // Update double buffer timers
  202. uint16_t deltaTime = timer_elapsed32(rgb_counters_buffer);
  203. rgb_counters_buffer = timer_read32();
  204. if (g_rgb_counters.any_key_hit < UINT32_MAX) {
  205. if (UINT32_MAX - deltaTime < g_rgb_counters.any_key_hit) {
  206. g_rgb_counters.any_key_hit = UINT32_MAX;
  207. } else {
  208. g_rgb_counters.any_key_hit += deltaTime;
  209. }
  210. }
  211. // Update double buffer last hit timers
  212. #ifdef RGB_MATRIX_KEYREACTIVE_ENABLED
  213. uint8_t count = last_hit_buffer.count;
  214. for (uint8_t i = 0; i < count; ++i) {
  215. if (UINT16_MAX - deltaTime < last_hit_buffer.tick[i]) {
  216. last_hit_buffer.count--;
  217. continue;
  218. }
  219. last_hit_buffer.tick[i] += deltaTime;
  220. }
  221. #endif // RGB_MATRIX_KEYREACTIVE_ENABLED
  222. }
  223. static void rgb_task_sync(void) {
  224. // next task
  225. if (timer_elapsed32(g_rgb_counters.tick) >= RGB_MATRIX_LED_FLUSH_LIMIT) rgb_task_state = STARTING;
  226. }
  227. static void rgb_task_start(void) {
  228. // reset iter
  229. rgb_effect_params.iter = 0;
  230. // update double buffers
  231. g_rgb_counters.tick = rgb_counters_buffer;
  232. #ifdef RGB_MATRIX_KEYREACTIVE_ENABLED
  233. g_last_hit_tracker = last_hit_buffer;
  234. #endif // RGB_MATRIX_KEYREACTIVE_ENABLED
  235. // next task
  236. rgb_task_state = RENDERING;
  237. }
  238. static void rgb_task_render(uint8_t effect) {
  239. bool rendering = false;
  240. rgb_effect_params.init = (effect != rgb_last_effect) || (rgb_matrix_config.enable != rgb_last_enable);
  241. // each effect can opt to do calculations
  242. // and/or request PWM buffer updates.
  243. switch (effect) {
  244. case RGB_MATRIX_NONE:
  245. rendering = rgb_matrix_none(&rgb_effect_params);
  246. break;
  247. // ---------------------------------------------
  248. // -----Begin rgb effect switch case macros-----
  249. #define RGB_MATRIX_EFFECT(name, ...) \
  250. case RGB_MATRIX_##name: \
  251. rendering = name(&rgb_effect_params); \
  252. break;
  253. #include "rgb_matrix_animations/rgb_matrix_effects.inc"
  254. #undef RGB_MATRIX_EFFECT
  255. #if defined(RGB_MATRIX_CUSTOM_KB) || defined(RGB_MATRIX_CUSTOM_USER)
  256. # define RGB_MATRIX_EFFECT(name, ...) \
  257. case RGB_MATRIX_CUSTOM_##name: \
  258. rendering = name(&rgb_effect_params); \
  259. break;
  260. # ifdef RGB_MATRIX_CUSTOM_KB
  261. # include "rgb_matrix_kb.inc"
  262. # endif
  263. # ifdef RGB_MATRIX_CUSTOM_USER
  264. # include "rgb_matrix_user.inc"
  265. # endif
  266. # undef RGB_MATRIX_EFFECT
  267. #endif
  268. // -----End rgb effect switch case macros-------
  269. // ---------------------------------------------
  270. // Factory default magic value
  271. case UINT8_MAX: {
  272. rgb_matrix_test();
  273. rgb_task_state = FLUSHING;
  274. }
  275. return;
  276. }
  277. rgb_effect_params.iter++;
  278. // next task
  279. if (!rendering) {
  280. rgb_task_state = FLUSHING;
  281. if (!rgb_effect_params.init && effect == RGB_MATRIX_NONE) {
  282. // We only need to flush once if we are RGB_MATRIX_NONE
  283. rgb_task_state = SYNCING;
  284. }
  285. }
  286. }
  287. static void rgb_task_flush(uint8_t effect) {
  288. // update last trackers after the first full render so we can init over several frames
  289. rgb_last_effect = effect;
  290. rgb_last_enable = rgb_matrix_config.enable;
  291. // update pwm buffers
  292. rgb_matrix_update_pwm_buffers();
  293. // next task
  294. rgb_task_state = SYNCING;
  295. }
  296. void rgb_matrix_task(void) {
  297. rgb_task_timers();
  298. // Ideally we would also stop sending zeros to the LED driver PWM buffers
  299. // while suspended and just do a software shutdown. This is a cheap hack for now.
  300. bool suspend_backlight = ((g_suspend_state && RGB_DISABLE_WHEN_USB_SUSPENDED) || (RGB_DISABLE_AFTER_TIMEOUT > 0 && g_rgb_counters.any_key_hit > RGB_DISABLE_AFTER_TIMEOUT * 60 * 20));
  301. uint8_t effect = suspend_backlight || !rgb_matrix_config.enable ? 0 : rgb_matrix_config.mode;
  302. switch (rgb_task_state) {
  303. case STARTING:
  304. rgb_task_start();
  305. break;
  306. case RENDERING:
  307. rgb_task_render(effect);
  308. break;
  309. case FLUSHING:
  310. rgb_task_flush(effect);
  311. break;
  312. case SYNCING:
  313. rgb_task_sync();
  314. break;
  315. }
  316. if (!suspend_backlight) {
  317. rgb_matrix_indicators();
  318. }
  319. }
  320. void rgb_matrix_indicators(void) {
  321. rgb_matrix_indicators_kb();
  322. rgb_matrix_indicators_user();
  323. }
  324. __attribute__((weak)) void rgb_matrix_indicators_kb(void) {}
  325. __attribute__((weak)) void rgb_matrix_indicators_user(void) {}
  326. void rgb_matrix_init(void) {
  327. rgb_matrix_driver.init();
  328. // TODO: put the 1 second startup delay here?
  329. #ifdef RGB_MATRIX_KEYREACTIVE_ENABLED
  330. g_last_hit_tracker.count = 0;
  331. for (uint8_t i = 0; i < LED_HITS_TO_REMEMBER; ++i) {
  332. g_last_hit_tracker.tick[i] = UINT16_MAX;
  333. }
  334. last_hit_buffer.count = 0;
  335. for (uint8_t i = 0; i < LED_HITS_TO_REMEMBER; ++i) {
  336. last_hit_buffer.tick[i] = UINT16_MAX;
  337. }
  338. #endif // RGB_MATRIX_KEYREACTIVE_ENABLED
  339. if (!eeconfig_is_enabled()) {
  340. dprintf("rgb_matrix_init_drivers eeconfig is not enabled.\n");
  341. eeconfig_init();
  342. eeconfig_update_rgb_matrix_default();
  343. }
  344. eeconfig_read_rgb_matrix();
  345. if (!rgb_matrix_config.mode) {
  346. dprintf("rgb_matrix_init_drivers rgb_matrix_config.mode = 0. Write default values to EEPROM.\n");
  347. eeconfig_update_rgb_matrix_default();
  348. }
  349. eeconfig_debug_rgb_matrix(); // display current eeprom values
  350. }
  351. void rgb_matrix_set_suspend_state(bool state) { g_suspend_state = state; }
  352. void rgb_matrix_toggle(void) {
  353. rgb_matrix_config.enable ^= 1;
  354. rgb_task_state = STARTING;
  355. eeconfig_update_rgb_matrix();
  356. }
  357. void rgb_matrix_enable(void) {
  358. rgb_matrix_enable_noeeprom();
  359. eeconfig_update_rgb_matrix();
  360. }
  361. void rgb_matrix_enable_noeeprom(void) {
  362. if (!rgb_matrix_config.enable) rgb_task_state = STARTING;
  363. rgb_matrix_config.enable = 1;
  364. }
  365. void rgb_matrix_disable(void) {
  366. rgb_matrix_disable_noeeprom();
  367. eeconfig_update_rgb_matrix();
  368. }
  369. void rgb_matrix_disable_noeeprom(void) {
  370. if (rgb_matrix_config.enable) rgb_task_state = STARTING;
  371. rgb_matrix_config.enable = 0;
  372. }
  373. void rgb_matrix_step(void) {
  374. rgb_matrix_config.mode++;
  375. if (rgb_matrix_config.mode >= RGB_MATRIX_EFFECT_MAX) rgb_matrix_config.mode = 1;
  376. rgb_task_state = STARTING;
  377. eeconfig_update_rgb_matrix();
  378. }
  379. void rgb_matrix_step_reverse(void) {
  380. rgb_matrix_config.mode--;
  381. if (rgb_matrix_config.mode < 1) rgb_matrix_config.mode = RGB_MATRIX_EFFECT_MAX - 1;
  382. rgb_task_state = STARTING;
  383. eeconfig_update_rgb_matrix();
  384. }
  385. void rgb_matrix_increase_hue(void) {
  386. rgb_matrix_config.hsv.h += RGB_MATRIX_HUE_STEP;
  387. eeconfig_update_rgb_matrix();
  388. }
  389. void rgb_matrix_decrease_hue(void) {
  390. rgb_matrix_config.hsv.h -= RGB_MATRIX_HUE_STEP;
  391. eeconfig_update_rgb_matrix();
  392. }
  393. void rgb_matrix_increase_sat(void) {
  394. rgb_matrix_config.hsv.s = qadd8(rgb_matrix_config.hsv.s, RGB_MATRIX_SAT_STEP);
  395. eeconfig_update_rgb_matrix();
  396. }
  397. void rgb_matrix_decrease_sat(void) {
  398. rgb_matrix_config.hsv.s = qsub8(rgb_matrix_config.hsv.s, RGB_MATRIX_SAT_STEP);
  399. eeconfig_update_rgb_matrix();
  400. }
  401. void rgb_matrix_increase_val(void) {
  402. rgb_matrix_config.hsv.v = qadd8(rgb_matrix_config.hsv.v, RGB_MATRIX_VAL_STEP);
  403. if (rgb_matrix_config.hsv.v > RGB_MATRIX_MAXIMUM_BRIGHTNESS) rgb_matrix_config.hsv.v = RGB_MATRIX_MAXIMUM_BRIGHTNESS;
  404. eeconfig_update_rgb_matrix();
  405. }
  406. void rgb_matrix_decrease_val(void) {
  407. rgb_matrix_config.hsv.v = qsub8(rgb_matrix_config.hsv.v, RGB_MATRIX_VAL_STEP);
  408. eeconfig_update_rgb_matrix();
  409. }
  410. void rgb_matrix_increase_speed(void) {
  411. rgb_matrix_config.speed = qadd8(rgb_matrix_config.speed, RGB_MATRIX_SPD_STEP);
  412. eeconfig_update_rgb_matrix();
  413. }
  414. void rgb_matrix_decrease_speed(void) {
  415. rgb_matrix_config.speed = qsub8(rgb_matrix_config.speed, RGB_MATRIX_SPD_STEP);
  416. eeconfig_update_rgb_matrix();
  417. }
  418. led_flags_t rgb_matrix_get_flags(void) { return rgb_effect_params.flags; }
  419. void rgb_matrix_set_flags(led_flags_t flags) { rgb_effect_params.flags = flags; }
  420. void rgb_matrix_mode(uint8_t mode) {
  421. rgb_matrix_config.mode = mode;
  422. rgb_task_state = STARTING;
  423. eeconfig_update_rgb_matrix();
  424. }
  425. void rgb_matrix_mode_noeeprom(uint8_t mode) { rgb_matrix_config.mode = mode; }
  426. uint8_t rgb_matrix_get_mode(void) { return rgb_matrix_config.mode; }
  427. void rgb_matrix_sethsv(uint16_t hue, uint8_t sat, uint8_t val) {
  428. rgb_matrix_sethsv_noeeprom(hue, sat, val);
  429. eeconfig_update_rgb_matrix();
  430. }
  431. void rgb_matrix_sethsv_noeeprom(uint16_t hue, uint8_t sat, uint8_t val) {
  432. rgb_matrix_config.hsv.h = hue;
  433. rgb_matrix_config.hsv.s = sat;
  434. rgb_matrix_config.hsv.v = val;
  435. if (rgb_matrix_config.hsv.v > RGB_MATRIX_MAXIMUM_BRIGHTNESS) rgb_matrix_config.hsv.v = RGB_MATRIX_MAXIMUM_BRIGHTNESS;
  436. }