rgb_stuff.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. #include "drashna.h"
  2. #include "rgb_stuff.h"
  3. #include "eeprom.h"
  4. extern rgblight_config_t rgblight_config;
  5. bool has_initialized;
  6. void rgblight_sethsv_default_helper(uint8_t index) { rgblight_sethsv_at(rgblight_config.hue, rgblight_config.sat, rgblight_config.val, index); }
  7. /* Custom indicators for modifiers.
  8. * This allows for certain lights to be lit up, based on what mods are active, giving some visual feedback.
  9. * This is especially useful for One Shot Mods, since it's not always obvious if they're still lit up.
  10. */
  11. #ifdef INDICATOR_LIGHTS
  12. void set_rgb_indicators(uint8_t this_mod, uint8_t this_led, uint8_t this_osm) {
  13. if (userspace_config.rgb_layer_change && get_highest_layer(layer_state) == 0) {
  14. if ((this_mod | this_osm) & MOD_MASK_SHIFT || this_led & (1 << USB_LED_CAPS_LOCK)) {
  15. # ifdef SHFT_LED1
  16. rgblight_sethsv_at(120, 255, 255, SHFT_LED1);
  17. # endif // SHFT_LED1
  18. # ifdef SHFT_LED2
  19. rgblight_sethsv_at(120, 255, 255, SHFT_LED2);
  20. # endif // SHFT_LED2
  21. } else {
  22. # ifdef SHFT_LED1
  23. rgblight_sethsv_default_helper(SHFT_LED1);
  24. # endif // SHFT_LED1
  25. # ifdef SHFT_LED2
  26. rgblight_sethsv_default_helper(SHFT_LED2);
  27. # endif // SHFT_LED2
  28. }
  29. if ((this_mod | this_osm) & MOD_MASK_CTRL) {
  30. # ifdef CTRL_LED1
  31. rgblight_sethsv_at(0, 255, 255, CTRL_LED1);
  32. # endif // CTRL_LED1
  33. # ifdef CTRL_LED2
  34. rgblight_sethsv_at(0, 255, 255, CTRL_LED2);
  35. # endif // CTRL_LED2
  36. } else {
  37. # ifdef CTRL_LED1
  38. rgblight_sethsv_default_helper(CTRL_LED1);
  39. # endif // CTRL_LED1
  40. # ifdef CTRL_LED2
  41. rgblight_sethsv_default_helper(CTRL_LED2);
  42. # endif // CTRL_LED2
  43. }
  44. if ((this_mod | this_osm) & MOD_MASK_GUI) {
  45. # ifdef GUI_LED1
  46. rgblight_sethsv_at(51, 255, 255, GUI_LED1);
  47. # endif // GUI_LED1
  48. # ifdef GUI_LED2
  49. rgblight_sethsv_at(51, 255, 255, GUI_LED2);
  50. # endif // GUI_LED2
  51. } else {
  52. # ifdef GUI_LED1
  53. rgblight_sethsv_default_helper(GUI_LED1);
  54. # endif // GUI_LED1
  55. # ifdef GUI_LED2
  56. rgblight_sethsv_default_helper(GUI_LED2);
  57. # endif // GUI_LED2
  58. }
  59. if ((this_mod | this_osm) & MOD_MASK_ALT) {
  60. # ifdef ALT_LED1
  61. rgblight_sethsv_at(240, 255, 255, ALT_LED1);
  62. # endif // ALT_LED1
  63. # ifdef GUI_LED2
  64. rgblight_sethsv_at(240, 255, 255, ALT_LED2);
  65. # endif // GUI_LED2
  66. } else {
  67. # ifdef GUI_LED1
  68. rgblight_sethsv_default_helper(ALT_LED1);
  69. # endif // GUI_LED1
  70. # ifdef GUI_LED2
  71. rgblight_sethsv_default_helper(ALT_LED2);
  72. # endif // GUI_LED2
  73. }
  74. }
  75. }
  76. /* Function for the indicators */
  77. void matrix_scan_indicator(void) {
  78. if (has_initialized) {
  79. set_rgb_indicators(get_mods(), host_keyboard_leds(), get_oneshot_mods());
  80. }
  81. }
  82. #endif // INDICATOR_LIGHTS
  83. #ifdef RGBLIGHT_TWINKLE
  84. static rgblight_fadeout lights[RGBLED_NUM];
  85. __attribute__((weak)) bool rgblight_twinkle_is_led_used_keymap(uint8_t index) { return false; }
  86. /* This function checks for used LEDs. This way, collisions don't occur and cause weird rendering */
  87. bool rgblight_twinkle_is_led_used(uint8_t index) {
  88. switch (index) {
  89. # ifdef INDICATOR_LIGHTS
  90. # ifdef SHFT_LED1
  91. case SHFT_LED1:
  92. return true;
  93. # endif // SHFT_LED1
  94. # ifdef SHFT_LED2
  95. case SHFT_LED2:
  96. return true;
  97. # endif // SHFT_LED2
  98. # ifdef CTRL_LED1
  99. case CTRL_LED1:
  100. return true;
  101. # endif // CTRL_LED1
  102. # ifdef CTRL_LED2
  103. case CTRL_LED2:
  104. return true;
  105. # endif // CTRL_LED2
  106. # ifdef GUI_LED1
  107. case GUI_LED1:
  108. return true;
  109. # endif // GUI_LED1
  110. # ifdef GUI_LED2
  111. case GUI_LED2:
  112. return true;
  113. # endif // GUI_LED2
  114. # ifdef ALT_LED1
  115. case ALT_LED1:
  116. return true;
  117. # endif // ALT_LED1
  118. # ifdef ALT_LED2
  119. case ALT_LED2:
  120. return true;
  121. # endif // ALT_LED2
  122. # endif // INDICATOR_LIGHTS
  123. default:
  124. return rgblight_twinkle_is_led_used_keymap(index);
  125. }
  126. }
  127. /* Handler for fading/twinkling effect */
  128. void scan_rgblight_fadeout(void) { // Don't effing change this function .... rgblight_sethsv is supppppper intensive
  129. bool litup = false;
  130. for (uint8_t light_index = 0; light_index < RGBLED_NUM; ++light_index) {
  131. if (lights[light_index].enabled && timer_elapsed(lights[light_index].timer) > 10) {
  132. rgblight_fadeout *light = &lights[light_index];
  133. litup = true;
  134. if (light->life) {
  135. light->life -= 1;
  136. if (get_highest_layer(layer_state) == 0) {
  137. sethsv(light->hue + rand() % 0xF, 255, light->life, (LED_TYPE *)&led[light_index]);
  138. }
  139. light->timer = timer_read();
  140. } else {
  141. if (light->enabled && get_highest_layer(layer_state) == 0) {
  142. rgblight_sethsv_default_helper(light_index);
  143. }
  144. litup = light->enabled = false;
  145. }
  146. }
  147. }
  148. if (litup && get_highest_layer(layer_state) == 0) {
  149. rgblight_set();
  150. }
  151. }
  152. /* Triggers a LED to fade/twinkle.
  153. * This function handles the selection of the LED and prepres for it to be used.
  154. */
  155. void start_rgb_light(void) {
  156. uint8_t indices[RGBLED_NUM];
  157. uint8_t indices_count = 0;
  158. uint8_t min_life = 0xFF;
  159. uint8_t min_life_index = -1;
  160. for (uint8_t index = 0; index < RGBLED_NUM; ++index) {
  161. if (rgblight_twinkle_is_led_used(index)) {
  162. continue;
  163. }
  164. if (lights[index].enabled) {
  165. if (min_life_index == -1 || lights[index].life < min_life) {
  166. min_life = lights[index].life;
  167. min_life_index = index;
  168. }
  169. continue;
  170. }
  171. indices[indices_count] = index;
  172. ++indices_count;
  173. }
  174. uint8_t light_index;
  175. if (!indices_count) {
  176. light_index = min_life_index;
  177. } else {
  178. light_index = indices[rand() % indices_count];
  179. }
  180. rgblight_fadeout *light = &lights[light_index];
  181. light->enabled = true;
  182. light->timer = timer_read();
  183. light->life = 0xC0 + rand() % 0x40;
  184. light->hue = rgblight_config.hue + (rand() % 0xB4) - 0x54;
  185. rgblight_sethsv_at(light->hue, 255, light->life, light_index);
  186. }
  187. #endif
  188. bool process_record_user_rgb_light(uint16_t keycode, keyrecord_t *record) {
  189. uint16_t temp_keycode = keycode;
  190. // Filter out the actual keycode from MT and LT keys.
  191. if ((keycode >= QK_MOD_TAP && keycode <= QK_MOD_TAP_MAX) || (keycode >= QK_LAYER_TAP && keycode <= QK_LAYER_TAP_MAX)) {
  192. temp_keycode &= 0xFF;
  193. }
  194. switch (temp_keycode) {
  195. #ifdef RGBLIGHT_TWINKLE
  196. case KC_A ... KC_SLASH:
  197. case KC_F1 ... KC_F12:
  198. case KC_INSERT ... KC_UP:
  199. case KC_KP_SLASH ... KC_KP_DOT:
  200. case KC_F13 ... KC_F24:
  201. case KC_AUDIO_MUTE ... KC_MEDIA_REWIND:
  202. if (record->event.pressed) {
  203. start_rgb_light();
  204. }
  205. break;
  206. #endif // RGBLIGHT_TWINKLE
  207. }
  208. return true;
  209. }
  210. void keyboard_post_init_rgb_light(void) {
  211. #if defined(RGBLIGHT_STARTUP_ANIMATION)
  212. bool is_enabled = rgblight_config.enable;
  213. if (userspace_config.rgb_layer_change) {
  214. rgblight_enable_noeeprom();
  215. }
  216. if (rgblight_config.enable) {
  217. layer_state_set_rgb_light(layer_state);
  218. uint16_t old_hue = rgblight_config.hue;
  219. rgblight_mode_noeeprom(RGBLIGHT_MODE_STATIC_LIGHT);
  220. for (uint16_t i = 255; i > 0; i--) {
  221. rgblight_sethsv_noeeprom((i + old_hue) % 255, 255, 255);
  222. matrix_scan();
  223. wait_ms(10);
  224. }
  225. }
  226. if (!is_enabled) {
  227. rgblight_disable_noeeprom();
  228. }
  229. #endif
  230. layer_state_set_rgb_light(layer_state);
  231. }
  232. void matrix_scan_rgb_light(void) {
  233. #ifdef RGBLIGHT_ENABLE
  234. # ifdef RGBLIGHT_TWINKLE
  235. scan_rgblight_fadeout();
  236. # endif // RGBLIGHT_ENABLE
  237. # ifdef INDICATOR_LIGHTS
  238. matrix_scan_indicator();
  239. # endif
  240. #endif
  241. }
  242. void rgblight_set_hsv_and_mode(uint8_t hue, uint8_t sat, uint8_t val, uint8_t mode) {
  243. rgblight_sethsv_noeeprom(hue, sat, val);
  244. wait_us(175); // Add a slight delay between color and mode to ensure it's processed correctly
  245. rgblight_mode_noeeprom(mode);
  246. }
  247. layer_state_t layer_state_set_rgb_light(layer_state_t state) {
  248. #ifdef RGBLIGHT_ENABLE
  249. if (userspace_config.rgb_layer_change) {
  250. switch (get_highest_layer(state)) {
  251. case _MACROS:
  252. rgblight_set_hsv_and_mode(HSV_ORANGE, userspace_config.is_overwatch ? RGBLIGHT_MODE_SNAKE + 2 : RGBLIGHT_MODE_SNAKE + 3);
  253. break;
  254. case _MEDIA:
  255. rgblight_set_hsv_and_mode(HSV_CHARTREUSE, RGBLIGHT_MODE_KNIGHT + 1);
  256. break;
  257. case _GAMEPAD:
  258. rgblight_set_hsv_and_mode(HSV_ORANGE, RGBLIGHT_MODE_SNAKE + 2);
  259. break;
  260. case _DIABLO:
  261. rgblight_set_hsv_and_mode(HSV_RED, RGBLIGHT_MODE_BREATHING + 3);
  262. break;
  263. case _RAISE:
  264. rgblight_set_hsv_and_mode(HSV_YELLOW, RGBLIGHT_MODE_BREATHING + 3);
  265. break;
  266. case _LOWER:
  267. rgblight_set_hsv_and_mode(HSV_GREEN, RGBLIGHT_MODE_BREATHING + 3);
  268. break;
  269. case _ADJUST:
  270. rgblight_set_hsv_and_mode(HSV_RED, RGBLIGHT_MODE_KNIGHT + 2);
  271. break;
  272. default: // for any other layers, or the default layer
  273. {
  274. uint8_t mode = get_highest_layer(state) == _MODS ? RGBLIGHT_MODE_BREATHING : RGBLIGHT_MODE_STATIC_LIGHT;
  275. switch (get_highest_layer(default_layer_state)) {
  276. case _COLEMAK:
  277. rgblight_set_hsv_and_mode(HSV_MAGENTA, mode);
  278. break;
  279. case _DVORAK:
  280. rgblight_set_hsv_and_mode(HSV_SPRINGGREEN, mode);
  281. break;
  282. case _WORKMAN:
  283. rgblight_set_hsv_and_mode(HSV_GOLDENROD, mode);
  284. break;
  285. case _NORMAN:
  286. rgblight_set_hsv_and_mode(HSV_CORAL, mode);
  287. break;
  288. case _MALTRON:
  289. rgblight_set_hsv_and_mode(HSV_YELLOW, mode);
  290. break;
  291. case _EUCALYN:
  292. rgblight_set_hsv_and_mode(HSV_PINK, mode);
  293. break;
  294. case _CARPLAX:
  295. rgblight_set_hsv_and_mode(HSV_BLUE, mode);
  296. break;
  297. default:
  298. rgblight_set_hsv_and_mode(HSV_CYAN, mode);
  299. break;
  300. }
  301. break;
  302. }
  303. }
  304. }
  305. #endif // RGBLIGHT_ENABLE
  306. return state;
  307. }