rgb_matrix_effects.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. // Copyright 2021 Victor Toni (@vitoni)
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #include <stdint.h>
  5. #include <stdbool.h>
  6. /**
  7. * States reflecting the state of the keyboard.
  8. * Dependeing on these states various effects can set for the RGB matrix.
  9. */
  10. enum states {
  11. REGULAR //!< when in regular use
  12. #if defined(RGB_IDLE_TIMEOUT)
  13. ,IDLE_FADE_OUT //!< when started idling
  14. ,IDLE //!< when idling
  15. #endif
  16. #if defined(RGB_FADE_IN) || defined(RGB_IDLE_TIMEOUT)
  17. ,FADE_IN //!< when starting initially or before going back to REGULAR
  18. #endif
  19. #if defined(RGB_DISABLE_WITH_FADE_OUT)
  20. ,FADE_OUT //!< before supending
  21. #endif
  22. ,SUSPENDED //!< expecting to be suspended by RGB_MATRIX_TIMEOUT any time
  23. };
  24. /**
  25. * @brief Scales down `g_rgb_timer` so that it can be used for RGB effects.
  26. * @details Usually these calculations aredone internally by some RGB effects.
  27. This method exposed to scaling so that all effects to have same timebase. If `rgb_matrix_config.speed` all effects are affected the same.
  28. * @param[in] factor The factor can be used to speed up some operations in relation to others.
  29. * @return scaled down timer taking into account the given factor
  30. * @see g_rgb_timer
  31. * @see rgb_matrix_config.speed
  32. */
  33. uint8_t rgb_time_2_scale_w_factor(const uint8_t factor);
  34. /**
  35. * @brief Scales down `g_rgb_timer` so that it can be used for RGB effects.
  36. * @return scaled down timer
  37. * @see rgb_time_2_scale_w_factor()
  38. */
  39. uint8_t rgb_time_2_scale(void);
  40. /**
  41. * @brief Inverse function to calculate time required to execute `timer` steps.
  42. * @details This method allows calculation of the time needed to execute N `timer`steps.
  43. Usefull when using a scaled down time but requiring the time needed to perform these steps.
  44. * @param[in] scaled_time scaled down timer to inverse to time
  45. * @return time corresponding to scaled down time
  46. * @see rgb_time_2_scale()
  47. */
  48. uint16_t scale_2_rgb_time(const uint8_t scaled_time);
  49. /**
  50. * @brief Convenience method to eventually skip the value part when setting HSV.
  51. * @details When setting HSV this includes the value/brightness.
  52. As changing brightness might interfer with fading or breathing effects,
  53. this method can skip the value part of HSV (depending on the preprocessor flag: RGB_FADE_IN).
  54. * @param[in] hue Hue
  55. * @param[in] sat Saturation
  56. * @param[in] hue Value (brightness)
  57. * @see rgb_matrix_sethsv_noeeprom()
  58. */
  59. void rgb_matrix_sethsv_noeeprom_user(const uint16_t hue, const uint8_t sat, const uint8_t val);
  60. #if defined(RGB_FADE_IN) || defined(RGB_DISABLE_WITH_FADE_OUT) || defined(RGB_IDLE_TIMEOUT)
  61. # if defined(RGB_MATRIX_MAXIMUM_BRIGHTNESS)
  62. # if (RGB_MATRIX_MAXIMUM_BRIGHTNESS) < 1
  63. # error "RGB_MATRIX_MAXIMUM_BRIGHTNESS must not be less than ONE"
  64. # endif
  65. # if UINT8_MAX < (RGB_MATRIX_MAXIMUM_BRIGHTNESS)
  66. # error "RGB_MATRIX_MAXIMUM_BRIGHTNESS must not be larger than UINT8_MAX"
  67. # endif
  68. # else
  69. # define RGB_MATRIX_MAXIMUM_BRIGHTNESS 200
  70. # endif
  71. #endif
  72. #if defined(RGB_FADE_IN) || defined(RGB_IDLE_TIMEOUT)
  73. /**
  74. * @brief Calculates the time offset required by fade in.
  75. * @details Using an arbitrary timer any point on the sine curve might be pointed to.
  76. * The offset is calculated so that
  77. * a) the point is at the lowest point in the curve and the curve is raising
  78. * b) the point is near the current brightness (eg. fade in might be called while fading out and the lowest value has not yet been reached).
  79. * @param[in] time Current time usually represented by a(usually scaled) timer
  80. * @return Offset required so that time matches the current brightness
  81. */
  82. uint8_t calc_fade_in_offset(const uint8_t time);
  83. /**
  84. * @brief Increases value/brightness until reaching RGB_MATRIX_MAXIMUM_BRIGHTNESS based on given timer.
  85. * @param[in] time A (usually scaled) timer
  86. * @return Returns `true` if RGB_MATRIX_MAXIMUM_BRIGHTNESS has been reached, `false` otherwise.
  87. */
  88. bool fade_in(const uint8_t time);
  89. #endif
  90. #if defined(RGB_DISABLE_WITH_FADE_OUT)
  91. # if !defined(RGB_MATRIX_TIMEOUT)
  92. # warning "RGB_DISABLE_WITH_FADE_OUT expects RGB_MATRIX_TIMEOUT to be defined"
  93. # endif
  94. #endif
  95. #if defined(RGB_DISABLE_WITH_FADE_OUT) || defined(RGB_IDLE_TIMEOUT)
  96. /**
  97. * @brief Calculates the time offset required by fade out.
  98. * @details Using an arbitrary timer any point on the Sinus curve might be pointed to.
  99. * The offest is calculated so that
  100. * a) the point is at the highest point in the curve and the curve is failing
  101. * b) the point is near the current brightness (eg. fade out might be called while on breath effect).
  102. * @param[in] time Current time usually represented by a(usually scaled) timer
  103. * @return Offset required so that time matches the current brightness
  104. */
  105. uint8_t calc_fade_out_offset(const uint8_t time);
  106. #endif
  107. #if defined(RGB_DISABLE_WITH_FADE_OUT)
  108. /**
  109. * @brief Decreases value/brightness until reaching 0 based on given timer.
  110. * @param[in] time A (usually scaled) timer
  111. * @return Returns `true` if 0 has been reached, `false` otherwise.
  112. */
  113. bool fade_out(const uint8_t time);
  114. #endif
  115. #if defined(RGB_IDLE_TIMEOUT)
  116. # if RGB_IDLE_TIMEOUT < 0
  117. # error "RGB_IDLE_TIMEOUT must not be less than ZERO"
  118. # endif
  119. # if !defined(RGB_IDLE_MINIMUM_BRIGHTNESS)
  120. // minimum brightness when idling
  121. # define RGB_IDLE_MINIMUM_BRIGHTNESS (RGB_MATRIX_MAXIMUM_BRIGHTNESS/5)
  122. # endif
  123. # if RGB_IDLE_MINIMUM_BRIGHTNESS < 0
  124. # error "RGB_IDLE_MINIMUM_BRIGHTNESS must not be less than ZERO"
  125. # endif // RGB_IDLE_MINIMUM_BRIGHTNESS < 0
  126. # if RGB_MATRIX_MAXIMUM_BRIGHTNESS <= RGB_IDLE_MINIMUM_BRIGHTNESS
  127. # error "RGB_IDLE_MINIMUM_BRIGHTNESS must be less than RGB_MATRIX_MAXIMUM_BRIGHTNESS"
  128. # endif // RGB_MATRIX_MAXIMUM_BRIGHTNESS <= RGB_IDLE_MINIMUM_BRIGHTNESS
  129. /**
  130. * @brief Decreases value/brightness until reaching `RGB_IDLE_MINIMUM_BRIGHTNESS` based on given timer.
  131. * @param[in] time A (usually scaled) timer
  132. * @return Returns `true` if `RGB_IDLE_MINIMUM_BRIGHTNESS` has been reached, `false` otherwise.
  133. */
  134. bool idle_fade_out(const uint8_t time);
  135. #if defined(RGB_IDLE_BREATHE)
  136. # if !defined(RGB_IDLE_MAXIMUM_BRIGHTNESS)
  137. // maximum brightness when idling
  138. # define RGB_IDLE_MAXIMUM_BRIGHTNESS (RGB_MATRIX_MAXIMUM_BRIGHTNESS*3/5)
  139. # endif
  140. # if !(0 <= RGB_IDLE_MAXIMUM_BRIGHTNESS)
  141. # error "RGB_IDLE_MINIMUM_BRIGHTNESS must not be less than ZERO, was: " RGB_IDLE_MAXIMUM_BRIGHTNESS
  142. # endif // RGB_IDLE_MAXIMUM_BRIGHTNESS < 0
  143. # if !(RGB_IDLE_MINIMUM_BRIGHTNESS < RGB_IDLE_MAXIMUM_BRIGHTNESS)
  144. # error "RGB_IDLE_MINIMUM_BRIGHTNESS must be less than RGB_IDLE_MAXIMUM_BRIGHTNESS"
  145. # endif // RGB_IDLE_MAXIMUM_BRIGHTNESS <= RGB_IDLE_MINIMUM_BRIGHTNESS
  146. # if !(RGB_IDLE_MAXIMUM_BRIGHTNESS <= RGB_MATRIX_MAXIMUM_BRIGHTNESS)
  147. # error "RGB_IDLE_MAXIMUM_BRIGHTNESS must be less than or equal to RGB_MATRIX_MAXIMUM_BRIGHTNESS"
  148. # endif // RGB_MATRIX_MAXIMUM_BRIGHTNESS <= RGB_IDLE_MAXIMUM_BRIGHTNESS
  149. /**
  150. * @brief Changes value/brightness to create a breathing effect based on given timer.
  151. * @details Brightness will breathe in the range starting from `RGB_IDLE_MINIMUM_BRIGHTNESS` to `RGB_IDLE_MAXIMUM_BRIGHTNESS`.
  152. * @param[in] time A (usually scaled) timer
  153. */
  154. void idle_breathe(const uint8_t time);
  155. #endif // RGB_IDLE_BREATHE
  156. #endif // RGB_IDLE_TIMEOUT