rgb_stuff.c 11 KB

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