rgb_stuff.c 9.6 KB

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