rgb_stuff.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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. if ((keycode >= QK_MOD_TAP && keycode <= QK_MOD_TAP_MAX) || (keycode >= QK_LAYER_TAP && keycode <= QK_LAYER_TAP_MAX)) {
  186. keycode = keycode & 0xFF;
  187. }
  188. switch (keycode) {
  189. #ifdef RGBLIGHT_TWINKLE
  190. case KC_A ... KC_SLASH:
  191. case KC_F1 ... KC_F12:
  192. case KC_INSERT ... KC_UP:
  193. case KC_KP_SLASH ... KC_KP_DOT:
  194. case KC_F13 ... KC_F24:
  195. case KC_AUDIO_MUTE ... KC_MEDIA_REWIND:
  196. if (record->event.pressed) { start_rgb_light(); }
  197. return true; break;
  198. #endif // RGBLIGHT_TWINKLE
  199. case KC_RGB_T: // This allows me to use underglow as layer indication, or as normal
  200. #if defined(RGBLIGHT_ENABLE) || defined(RGB_MATRIX_ENABLE)
  201. if (record->event.pressed) {
  202. userspace_config.rgb_layer_change ^= 1;
  203. xprintf("rgblight layer change [EEPROM]: %u\n", userspace_config.rgb_layer_change);
  204. eeconfig_update_user(userspace_config.raw);
  205. if (userspace_config.rgb_layer_change) {
  206. layer_state_set(layer_state); // This is needed to immediately set the layer color (looks better)
  207. }
  208. }
  209. #endif // RGBLIGHT_ENABLE
  210. return false; break;
  211. #ifdef RGBLIGHT_ENABLE
  212. case RGB_MODE_FORWARD ... RGB_MODE_GRADIENT: // quantum_keycodes.h L400 for definitions
  213. if (record->event.pressed) { //This disables layer indication, as it's assumed that if you're changing this ... you want that disabled
  214. if (userspace_config.rgb_layer_change) {
  215. userspace_config.rgb_layer_change = false;
  216. xprintf("rgblight layer change [EEPROM]: %u\n", userspace_config.rgb_layer_change);
  217. eeconfig_update_user(userspace_config.raw);
  218. }
  219. }
  220. return true; break;
  221. #endif // RGBLIGHT_ENABLE
  222. }
  223. return true;
  224. }
  225. void matrix_init_rgb(void) {
  226. // #ifdef RGBLIGHT_ENABLE
  227. // if (userspace_config.rgb_layer_change) {
  228. // rgblight_enable_noeeprom();
  229. // switch (biton32(eeconfig_read_default_layer())) {
  230. // case _COLEMAK:
  231. // rgblight_sethsv_noeeprom_magenta(); break;
  232. // case _DVORAK:
  233. // rgblight_sethsv_noeeprom_springgreen(); break;
  234. // case _WORKMAN:
  235. // rgblight_sethsv_noeeprom_goldenrod(); break;
  236. // default:
  237. // rgblight_sethsv_noeeprom_cyan(); break;
  238. // }
  239. // rgblight_mode(RGBLIGHT_MODE_STATIC_LIGHT);
  240. // }
  241. // #endif
  242. }
  243. void matrix_scan_rgb(void) {
  244. #ifdef RGBLIGHT_TWINKLE
  245. scan_rgblight_fadeout();
  246. #endif // RGBLIGHT_ENABLE
  247. #ifdef INDICATOR_LIGHTS
  248. matrix_scan_indicator();
  249. #endif
  250. }
  251. uint32_t layer_state_set_rgb(uint32_t state) {
  252. #ifdef RGBLIGHT_ENABLE
  253. if (userspace_config.rgb_layer_change) {
  254. switch (biton32(state)) {
  255. case _MACROS:
  256. rgblight_sethsv_noeeprom_orange();
  257. userspace_config.is_overwatch ? rgblight_effect_snake(RGBLIGHT_MODE_SNAKE + 2) : rgblight_effect_snake(RGBLIGHT_MODE_SNAKE + 3);
  258. break;
  259. case _MEDIA:
  260. rgblight_sethsv_noeeprom_chartreuse();
  261. rgblight_mode_noeeprom(RGBLIGHT_MODE_KNIGHT + 1);
  262. break;
  263. case _GAMEPAD:
  264. rgblight_sethsv_noeeprom_orange();
  265. rgblight_mode_noeeprom(RGBLIGHT_MODE_SNAKE + 2);
  266. break;
  267. case _DIABLO:
  268. rgblight_sethsv_noeeprom_red();
  269. rgblight_mode_noeeprom(RGBLIGHT_MODE_BREATHING + 3);
  270. break;
  271. case _RAISE:
  272. rgblight_sethsv_noeeprom_yellow();
  273. rgblight_mode_noeeprom(RGBLIGHT_MODE_BREATHING + 3);
  274. break;
  275. case _LOWER:
  276. rgblight_sethsv_noeeprom_green();
  277. rgblight_mode_noeeprom(RGBLIGHT_MODE_BREATHING + 3);
  278. break;
  279. case _ADJUST:
  280. rgblight_sethsv_noeeprom_red();
  281. rgblight_mode_noeeprom(RGBLIGHT_MODE_KNIGHT + 2);
  282. break;
  283. default: // for any other layers, or the default layer
  284. switch (biton32(default_layer_state)) {
  285. case _COLEMAK:
  286. rgblight_sethsv_noeeprom_magenta(); break;
  287. case _DVORAK:
  288. rgblight_sethsv_noeeprom_springgreen(); break;
  289. case _WORKMAN:
  290. rgblight_sethsv_noeeprom_goldenrod(); break;
  291. case _NORMAN:
  292. rgblight_sethsv_noeeprom_coral(); break;
  293. case _MALTRON:
  294. rgblight_sethsv_noeeprom_yellow(); break;
  295. case _EUCALYN:
  296. rgblight_sethsv_noeeprom_pink(); break;
  297. case _CARPLAX:
  298. rgblight_sethsv_noeeprom_blue(); break;
  299. default:
  300. rgblight_sethsv_noeeprom_cyan(); break;
  301. }
  302. 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
  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. }
  310. uint32_t default_layer_state_set_rgb(uint32_t state) {
  311. #ifdef RGBLIGHT_ENABLE
  312. if (userspace_config.rgb_layer_change) {
  313. rgblight_config_t temp_rgblight_config = rgblight_config;
  314. switch (biton32(state)) {
  315. case _COLEMAK:
  316. temp_rgblight_config.hue = 300;
  317. temp_rgblight_config.val = 255;
  318. temp_rgblight_config.sat = 255;
  319. temp_rgblight_config.mode = 1;
  320. break;
  321. case _DVORAK:
  322. temp_rgblight_config.hue = 150;
  323. temp_rgblight_config.val = 255;
  324. temp_rgblight_config.sat = 255;
  325. temp_rgblight_config.mode = 1;
  326. case _WORKMAN:
  327. temp_rgblight_config.hue = 43;
  328. temp_rgblight_config.val = 218;
  329. temp_rgblight_config.sat = 218;
  330. temp_rgblight_config.mode = 1;
  331. default:
  332. temp_rgblight_config.hue = 180;
  333. temp_rgblight_config.val = 255;
  334. temp_rgblight_config.sat = 255;
  335. temp_rgblight_config.mode = 1;
  336. }
  337. if (temp_rgblight_config.raw != eeconfig_read_rgblight()) {
  338. 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);
  339. eeconfig_update_rgblight(temp_rgblight_config.raw);
  340. }
  341. }
  342. #endif // RGBLIGHT_ENABLE
  343. return state;
  344. }