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.

131 lines
4.3 KiB

  1. // Copyright 2021 Victor Toni (@vitoni)
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include "vitoni.h"
  4. #include <rgb_matrix.h>
  5. #include <lib/lib8tion/lib8tion.h>
  6. #include "rgb_matrix_effects.h"
  7. #include "utils.h"
  8. #if defined(RGB_FADE_IN) || defined(RGB_DISABLE_WITH_FADE_OUT) || defined(RGB_IDLE_TIMEOUT)
  9. static uint8_t state;
  10. // flag used to indicate that offset calculation is needed to adjust the timer,
  11. // so that it matches the index used for sine calculation
  12. static bool calc_offset;
  13. void matrix_scan_user_rgb(void) {
  14. #if defined(RGB_DISABLE_WITH_FADE_OUT) || defined(RGB_IDLE_TIMEOUT)
  15. const uint8_t time = rgb_time_2_scale();
  16. #endif
  17. static uint8_t time_offset;
  18. const uint32_t inactivity_millis = last_input_activity_elapsed();
  19. #if defined(RGB_IDLE_TIMEOUT)
  20. if (IDLE != state && RGB_IDLE_TIMEOUT <= inactivity_millis) {
  21. update_value(&state, IDLE_FADE_OUT, &calc_offset);
  22. }
  23. #endif
  24. #if defined(RGB_DISABLE_WITH_FADE_OUT)
  25. const uint32_t fade_out_duration = scale_2_rgb_time(128);
  26. const uint32_t start_fade_out_after_millis = (RGB_MATRIX_TIMEOUT) > fade_out_duration
  27. ? (RGB_MATRIX_TIMEOUT) - fade_out_duration
  28. : 0;
  29. if (start_fade_out_after_millis <= inactivity_millis) {
  30. update_value(&state, FADE_OUT, &calc_offset);
  31. }
  32. #elif defined(RGB_MATRIX_TIMEOUT)
  33. // having to set brightness "manually" to black as starting point for fade in
  34. // for the time when returning from suspended state
  35. if (RGB_MATRIX_TIMEOUT <= inactivity_millis + 15) {
  36. rgb_matrix_config.hsv.v = 0;
  37. state = SUSPENDED;
  38. }
  39. #endif
  40. switch(state) {
  41. #if defined(RGB_IDLE_TIMEOUT)
  42. case IDLE_FADE_OUT:
  43. if (calc_offset) {
  44. time_offset = calc_fade_out_offset(time);
  45. // resetting flag for subsequent calls
  46. calc_offset = false;
  47. }
  48. if (idle_fade_out(time + time_offset)) {
  49. update_value(&state, IDLE, &calc_offset);
  50. }
  51. break;
  52. case IDLE:
  53. #if defined(RGB_IDLE_BREATHE)
  54. if (calc_offset) {
  55. // no need to calculate time_offset since we are aligned already due to IDLE_FADE_OUT
  56. // resetting flag for subsequent calls
  57. calc_offset = false;
  58. }
  59. idle_breathe(time + time_offset);
  60. #endif
  61. break;
  62. #endif
  63. #if defined(RGB_DISABLE_WITH_FADE_OUT)
  64. case FADE_OUT:
  65. if (calc_offset) {
  66. time_offset = calc_fade_out_offset(time);
  67. // resetting flag for subsequent calls
  68. calc_offset = false;
  69. }
  70. if (fade_out(time + time_offset)) {
  71. update_value(&state, SUSPENDED, &calc_offset);
  72. }
  73. break;
  74. #endif
  75. #if defined(RGB_FADE_IN) || defined(RGB_IDLE_TIMEOUT)
  76. case FADE_IN:
  77. {
  78. // since we want to be active, fade in should be faster than e.g. fading out
  79. const uint8_t fade_in_time = rgb_time_2_scale_w_factor(4);
  80. if (calc_offset) {
  81. time_offset = calc_fade_in_offset(fade_in_time);
  82. // resetting flag for subsequent calls
  83. calc_offset = false;
  84. }
  85. if (fade_in(fade_in_time + time_offset)) {
  86. update_value(&state, REGULAR, &calc_offset);
  87. }
  88. }
  89. break;
  90. #endif
  91. default:
  92. break;
  93. }
  94. }
  95. #if defined(RGB_FADE_IN) || defined(RGB_IDLE_TIMEOUT)
  96. bool process_record_user_rgb(const uint16_t keycode, const keyrecord_t *record) {
  97. // if we are in a non regular state we might have faded out (eventually partially)
  98. // so we restore brightness (to max as we don't keep track of manually changed brightness)
  99. // if (REGULAR != state && FADE_IN != state) {
  100. if (FADE_IN != state && REGULAR != state) {
  101. update_value(&state, FADE_IN, &calc_offset);
  102. }
  103. return true; // Process all other keycodes normally
  104. }
  105. void suspend_wakeup_init_user(void) {
  106. if (FADE_IN != state) {
  107. // setting brightness to black as starting point for fade in
  108. rgb_matrix_config.hsv.v = 0;
  109. update_value(&state, FADE_IN, &calc_offset);
  110. }
  111. }
  112. #endif // defined(RGB_FADE_IN)
  113. #endif // defined(RGB_FADE_IN) || defined(RGB_DISABLE_WITH_FADE_OUT)