process_rgb.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /* Copyright 2019
  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 "process_rgb.h"
  17. #include "rgb.h"
  18. typedef void (*rgb_func_pointer)(void);
  19. /**
  20. * Wrapper for inc/dec rgb keycode
  21. *
  22. * noinline to optimise for firmware size not speed (not in hot path)
  23. */
  24. static void __attribute__((noinline)) handleKeycodeRGB(const uint8_t is_shifted, const rgb_func_pointer inc_func, const rgb_func_pointer dec_func) {
  25. if (is_shifted) {
  26. dec_func();
  27. } else {
  28. inc_func();
  29. }
  30. }
  31. /**
  32. * Wrapper for animation mode
  33. * - if not in animation family -> jump to that animation
  34. * - otherwise -> wrap round animation speed
  35. *
  36. * noinline to optimise for firmware size not speed (not in hot path)
  37. */
  38. static void __attribute__((noinline, unused)) handleKeycodeRGBMode(const uint8_t start, const uint8_t end) {
  39. if ((start <= rgblight_get_mode()) && (rgblight_get_mode() < end)) {
  40. rgblight_step();
  41. } else {
  42. rgblight_mode(start);
  43. }
  44. }
  45. /**
  46. * Handle keycodes for both rgblight and rgbmatrix
  47. */
  48. bool process_rgb(const uint16_t keycode, const keyrecord_t *record) {
  49. #ifndef SPLIT_KEYBOARD
  50. if (record->event.pressed) {
  51. #else
  52. // Split keyboards need to trigger on key-up for edge-case issue
  53. if (!record->event.pressed) {
  54. #endif
  55. uint8_t shifted = get_mods() & (MOD_BIT(KC_LSHIFT) | MOD_BIT(KC_RSHIFT));
  56. switch (keycode) {
  57. case RGB_TOG:
  58. rgblight_toggle();
  59. return false;
  60. case RGB_MODE_FORWARD:
  61. handleKeycodeRGB(shifted, rgblight_step, rgblight_step_reverse);
  62. return false;
  63. case RGB_MODE_REVERSE:
  64. handleKeycodeRGB(shifted, rgblight_step_reverse, rgblight_step);
  65. return false;
  66. case RGB_HUI:
  67. handleKeycodeRGB(shifted, rgblight_increase_hue, rgblight_decrease_hue);
  68. return false;
  69. case RGB_HUD:
  70. handleKeycodeRGB(shifted, rgblight_decrease_hue, rgblight_increase_hue);
  71. return false;
  72. case RGB_SAI:
  73. handleKeycodeRGB(shifted, rgblight_increase_sat, rgblight_decrease_sat);
  74. return false;
  75. case RGB_SAD:
  76. handleKeycodeRGB(shifted, rgblight_decrease_sat, rgblight_increase_sat);
  77. return false;
  78. case RGB_VAI:
  79. handleKeycodeRGB(shifted, rgblight_increase_val, rgblight_decrease_val);
  80. return false;
  81. case RGB_VAD:
  82. handleKeycodeRGB(shifted, rgblight_decrease_val, rgblight_increase_val);
  83. return false;
  84. case RGB_SPI:
  85. handleKeycodeRGB(shifted, rgblight_increase_speed, rgblight_decrease_speed);
  86. return false;
  87. case RGB_SPD:
  88. handleKeycodeRGB(shifted, rgblight_decrease_speed, rgblight_increase_speed);
  89. return false;
  90. case RGB_MODE_PLAIN:
  91. rgblight_mode(RGBLIGHT_MODE_STATIC_LIGHT);
  92. return false;
  93. case RGB_MODE_BREATHE:
  94. #ifdef RGBLIGHT_EFFECT_BREATHING
  95. handleKeycodeRGBMode(RGBLIGHT_MODE_BREATHING, RGBLIGHT_MODE_BREATHING_end);
  96. #endif
  97. return false;
  98. case RGB_MODE_RAINBOW:
  99. #ifdef RGBLIGHT_EFFECT_RAINBOW_MOOD
  100. handleKeycodeRGBMode(RGBLIGHT_MODE_RAINBOW_MOOD, RGBLIGHT_MODE_RAINBOW_MOOD_end);
  101. #endif
  102. return false;
  103. case RGB_MODE_SWIRL:
  104. #ifdef RGBLIGHT_EFFECT_RAINBOW_SWIRL
  105. handleKeycodeRGBMode(RGBLIGHT_MODE_RAINBOW_SWIRL, RGBLIGHT_MODE_RAINBOW_SWIRL_end);
  106. #endif
  107. return false;
  108. case RGB_MODE_SNAKE:
  109. #ifdef RGBLIGHT_EFFECT_SNAKE
  110. handleKeycodeRGBMode(RGBLIGHT_MODE_SNAKE, RGBLIGHT_MODE_SNAKE_end);
  111. #endif
  112. return false;
  113. case RGB_MODE_KNIGHT:
  114. #ifdef RGBLIGHT_EFFECT_KNIGHT
  115. handleKeycodeRGBMode(RGBLIGHT_MODE_KNIGHT, RGBLIGHT_MODE_KNIGHT_end);
  116. #endif
  117. return false;
  118. case RGB_MODE_XMAS:
  119. #ifdef RGBLIGHT_EFFECT_CHRISTMAS
  120. rgblight_mode(RGBLIGHT_MODE_CHRISTMAS);
  121. #endif
  122. return false;
  123. case RGB_MODE_GRADIENT:
  124. #ifdef RGBLIGHT_EFFECT_STATIC_GRADIENT
  125. handleKeycodeRGBMode(RGBLIGHT_MODE_STATIC_GRADIENT, RGBLIGHT_MODE_STATIC_GRADIENT_end);
  126. #endif
  127. return false;
  128. case RGB_MODE_RGBTEST:
  129. #ifdef RGBLIGHT_EFFECT_RGB_TEST
  130. rgblight_mode(RGBLIGHT_MODE_RGB_TEST);
  131. #endif
  132. return false;
  133. }
  134. }
  135. return true;
  136. }