rgb_matrix_stuff.c 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /* Copyright 2020 Christopher Courtney, aka Drashna Jael're (@drashna) <drashna@live.com>
  2. *
  3. * This program is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation, either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include "drashna.h"
  17. #include "rgb_matrix.h"
  18. #include "lib/lib8tion/lib8tion.h"
  19. extern led_config_t g_led_config;
  20. static uint32_t hypno_timer;
  21. #if defined(SPLIT_KEYBOARD) || defined(KEYBOARD_ergodox_ez) || defined(KEYBOARD_crkbd)
  22. # define RGB_MATRIX_REST_MODE RGB_MATRIX_CYCLE_OUT_IN_DUAL
  23. #else
  24. # define RGB_MATRIX_REST_MODE RGB_MATRIX_CYCLE_OUT_IN
  25. #endif
  26. void rgb_matrix_layer_helper(uint8_t hue, uint8_t sat, uint8_t val, uint8_t mode, uint8_t speed, uint8_t led_type, uint8_t led_min, uint8_t led_max) {
  27. HSV hsv = {hue, sat, val};
  28. if (hsv.v > rgb_matrix_get_val()) { hsv.v = rgb_matrix_get_val(); }
  29. switch (mode) {
  30. case 1: // breathing
  31. {
  32. uint16_t time = scale16by8(g_rgb_timer, speed / 8);
  33. hsv.v = scale8(abs8(sin8(time) - 128) * 2, hsv.v);
  34. RGB rgb = hsv_to_rgb(hsv);
  35. for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) {
  36. if (HAS_FLAGS(g_led_config.flags[i], led_type)) { RGB_MATRIX_INDICATOR_SET_COLOR(i, rgb.r, rgb.g, rgb.b); }
  37. }
  38. break;
  39. }
  40. default: // Solid Color
  41. {
  42. RGB rgb = hsv_to_rgb(hsv);
  43. for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) {
  44. if (HAS_FLAGS(g_led_config.flags[i], led_type)) { RGB_MATRIX_INDICATOR_SET_COLOR(i, rgb.r, rgb.g, rgb.b); }
  45. }
  46. break;
  47. }
  48. }
  49. }
  50. __attribute__((weak)) void rgb_matrix_indicator_keymap(void) {}
  51. void matrix_scan_rgb_matrix(void) {
  52. #if defined(RGB_MATRIX_FRAMEBUFFER_EFFECTS)
  53. if (userspace_config.rgb_matrix_idle_anim && rgb_matrix_get_mode() == RGB_MATRIX_TYPING_HEATMAP && sync_timer_elapsed32(hypno_timer) > 15000) { rgb_matrix_mode_noeeprom(RGB_MATRIX_REST_MODE); }
  54. #endif
  55. rgb_matrix_indicator_keymap();
  56. }
  57. void keyboard_post_init_rgb_matrix(void) {
  58. #if defined(RGB_MATRIX_FRAMEBUFFER_EFFECTS)
  59. if (userspace_config.rgb_matrix_idle_anim) { rgb_matrix_mode_noeeprom(RGB_MATRIX_REST_MODE); }
  60. #endif
  61. }
  62. bool process_record_user_rgb_matrix(uint16_t keycode, keyrecord_t *record) {
  63. #if defined(RGB_MATRIX_FRAMEBUFFER_EFFECTS)
  64. hypno_timer = sync_timer_read32();
  65. if (userspace_config.rgb_matrix_idle_anim && rgb_matrix_get_mode() == RGB_MATRIX_REST_MODE) { rgb_matrix_mode_noeeprom(RGB_MATRIX_TYPING_HEATMAP); }
  66. #endif
  67. switch (keycode) {
  68. case RGB_IDL: // This allows me to use underglow as layer indication, or as normal
  69. #if defined(RGB_MATRIX_ENABLE) && defined(RGB_MATRIX_FRAMEBUFFER_EFFECTS)
  70. if (record->event.pressed) {
  71. userspace_config.rgb_matrix_idle_anim ^= 1;
  72. dprintf("RGB Matrix Idle Animation [EEPROM]: %u\n", userspace_config.rgb_matrix_idle_anim);
  73. eeconfig_update_user(userspace_config.raw);
  74. if (userspace_config.rgb_matrix_idle_anim) { rgb_matrix_mode_noeeprom(RGB_MATRIX_TYPING_HEATMAP); }
  75. }
  76. #endif
  77. break;
  78. }
  79. return true;
  80. }