rgb_stuff.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. #include "drashna.h"
  2. #include "rgb_stuff.h"
  3. #include "eeprom.h"
  4. #if defined(RGBLIGHT_ENABLE)
  5. extern rgblight_config_t rgblight_config;
  6. bool has_initialized;
  7. void rgblight_sethsv_default_helper(uint8_t index) { rgblight_sethsv_at(rgblight_config.hue, rgblight_config.sat, rgblight_config.val, index); }
  8. #endif // RGBLIGHT_ENABLE
  9. #if defined(RGB_MATRIX_ENABLE)
  10. static uint32_t hypno_timer;
  11. # if defined(SPLIT_KEYBOARD) || defined(KEYBOARD_ergodox_ez) || defined(KEYBOARD_crkbd)
  12. # define RGB_MATRIX_REST_MODE RGB_MATRIX_CYCLE_OUT_IN_DUAL
  13. # else
  14. # define RGB_MATRIX_REST_MODE RGB_MATRIX_CYCLE_OUT_IN
  15. # endif
  16. #endif
  17. /* Custom indicators for modifiers.
  18. * This allows for certain lights to be lit up, based on what mods are active, giving some visual feedback.
  19. * This is especially useful for One Shot Mods, since it's not always obvious if they're still lit up.
  20. */
  21. #ifdef RGBLIGHT_ENABLE
  22. # ifdef INDICATOR_LIGHTS
  23. void set_rgb_indicators(uint8_t this_mod, uint8_t this_led, uint8_t this_osm) {
  24. if (userspace_config.rgb_layer_change && get_highest_layer(layer_state) == 0) {
  25. if ((this_mod | this_osm) & MOD_MASK_SHIFT || this_led & (1 << USB_LED_CAPS_LOCK)) {
  26. # ifdef SHFT_LED1
  27. rgblight_sethsv_at(120, 255, 255, SHFT_LED1);
  28. # endif // SHFT_LED1
  29. # ifdef SHFT_LED2
  30. rgblight_sethsv_at(120, 255, 255, SHFT_LED2);
  31. # endif // SHFT_LED2
  32. } else {
  33. # ifdef SHFT_LED1
  34. rgblight_sethsv_default_helper(SHFT_LED1);
  35. # endif // SHFT_LED1
  36. # ifdef SHFT_LED2
  37. rgblight_sethsv_default_helper(SHFT_LED2);
  38. # endif // SHFT_LED2
  39. }
  40. if ((this_mod | this_osm) & MOD_MASK_CTRL) {
  41. # ifdef CTRL_LED1
  42. rgblight_sethsv_at(0, 255, 255, CTRL_LED1);
  43. # endif // CTRL_LED1
  44. # ifdef CTRL_LED2
  45. rgblight_sethsv_at(0, 255, 255, CTRL_LED2);
  46. # endif // CTRL_LED2
  47. } else {
  48. # ifdef CTRL_LED1
  49. rgblight_sethsv_default_helper(CTRL_LED1);
  50. # endif // CTRL_LED1
  51. # ifdef CTRL_LED2
  52. rgblight_sethsv_default_helper(CTRL_LED2);
  53. # endif // CTRL_LED2
  54. }
  55. if ((this_mod | this_osm) & MOD_MASK_GUI) {
  56. # ifdef GUI_LED1
  57. rgblight_sethsv_at(51, 255, 255, GUI_LED1);
  58. # endif // GUI_LED1
  59. # ifdef GUI_LED2
  60. rgblight_sethsv_at(51, 255, 255, GUI_LED2);
  61. # endif // GUI_LED2
  62. } else {
  63. # ifdef GUI_LED1
  64. rgblight_sethsv_default_helper(GUI_LED1);
  65. # endif // GUI_LED1
  66. # ifdef GUI_LED2
  67. rgblight_sethsv_default_helper(GUI_LED2);
  68. # endif // GUI_LED2
  69. }
  70. if ((this_mod | this_osm) & MOD_MASK_ALT) {
  71. # ifdef ALT_LED1
  72. rgblight_sethsv_at(240, 255, 255, ALT_LED1);
  73. # endif // ALT_LED1
  74. # ifdef GUI_LED2
  75. rgblight_sethsv_at(240, 255, 255, ALT_LED2);
  76. # endif // GUI_LED2
  77. } else {
  78. # ifdef GUI_LED1
  79. rgblight_sethsv_default_helper(ALT_LED1);
  80. # endif // GUI_LED1
  81. # ifdef GUI_LED2
  82. rgblight_sethsv_default_helper(ALT_LED2);
  83. # endif // GUI_LED2
  84. }
  85. }
  86. }
  87. /* Function for the indicators */
  88. void matrix_scan_indicator(void) {
  89. if (has_initialized) {
  90. set_rgb_indicators(get_mods(), host_keyboard_leds(), get_oneshot_mods());
  91. }
  92. }
  93. # endif // INDICATOR_LIGHTS
  94. # ifdef RGBLIGHT_TWINKLE
  95. static rgblight_fadeout lights[RGBLED_NUM];
  96. __attribute__((weak)) bool rgblight_twinkle_is_led_used_keymap(uint8_t index) { return false; }
  97. /* This function checks for used LEDs. This way, collisions don't occur and cause weird rendering */
  98. bool rgblight_twinkle_is_led_used(uint8_t index) {
  99. switch (index) {
  100. # ifdef INDICATOR_LIGHTS
  101. # ifdef SHFT_LED1
  102. case SHFT_LED1:
  103. return true;
  104. # endif // SHFT_LED1
  105. # ifdef SHFT_LED2
  106. case SHFT_LED2:
  107. return true;
  108. # endif // SHFT_LED2
  109. # ifdef CTRL_LED1
  110. case CTRL_LED1:
  111. return true;
  112. # endif // CTRL_LED1
  113. # ifdef CTRL_LED2
  114. case CTRL_LED2:
  115. return true;
  116. # endif // CTRL_LED2
  117. # ifdef GUI_LED1
  118. case GUI_LED1:
  119. return true;
  120. # endif // GUI_LED1
  121. # ifdef GUI_LED2
  122. case GUI_LED2:
  123. return true;
  124. # endif // GUI_LED2
  125. # ifdef ALT_LED1
  126. case ALT_LED1:
  127. return true;
  128. # endif // ALT_LED1
  129. # ifdef ALT_LED2
  130. case ALT_LED2:
  131. return true;
  132. # endif // ALT_LED2
  133. # endif // INDICATOR_LIGHTS
  134. default:
  135. return rgblight_twinkle_is_led_used_keymap(index);
  136. }
  137. }
  138. /* Handler for fading/twinkling effect */
  139. void scan_rgblight_fadeout(void) { // Don't effing change this function .... rgblight_sethsv is supppppper intensive
  140. bool litup = false;
  141. for (uint8_t light_index = 0; light_index < RGBLED_NUM; ++light_index) {
  142. if (lights[light_index].enabled && timer_elapsed(lights[light_index].timer) > 10) {
  143. rgblight_fadeout *light = &lights[light_index];
  144. litup = true;
  145. if (light->life) {
  146. light->life -= 1;
  147. if (get_highest_layer(layer_state) == 0) {
  148. sethsv(light->hue + rand() % 0xF, 255, light->life, (LED_TYPE *)&led[light_index]);
  149. }
  150. light->timer = timer_read();
  151. } else {
  152. if (light->enabled && get_highest_layer(layer_state) == 0) {
  153. rgblight_sethsv_default_helper(light_index);
  154. }
  155. litup = light->enabled = false;
  156. }
  157. }
  158. }
  159. if (litup && get_highest_layer(layer_state) == 0) {
  160. rgblight_set();
  161. }
  162. }
  163. /* Triggers a LED to fade/twinkle.
  164. * This function handles the selection of the LED and prepres for it to be used.
  165. */
  166. void start_rgb_light(void) {
  167. uint8_t indices[RGBLED_NUM];
  168. uint8_t indices_count = 0;
  169. uint8_t min_life = 0xFF;
  170. uint8_t min_life_index = -1;
  171. for (uint8_t index = 0; index < RGBLED_NUM; ++index) {
  172. if (rgblight_twinkle_is_led_used(index)) {
  173. continue;
  174. }
  175. if (lights[index].enabled) {
  176. if (min_life_index == -1 || lights[index].life < min_life) {
  177. min_life = lights[index].life;
  178. min_life_index = index;
  179. }
  180. continue;
  181. }
  182. indices[indices_count] = index;
  183. ++indices_count;
  184. }
  185. uint8_t light_index;
  186. if (!indices_count) {
  187. light_index = min_life_index;
  188. } else {
  189. light_index = indices[rand() % indices_count];
  190. }
  191. rgblight_fadeout *light = &lights[light_index];
  192. light->enabled = true;
  193. light->timer = timer_read();
  194. light->life = 0xC0 + rand() % 0x40;
  195. light->hue = rgblight_config.hue + (rand() % 0xB4) - 0x54;
  196. rgblight_sethsv_at(light->hue, 255, light->life, light_index);
  197. }
  198. # endif
  199. #endif // RGBLIGHT_ENABLE
  200. bool process_record_user_rgb(uint16_t keycode, keyrecord_t *record) {
  201. uint16_t temp_keycode = keycode;
  202. // Filter out the actual keycode from MT and LT keys.
  203. if ((keycode >= QK_MOD_TAP && keycode <= QK_MOD_TAP_MAX) || (keycode >= QK_LAYER_TAP && keycode <= QK_LAYER_TAP_MAX)) {
  204. temp_keycode &= 0xFF;
  205. }
  206. #if defined(RGB_MATRIX_ENABLE) && defined(RGB_MATRIX_FRAMEBUFFER_EFFECTS)
  207. hypno_timer = timer_read32();
  208. if (userspace_config.rgb_matrix_idle_anim && rgb_matrix_get_mode() == RGB_MATRIX_REST_MODE) {
  209. rgb_matrix_mode_noeeprom(RGB_MATRIX_TYPING_HEATMAP);
  210. }
  211. #endif
  212. switch (temp_keycode) {
  213. #ifdef RGBLIGHT_TWINKLE
  214. case KC_A ... KC_SLASH:
  215. case KC_F1 ... KC_F12:
  216. case KC_INSERT ... KC_UP:
  217. case KC_KP_SLASH ... KC_KP_DOT:
  218. case KC_F13 ... KC_F24:
  219. case KC_AUDIO_MUTE ... KC_MEDIA_REWIND:
  220. if (record->event.pressed) {
  221. start_rgb_light();
  222. }
  223. break;
  224. #endif // RGBLIGHT_TWINKLE
  225. case KC_RGB_T: // This allows me to use underglow as layer indication, or as normal
  226. #if defined(RGBLIGHT_ENABLE) || defined(RGB_MATRIX_ENABLE)
  227. if (record->event.pressed) {
  228. userspace_config.rgb_layer_change ^= 1;
  229. dprintf("rgblight layer change [EEPROM]: %u\n", userspace_config.rgb_layer_change);
  230. eeconfig_update_user(userspace_config.raw);
  231. if (userspace_config.rgb_layer_change) {
  232. layer_state_set(layer_state); // This is needed to immediately set the layer color (looks better)
  233. }
  234. }
  235. #endif // RGBLIGHT_ENABLE
  236. break;
  237. case RGB_IDL: // This allows me to use underglow as layer indication, or as normal
  238. #if defined(RGB_MATRIX_ENABLE) && defined(RGB_MATRIX_FRAMEBUFFER_EFFECTS)
  239. if (record->event.pressed) {
  240. userspace_config.rgb_matrix_idle_anim ^= 1;
  241. dprintf("RGB Matrix Idle Animation [EEPROM]: %u\n", userspace_config.rgb_matrix_idle_anim);
  242. eeconfig_update_user(userspace_config.raw);
  243. if (userspace_config.rgb_matrix_idle_anim) {
  244. rgb_matrix_mode_noeeprom(RGB_MATRIX_TYPING_HEATMAP);
  245. }
  246. }
  247. #endif
  248. break;
  249. case RGB_MODE_FORWARD ... RGB_MODE_GRADIENT: // quantum_keycodes.h L400 for definitions
  250. if (record->event.pressed) {
  251. bool is_eeprom_updated = false;
  252. #ifdef RGBLIGHT_ENABLE
  253. // This disables layer indication, as it's assumed that if you're changing this ... you want that disabled
  254. if (userspace_config.rgb_layer_change) {
  255. userspace_config.rgb_layer_change = false;
  256. dprintf("rgblight layer change [EEPROM]: %u\n", userspace_config.rgb_layer_change);
  257. is_eeprom_updated = true;
  258. }
  259. #endif
  260. #if defined(RGB_MATRIX_ENABLE) && defined(RGB_MATRIX_FRAMEBUFFER_EFFECTS)
  261. if (userspace_config.rgb_matrix_idle_anim) {
  262. userspace_config.rgb_matrix_idle_anim = false;
  263. dprintf("RGB Matrix Idle Animation [EEPROM]: %u\n", userspace_config.rgb_matrix_idle_anim);
  264. is_eeprom_updated = true;
  265. }
  266. #endif
  267. if (is_eeprom_updated) {
  268. eeconfig_update_user(userspace_config.raw);
  269. }
  270. }
  271. break;
  272. }
  273. return true;
  274. }
  275. void keyboard_post_init_rgb(void) {
  276. #if defined(RGBLIGHT_ENABLE)
  277. # if defined(RGBLIGHT_STARTUP_ANIMATION)
  278. bool is_enabled = rgblight_config.enable;
  279. if (userspace_config.rgb_layer_change) {
  280. rgblight_enable_noeeprom();
  281. }
  282. if (rgblight_config.enable) {
  283. layer_state_set_user(layer_state);
  284. uint16_t old_hue = rgblight_config.hue;
  285. rgblight_mode_noeeprom(RGBLIGHT_MODE_STATIC_LIGHT);
  286. for (uint16_t i = 255; i > 0; i--) {
  287. rgblight_sethsv_noeeprom((i + old_hue) % 255, 255, 255);
  288. matrix_scan();
  289. wait_ms(10);
  290. }
  291. }
  292. if (!is_enabled) {
  293. rgblight_disable_noeeprom();
  294. }
  295. # endif
  296. layer_state_set_user(layer_state);
  297. #endif
  298. #if defined(RGB_MATRIX_ENABLE) && defined(RGB_MATRIX_FRAMEBUFFER_EFFECTS)
  299. if (userspace_config.rgb_matrix_idle_anim) {
  300. rgb_matrix_mode_noeeprom(RGB_MATRIX_REST_MODE);
  301. }
  302. #endif
  303. }
  304. void matrix_scan_rgb(void) {
  305. #ifdef RGBLIGHT_ENABLE
  306. # ifdef RGBLIGHT_TWINKLE
  307. scan_rgblight_fadeout();
  308. # endif // RGBLIGHT_ENABLE
  309. # ifdef INDICATOR_LIGHTS
  310. matrix_scan_indicator();
  311. # endif
  312. #endif
  313. #if defined(RGB_MATRIX_ENABLE) && defined(RGB_MATRIX_FRAMEBUFFER_EFFECTS)
  314. if (userspace_config.rgb_matrix_idle_anim && rgb_matrix_get_mode() == RGB_MATRIX_TYPING_HEATMAP && timer_elapsed32(hypno_timer) > 15000) {
  315. rgb_matrix_mode_noeeprom(RGB_MATRIX_REST_MODE);
  316. }
  317. #endif
  318. }
  319. #ifdef RGBLIGHT_ENABLE
  320. void rgblight_set_hsv_and_mode(uint8_t hue, uint8_t sat, uint8_t val, uint8_t mode) {
  321. rgblight_sethsv_noeeprom(hue, sat, val);
  322. wait_us(175); // Add a slight delay between color and mode to ensure it's processed correctly
  323. rgblight_mode_noeeprom(mode);
  324. }
  325. #endif
  326. layer_state_t layer_state_set_rgb(layer_state_t state) {
  327. #ifdef RGBLIGHT_ENABLE
  328. if (userspace_config.rgb_layer_change) {
  329. switch (get_highest_layer(state)) {
  330. case _MACROS:
  331. rgblight_set_hsv_and_mode(HSV_ORANGE, userspace_config.is_overwatch ? RGBLIGHT_MODE_SNAKE + 2 : RGBLIGHT_MODE_SNAKE + 3);
  332. break;
  333. case _MEDIA:
  334. rgblight_set_hsv_and_mode(HSV_CHARTREUSE, RGBLIGHT_MODE_KNIGHT + 1);
  335. break;
  336. case _GAMEPAD:
  337. rgblight_set_hsv_and_mode(HSV_ORANGE, RGBLIGHT_MODE_SNAKE + 2);
  338. break;
  339. case _DIABLO:
  340. rgblight_set_hsv_and_mode(HSV_RED, RGBLIGHT_MODE_BREATHING + 3);
  341. break;
  342. case _RAISE:
  343. rgblight_set_hsv_and_mode(HSV_YELLOW, RGBLIGHT_MODE_BREATHING + 3);
  344. break;
  345. case _LOWER:
  346. rgblight_set_hsv_and_mode(HSV_GREEN, RGBLIGHT_MODE_BREATHING + 3);
  347. break;
  348. case _ADJUST:
  349. rgblight_set_hsv_and_mode(HSV_RED, RGBLIGHT_MODE_KNIGHT + 2);
  350. break;
  351. default: // for any other layers, or the default layer
  352. {
  353. uint8_t mode = get_highest_layer(state) == _MODS ? RGBLIGHT_MODE_BREATHING : RGBLIGHT_MODE_STATIC_LIGHT;
  354. switch (get_highest_layer(default_layer_state)) {
  355. case _COLEMAK:
  356. rgblight_set_hsv_and_mode(HSV_MAGENTA, mode);
  357. break;
  358. case _DVORAK:
  359. rgblight_set_hsv_and_mode(HSV_SPRINGGREEN, mode);
  360. break;
  361. case _WORKMAN:
  362. rgblight_set_hsv_and_mode(HSV_GOLDENROD, mode);
  363. break;
  364. case _NORMAN:
  365. rgblight_set_hsv_and_mode(HSV_CORAL, mode);
  366. break;
  367. case _MALTRON:
  368. rgblight_set_hsv_and_mode(HSV_YELLOW, mode);
  369. break;
  370. case _EUCALYN:
  371. rgblight_set_hsv_and_mode(HSV_PINK, mode);
  372. break;
  373. case _CARPLAX:
  374. rgblight_set_hsv_and_mode(HSV_BLUE, mode);
  375. break;
  376. default:
  377. rgblight_set_hsv_and_mode(HSV_CYAN, mode);
  378. break;
  379. }
  380. break;
  381. }
  382. }
  383. }
  384. #endif // RGBLIGHT_ENABLE
  385. return state;
  386. }
  387. #ifdef RGB_MATRIX_ENABLE
  388. # include "lib/lib8tion/lib8tion.h"
  389. extern led_config_t g_led_config;
  390. void rgb_matrix_layer_helper(uint8_t hue, uint8_t sat, uint8_t val, uint8_t mode, uint8_t speed, uint8_t led_type) {
  391. HSV hsv = {hue, sat, val};
  392. if (hsv.v > rgb_matrix_config.hsv.v) {
  393. hsv.v = rgb_matrix_config.hsv.v;
  394. }
  395. switch (mode) {
  396. case 1: // breathing
  397. {
  398. uint16_t time = scale16by8(g_rgb_counters.tick, speed / 8);
  399. hsv.v = scale8(abs8(sin8(time) - 128) * 2, hsv.v);
  400. RGB rgb = hsv_to_rgb(hsv);
  401. for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) {
  402. if (HAS_FLAGS(g_led_config.flags[i], led_type)) {
  403. rgb_matrix_set_color(i, rgb.r, rgb.g, rgb.b);
  404. }
  405. }
  406. break;
  407. }
  408. default: // Solid Color
  409. {
  410. RGB rgb = hsv_to_rgb(hsv);
  411. for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) {
  412. if (HAS_FLAGS(g_led_config.flags[i], led_type)) {
  413. rgb_matrix_set_color(i, rgb.r, rgb.g, rgb.b);
  414. }
  415. }
  416. break;
  417. }
  418. }
  419. }
  420. #endif