logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

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. #include <string.h>
  17. #include <math.h>
  18. #include <lib/lib8tion/lib8tion.h>
  19. #include "oled/oled_stuff.h"
  20. // Each layer gets a name for readability, which is then used in the keymap matrix below.
  21. // The underscores don't mean anything - you can have a layer called STUFF or any other name.
  22. // Layer names don't all need to be of the same length, obviously, and you can also skip them
  23. // entirely and just use numbers.
  24. enum layer_names {
  25. _BASE,
  26. _WAVE,
  27. _FN,
  28. };
  29. // For CUSTOM_GRADIENT
  30. hsv_t gradient_0 = {205, 250, 255};
  31. hsv_t gradient_100 = {140, 215, 125};
  32. bool reflected_gradient = false;
  33. uint8_t gp_i = 0;
  34. typedef struct {
  35. hsv_t gradient_0;
  36. hsv_t gradient_1;
  37. bool reflected;
  38. } CUSTOM_PRESETS;
  39. enum user_rgb_mode {
  40. RGB_MODE_ALL,
  41. RGB_MODE_NONE,
  42. };
  43. typedef union {
  44. uint32_t raw;
  45. struct {
  46. uint8_t rgb_mode :8;
  47. };
  48. } user_config_t;
  49. user_config_t user_config;
  50. enum layer_keycodes {
  51. //Custom Gradient control keycode
  52. G1_HUI = SAFE_RANGE, //Custom gradient color 1 hue increase
  53. G1_HUD, //Custom gradient color 1 hue decrease
  54. G1_SAI, //Custom gradient color 1 saturation increase
  55. G1_SAD, //Custom gradient color 1 saturation decrease
  56. G1_VAI, //Custom gradient color 1 value increase
  57. G1_VAD, //Custom gradient color 1 value decrease
  58. G2_HUI, //Custom gradient color 2 hue increase
  59. G2_HUD, //Custom gradient color 2 hue decrease
  60. G2_SAI, //Custom gradient color 2 saturation increase
  61. G2_SAD, //Custom gradient color 2 saturation decrease
  62. G2_VAI, //Custom gradient color 2 value increase
  63. G2_VAD, //Custom gradient color 2 value decrease
  64. G_PRE, //Gradient presets
  65. REF_G, //Toggle between linear and reflected gradient
  66. G_FLIP, //Flip the gradient colors
  67. //Custom led effect keycode
  68. RGB_C_E, //Cycle user effect
  69. };
  70. void keyboard_post_init_user(void) {
  71. user_config.raw = eeconfig_read_user();
  72. switch (user_config.rgb_mode) {
  73. case RGB_MODE_ALL:
  74. rgb_matrix_set_flags(LED_FLAG_ALL);
  75. rgb_matrix_enable_noeeprom();
  76. break;
  77. case RGB_MODE_NONE:
  78. rgb_matrix_set_flags(LED_FLAG_NONE);
  79. rgb_matrix_set_color_all(0, 0, 0);
  80. break;
  81. }
  82. }
  83. bool process_record_user(uint16_t keycode, keyrecord_t *record) {
  84. process_record_user_oled(keycode, record);
  85. uint8_t color_adj_step = 5;
  86. CUSTOM_PRESETS gradient_presets[] = {
  87. {{41 , 255, 255}, {233, 245, 255}, false },
  88. {{45 , 245, 155}, {160, 255, 80}, false },
  89. {{173, 245, 40}, {41 , 255, 205}, true },
  90. {{32 , 255, 165}, {217, 185, 70}, false },
  91. {{240, 255, 145}, {115, 255, 245}, true },
  92. {{118, 255, 255}, {242, 255, 255}, false },
  93. {{212, 0 , 0}, {223, 235, 165}, true },
  94. {{205, 250, 255}, {140, 215, 125}, false },
  95. };
  96. uint8_t gp_length = ARRAY_SIZE(gradient_presets);
  97. switch (keycode) {
  98. case G1_HUI:
  99. if (record->event.pressed) {
  100. gradient_0.h += color_adj_step;
  101. dprintf("Gradient 0 HSV: %d, %d, %d\n", gradient_0.h, gradient_0.s, gradient_0.v);
  102. }
  103. return false;
  104. case G1_HUD:
  105. if (record->event.pressed) {
  106. gradient_0.h -= color_adj_step;
  107. dprintf("Gradient 0 HSV: %d, %d, %d\n", gradient_0.h, gradient_0.s, gradient_0.v);
  108. }
  109. return false;
  110. case G1_SAI:
  111. if (record->event.pressed) {
  112. gradient_0.s = (gradient_0.s + color_adj_step * 2 <= 255) ? gradient_0.s + color_adj_step * 2 : 255;
  113. dprintf("Gradient 0 HSV: %d, %d, %d\n", gradient_0.h, gradient_0.s, gradient_0.v);
  114. }
  115. return false;
  116. case G1_SAD:
  117. if (record->event.pressed) {
  118. gradient_0.s = (gradient_0.s - color_adj_step * 2 >= 0) ? gradient_0.s - color_adj_step * 2 : 0;
  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_VAI:
  123. if (record->event.pressed) {
  124. gradient_0.v = (gradient_0.v + color_adj_step * 2 <= 255) ? gradient_0.v + color_adj_step * 2 : 255;
  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_VAD:
  129. if (record->event.pressed) {
  130. gradient_0.v = (gradient_0.v - color_adj_step * 2 >= 0) ? gradient_0.v - color_adj_step * 2 : 0;
  131. dprintf("Gradient 0 HSV: %d, %d, %d\n", gradient_0.h, gradient_0.s, gradient_0.v);
  132. }
  133. return false;
  134. case G2_HUI:
  135. if (record->event.pressed) {
  136. gradient_100.h += color_adj_step;
  137. dprintf("Gradient 100 HSV: %d, %d, %d\n", gradient_100.h, gradient_100.s, gradient_100.v);
  138. }
  139. return false;
  140. case G2_HUD:
  141. if (record->event.pressed) {
  142. gradient_100.h -= color_adj_step;
  143. dprintf("Gradient 100 HSV: %d, %d, %d\n", gradient_100.h, gradient_100.s, gradient_100.v);
  144. }
  145. return false;
  146. case G2_SAI:
  147. if (record->event.pressed) {
  148. gradient_100.s = (gradient_100.s + color_adj_step * 2 <= 255) ? gradient_100.s + color_adj_step * 2 : 255;
  149. dprintf("Gradient 100 HSV: %d, %d, %d\n", gradient_100.h, gradient_100.s, gradient_100.v);
  150. }
  151. return false;
  152. case G2_SAD:
  153. if (record->event.pressed) {
  154. gradient_100.s = (gradient_100.s - color_adj_step * 2 >= 0) ? gradient_100.s - color_adj_step * 2 : 0;
  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_VAI:
  159. if (record->event.pressed) {
  160. gradient_100.v = (gradient_100.v + color_adj_step * 2 <= 255) ? gradient_100.v + color_adj_step * 2 : 255;
  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_VAD:
  165. if (record->event.pressed) {
  166. gradient_100.v = (gradient_100.v - color_adj_step * 2 >= 0) ? gradient_100.v - color_adj_step * 2 : 0;
  167. dprintf("Gradient 100 HSV: %d, %d, %d\n", gradient_100.h, gradient_100.s, gradient_100.v);
  168. }
  169. return false;
  170. case G_PRE:
  171. if (record->event.pressed) {
  172. gp_i = (gp_i + gp_length ) % gp_length;
  173. gradient_0 = gradient_presets[gp_i].gradient_0;
  174. gradient_100 = gradient_presets[gp_i].gradient_1;
  175. reflected_gradient = gradient_presets[gp_i].reflected;
  176. gp_i += 1;
  177. }
  178. return false;
  179. case REF_G:
  180. if (record->event.pressed) {
  181. reflected_gradient = !reflected_gradient;
  182. }
  183. return false;
  184. case G_FLIP:
  185. if (record->event.pressed) {
  186. hsv_t temp_color = gradient_0;
  187. gradient_0 = gradient_100;
  188. gradient_100 = temp_color;
  189. }
  190. return false;
  191. case RGB_C_E:
  192. if (record->event.pressed) {
  193. switch (rgb_matrix_get_mode()) {
  194. case RGB_MATRIX_CUSTOM_CUSTOM_GRADIENT:
  195. rgb_matrix_mode(RGB_MATRIX_CUSTOM_COOL_DIAGONAL);
  196. return false;
  197. case RGB_MATRIX_CUSTOM_COOL_DIAGONAL:
  198. rgb_matrix_mode(RGB_MATRIX_CUSTOM_FLOWER_BLOOMING);
  199. return false;
  200. case RGB_MATRIX_CUSTOM_FLOWER_BLOOMING:
  201. rgb_matrix_mode(RGB_MATRIX_CUSTOM_KITT);
  202. return false;
  203. case RGB_MATRIX_CUSTOM_KITT:
  204. rgb_matrix_mode(RGB_MATRIX_CUSTOM_RANDOM_BREATH_RAINBOW);
  205. return false;
  206. default:
  207. rgb_matrix_mode(RGB_MATRIX_CUSTOM_CUSTOM_GRADIENT);
  208. return false;
  209. }
  210. }
  211. return false;
  212. case QK_RGB_MATRIX_TOGGLE:
  213. if (record->event.pressed) {
  214. switch (rgb_matrix_get_flags()) {
  215. case LED_FLAG_ALL: {
  216. rgb_matrix_set_flags(LED_FLAG_NONE);
  217. rgb_matrix_set_color_all(0, 0, 0);
  218. user_config.rgb_mode = RGB_MODE_NONE;
  219. }
  220. break;
  221. default: {
  222. rgb_matrix_set_flags(LED_FLAG_ALL);
  223. rgb_matrix_enable_noeeprom();
  224. user_config.rgb_mode = RGB_MODE_ALL;
  225. }
  226. break;
  227. }
  228. eeconfig_update_user(user_config.raw);
  229. }
  230. return false;
  231. }
  232. return true;
  233. }
  234. bool rgb_matrix_indicators_user(void) {
  235. uint8_t side_leds_left[3] = {17, 18, 19};
  236. uint8_t side_leds_right[3] = { 4, 5, 6};
  237. hsv_t hsv = rgb_matrix_config.hsv;
  238. uint8_t time = scale16by8(g_rgb_timer, qadd8(32, 1));
  239. hsv.h = time;
  240. rgb_t rgb = hsv_to_rgb(hsv);
  241. if ((rgb_matrix_get_flags() & LED_FLAG_ALL)) {
  242. if (host_keyboard_led_state().caps_lock) {
  243. for (uint8_t i = 0; i < 3; i++)
  244. {
  245. rgb_matrix_set_color(side_leds_left[i], rgb.r, rgb.g, rgb.b);
  246. }
  247. }
  248. if (host_keyboard_led_state().scroll_lock) {
  249. for (uint8_t i = 0; i < 3; i++)
  250. {
  251. rgb_matrix_set_color(side_leds_right[i], rgb.r, rgb.g, rgb.b);
  252. }
  253. }
  254. } else {
  255. if (host_keyboard_led_state().caps_lock) {
  256. for (uint8_t i = 0; i < 3; i++)
  257. {
  258. rgb_matrix_set_color(side_leds_left[i], rgb.r, rgb.g, rgb.b);
  259. }
  260. } else {
  261. for (uint8_t i = 0; i < 3; i++)
  262. {
  263. rgb_matrix_set_color(side_leds_left[i], 0, 0, 0);
  264. }
  265. }
  266. if (host_keyboard_led_state().scroll_lock) {
  267. for (uint8_t i = 0; i < 3; i++)
  268. {
  269. rgb_matrix_set_color(side_leds_right[i], rgb.r, rgb.g, rgb.b);
  270. }
  271. } else {
  272. for (uint8_t i = 0; i < 3; i++)
  273. {
  274. rgb_matrix_set_color(side_leds_right[i], 0, 0, 0);
  275. }
  276. }
  277. }
  278. return false;
  279. }