rgb_stuff.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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. #elif defined(RGB_MATRIX_ENABLE)
  7. extern rgb_config_t rgb_matrix_config;
  8. #endif
  9. #ifdef RGBLIGHT_ENABLE
  10. void rgblight_sethsv_default_helper(uint8_t index) {
  11. rgblight_sethsv_at(rgblight_config.hue, rgblight_config.sat, rgblight_config.val, index);
  12. }
  13. #endif // RGBLIGHT_ENABLE
  14. #ifdef INDICATOR_LIGHTS
  15. void set_rgb_indicators(uint8_t this_mod, uint8_t this_led, uint8_t this_osm) {
  16. if (userspace_config.rgb_layer_change && biton32(layer_state) == 0) {
  17. if (this_mod & MODS_SHIFT_MASK || this_led & (1<<USB_LED_CAPS_LOCK) || this_osm & MODS_SHIFT_MASK) {
  18. #ifdef SHFT_LED1
  19. rgblight_sethsv_at(120, 255, 255, SHFT_LED1);
  20. #endif // SHFT_LED1
  21. #ifdef SHFT_LED2
  22. rgblight_sethsv_at(120, 255, 255, SHFT_LED2);
  23. #endif // SHFT_LED2
  24. } else {
  25. #ifdef SHFT_LED1
  26. rgblight_sethsv_default_helper(SHFT_LED1);
  27. #endif // SHFT_LED1
  28. #ifdef SHFT_LED2
  29. rgblight_sethsv_default_helper(SHFT_LED2);
  30. #endif // SHFT_LED2
  31. }
  32. if (this_mod & MODS_CTRL_MASK || this_osm & MODS_CTRL_MASK) {
  33. #ifdef CTRL_LED1
  34. rgblight_sethsv_at(0, 255, 255, CTRL_LED1);
  35. #endif // CTRL_LED1
  36. #ifdef CTRL_LED2
  37. rgblight_sethsv_at(0, 255, 255, CTRL_LED2);
  38. #endif // CTRL_LED2
  39. } else {
  40. #ifdef CTRL_LED1
  41. rgblight_sethsv_default_helper(CTRL_LED1);
  42. #endif // CTRL_LED1
  43. #ifdef CTRL_LED2
  44. rgblight_sethsv_default_helper(CTRL_LED2);
  45. #endif // CTRL_LED2
  46. }
  47. if (this_mod & MODS_GUI_MASK || this_osm & MODS_GUI_MASK) {
  48. #ifdef GUI_LED1
  49. rgblight_sethsv_at(51, 255, 255, GUI_LED1);
  50. #endif // GUI_LED1
  51. #ifdef GUI_LED2
  52. rgblight_sethsv_at(51, 255, 255, GUI_LED2);
  53. #endif // GUI_LED2
  54. } else {
  55. #ifdef GUI_LED1
  56. rgblight_sethsv_default_helper(GUI_LED1);
  57. #endif // GUI_LED1
  58. #ifdef GUI_LED2
  59. rgblight_sethsv_default_helper(GUI_LED2);
  60. #endif // GUI_LED2
  61. }
  62. if (this_mod & MODS_ALT_MASK || this_osm & MODS_ALT_MASK) {
  63. #ifdef ALT_LED1
  64. rgblight_sethsv_at(240, 255, 255, ALT_LED1);
  65. #endif // ALT_LED1
  66. #ifdef GUI_LED2
  67. rgblight_sethsv_at(240, 255, 255, ALT_LED2);
  68. #endif // GUI_LED2
  69. } else {
  70. #ifdef GUI_LED1
  71. rgblight_sethsv_default_helper(ALT_LED1);
  72. #endif // GUI_LED1
  73. #ifdef GUI_LED2
  74. rgblight_sethsv_default_helper(ALT_LED2);
  75. #endif // GUI_LED2
  76. }
  77. }
  78. }
  79. void matrix_scan_indicator(void) {
  80. set_rgb_indicators(get_mods(), host_keyboard_leds(), get_oneshot_mods());
  81. }
  82. #endif //INDICATOR_LIGHTS
  83. #ifdef RGBLIGHT_TWINKLE
  84. static rgblight_fadeout lights[RGBLED_NUM];
  85. __attribute__ ((weak))
  86. bool rgblight_twinkle_is_led_used_keymap(uint8_t index) { return false; }
  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. void scan_rgblight_fadeout(void) { // Don't effing change this function .... rgblight_sethsv is supppppper intensive
  128. bool litup = false;
  129. for (uint8_t light_index = 0 ; light_index < RGBLED_NUM ; ++light_index ) {
  130. if (lights[light_index].enabled && timer_elapsed(lights[light_index].timer) > 10) {
  131. rgblight_fadeout *light = &lights[light_index];
  132. litup = true;
  133. if (light->life) {
  134. light->life -= 1;
  135. if (biton32(layer_state) == 0) {
  136. sethsv(light->hue + rand() % 0xF, 255, light->life, (LED_TYPE *)&led[light_index]);
  137. }
  138. light->timer = timer_read();
  139. }
  140. else {
  141. if (light->enabled && biton32(layer_state) == 0) { rgblight_sethsv_default_helper(light_index); }
  142. litup = light->enabled = false;
  143. }
  144. }
  145. }
  146. if (litup && biton32(layer_state) == 0) {
  147. rgblight_set();
  148. }
  149. }
  150. void start_rgb_light(void) {
  151. uint8_t indices[RGBLED_NUM];
  152. uint8_t indices_count = 0;
  153. uint8_t min_life = 0xFF;
  154. uint8_t min_life_index = -1;
  155. for (uint8_t index = 0 ; index < RGBLED_NUM ; ++index ) {
  156. if (rgblight_twinkle_is_led_used(index)) { continue; }
  157. if (lights[index].enabled) {
  158. if (min_life_index == -1 ||
  159. lights[index].life < min_life)
  160. {
  161. min_life = lights[index].life;
  162. min_life_index = index;
  163. }
  164. continue;
  165. }
  166. indices[indices_count] = index;
  167. ++indices_count;
  168. }
  169. uint8_t light_index;
  170. if (!indices_count) {
  171. light_index = min_life_index;
  172. }
  173. else {
  174. light_index = indices[rand() % indices_count];
  175. }
  176. rgblight_fadeout *light = &lights[light_index];
  177. light->enabled = true;
  178. light->timer = timer_read();
  179. light->life = 0xC0 + rand() % 0x40;
  180. light->hue = rgblight_config.hue + (rand() % 0xB4) - 0x54;
  181. rgblight_sethsv_at(light->hue, 255, light->life, light_index);
  182. }
  183. #endif
  184. bool process_record_user_rgb(uint16_t keycode, keyrecord_t *record) {
  185. switch (keycode) {
  186. #ifdef RGBLIGHT_TWINKLE
  187. case KC_A ... KC_SLASH:
  188. case KC_F1 ... KC_F12:
  189. case KC_INSERT ... KC_UP:
  190. case KC_KP_SLASH ... KC_KP_DOT:
  191. case KC_F13 ... KC_F24:
  192. case KC_AUDIO_MUTE ... KC_MEDIA_REWIND:
  193. if (record->event.pressed) { start_rgb_light(); }
  194. return true; break;
  195. #endif // RGBLIGHT_TWINKLE
  196. case KC_RGB_T: // This allows me to use underglow as layer indication, or as normal
  197. #if defined(RGBLIGHT_ENABLE) || defined(RGB_MATRIX_ENABLE)
  198. if (record->event.pressed) {
  199. userspace_config.rgb_layer_change ^= 1;
  200. xprintf("rgblight layer change [EEPROM]: %u\n", userspace_config.rgb_layer_change);
  201. eeconfig_update_user(userspace_config.raw);
  202. if (userspace_config.rgb_layer_change) {
  203. layer_state_set(layer_state); // This is needed to immediately set the layer color (looks better)
  204. }
  205. }
  206. #endif // RGBLIGHT_ENABLE
  207. return false; break;
  208. #ifdef RGBLIGHT_ENABLE
  209. case RGB_MODE_FORWARD ... RGB_MODE_GRADIENT: // quantum_keycodes.h L400 for definitions
  210. if (record->event.pressed) { //This disables layer indication, as it's assumed that if you're changing this ... you want that disabled
  211. if (userspace_config.rgb_layer_change) {
  212. userspace_config.rgb_layer_change = false;
  213. xprintf("rgblight layer change [EEPROM]: %u\n", userspace_config.rgb_layer_change);
  214. eeconfig_update_user(userspace_config.raw);
  215. }
  216. }
  217. return true; break;
  218. #endif // RGBLIGHT_ENABLE
  219. }
  220. return true;
  221. }
  222. void matrix_init_rgb(void) {
  223. // #ifdef RGBLIGHT_ENABLE
  224. // if (userspace_config.rgb_layer_change) {
  225. // rgblight_enable_noeeprom();
  226. // switch (biton32(eeconfig_read_default_layer())) {
  227. // case _COLEMAK:
  228. // rgblight_sethsv_noeeprom_magenta(); break;
  229. // case _DVORAK:
  230. // rgblight_sethsv_noeeprom_springgreen(); break;
  231. // case _WORKMAN:
  232. // rgblight_sethsv_noeeprom_goldenrod(); break;
  233. // default:
  234. // rgblight_sethsv_noeeprom_cyan(); break;
  235. // }
  236. // rgblight_mode(RGBLIGHT_MODE_STATIC_LIGHT);
  237. // }
  238. // #endif
  239. }
  240. void matrix_scan_rgb(void) {
  241. #ifdef RGBLIGHT_TWINKLE
  242. scan_rgblight_fadeout();
  243. #endif // RGBLIGHT_ENABLE
  244. #ifdef INDICATOR_LIGHTS
  245. matrix_scan_indicator();
  246. #endif
  247. }
  248. uint32_t layer_state_set_rgb(uint32_t state) {
  249. #ifdef RGBLIGHT_ENABLE
  250. if (userspace_config.rgb_layer_change) {
  251. switch (biton32(state)) {
  252. case _MACROS:
  253. rgblight_sethsv_noeeprom_orange();
  254. userspace_config.is_overwatch ? rgblight_effect_snake(RGBLIGHT_MODE_SNAKE + 2) : rgblight_effect_snake(RGBLIGHT_MODE_SNAKE + 3);
  255. break;
  256. case _MEDIA:
  257. rgblight_sethsv_noeeprom_chartreuse();
  258. rgblight_mode_noeeprom(RGBLIGHT_MODE_KNIGHT + 1);
  259. break;
  260. case _GAMEPAD:
  261. rgblight_sethsv_noeeprom_orange();
  262. rgblight_mode_noeeprom(RGBLIGHT_MODE_SNAKE + 2);
  263. break;
  264. case _DIABLO:
  265. rgblight_sethsv_noeeprom_red();
  266. rgblight_mode_noeeprom(RGBLIGHT_MODE_BREATHING + 3);
  267. break;
  268. case _RAISE:
  269. rgblight_sethsv_noeeprom_yellow();
  270. rgblight_mode_noeeprom(RGBLIGHT_MODE_BREATHING + 3);
  271. break;
  272. case _LOWER:
  273. rgblight_sethsv_noeeprom_green();
  274. rgblight_mode_noeeprom(RGBLIGHT_MODE_BREATHING + 3);
  275. break;
  276. case _ADJUST:
  277. rgblight_sethsv_noeeprom_red();
  278. rgblight_mode_noeeprom(RGBLIGHT_MODE_KNIGHT + 2);
  279. break;
  280. default: // for any other layers, or the default layer
  281. switch (biton32(default_layer_state)) {
  282. case _COLEMAK:
  283. rgblight_sethsv_noeeprom_magenta(); break;
  284. case _DVORAK:
  285. rgblight_sethsv_noeeprom_springgreen(); break;
  286. case _WORKMAN:
  287. rgblight_sethsv_noeeprom_goldenrod(); break;
  288. default:
  289. rgblight_sethsv_noeeprom_cyan(); break;
  290. }
  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. break;
  293. }
  294. // layer_state_set_indicator(); // Runs every scan, so need to call this here .... since I can't get it working "right" anyhow
  295. }
  296. #endif // RGBLIGHT_ENABLE
  297. return state;
  298. }
  299. uint32_t default_layer_state_set_rgb(uint32_t state) {
  300. #ifdef RGBLIGHT_ENABLE
  301. if (userspace_config.rgb_layer_change) {
  302. rgblight_config_t temp_rgblight_config = rgblight_config;
  303. switch (biton32(state)) {
  304. case _COLEMAK:
  305. temp_rgblight_config.hue = 300;
  306. temp_rgblight_config.val = 255;
  307. temp_rgblight_config.sat = 255;
  308. temp_rgblight_config.mode = 1;
  309. break;
  310. case _DVORAK:
  311. temp_rgblight_config.hue = 150;
  312. temp_rgblight_config.val = 255;
  313. temp_rgblight_config.sat = 255;
  314. temp_rgblight_config.mode = 1;
  315. case _WORKMAN:
  316. temp_rgblight_config.hue = 43;
  317. temp_rgblight_config.val = 218;
  318. temp_rgblight_config.sat = 218;
  319. temp_rgblight_config.mode = 1;
  320. default:
  321. temp_rgblight_config.hue = 180;
  322. temp_rgblight_config.val = 255;
  323. temp_rgblight_config.sat = 255;
  324. temp_rgblight_config.mode = 1;
  325. }
  326. if (temp_rgblight_config.raw != eeconfig_read_rgblight()) {
  327. xprintf("rgblight set default layer hsv [EEPROM]: %u,%u,%u,%u\n", temp_rgblight_config.hue, temp_rgblight_config.sat, temp_rgblight_config.val, temp_rgblight_config.mode);
  328. eeconfig_update_rgblight(temp_rgblight_config.raw);
  329. }
  330. }
  331. #endif // RGBLIGHT_ENABLE
  332. return state;
  333. }