keymap_stuff.h (11542B)
1 /* Copyright 2022 HorrorTroll <https://github.com/HorrorTroll> 2 * 3 * This program is free software: you can redistribute it and/or modify 4 * it under the terms of the GNU General Public License as published by 5 * the Free Software Foundation, either version 2 of the License, or 6 * (at your option) any later version. 7 * 8 * This program is distributed in the hope that it will be useful, 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * GNU General Public License for more details. 12 * 13 * You should have received a copy of the GNU General Public License 14 * along with this program. If not, see <http://www.gnu.org/licenses/>. 15 */ 16 17 #include <string.h> 18 #include <math.h> 19 20 #include <lib/lib8tion/lib8tion.h> 21 22 #include "oled/oled_stuff.h" 23 24 // Each layer gets a name for readability, which is then used in the keymap matrix below. 25 // The underscores don't mean anything - you can have a layer called STUFF or any other name. 26 // Layer names don't all need to be of the same length, obviously, and you can also skip them 27 // entirely and just use numbers. 28 29 enum layer_names { 30 _BASE, 31 _WAVE, 32 _FN, 33 }; 34 35 // For CUSTOM_GRADIENT 36 hsv_t gradient_0 = {205, 250, 255}; 37 hsv_t gradient_100 = {140, 215, 125}; 38 bool reflected_gradient = false; 39 uint8_t gp_i = 0; 40 41 typedef struct { 42 hsv_t gradient_0; 43 hsv_t gradient_1; 44 bool reflected; 45 } CUSTOM_PRESETS; 46 47 enum user_rgb_mode { 48 RGB_MODE_ALL, 49 RGB_MODE_NONE, 50 }; 51 52 typedef union { 53 uint32_t raw; 54 struct { 55 uint8_t rgb_mode :8; 56 }; 57 } user_config_t; 58 59 user_config_t user_config; 60 61 enum layer_keycodes { 62 //Custom Gradient control keycode 63 G1_HUI = SAFE_RANGE, //Custom gradient color 1 hue increase 64 G1_HUD, //Custom gradient color 1 hue decrease 65 G1_SAI, //Custom gradient color 1 saturation increase 66 G1_SAD, //Custom gradient color 1 saturation decrease 67 G1_VAI, //Custom gradient color 1 value increase 68 G1_VAD, //Custom gradient color 1 value decrease 69 G2_HUI, //Custom gradient color 2 hue increase 70 G2_HUD, //Custom gradient color 2 hue decrease 71 G2_SAI, //Custom gradient color 2 saturation increase 72 G2_SAD, //Custom gradient color 2 saturation decrease 73 G2_VAI, //Custom gradient color 2 value increase 74 G2_VAD, //Custom gradient color 2 value decrease 75 G_PRE, //Gradient presets 76 REF_G, //Toggle between linear and reflected gradient 77 G_FLIP, //Flip the gradient colors 78 79 //Custom led effect keycode 80 RGB_C_E, //Cycle user effect 81 }; 82 83 void keyboard_post_init_user(void) { 84 user_config.raw = eeconfig_read_user(); 85 switch (user_config.rgb_mode) { 86 case RGB_MODE_ALL: 87 rgb_matrix_set_flags(LED_FLAG_ALL); 88 rgb_matrix_enable_noeeprom(); 89 break; 90 case RGB_MODE_NONE: 91 rgb_matrix_set_flags(LED_FLAG_NONE); 92 rgb_matrix_set_color_all(0, 0, 0); 93 break; 94 } 95 } 96 97 bool process_record_user(uint16_t keycode, keyrecord_t *record) { 98 process_record_user_oled(keycode, record); 99 100 uint8_t color_adj_step = 5; 101 102 CUSTOM_PRESETS gradient_presets[] = { 103 {{41 , 255, 255}, {233, 245, 255}, false }, 104 {{45 , 245, 155}, {160, 255, 80}, false }, 105 {{173, 245, 40}, {41 , 255, 205}, true }, 106 {{32 , 255, 165}, {217, 185, 70}, false }, 107 {{240, 255, 145}, {115, 255, 245}, true }, 108 {{118, 255, 255}, {242, 255, 255}, false }, 109 {{212, 0 , 0}, {223, 235, 165}, true }, 110 {{205, 250, 255}, {140, 215, 125}, false }, 111 }; 112 113 uint8_t gp_length = ARRAY_SIZE(gradient_presets); 114 115 switch (keycode) { 116 case G1_HUI: 117 if (record->event.pressed) { 118 gradient_0.h += color_adj_step; 119 dprintf("Gradient 0 HSV: %d, %d, %d\n", gradient_0.h, gradient_0.s, gradient_0.v); 120 } 121 return false; 122 case G1_HUD: 123 if (record->event.pressed) { 124 gradient_0.h -= color_adj_step; 125 dprintf("Gradient 0 HSV: %d, %d, %d\n", gradient_0.h, gradient_0.s, gradient_0.v); 126 } 127 return false; 128 case G1_SAI: 129 if (record->event.pressed) { 130 gradient_0.s = (gradient_0.s + color_adj_step * 2 <= 255) ? gradient_0.s + color_adj_step * 2 : 255; 131 dprintf("Gradient 0 HSV: %d, %d, %d\n", gradient_0.h, gradient_0.s, gradient_0.v); 132 } 133 return false; 134 case G1_SAD: 135 if (record->event.pressed) { 136 gradient_0.s = (gradient_0.s - color_adj_step * 2 >= 0) ? gradient_0.s - color_adj_step * 2 : 0; 137 dprintf("Gradient 0 HSV: %d, %d, %d\n", gradient_0.h, gradient_0.s, gradient_0.v); 138 } 139 return false; 140 case G1_VAI: 141 if (record->event.pressed) { 142 gradient_0.v = (gradient_0.v + color_adj_step * 2 <= 255) ? gradient_0.v + color_adj_step * 2 : 255; 143 dprintf("Gradient 0 HSV: %d, %d, %d\n", gradient_0.h, gradient_0.s, gradient_0.v); 144 } 145 return false; 146 case G1_VAD: 147 if (record->event.pressed) { 148 gradient_0.v = (gradient_0.v - color_adj_step * 2 >= 0) ? gradient_0.v - color_adj_step * 2 : 0; 149 dprintf("Gradient 0 HSV: %d, %d, %d\n", gradient_0.h, gradient_0.s, gradient_0.v); 150 } 151 return false; 152 case G2_HUI: 153 if (record->event.pressed) { 154 gradient_100.h += color_adj_step; 155 dprintf("Gradient 100 HSV: %d, %d, %d\n", gradient_100.h, gradient_100.s, gradient_100.v); 156 } 157 return false; 158 case G2_HUD: 159 if (record->event.pressed) { 160 gradient_100.h -= color_adj_step; 161 dprintf("Gradient 100 HSV: %d, %d, %d\n", gradient_100.h, gradient_100.s, gradient_100.v); 162 } 163 return false; 164 case G2_SAI: 165 if (record->event.pressed) { 166 gradient_100.s = (gradient_100.s + color_adj_step * 2 <= 255) ? gradient_100.s + color_adj_step * 2 : 255; 167 dprintf("Gradient 100 HSV: %d, %d, %d\n", gradient_100.h, gradient_100.s, gradient_100.v); 168 } 169 return false; 170 case G2_SAD: 171 if (record->event.pressed) { 172 gradient_100.s = (gradient_100.s - color_adj_step * 2 >= 0) ? gradient_100.s - color_adj_step * 2 : 0; 173 dprintf("Gradient 100 HSV: %d, %d, %d\n", gradient_100.h, gradient_100.s, gradient_100.v); 174 } 175 return false; 176 case G2_VAI: 177 if (record->event.pressed) { 178 gradient_100.v = (gradient_100.v + color_adj_step * 2 <= 255) ? gradient_100.v + color_adj_step * 2 : 255; 179 dprintf("Gradient 100 HSV: %d, %d, %d\n", gradient_100.h, gradient_100.s, gradient_100.v); 180 } 181 return false; 182 case G2_VAD: 183 if (record->event.pressed) { 184 gradient_100.v = (gradient_100.v - color_adj_step * 2 >= 0) ? gradient_100.v - color_adj_step * 2 : 0; 185 dprintf("Gradient 100 HSV: %d, %d, %d\n", gradient_100.h, gradient_100.s, gradient_100.v); 186 } 187 return false; 188 case G_PRE: 189 if (record->event.pressed) { 190 gp_i = (gp_i + gp_length ) % gp_length; 191 192 gradient_0 = gradient_presets[gp_i].gradient_0; 193 gradient_100 = gradient_presets[gp_i].gradient_1; 194 reflected_gradient = gradient_presets[gp_i].reflected; 195 196 gp_i += 1; 197 } 198 return false; 199 case REF_G: 200 if (record->event.pressed) { 201 reflected_gradient = !reflected_gradient; 202 } 203 return false; 204 case G_FLIP: 205 if (record->event.pressed) { 206 hsv_t temp_color = gradient_0; 207 gradient_0 = gradient_100; 208 gradient_100 = temp_color; 209 } 210 return false; 211 case RGB_C_E: 212 if (record->event.pressed) { 213 switch (rgb_matrix_get_mode()) { 214 case RGB_MATRIX_CUSTOM_CUSTOM_GRADIENT: 215 rgb_matrix_mode(RGB_MATRIX_CUSTOM_COOL_DIAGONAL); 216 return false; 217 case RGB_MATRIX_CUSTOM_COOL_DIAGONAL: 218 rgb_matrix_mode(RGB_MATRIX_CUSTOM_FLOWER_BLOOMING); 219 return false; 220 case RGB_MATRIX_CUSTOM_FLOWER_BLOOMING: 221 rgb_matrix_mode(RGB_MATRIX_CUSTOM_KITT); 222 return false; 223 case RGB_MATRIX_CUSTOM_KITT: 224 rgb_matrix_mode(RGB_MATRIX_CUSTOM_RANDOM_BREATH_RAINBOW); 225 return false; 226 default: 227 rgb_matrix_mode(RGB_MATRIX_CUSTOM_CUSTOM_GRADIENT); 228 return false; 229 } 230 } 231 return false; 232 case QK_RGB_MATRIX_TOGGLE: 233 if (record->event.pressed) { 234 switch (rgb_matrix_get_flags()) { 235 case LED_FLAG_ALL: { 236 rgb_matrix_set_flags(LED_FLAG_NONE); 237 rgb_matrix_set_color_all(0, 0, 0); 238 user_config.rgb_mode = RGB_MODE_NONE; 239 } 240 break; 241 default: { 242 rgb_matrix_set_flags(LED_FLAG_ALL); 243 rgb_matrix_enable_noeeprom(); 244 user_config.rgb_mode = RGB_MODE_ALL; 245 } 246 break; 247 } 248 eeconfig_update_user(user_config.raw); 249 } 250 return false; 251 } 252 return true; 253 } 254 255 bool rgb_matrix_indicators_user(void) { 256 uint8_t side_leds_left[3] = {17, 18, 19}; 257 uint8_t side_leds_right[3] = { 4, 5, 6}; 258 hsv_t hsv = rgb_matrix_config.hsv; 259 uint8_t time = scale16by8(g_rgb_timer, qadd8(32, 1)); 260 hsv.h = time; 261 rgb_t rgb = hsv_to_rgb(hsv); 262 263 if ((rgb_matrix_get_flags() & LED_FLAG_ALL)) { 264 if (host_keyboard_led_state().caps_lock) { 265 for (uint8_t i = 0; i < 3; i++) 266 { 267 rgb_matrix_set_color(side_leds_left[i], rgb.r, rgb.g, rgb.b); 268 } 269 } 270 if (host_keyboard_led_state().scroll_lock) { 271 for (uint8_t i = 0; i < 3; i++) 272 { 273 rgb_matrix_set_color(side_leds_right[i], rgb.r, rgb.g, rgb.b); 274 } 275 } 276 } else { 277 if (host_keyboard_led_state().caps_lock) { 278 for (uint8_t i = 0; i < 3; i++) 279 { 280 rgb_matrix_set_color(side_leds_left[i], rgb.r, rgb.g, rgb.b); 281 } 282 } else { 283 for (uint8_t i = 0; i < 3; i++) 284 { 285 rgb_matrix_set_color(side_leds_left[i], 0, 0, 0); 286 } 287 } 288 if (host_keyboard_led_state().scroll_lock) { 289 for (uint8_t i = 0; i < 3; i++) 290 { 291 rgb_matrix_set_color(side_leds_right[i], rgb.r, rgb.g, rgb.b); 292 } 293 } else { 294 for (uint8_t i = 0; i < 3; i++) 295 { 296 rgb_matrix_set_color(side_leds_right[i], 0, 0, 0); 297 } 298 } 299 } 300 return false; 301 }