rgb_matrix_stuff.c 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. 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) {
  22. HSV hsv = {hue, sat, val};
  23. if (hsv.v > rgb_matrix_get_val()) {
  24. hsv.v = rgb_matrix_get_val();
  25. }
  26. switch (mode) {
  27. case 1: // breathing
  28. {
  29. uint16_t time = scale16by8(g_rgb_timer, speed / 8);
  30. hsv.v = scale8(abs8(sin8(time) - 128) * 2, hsv.v);
  31. RGB rgb = hsv_to_rgb(hsv);
  32. for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) {
  33. if (HAS_FLAGS(g_led_config.flags[i], led_type)) {
  34. RGB_MATRIX_INDICATOR_SET_COLOR(i, rgb.r, rgb.g, rgb.b);
  35. }
  36. }
  37. break;
  38. }
  39. default: // Solid Color
  40. {
  41. RGB rgb = hsv_to_rgb(hsv);
  42. for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) {
  43. if (HAS_FLAGS(g_led_config.flags[i], led_type)) {
  44. RGB_MATRIX_INDICATOR_SET_COLOR(i, rgb.r, rgb.g, rgb.b);
  45. }
  46. }
  47. break;
  48. }
  49. }
  50. }
  51. __attribute__((weak)) void rgb_matrix_indicator_keymap(void) {}
  52. void matrix_scan_rgb_matrix(void) {
  53. #if defined(RGB_MATRIX_FRAMEBUFFER_EFFECTS)
  54. if (userspace_config.rgb_matrix_idle_anim && rgb_matrix_get_mode() == RGB_MATRIX_TYPING_HEATMAP && sync_timer_elapsed32(hypno_timer) > 15000) {
  55. rgb_matrix_mode_noeeprom(RGB_MATRIX_REST_MODE);
  56. }
  57. #endif
  58. rgb_matrix_indicator_keymap();
  59. }
  60. void keyboard_post_init_rgb_matrix(void) {
  61. #if defined(RGB_MATRIX_FRAMEBUFFER_EFFECTS)
  62. if (userspace_config.rgb_matrix_idle_anim) {
  63. rgb_matrix_mode_noeeprom(RGB_MATRIX_REST_MODE);
  64. }
  65. #endif
  66. }
  67. bool process_record_user_rgb_matrix(uint16_t keycode, keyrecord_t *record) {
  68. #if defined(RGB_MATRIX_FRAMEBUFFER_EFFECTS)
  69. hypno_timer = sync_timer_read32();
  70. if (userspace_config.rgb_matrix_idle_anim && rgb_matrix_get_mode() == RGB_MATRIX_REST_MODE) {
  71. rgb_matrix_mode_noeeprom(RGB_MATRIX_TYPING_HEATMAP);
  72. }
  73. #endif
  74. switch (keycode) {
  75. case RGB_IDL: // This allows me to use underglow as layer indication, or as normal
  76. #if defined(RGB_MATRIX_ENABLE) && defined(RGB_MATRIX_FRAMEBUFFER_EFFECTS)
  77. if (record->event.pressed) {
  78. userspace_config.rgb_matrix_idle_anim ^= 1;
  79. dprintf("RGB Matrix Idle Animation [EEPROM]: %u\n", userspace_config.rgb_matrix_idle_anim);
  80. eeconfig_update_user(userspace_config.raw);
  81. if (userspace_config.rgb_matrix_idle_anim) {
  82. rgb_matrix_mode_noeeprom(RGB_MATRIX_TYPING_HEATMAP);
  83. }
  84. }
  85. #endif
  86. break;
  87. }
  88. return true;
  89. }