rgb_stuff.c 7.8 KB

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