rgb_stuff.c 11 KB

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