rgb_stuff.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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. #endif
  8. #ifdef RGBLIGHT_ENABLE
  9. void rgblight_sethsv_default_helper(uint8_t index) { rgblight_sethsv_at(rgblight_config.hue, rgblight_config.sat, rgblight_config.val, index); }
  10. #endif // RGBLIGHT_ENABLE
  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 && biton32(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. void matrix_scan_indicator(void) {
  77. if (has_initialized) {
  78. set_rgb_indicators(get_mods(), host_keyboard_leds(), get_oneshot_mods());
  79. }
  80. }
  81. #endif // INDICATOR_LIGHTS
  82. #ifdef RGBLIGHT_TWINKLE
  83. static rgblight_fadeout lights[RGBLED_NUM];
  84. __attribute__((weak)) bool rgblight_twinkle_is_led_used_keymap(uint8_t index) { return false; }
  85. bool rgblight_twinkle_is_led_used(uint8_t index) {
  86. switch (index) {
  87. # ifdef INDICATOR_LIGHTS
  88. # ifdef SHFT_LED1
  89. case SHFT_LED1:
  90. return true;
  91. # endif // SHFT_LED1
  92. # ifdef SHFT_LED2
  93. case SHFT_LED2:
  94. return true;
  95. # endif // SHFT_LED2
  96. # ifdef CTRL_LED1
  97. case CTRL_LED1:
  98. return true;
  99. # endif // CTRL_LED1
  100. # ifdef CTRL_LED2
  101. case CTRL_LED2:
  102. return true;
  103. # endif // CTRL_LED2
  104. # ifdef GUI_LED1
  105. case GUI_LED1:
  106. return true;
  107. # endif // GUI_LED1
  108. # ifdef GUI_LED2
  109. case GUI_LED2:
  110. return true;
  111. # endif // GUI_LED2
  112. # ifdef ALT_LED1
  113. case ALT_LED1:
  114. return true;
  115. # endif // ALT_LED1
  116. # ifdef ALT_LED2
  117. case ALT_LED2:
  118. return true;
  119. # endif // ALT_LED2
  120. # endif // INDICATOR_LIGHTS
  121. default:
  122. return rgblight_twinkle_is_led_used_keymap(index);
  123. }
  124. }
  125. void scan_rgblight_fadeout(void) { // Don't effing change this function .... rgblight_sethsv is supppppper intensive
  126. bool litup = false;
  127. for (uint8_t light_index = 0; light_index < RGBLED_NUM; ++light_index) {
  128. if (lights[light_index].enabled && timer_elapsed(lights[light_index].timer) > 10) {
  129. rgblight_fadeout *light = &lights[light_index];
  130. litup = true;
  131. if (light->life) {
  132. light->life -= 1;
  133. if (biton32(layer_state) == 0) {
  134. sethsv(light->hue + rand() % 0xF, 255, light->life, (LED_TYPE *)&led[light_index]);
  135. }
  136. light->timer = timer_read();
  137. } else {
  138. if (light->enabled && biton32(layer_state) == 0) {
  139. rgblight_sethsv_default_helper(light_index);
  140. }
  141. litup = light->enabled = false;
  142. }
  143. }
  144. }
  145. if (litup && biton32(layer_state) == 0) {
  146. rgblight_set();
  147. }
  148. }
  149. void start_rgb_light(void) {
  150. uint8_t indices[RGBLED_NUM];
  151. uint8_t indices_count = 0;
  152. uint8_t min_life = 0xFF;
  153. uint8_t min_life_index = -1;
  154. for (uint8_t index = 0; index < RGBLED_NUM; ++index) {
  155. if (rgblight_twinkle_is_led_used(index)) {
  156. continue;
  157. }
  158. if (lights[index].enabled) {
  159. if (min_life_index == -1 || lights[index].life < min_life) {
  160. min_life = lights[index].life;
  161. min_life_index = index;
  162. }
  163. continue;
  164. }
  165. indices[indices_count] = index;
  166. ++indices_count;
  167. }
  168. uint8_t light_index;
  169. if (!indices_count) {
  170. light_index = min_life_index;
  171. } else {
  172. light_index = indices[rand() % indices_count];
  173. }
  174. rgblight_fadeout *light = &lights[light_index];
  175. light->enabled = true;
  176. light->timer = timer_read();
  177. light->life = 0xC0 + rand() % 0x40;
  178. light->hue = rgblight_config.hue + (rand() % 0xB4) - 0x54;
  179. rgblight_sethsv_at(light->hue, 255, light->life, light_index);
  180. }
  181. #endif
  182. bool process_record_user_rgb(uint16_t keycode, keyrecord_t *record) {
  183. if ((keycode >= QK_MOD_TAP && keycode <= QK_MOD_TAP_MAX) || (keycode >= QK_LAYER_TAP && keycode <= QK_LAYER_TAP_MAX)) {
  184. keycode = keycode & 0xFF;
  185. }
  186. switch (keycode) {
  187. #ifdef RGBLIGHT_TWINKLE
  188. case KC_A ... KC_SLASH:
  189. case KC_F1 ... KC_F12:
  190. case KC_INSERT ... KC_UP:
  191. case KC_KP_SLASH ... KC_KP_DOT:
  192. case KC_F13 ... KC_F24:
  193. case KC_AUDIO_MUTE ... KC_MEDIA_REWIND:
  194. if (record->event.pressed) {
  195. start_rgb_light();
  196. }
  197. return true;
  198. break;
  199. #endif // RGBLIGHT_TWINKLE
  200. case KC_RGB_T: // This allows me to use underglow as layer indication, or as normal
  201. #if defined(RGBLIGHT_ENABLE) || defined(RGB_MATRIX_ENABLE)
  202. if (record->event.pressed) {
  203. userspace_config.rgb_layer_change ^= 1;
  204. xprintf("rgblight layer change [EEPROM]: %u\n", userspace_config.rgb_layer_change);
  205. eeconfig_update_user(userspace_config.raw);
  206. if (userspace_config.rgb_layer_change) {
  207. layer_state_set(layer_state); // This is needed to immediately set the layer color (looks better)
  208. }
  209. }
  210. #endif // RGBLIGHT_ENABLE
  211. return false;
  212. break;
  213. #ifdef RGBLIGHT_ENABLE
  214. case RGB_MODE_FORWARD ... RGB_MODE_GRADIENT: // quantum_keycodes.h L400 for definitions
  215. if (record->event.pressed) { // This disables layer indication, as it's assumed that if you're changing this ... you want that disabled
  216. if (userspace_config.rgb_layer_change) {
  217. userspace_config.rgb_layer_change = false;
  218. xprintf("rgblight layer change [EEPROM]: %u\n", userspace_config.rgb_layer_change);
  219. eeconfig_update_user(userspace_config.raw);
  220. }
  221. }
  222. return true;
  223. break;
  224. #endif // RGBLIGHT_ENABLE
  225. }
  226. return true;
  227. }
  228. void keyboard_post_init_rgb(void) {
  229. #if defined(RGBLIGHT_ENABLE) && defined(RGBLIGHT_STARTUP_ANIMATION)
  230. bool is_enabled = rgblight_config.enable;
  231. if (userspace_config.rgb_layer_change) {
  232. rgblight_enable_noeeprom();
  233. }
  234. if (rgblight_config.enable) {
  235. layer_state_set_user(layer_state);
  236. uint16_t old_hue = rgblight_config.hue;
  237. rgblight_mode_noeeprom(RGBLIGHT_MODE_STATIC_LIGHT);
  238. for (uint16_t i = 255; i > 0; i--) {
  239. rgblight_sethsv_noeeprom((i + old_hue) % 255, 255, 255);
  240. matrix_scan();
  241. wait_ms(10);
  242. }
  243. }
  244. if (!is_enabled) {
  245. rgblight_disable_noeeprom();
  246. }
  247. #endif
  248. layer_state_set_user(layer_state);
  249. }
  250. void matrix_scan_rgb(void) {
  251. #ifdef RGBLIGHT_TWINKLE
  252. scan_rgblight_fadeout();
  253. #endif // RGBLIGHT_ENABLE
  254. #ifdef INDICATOR_LIGHTS
  255. matrix_scan_indicator();
  256. #endif
  257. }
  258. layer_state_t layer_state_set_rgb(layer_state_t state) {
  259. #ifdef RGBLIGHT_ENABLE
  260. if (userspace_config.rgb_layer_change) {
  261. switch (biton32(state)) {
  262. case _MACROS:
  263. rgblight_sethsv_noeeprom_orange();
  264. userspace_config.is_overwatch ? rgblight_mode_noeeprom(RGBLIGHT_MODE_SNAKE + 2) : rgblight_mode_noeeprom(RGBLIGHT_MODE_SNAKE + 3);
  265. break;
  266. case _MEDIA:
  267. rgblight_sethsv_noeeprom_chartreuse();
  268. rgblight_mode_noeeprom(RGBLIGHT_MODE_KNIGHT + 1);
  269. break;
  270. case _GAMEPAD:
  271. rgblight_sethsv_noeeprom_orange();
  272. rgblight_mode_noeeprom(RGBLIGHT_MODE_SNAKE + 2);
  273. break;
  274. case _DIABLO:
  275. rgblight_sethsv_noeeprom_red();
  276. rgblight_mode_noeeprom(RGBLIGHT_MODE_BREATHING + 3);
  277. break;
  278. case _RAISE:
  279. rgblight_sethsv_noeeprom_yellow();
  280. rgblight_mode_noeeprom(RGBLIGHT_MODE_BREATHING + 3);
  281. break;
  282. case _LOWER:
  283. rgblight_sethsv_noeeprom_green();
  284. rgblight_mode_noeeprom(RGBLIGHT_MODE_BREATHING + 3);
  285. break;
  286. case _ADJUST:
  287. rgblight_sethsv_noeeprom_red();
  288. rgblight_mode_noeeprom(RGBLIGHT_MODE_KNIGHT + 2);
  289. break;
  290. default: // for any other layers, or the default layer
  291. switch (biton32(default_layer_state)) {
  292. case _COLEMAK:
  293. rgblight_sethsv_noeeprom_magenta();
  294. break;
  295. case _DVORAK:
  296. rgblight_sethsv_noeeprom_springgreen();
  297. break;
  298. case _WORKMAN:
  299. rgblight_sethsv_noeeprom_goldenrod();
  300. break;
  301. case _NORMAN:
  302. rgblight_sethsv_noeeprom_coral();
  303. break;
  304. case _MALTRON:
  305. rgblight_sethsv_noeeprom_yellow();
  306. break;
  307. case _EUCALYN:
  308. rgblight_sethsv_noeeprom_pink();
  309. break;
  310. case _CARPLAX:
  311. rgblight_sethsv_noeeprom_blue();
  312. break;
  313. default:
  314. rgblight_sethsv_noeeprom_cyan();
  315. break;
  316. }
  317. 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
  318. break;
  319. }
  320. }
  321. #endif // RGBLIGHT_ENABLE
  322. return state;
  323. }
  324. #ifdef RGB_MATRIX_ENABLE
  325. # include "lib/lib8tion/lib8tion.h"
  326. extern led_config_t g_led_config;
  327. void rgb_matrix_layer_helper(uint8_t hue, uint8_t sat, uint8_t val, uint8_t mode, uint8_t speed, uint8_t led_type) {
  328. HSV hsv = {hue, sat, val};
  329. if (hsv.v > rgb_matrix_config.hsv.v) {
  330. hsv.v = rgb_matrix_config.hsv.v;
  331. }
  332. switch (mode) {
  333. case 1: // breathing
  334. {
  335. uint16_t time = scale16by8(g_rgb_counters.tick, speed / 8);
  336. hsv.v = scale8(abs8(sin8(time) - 128) * 2, hsv.v);
  337. RGB rgb = hsv_to_rgb(hsv);
  338. for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) {
  339. if (HAS_FLAGS(g_led_config.flags[i], led_type)) {
  340. rgb_matrix_set_color(i, rgb.r, rgb.g, rgb.b);
  341. }
  342. }
  343. break;
  344. }
  345. default: // Solid Color
  346. {
  347. RGB rgb = hsv_to_rgb(hsv);
  348. for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) {
  349. if (HAS_FLAGS(g_led_config.flags[i], led_type)) {
  350. rgb_matrix_set_color(i, rgb.r, rgb.g, rgb.b);
  351. }
  352. }
  353. break;
  354. }
  355. }
  356. }
  357. #endif