rgb_stuff.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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) { rgblight_sethsv_default_helper(light_index); }
  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. #ifdef RGBLIGHT_ENABLE
  230. rgblight_enable_noeeprom();
  231. layer_state_set_user(layer_state);
  232. uint16_t old_hue = rgblight_config.hue;
  233. rgblight_mode_noeeprom(RGBLIGHT_MODE_STATIC_LIGHT);
  234. for (uint16_t i = 360; i > 0; i--) {
  235. rgblight_sethsv_noeeprom( ( i + old_hue) % 360, 255, 255);
  236. wait_ms(10);
  237. }
  238. layer_state_set_user(layer_state);
  239. #endif
  240. }
  241. void matrix_scan_rgb(void) {
  242. #ifdef RGBLIGHT_TWINKLE
  243. scan_rgblight_fadeout();
  244. #endif // RGBLIGHT_ENABLE
  245. #ifdef INDICATOR_LIGHTS
  246. matrix_scan_indicator();
  247. #endif
  248. }
  249. uint32_t layer_state_set_rgb(uint32_t state) {
  250. #ifdef RGBLIGHT_ENABLE
  251. if (userspace_config.rgb_layer_change) {
  252. switch (biton32(state)) {
  253. case _MACROS:
  254. rgblight_sethsv_noeeprom_orange();
  255. userspace_config.is_overwatch ? rgblight_effect_snake(RGBLIGHT_MODE_SNAKE + 2) : rgblight_effect_snake(RGBLIGHT_MODE_SNAKE + 3);
  256. break;
  257. case _MEDIA:
  258. rgblight_sethsv_noeeprom_chartreuse();
  259. rgblight_mode_noeeprom(RGBLIGHT_MODE_KNIGHT + 1);
  260. break;
  261. case _GAMEPAD:
  262. rgblight_sethsv_noeeprom_orange();
  263. rgblight_mode_noeeprom(RGBLIGHT_MODE_SNAKE + 2);
  264. break;
  265. case _DIABLO:
  266. rgblight_sethsv_noeeprom_red();
  267. rgblight_mode_noeeprom(RGBLIGHT_MODE_BREATHING + 3);
  268. break;
  269. case _RAISE:
  270. rgblight_sethsv_noeeprom_yellow();
  271. rgblight_mode_noeeprom(RGBLIGHT_MODE_BREATHING + 3);
  272. break;
  273. case _LOWER:
  274. rgblight_sethsv_noeeprom_green();
  275. rgblight_mode_noeeprom(RGBLIGHT_MODE_BREATHING + 3);
  276. break;
  277. case _ADJUST:
  278. rgblight_sethsv_noeeprom_red();
  279. rgblight_mode_noeeprom(RGBLIGHT_MODE_KNIGHT + 2);
  280. break;
  281. default: // for any other layers, or the default layer
  282. switch (biton32(default_layer_state)) {
  283. case _COLEMAK:
  284. rgblight_sethsv_noeeprom_magenta(); break;
  285. case _DVORAK:
  286. rgblight_sethsv_noeeprom_springgreen(); break;
  287. case _WORKMAN:
  288. rgblight_sethsv_noeeprom_goldenrod(); break;
  289. case _NORMAN:
  290. rgblight_sethsv_noeeprom_coral(); break;
  291. case _MALTRON:
  292. rgblight_sethsv_noeeprom_yellow(); break;
  293. case _EUCALYN:
  294. rgblight_sethsv_noeeprom_pink(); break;
  295. case _CARPLAX:
  296. rgblight_sethsv_noeeprom_blue(); break;
  297. default:
  298. rgblight_sethsv_noeeprom_cyan(); break;
  299. }
  300. 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
  301. break;
  302. }
  303. // layer_state_set_indicator(); // Runs every scan, so need to call this here .... since I can't get it working "right" anyhow
  304. }
  305. #endif // RGBLIGHT_ENABLE
  306. return state;
  307. }
  308. #if 0
  309. uint32_t default_layer_state_set_rgb(uint32_t state) {
  310. #ifdef RGBLIGHT_ENABLE
  311. if (userspace_config.rgb_layer_change) {
  312. rgblight_config_t temp_rgblight_config = rgblight_config;
  313. switch (biton32(state)) {
  314. case _COLEMAK:
  315. temp_rgblight_config.hue = 300;
  316. temp_rgblight_config.val = 255;
  317. temp_rgblight_config.sat = 255;
  318. temp_rgblight_config.mode = 1;
  319. break;
  320. case _DVORAK:
  321. temp_rgblight_config.hue = 150;
  322. temp_rgblight_config.val = 255;
  323. temp_rgblight_config.sat = 255;
  324. temp_rgblight_config.mode = 1;
  325. case _WORKMAN:
  326. temp_rgblight_config.hue = 43;
  327. temp_rgblight_config.val = 218;
  328. temp_rgblight_config.sat = 218;
  329. temp_rgblight_config.mode = 1;
  330. default:
  331. temp_rgblight_config.hue = 180;
  332. temp_rgblight_config.val = 255;
  333. temp_rgblight_config.sat = 255;
  334. temp_rgblight_config.mode = 1;
  335. }
  336. if (temp_rgblight_config.raw != eeconfig_read_rgblight()) {
  337. 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);
  338. eeconfig_update_rgblight(temp_rgblight_config.raw);
  339. }
  340. }
  341. #endif // RGBLIGHT_ENABLE
  342. return state;
  343. }
  344. #endif