2
0

rgb_stuff.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. #include "drashna.h"
  2. #include "rgb_stuff.h"
  3. #include "eeprom.h"
  4. extern rgblight_config_t rgblight_config;
  5. extern userspace_config_t userspace_config;
  6. #ifdef RGBLIGHT_ENABLE
  7. void rgblight_sethsv_default_helper(uint8_t index) {
  8. rgblight_sethsv_at(rgblight_config.hue, rgblight_config.sat, rgblight_config.val, index);
  9. }
  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 & MODS_SHIFT_MASK || this_led & (1<<USB_LED_CAPS_LOCK) || this_osm & MODS_SHIFT_MASK) {
  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 & MODS_CTRL_MASK || this_osm & MODS_CTRL_MASK) {
  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 & MODS_GUI_MASK || this_osm & MODS_GUI_MASK) {
  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 & MODS_ALT_MASK || this_osm & MODS_ALT_MASK) {
  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. set_rgb_indicators(get_mods(), host_keyboard_leds(), get_oneshot_mods());
  78. }
  79. #endif //INDICATOR_LIGHTS
  80. #ifdef RGBLIGHT_TWINKLE
  81. static rgblight_fadeout lights[RGBLED_NUM];
  82. __attribute__ ((weak))
  83. bool rgblight_twinkle_is_led_used_keymap(uint8_t index) { return false; }
  84. bool rgblight_twinkle_is_led_used(uint8_t index) {
  85. switch (index) {
  86. #ifdef INDICATOR_LIGHTS
  87. #ifdef SHFT_LED1
  88. case SHFT_LED1:
  89. return true;
  90. #endif //SHFT_LED1
  91. #ifdef SHFT_LED2
  92. case SHFT_LED2:
  93. return true;
  94. #endif //SHFT_LED2
  95. #ifdef CTRL_LED1
  96. case CTRL_LED1:
  97. return true;
  98. #endif //CTRL_LED1
  99. #ifdef CTRL_LED2
  100. case CTRL_LED2:
  101. return true;
  102. #endif //CTRL_LED2
  103. #ifdef GUI_LED1
  104. case GUI_LED1:
  105. return true;
  106. #endif //GUI_LED1
  107. #ifdef GUI_LED2
  108. case GUI_LED2:
  109. return true;
  110. #endif //GUI_LED2
  111. #ifdef ALT_LED1
  112. case ALT_LED1:
  113. return true;
  114. #endif //ALT_LED1
  115. #ifdef ALT_LED2
  116. case ALT_LED2:
  117. return true;
  118. #endif //ALT_LED2
  119. #endif //INDICATOR_LIGHTS
  120. default:
  121. return rgblight_twinkle_is_led_used_keymap(index);
  122. }
  123. }
  124. void scan_rgblight_fadeout(void) { // Don't effing change this function .... rgblight_sethsv is supppppper intensive
  125. bool litup = false;
  126. for (uint8_t light_index = 0 ; light_index < RGBLED_NUM ; ++light_index ) {
  127. if (lights[light_index].enabled && timer_elapsed(lights[light_index].timer) > 10) {
  128. rgblight_fadeout *light = &lights[light_index];
  129. litup = true;
  130. if (light->life) {
  131. light->life -= 1;
  132. if (biton32(layer_state) == 0) {
  133. sethsv(light->hue + rand() % 0xF, 255, light->life, (LED_TYPE *)&led[light_index]);
  134. }
  135. light->timer = timer_read();
  136. }
  137. else {
  138. if (light->enabled && biton32(layer_state) == 0) { rgblight_sethsv_default_helper(light_index); }
  139. litup = light->enabled = false;
  140. }
  141. }
  142. }
  143. if (litup && biton32(layer_state) == 0) {
  144. rgblight_set();
  145. }
  146. }
  147. void start_rgb_light(void) {
  148. uint8_t indices[RGBLED_NUM];
  149. uint8_t indices_count = 0;
  150. uint8_t min_life = 0xFF;
  151. uint8_t min_life_index = -1;
  152. for (uint8_t index = 0 ; index < RGBLED_NUM ; ++index ) {
  153. if (rgblight_twinkle_is_led_used(index)) { continue; }
  154. if (lights[index].enabled) {
  155. if (min_life_index == -1 ||
  156. lights[index].life < min_life)
  157. {
  158. min_life = lights[index].life;
  159. min_life_index = index;
  160. }
  161. continue;
  162. }
  163. indices[indices_count] = index;
  164. ++indices_count;
  165. }
  166. uint8_t light_index;
  167. if (!indices_count) {
  168. light_index = min_life_index;
  169. }
  170. else {
  171. light_index = indices[rand() % indices_count];
  172. }
  173. rgblight_fadeout *light = &lights[light_index];
  174. light->enabled = true;
  175. light->timer = timer_read();
  176. light->life = 0xC0 + rand() % 0x40;
  177. light->hue = rgblight_config.hue + (rand() % 0xB4) - 0x54;
  178. rgblight_sethsv_at(light->hue, 255, light->life, light_index);
  179. }
  180. #endif
  181. bool process_record_user_rgb(uint16_t keycode, keyrecord_t *record) {
  182. switch (keycode) {
  183. #ifdef RGBLIGHT_TWINKLE
  184. case KC_A ... KC_SLASH:
  185. case KC_F1 ... KC_F12:
  186. case KC_INSERT ... KC_UP:
  187. case KC_KP_SLASH ... KC_KP_DOT:
  188. case KC_F13 ... KC_F24:
  189. case KC_AUDIO_MUTE ... KC_MEDIA_REWIND:
  190. if (record->event.pressed) { start_rgb_light(); }
  191. return true; break;
  192. #endif // RGBLIGHT_TWINKLE
  193. case KC_RGB_T: // This allows me to use underglow as layer indication, or as normal
  194. #ifdef RGBLIGHT_ENABLE
  195. if (record->event.pressed) {
  196. userspace_config.rgb_layer_change ^= 1;
  197. xprintf("rgblight layer change [EEPROM]: %u\n", userspace_config.rgb_layer_change);
  198. eeconfig_update_user(userspace_config.raw);
  199. if (userspace_config.rgb_layer_change) {
  200. layer_state_set(layer_state); // This is needed to immediately set the layer color (looks better)
  201. }
  202. }
  203. #endif // RGBLIGHT_ENABLE
  204. return false; break;
  205. #ifdef RGBLIGHT_ENABLE
  206. case RGB_MODE_FORWARD ... RGB_MODE_GRADIENT: // quantum_keycodes.h L400 for definitions
  207. if (record->event.pressed) { //This disables layer indication, as it's assumed that if you're changing this ... you want that disabled
  208. if (userspace_config.rgb_layer_change) {
  209. userspace_config.rgb_layer_change = false;
  210. xprintf("rgblight layer change [EEPROM]: %u\n", userspace_config.rgb_layer_change);
  211. eeconfig_update_user(userspace_config.raw);
  212. }
  213. }
  214. return true; break;
  215. #endif // RGBLIGHT_ENABLE
  216. }
  217. return true;
  218. }
  219. void matrix_init_rgb(void) {
  220. if (userspace_config.rgb_layer_change) {
  221. rgblight_init();
  222. rgblight_enable_noeeprom();
  223. switch (biton32(eeconfig_read_default_layer())) {
  224. case _COLEMAK:
  225. rgblight_sethsv_noeeprom_magenta(); break;
  226. case _DVORAK:
  227. rgblight_sethsv_noeeprom_springgreen(); break;
  228. case _WORKMAN:
  229. rgblight_sethsv_noeeprom_goldenrod(); break;
  230. default:
  231. rgblight_sethsv_noeeprom_cyan(); break;
  232. }
  233. rgblight_mode(RGBLIGHT_MODE_STATIC_LIGHT);
  234. }
  235. }
  236. void matrix_scan_rgb(void) {
  237. #ifdef RGBLIGHT_TWINKLE
  238. scan_rgblight_fadeout();
  239. #endif // RGBLIGHT_ENABLE
  240. #ifdef INDICATOR_LIGHTS
  241. matrix_scan_indicator();
  242. #endif
  243. }
  244. uint32_t layer_state_set_rgb(uint32_t state) {
  245. #ifdef RGBLIGHT_ENABLE
  246. static bool has_ran;
  247. if (userspace_config.rgb_layer_change) {
  248. switch (biton32(state)) {
  249. case _MACROS:
  250. rgblight_sethsv_noeeprom_orange();
  251. userspace_config.is_overwatch ? rgblight_effect_snake(RGBLIGHT_MODE_SNAKE + 2) : rgblight_effect_snake(RGBLIGHT_MODE_SNAKE + 3);
  252. break;
  253. case _MEDIA:
  254. rgblight_sethsv_noeeprom_chartreuse();
  255. rgblight_mode_noeeprom(RGBLIGHT_MODE_KNIGHT + 1);
  256. break;
  257. case _GAMEPAD:
  258. rgblight_sethsv_noeeprom_orange();
  259. rgblight_mode_noeeprom(RGBLIGHT_MODE_SNAKE + 2);
  260. break;
  261. case _DIABLO:
  262. rgblight_sethsv_noeeprom_red();
  263. rgblight_mode_noeeprom(RGBLIGHT_MODE_BREATHING + 3);
  264. break;
  265. case _RAISE:
  266. rgblight_sethsv_noeeprom_yellow();
  267. rgblight_mode_noeeprom(RGBLIGHT_MODE_BREATHING + 3);
  268. break;
  269. case _LOWER:
  270. rgblight_sethsv_noeeprom_green();
  271. rgblight_mode_noeeprom(RGBLIGHT_MODE_BREATHING + 3);
  272. break;
  273. case _ADJUST:
  274. rgblight_sethsv_noeeprom_red();
  275. rgblight_mode_noeeprom(RGBLIGHT_MODE_KNIGHT + 2);
  276. break;
  277. default: // for any other layers, or the default layer
  278. switch (biton32(default_layer_state)) {
  279. case _COLEMAK:
  280. rgblight_sethsv_noeeprom_magenta(); break;
  281. case _DVORAK:
  282. rgblight_sethsv_noeeprom_springgreen(); break;
  283. case _WORKMAN:
  284. rgblight_sethsv_noeeprom_goldenrod(); break;
  285. default:
  286. rgblight_sethsv_noeeprom_cyan(); break;
  287. }
  288. if (has_ran) {
  289. biton32(state) == _MODS ? rgblight_mode(RGBLIGHT_MODE_BREATHING) : rgblight_mode(RGBLIGHT_MODE_STATIC_LIGHT); // if _MODS layer is on, then breath to denote it
  290. } else {
  291. 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
  292. has_ran = true;
  293. }
  294. break;
  295. }
  296. // layer_state_set_indicator(); // Runs every scan, so need to call this here .... since I can't get it working "right" anyhow
  297. }
  298. #endif // RGBLIGHT_ENABLE
  299. return state;
  300. }