rgb_stuff.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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 && biton32(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 (biton32(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 && biton32(layer_state) == 0) {
  153. rgblight_sethsv_default_helper(light_index);
  154. }
  155. litup = light->enabled = false;
  156. }
  157. }
  158. }
  159. if (litup && biton32(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) { rgb_matrix_mode_noeeprom(RGB_MATRIX_TYPING_HEATMAP); }
  244. }
  245. #endif
  246. break;
  247. case RGB_MODE_FORWARD ... RGB_MODE_GRADIENT: // quantum_keycodes.h L400 for definitions
  248. if (record->event.pressed) {
  249. bool is_eeprom_updated = false;
  250. #ifdef RGBLIGHT_ENABLE
  251. // This disables layer indication, as it's assumed that if you're changing this ... you want that disabled
  252. if (userspace_config.rgb_layer_change) {
  253. userspace_config.rgb_layer_change = false;
  254. dprintf("rgblight layer change [EEPROM]: %u\n", userspace_config.rgb_layer_change);
  255. is_eeprom_updated = true;
  256. }
  257. #endif
  258. #if defined(RGB_MATRIX_ENABLE) && defined(RGB_MATRIX_FRAMEBUFFER_EFFECTS)
  259. if (userspace_config.rgb_matrix_idle_anim) {
  260. userspace_config.rgb_matrix_idle_anim = false;
  261. dprintf("RGB Matrix Idle Animation [EEPROM]: %u\n", userspace_config.rgb_matrix_idle_anim);
  262. is_eeprom_updated = true;
  263. }
  264. #endif
  265. if (is_eeprom_updated) { eeconfig_update_user(userspace_config.raw); }
  266. }
  267. break;
  268. }
  269. return true;
  270. }
  271. void keyboard_post_init_rgb(void) {
  272. #if defined(RGBLIGHT_ENABLE)
  273. # if defined(RGBLIGHT_STARTUP_ANIMATION)
  274. bool is_enabled = rgblight_config.enable;
  275. if (userspace_config.rgb_layer_change) {
  276. rgblight_enable_noeeprom();
  277. }
  278. if (rgblight_config.enable) {
  279. layer_state_set_user(layer_state);
  280. uint16_t old_hue = rgblight_config.hue;
  281. rgblight_mode_noeeprom(RGBLIGHT_MODE_STATIC_LIGHT);
  282. for (uint16_t i = 255; i > 0; i--) {
  283. rgblight_sethsv_noeeprom((i + old_hue) % 255, 255, 255);
  284. matrix_scan();
  285. wait_ms(10);
  286. }
  287. }
  288. if (!is_enabled) {
  289. rgblight_disable_noeeprom();
  290. }
  291. # endif
  292. layer_state_set_user(layer_state);
  293. #endif
  294. #if defined(RGB_MATRIX_ENABLE) && defined(RGB_MATRIX_FRAMEBUFFER_EFFECTS)
  295. if (userspace_config.rgb_matrix_idle_anim) {
  296. rgb_matrix_mode_noeeprom(RGB_MATRIX_REST_MODE);
  297. }
  298. #endif
  299. }
  300. void matrix_scan_rgb(void) {
  301. #ifdef RGBLIGHT_ENABLE
  302. # ifdef RGBLIGHT_TWINKLE
  303. scan_rgblight_fadeout();
  304. # endif // RGBLIGHT_ENABLE
  305. # ifdef INDICATOR_LIGHTS
  306. matrix_scan_indicator();
  307. # endif
  308. #endif
  309. #if defined(RGB_MATRIX_ENABLE) && defined(RGB_MATRIX_FRAMEBUFFER_EFFECTS)
  310. if (userspace_config.rgb_matrix_idle_anim && rgb_matrix_get_mode() == RGB_MATRIX_TYPING_HEATMAP && timer_elapsed32(hypno_timer) > 15000) {
  311. rgb_matrix_mode_noeeprom(RGB_MATRIX_REST_MODE);
  312. }
  313. #endif
  314. }
  315. layer_state_t layer_state_set_rgb(layer_state_t state) {
  316. #ifdef RGBLIGHT_ENABLE
  317. if (userspace_config.rgb_layer_change) {
  318. switch (biton32(state)) {
  319. case _MACROS:
  320. rgblight_sethsv_noeeprom_orange();
  321. userspace_config.is_overwatch ? rgblight_mode_noeeprom(RGBLIGHT_MODE_SNAKE + 2) : rgblight_mode_noeeprom(RGBLIGHT_MODE_SNAKE + 3);
  322. break;
  323. case _MEDIA:
  324. rgblight_sethsv_noeeprom_chartreuse();
  325. rgblight_mode_noeeprom(RGBLIGHT_MODE_KNIGHT + 1);
  326. break;
  327. case _GAMEPAD:
  328. rgblight_sethsv_noeeprom_orange();
  329. rgblight_mode_noeeprom(RGBLIGHT_MODE_SNAKE + 2);
  330. break;
  331. case _DIABLO:
  332. rgblight_sethsv_noeeprom_red();
  333. rgblight_mode_noeeprom(RGBLIGHT_MODE_BREATHING + 3);
  334. break;
  335. case _RAISE:
  336. rgblight_sethsv_noeeprom_yellow();
  337. rgblight_mode_noeeprom(RGBLIGHT_MODE_BREATHING + 3);
  338. break;
  339. case _LOWER:
  340. rgblight_sethsv_noeeprom_green();
  341. rgblight_mode_noeeprom(RGBLIGHT_MODE_BREATHING + 3);
  342. break;
  343. case _ADJUST:
  344. rgblight_sethsv_noeeprom_red();
  345. rgblight_mode_noeeprom(RGBLIGHT_MODE_KNIGHT + 2);
  346. break;
  347. default: // for any other layers, or the default layer
  348. switch (biton32(default_layer_state)) {
  349. case _COLEMAK:
  350. rgblight_sethsv_noeeprom_magenta();
  351. break;
  352. case _DVORAK:
  353. rgblight_sethsv_noeeprom_springgreen();
  354. break;
  355. case _WORKMAN:
  356. rgblight_sethsv_noeeprom_goldenrod();
  357. break;
  358. case _NORMAN:
  359. rgblight_sethsv_noeeprom_coral();
  360. break;
  361. case _MALTRON:
  362. rgblight_sethsv_noeeprom_yellow();
  363. break;
  364. case _EUCALYN:
  365. rgblight_sethsv_noeeprom_pink();
  366. break;
  367. case _CARPLAX:
  368. rgblight_sethsv_noeeprom_blue();
  369. break;
  370. default:
  371. rgblight_sethsv_noeeprom_cyan();
  372. break;
  373. }
  374. biton32(state) == _MODS ? rgblight_mode_noeeprom(RGBLIGHT_MODE_BREATHING) : rgblight_mode_noeeprom(RGBLIGHT_MODE_STATIC_LIGHT); // if _MODS layer is on, then breath to denote it
  375. break;
  376. }
  377. }
  378. #endif // RGBLIGHT_ENABLE
  379. return state;
  380. }
  381. #ifdef RGB_MATRIX_ENABLE
  382. # include "lib/lib8tion/lib8tion.h"
  383. extern led_config_t g_led_config;
  384. void rgb_matrix_layer_helper(uint8_t hue, uint8_t sat, uint8_t val, uint8_t mode, uint8_t speed, uint8_t led_type) {
  385. HSV hsv = {hue, sat, val};
  386. if (hsv.v > rgb_matrix_config.hsv.v) {
  387. hsv.v = rgb_matrix_config.hsv.v;
  388. }
  389. switch (mode) {
  390. case 1: // breathing
  391. {
  392. uint16_t time = scale16by8(g_rgb_counters.tick, speed / 8);
  393. hsv.v = scale8(abs8(sin8(time) - 128) * 2, hsv.v);
  394. RGB rgb = hsv_to_rgb(hsv);
  395. for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) {
  396. if (HAS_FLAGS(g_led_config.flags[i], led_type)) {
  397. rgb_matrix_set_color(i, rgb.r, rgb.g, rgb.b);
  398. }
  399. }
  400. break;
  401. }
  402. default: // Solid Color
  403. {
  404. RGB rgb = hsv_to_rgb(hsv);
  405. for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) {
  406. if (HAS_FLAGS(g_led_config.flags[i], led_type)) {
  407. rgb_matrix_set_color(i, rgb.r, rgb.g, rgb.b);
  408. }
  409. }
  410. break;
  411. }
  412. }
  413. }
  414. #endif