keymap.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936
  1. #include QMK_KEYBOARD_H
  2. // uint8_t keyboard_leds(void)
  3. #include <tmk_core/protocol/arm_atsam/main_arm_atsam.h>
  4. #if ISSI3733_LED_COUNT == 119
  5. # define KEY_LED_COUNT 87
  6. #elif ISSI3733_LED_COUNT == 105
  7. # define KEY_LED_COUNT 67
  8. #endif
  9. #define min(x, y) (x < y ? x : y)
  10. extern issi3733_led_t *lede;
  11. extern issi3733_led_t led_map[];
  12. enum ctrl_keycodes {
  13. L_BRI = SAFE_RANGE, //LED Brightness Increase
  14. L_BRD, //LED Brightness Decrease
  15. L_PTN, //LED Pattern Select Next
  16. L_PTP, //LED Pattern Select Previous
  17. L_PSI, //LED Pattern Speed Increase
  18. L_PSD, //LED Pattern Speed Decrease
  19. L_T_MD, //LED Toggle Mode
  20. L_T_ONF, //LED Toggle On / Off
  21. L_ON, //LED On
  22. L_OFF, //LED Off
  23. L_T_BR, //LED Toggle Breath Effect
  24. L_T_PTD, //LED Toggle Scrolling Pattern Direction
  25. U_T_AUTO, //USB Extra Port Toggle Auto Detect / Always Active
  26. U_T_AGCR, //USB Toggle Automatic GCR control
  27. DBG_TOG, //DEBUG Toggle On / Off
  28. DBG_MTRX, //DEBUG Toggle Matrix Prints
  29. DBG_KBD, //DEBUG Toggle Keyboard Prints
  30. DBG_MOU, //DEBUG Toggle Mouse Prints
  31. MD_BOOT, //Restart into bootloader after hold timeout
  32. L_SP_PR, //LED Splash Pattern Select Previous
  33. L_SP_NE, //LED Splash Pattern Select Next
  34. L_SP_WD, //LED Splash Widen Wavefront width
  35. L_SP_NW, //LED Splash Narrow Wavefront width
  36. L_SP_FA, //LED Splash wave travel speed faster (shorter period)
  37. L_SP_SL, //LED Splash wave travel speed slower (longer period)
  38. L_CP_PR, //LED Color Pattern Select Previous
  39. L_CP_NX, //LEB Color Pattern Select Next
  40. };
  41. #define TG_NKRO MAGIC_TOGGLE_NKRO //Toggle 6KRO / NKRO mode
  42. #define ______ KC_TRNS
  43. keymap_config_t keymap_config;
  44. const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
  45. [0] = LAYOUT(
  46. KC_ESC, KC_F1, KC_F2, KC_F3, KC_F4, KC_F5, KC_F6, KC_F7, KC_F8, KC_F9, KC_F10, KC_F11, KC_F12, KC_PSCR, KC_SLCK, KC_PAUS, \
  47. KC_GRV, KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7, KC_8, KC_9, KC_0, KC_MINS, KC_EQL, KC_BSPC, KC_INS, KC_HOME, KC_PGUP, \
  48. KC_TAB, KC_Q, KC_W, KC_E, KC_R, KC_T, KC_Y, KC_U, KC_I, KC_O, KC_P, KC_LBRC, KC_RBRC, KC_BSLS, KC_DEL, KC_END, KC_PGDN, \
  49. KC_CAPS, KC_A, KC_S, KC_D, KC_F, KC_G, KC_H, KC_J, KC_K, KC_L, KC_SCLN, KC_QUOT, KC_ENT, \
  50. KC_LSFT, KC_Z, KC_X, KC_C, KC_V, KC_B, KC_N, KC_M, KC_COMM, KC_DOT, KC_SLSH, KC_RSFT, KC_UP, \
  51. KC_LCTL, KC_LGUI, KC_LALT, KC_SPC, KC_RALT, MO(1), KC_APP, KC_RCTL, KC_LEFT, KC_DOWN, KC_RGHT \
  52. ),
  53. [1] = LAYOUT(
  54. _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, KC_MUTE, _______, _______, \
  55. _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, KC_MPLY, KC_MSTP, KC_VOLU, \
  56. L_T_BR, L_PSD, L_BRI, L_PSI, _______, _______, _______, U_T_AUTO,U_T_AGCR,_______, MO(2), _______, _______, _______, KC_MPRV, KC_MNXT, KC_VOLD, \
  57. L_T_PTD, L_PTP, L_BRD, L_PTN, _______, _______, _______, _______, _______, _______, _______, _______, _______, \
  58. _______, L_T_MD, L_T_ONF, _______, _______, MD_BOOT, TG_NKRO, _______, _______, _______, _______, _______, _______, \
  59. _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______ \
  60. ),
  61. [2] = LAYOUT(
  62. _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, \
  63. _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, \
  64. L_CP_NX, L_SP_SL, L_SP_WD, L_SP_FA, _______, _______, L_CP_NX, L_SP_SL, L_SP_WD, L_SP_FA, _______, _______, _______, _______, _______, _______, _______, \
  65. L_CP_PR, L_SP_PR, L_SP_NW, L_SP_NE, _______, _______, L_CP_PR, L_SP_PR, L_SP_NW, L_SP_NE, _______, _______, _______, \
  66. _______, _______, _______, _______, _______, _______, TG_NKRO, _______, _______, _______, _______, _______, _______, \
  67. _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______ \
  68. ),
  69. /*
  70. [X] = LAYOUT(
  71. _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, \
  72. _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, \
  73. _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, \
  74. _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, \
  75. _______, _______, _______, _______, _______, _______, TG_NKRO, _______, _______, _______, _______, _______, _______, \
  76. _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______ \
  77. ),
  78. */
  79. };
  80. // see: /tmk_core/common/keycode.h
  81. uint8_t KEYCODE_TO_LED_ID[256];
  82. uint8_t DISTANCE_MAP[KEY_LED_COUNT+1][KEY_LED_COUNT+1];
  83. struct user_led_t {
  84. uint8_t state;
  85. uint8_t r;
  86. uint8_t g;
  87. uint8_t b;
  88. } USER_LED[KEY_LED_COUNT] = {
  89. };
  90. struct {
  91. uint8_t PATTERN_INDEX;
  92. uint8_t WAVE_FRONT_WIDTH;
  93. uint16_t WAVE_PERIOD;
  94. uint8_t COLOR_PATTERN_INDEX;
  95. uint8_t TRAVEL_DISTANCE;
  96. } USER_CONFIG = {
  97. .PATTERN_INDEX = 1,
  98. .WAVE_FRONT_WIDTH = 3,
  99. .WAVE_PERIOD = 50,
  100. .COLOR_PATTERN_INDEX = 0,
  101. .TRAVEL_DISTANCE = 25,
  102. };
  103. uint8_t ktli(uint16_t keycode){
  104. if(keycode < 256){
  105. // the array is initialized in `matrix_init_user()`
  106. return KEYCODE_TO_LED_ID[keycode];
  107. }
  108. switch(keycode){
  109. // definition of MO(layer): quantum/quantum_keycodes.h: line 614
  110. case MO(1): return 82;
  111. }
  112. return 0;
  113. };
  114. // Runs just one time when the keyboard initializes.
  115. static void init_keycode_to_led_map(void){
  116. uint16_t LED_MAP[MATRIX_ROWS][MATRIX_COLS] = LAYOUT(
  117. 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,
  118. 20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,
  119. 36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,
  120. 52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,
  121. #if KEY_LED_COUNT >= 87
  122. 68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87
  123. #endif
  124. );
  125. uint16_t key = 0;
  126. for(uint8_t y = 0; y < MATRIX_ROWS; ++y){
  127. for(uint8_t x = 0; x < MATRIX_COLS; ++x){
  128. key = keymaps[0][y][x];
  129. if(key < 256){
  130. KEYCODE_TO_LED_ID[key] = LED_MAP[y][x];
  131. }
  132. }
  133. }
  134. }
  135. // https://docs.qmk.fm/#/feature_terminal
  136. #define KEY_POSITION_MAP_ROWS 6
  137. #define KEY_POSITION_MAP_COLUMNS 20
  138. static void init_distance_map(void){
  139. uint16_t KEY_POSITION_MAP[KEY_POSITION_MAP_ROWS][KEY_POSITION_MAP_COLUMNS] = {
  140. { KC_NO, KC_ESC, KC_NO, KC_F1, KC_F2, KC_F3, KC_F4, KC_NO, KC_F5, KC_F6, KC_F7, KC_F8, KC_F9, KC_F10, KC_F11, KC_F12, KC_NO, KC_PSCR, KC_SLCK, KC_PAUS, },
  141. // { KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, },
  142. { KC_NO, KC_GRV, KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7, KC_8, KC_9, KC_0, KC_MINS, KC_EQL, KC_BSPC, KC_BSPC, KC_NO, KC_INS, KC_HOME, KC_PGUP, },
  143. { KC_NO, KC_TAB, KC_Q, KC_W, KC_E, KC_R, KC_T, KC_Y, KC_U, KC_I, KC_O, KC_P, KC_LBRC, KC_RBRC, KC_BSLS, KC_BSLS, KC_NO, KC_DEL, KC_END, KC_PGDN, },
  144. { KC_NO, KC_CAPS, KC_A, KC_S, KC_D, KC_F, KC_G, KC_H, KC_J, KC_K, KC_L, KC_SCLN, KC_QUOT, KC_ENT, KC_ENT, KC_ENT, KC_NO, KC_NO, KC_NO, KC_NO, },
  145. { KC_NO, KC_LSFT, KC_Z, KC_X, KC_C, KC_V, KC_B, KC_N, KC_M, KC_COMM, KC_DOT, KC_SLSH, KC_RSFT, KC_RSFT, KC_RSFT, KC_RSFT, KC_NO, KC_NO, KC_UP, KC_NO, },
  146. { KC_LCTL, KC_LGUI, KC_LALT, KC_SPC, KC_SPC, KC_SPC, KC_SPC, KC_SPC, KC_SPC, KC_RALT, KC_NO, MO(1), KC_APP, KC_RCTL, KC_RCTL, KC_RCTL, KC_NO, KC_LEFT, KC_DOWN, KC_RIGHT, },
  147. };
  148. uint8_t columns = KEY_POSITION_MAP_COLUMNS;
  149. uint8_t rows = KEY_POSITION_MAP_ROWS;
  150. for(uint8_t y = 0; y < rows; ++y){
  151. for(uint8_t x = 0; x < columns; ++x){
  152. uint8_t id1 = ktli(KEY_POSITION_MAP[y][x]);
  153. for(uint8_t j = y; j < rows; ++j){
  154. for(uint8_t i = 0; i < columns; ++i){
  155. uint8_t id2 = ktli(KEY_POSITION_MAP[j][i]);
  156. if(id1 == id2) continue;
  157. uint8_t dx = abs(i - x);
  158. uint8_t dy = abs(j - y);
  159. uint8_t dis = dx + dy;
  160. if(i < x && j > y){
  161. dis -= min(dx, dy);
  162. }
  163. uint8_t _dis = DISTANCE_MAP[id1][id2];
  164. if(_dis && _dis <= dis) continue;
  165. DISTANCE_MAP[id1][id2] = dis;
  166. DISTANCE_MAP[id2][id1] = dis;
  167. }
  168. }
  169. }
  170. }
  171. }
  172. void matrix_init_user(void) {
  173. init_keycode_to_led_map();
  174. init_distance_map();
  175. };
  176. // /tmk_core/protocol/arm_atsam/led_matrix.c: line 244
  177. uint8_t led_enabled;
  178. float led_animation_speed;
  179. uint8_t led_animation_direction;
  180. uint8_t led_animation_orientation;
  181. uint8_t led_animation_breathing;
  182. uint8_t led_animation_breathe_cur;
  183. uint8_t breathe_step;
  184. uint8_t breathe_dir;
  185. uint64_t led_next_run;
  186. uint8_t led_animation_id;
  187. uint8_t led_lighting_mode;
  188. issi3733_led_t *led_cur;
  189. uint8_t led_per_run;
  190. float breathe_mult;
  191. // overrided /tmk_core/protocol/arm_atsam/led_matrix.c: line 484
  192. void rgb_matrix_init_user(void){
  193. led_animation_speed = ANIMATION_SPEED_STEP * 15;
  194. led_per_run = 15;
  195. }
  196. // overrided /tmk_core/protocol/arm_atsam/led_matrix.c: line 262
  197. void led_matrix_run(void)
  198. {
  199. float ro;
  200. float go;
  201. float bo;
  202. float po;
  203. uint8_t led_this_run = 0;
  204. led_setup_t *f = (led_setup_t*)led_setups[led_animation_id];
  205. if (led_cur == 0) //Denotes start of new processing cycle in the case of chunked processing
  206. {
  207. led_cur = led_map;
  208. breathe_mult = 1;
  209. if (led_animation_breathing)
  210. {
  211. led_animation_breathe_cur += breathe_step * breathe_dir;
  212. if (led_animation_breathe_cur >= BREATHE_MAX_STEP)
  213. breathe_dir = -1;
  214. else if (led_animation_breathe_cur <= BREATHE_MIN_STEP)
  215. breathe_dir = 1;
  216. //Brightness curve created for 256 steps, 0 - ~98%
  217. breathe_mult = 0.000015 * led_animation_breathe_cur * led_animation_breathe_cur;
  218. if (breathe_mult > 1) breathe_mult = 1;
  219. else if (breathe_mult < 0) breathe_mult = 0;
  220. }
  221. }
  222. uint8_t fcur = 0;
  223. uint8_t fmax = 0;
  224. //Frames setup
  225. while (f[fcur].end != 1)
  226. {
  227. fcur++; //Count frames
  228. }
  229. fmax = fcur; //Store total frames count
  230. struct user_led_t user_led_cur;
  231. while (led_cur < lede && led_this_run < led_per_run)
  232. {
  233. ro = 0;
  234. go = 0;
  235. bo = 0;
  236. uint8_t led_index = led_cur - led_map; // only this part differs from the original function.
  237. if(led_index < KEY_LED_COUNT){ //
  238. user_led_cur = USER_LED[led_index]; // `struct user_led_t USER_LED[]` is stored globally.
  239. } //
  240. //
  241. if(led_index < KEY_LED_COUNT && user_led_cur.state){ // `user_led_cur` is just for convenience
  242. ro = user_led_cur.r; //
  243. go = user_led_cur.g; //
  244. bo = user_led_cur.b; //
  245. } //
  246. else if (led_lighting_mode == LED_MODE_KEYS_ONLY && led_cur->scan == 255)
  247. {
  248. //Do not act on this LED
  249. }
  250. else if (led_lighting_mode == LED_MODE_NON_KEYS_ONLY && led_cur->scan != 255)
  251. {
  252. //Do not act on this LED
  253. }
  254. else if (led_lighting_mode == LED_MODE_INDICATORS_ONLY)
  255. {
  256. //Do not act on this LED (Only show indicators)
  257. }
  258. else
  259. {
  260. //Act on LED
  261. for (fcur = 0; fcur < fmax; fcur++)
  262. {
  263. if (led_animation_orientation)
  264. {
  265. po = led_cur->py;
  266. }
  267. else
  268. {
  269. po = led_cur->px;
  270. }
  271. float pomod;
  272. pomod = (float)(g_tick % (uint32_t)(1000.0f / led_animation_speed)) / 10.0f * led_animation_speed;
  273. //Add in any moving effects
  274. if ((!led_animation_direction && f[fcur].ef & EF_SCR_R) || (led_animation_direction && (f[fcur].ef & EF_SCR_L)))
  275. {
  276. pomod *= 100.0f;
  277. pomod = (uint32_t)pomod % 10000;
  278. pomod /= 100.0f;
  279. po -= pomod;
  280. if (po > 100) po -= 100;
  281. else if (po < 0) po += 100;
  282. }
  283. else if ((!led_animation_direction && f[fcur].ef & EF_SCR_L) || (led_animation_direction && (f[fcur].ef & EF_SCR_R)))
  284. {
  285. pomod *= 100.0f;
  286. pomod = (uint32_t)pomod % 10000;
  287. pomod /= 100.0f;
  288. po += pomod;
  289. if (po > 100) po -= 100;
  290. else if (po < 0) po += 100;
  291. }
  292. //Check if LED's po is in current frame
  293. if (po < f[fcur].hs) continue;
  294. if (po > f[fcur].he) continue;
  295. //note: < 0 or > 100 continue
  296. //Calculate the po within the start-stop percentage for color blending
  297. po = (po - f[fcur].hs) / (f[fcur].he - f[fcur].hs);
  298. //Add in any color effects
  299. if (f[fcur].ef & EF_OVER)
  300. {
  301. ro = (po * (f[fcur].re - f[fcur].rs)) + f[fcur].rs;// + 0.5;
  302. go = (po * (f[fcur].ge - f[fcur].gs)) + f[fcur].gs;// + 0.5;
  303. bo = (po * (f[fcur].be - f[fcur].bs)) + f[fcur].bs;// + 0.5;
  304. }
  305. else if (f[fcur].ef & EF_SUBTRACT)
  306. {
  307. ro -= (po * (f[fcur].re - f[fcur].rs)) + f[fcur].rs;// + 0.5;
  308. go -= (po * (f[fcur].ge - f[fcur].gs)) + f[fcur].gs;// + 0.5;
  309. bo -= (po * (f[fcur].be - f[fcur].bs)) + f[fcur].bs;// + 0.5;
  310. }
  311. else
  312. {
  313. ro += (po * (f[fcur].re - f[fcur].rs)) + f[fcur].rs;// + 0.5;
  314. go += (po * (f[fcur].ge - f[fcur].gs)) + f[fcur].gs;// + 0.5;
  315. bo += (po * (f[fcur].be - f[fcur].bs)) + f[fcur].bs;// + 0.5;
  316. }
  317. }
  318. }
  319. //Clamp values 0-255
  320. if (ro > 255) ro = 255; else if (ro < 0) ro = 0;
  321. if (go > 255) go = 255; else if (go < 0) go = 0;
  322. if (bo > 255) bo = 255; else if (bo < 0) bo = 0;
  323. if (led_animation_breathing)
  324. {
  325. ro *= breathe_mult;
  326. go *= breathe_mult;
  327. bo *= breathe_mult;
  328. }
  329. *led_cur->rgb.r = (uint8_t)ro;
  330. *led_cur->rgb.g = (uint8_t)go;
  331. *led_cur->rgb.b = (uint8_t)bo;
  332. #ifdef USB_LED_INDICATOR_ENABLE
  333. if (keyboard_leds())
  334. {
  335. uint8_t kbled = keyboard_leds();
  336. if (
  337. #if USB_LED_NUM_LOCK_SCANCODE != 255
  338. (led_cur->scan == USB_LED_NUM_LOCK_SCANCODE && kbled & (1<<USB_LED_NUM_LOCK)) ||
  339. #endif //NUM LOCK
  340. #if USB_LED_CAPS_LOCK_SCANCODE != 255
  341. (led_cur->scan == USB_LED_CAPS_LOCK_SCANCODE && kbled & (1<<USB_LED_CAPS_LOCK)) ||
  342. #endif //CAPS LOCK
  343. #if USB_LED_SCROLL_LOCK_SCANCODE != 255
  344. (led_cur->scan == USB_LED_SCROLL_LOCK_SCANCODE && kbled & (1<<USB_LED_SCROLL_LOCK)) ||
  345. #endif //SCROLL LOCK
  346. #if USB_LED_COMPOSE_SCANCODE != 255
  347. (led_cur->scan == USB_LED_COMPOSE_SCANCODE && kbled & (1<<USB_LED_COMPOSE)) ||
  348. #endif //COMPOSE
  349. #if USB_LED_KANA_SCANCODE != 255
  350. (led_cur->scan == USB_LED_KANA_SCANCODE && kbled & (1<<USB_LED_KANA)) ||
  351. #endif //KANA
  352. (0))
  353. {
  354. if (*led_cur->rgb.r > 127) *led_cur->rgb.r = 0;
  355. else *led_cur->rgb.r = 255;
  356. if (*led_cur->rgb.g > 127) *led_cur->rgb.g = 0;
  357. else *led_cur->rgb.g = 255;
  358. if (*led_cur->rgb.b > 127) *led_cur->rgb.b = 0;
  359. else *led_cur->rgb.b = 255;
  360. }
  361. }
  362. #endif //USB_LED_INDICATOR_ENABLE
  363. led_cur++;
  364. led_this_run++;
  365. }
  366. }
  367. #define KEY_STROKES_LENGTH 20
  368. struct {
  369. bool alive;
  370. uint8_t led_id;
  371. uint32_t time;
  372. } KEY_STROKES[KEY_STROKES_LENGTH] = {{}};
  373. void set_led_rgb(uint8_t led_id, uint8_t r, uint8_t g, uint8_t b){
  374. issi3733_led_t *target_led = (led_map + led_id);
  375. *target_led->rgb.r = r;
  376. *target_led->rgb.g = g;
  377. *target_led->rgb.b = b;
  378. }
  379. uint8_t DISTANCE_FROM_LAST_KEYSTROKE[KEY_LED_COUNT+1];
  380. void calculate_keystroke_distance(void){
  381. bool alive;
  382. uint8_t led_id, period_passed;
  383. uint32_t t;
  384. for(uint8_t i = 0; i <= KEY_LED_COUNT; ++i){
  385. DISTANCE_FROM_LAST_KEYSTROKE[i] = 0;
  386. }
  387. for(uint8_t i = 0; i < KEY_STROKES_LENGTH; ++i){
  388. if(KEY_STROKES[i].alive){
  389. t = timer_elapsed32(KEY_STROKES[i].time);
  390. alive = 0;
  391. led_id = KEY_STROKES[i].led_id;
  392. period_passed = t / USER_CONFIG.WAVE_PERIOD;
  393. uint8_t delta_period;
  394. for(uint8_t j = 1; j <= KEY_LED_COUNT; ++j){
  395. delta_period = period_passed - DISTANCE_MAP[led_id][j];
  396. if(( delta_period < USER_CONFIG.WAVE_FRONT_WIDTH) && (
  397. DISTANCE_MAP[led_id][j] <= USER_CONFIG.TRAVEL_DISTANCE
  398. )){
  399. switch(USER_CONFIG.PATTERN_INDEX){
  400. case 3:
  401. case 4:
  402. case 5:
  403. case 6:
  404. DISTANCE_FROM_LAST_KEYSTROKE[j] += delta_period;
  405. break;
  406. default:
  407. DISTANCE_FROM_LAST_KEYSTROKE[j] = 1;
  408. break;
  409. }
  410. alive = 1;
  411. }
  412. }
  413. KEY_STROKES[i].alive = alive;
  414. }
  415. }
  416. }
  417. #define COLOR_PATTERN_RGB_COUNT 18
  418. static uint8_t COLOR_PATTERNS[][COLOR_PATTERN_RGB_COUNT][3] = {
  419. { // default rainbow color
  420. {255, 0, 0}, {255, 0, 0}, {255, 127, 0},
  421. {255, 127, 0}, {255, 255, 0}, {255, 255, 0},
  422. {120, 255, 0}, {120, 255, 0}, { 0, 255, 0},
  423. { 0, 255, 0}, { 0, 255, 120}, { 0, 255, 120},
  424. { 0, 0, 255}, { 0, 0, 255}, { 75, 0, 130},
  425. { 75, 0, 130}, { 43, 0, 130}, { 43, 0, 130},
  426. }, { // light rainbow color
  427. {248, 12, 18}, {238, 17, 0}, {255, 51, 17},
  428. {255, 68, 32}, {255, 102, 68}, {255, 153, 51},
  429. {254, 174, 45}, {204, 187, 51}, {208, 195, 16},
  430. {170, 204, 34}, {105, 208, 37}, { 34, 204, 170},
  431. { 18, 189, 185}, { 17, 170, 187}, { 68, 68, 221},
  432. { 51, 17, 187}, { 59, 12, 189}, { 68, 34, 153},
  433. }, { // white flat
  434. {255, 255, 255}, {255, 255, 255}, {255, 255, 255},
  435. {255, 255, 255}, {255, 255, 255}, {255, 255, 255},
  436. {255, 255, 255}, {255, 255, 255}, {255, 255, 255},
  437. {255, 255, 255}, {255, 255, 255}, {255, 255, 255},
  438. {255, 255, 255}, {255, 255, 255}, {255, 255, 255},
  439. {255, 255, 255}, {255, 255, 255}, {255, 255, 255},
  440. }, { // white fade, cos curve
  441. {255, 255, 255}, {255, 255, 255}, {252, 252, 252},
  442. {247, 247, 247}, {240, 240, 240}, {232, 232, 232},
  443. {221, 221, 221}, {209, 209, 209}, {196, 196, 196},
  444. {181, 181, 181}, {164, 164, 164}, {147, 147, 147},
  445. {128, 128, 128}, {108, 108, 108}, { 88, 88, 88},
  446. { 66, 66, 66}, { 45, 45, 45}, { 23, 23, 23},
  447. },
  448. };
  449. static const uint8_t COLOR_PATTERNS_COUNT = (
  450. sizeof(COLOR_PATTERNS) / sizeof(COLOR_PATTERNS[0]));
  451. void set_user_led_rgb(uint8_t i, uint8_t r, uint8_t g, uint8_t b){
  452. USER_LED[i-1].state = 1;
  453. USER_LED[i-1].r = r;
  454. USER_LED[i-1].g = g;
  455. USER_LED[i-1].b = b;
  456. }
  457. void unset_user_led_rgb(uint8_t i){
  458. USER_LED[i-1].state = 0;
  459. }
  460. void set_indicator_led_rgb(uint8_t i,
  461. uint8_t layer, uint8_t r, uint8_t g, uint8_t b){
  462. USER_LED[i-1].state |= 1 << layer;
  463. USER_LED[i-1].r = r;
  464. USER_LED[i-1].g = g;
  465. USER_LED[i-1].b = b;
  466. }
  467. void unset_indicator_led_rgb(uint8_t i, uint8_t layer){
  468. USER_LED[i-1].state &= ~(1 << layer);
  469. }
  470. void refresh_pattern_indicators(void){
  471. static uint8_t GRV_123456[] = {
  472. KC_GRV, KC_1, KC_2, KC_3, KC_4, KC_5, KC_6,
  473. };
  474. if(layer_state >= 0x04){
  475. for(uint8_t i = 0; i < 7; ++i){
  476. if(i == USER_CONFIG.PATTERN_INDEX){
  477. set_indicator_led_rgb(ktli(GRV_123456[i]), 2, 0, 0, 255);
  478. } else{
  479. set_indicator_led_rgb(ktli(GRV_123456[i]), 2, 0, 255, 0);
  480. }
  481. }
  482. } else{
  483. for(uint8_t i = 0; i < 7; ++i){
  484. unset_indicator_led_rgb(ktli(GRV_123456[i]), 2);
  485. }
  486. }
  487. }
  488. void refresh_color_pattern_indicators(void){
  489. static uint8_t ZXCVBNM_COMM_DOT[] = {
  490. KC_Z, KC_X, KC_C, KC_V, KC_B, KC_N, KC_M, KC_COMM, KC_DOT,
  491. };
  492. if(layer_state >= 0x04){
  493. uint8_t (*c)[3] = &COLOR_PATTERNS[USER_CONFIG.COLOR_PATTERN_INDEX][0];
  494. for(uint8_t i = 0; i < 9; ++i){
  495. set_indicator_led_rgb(ktli(ZXCVBNM_COMM_DOT[i]),
  496. 2, c[i][0], c[i][1], c[i][2]);
  497. }
  498. } else{
  499. for(uint8_t i = 0; i < 9; ++i){
  500. unset_indicator_led_rgb(ktli(ZXCVBNM_COMM_DOT[i]), 2);
  501. }
  502. }
  503. }
  504. // Runs constantly in the background, in a loop.
  505. void matrix_scan_user(void) {
  506. static uint32_t scan_timer = 0;
  507. static uint8_t last_layer = 0;
  508. uint8_t layer = 0;
  509. if(layer_state >= 0x04){
  510. layer = 2;
  511. } else if(layer_state >= 0x02){
  512. layer = 1;
  513. }
  514. calculate_keystroke_distance();
  515. #define USE_PATTERN 0
  516. #define BLACK_RGB 1
  517. #define COLOR_RGB 2
  518. uint8_t ci; // color index
  519. uint8_t *rgb;
  520. uint8_t handle_type;
  521. uint8_t distance;
  522. for(uint8_t i = 1; i <= KEY_LED_COUNT; ++i){
  523. if(USER_LED[i-1].state >= 2) continue;
  524. handle_type = USE_PATTERN;
  525. distance = DISTANCE_FROM_LAST_KEYSTROKE[i];
  526. switch(USER_CONFIG.PATTERN_INDEX){
  527. case 0: handle_type = USE_PATTERN; break;
  528. case 1: handle_type = distance ? USE_PATTERN : BLACK_RGB; break;
  529. case 2: handle_type = distance ? BLACK_RGB : USE_PATTERN; break;
  530. case 3: handle_type = distance ? COLOR_RGB : BLACK_RGB; break;
  531. case 4: handle_type = distance ? COLOR_RGB : USE_PATTERN; break;
  532. case 5:
  533. case 6: handle_type = distance ? COLOR_RGB : USE_PATTERN; break;
  534. }
  535. switch(handle_type){
  536. case USE_PATTERN: unset_user_led_rgb(i); break;
  537. case BLACK_RGB: set_user_led_rgb(i, 0, 0, 0); break;
  538. case COLOR_RGB:
  539. ci = (DISTANCE_FROM_LAST_KEYSTROKE[i] * COLOR_PATTERN_RGB_COUNT /
  540. USER_CONFIG.WAVE_FRONT_WIDTH) % COLOR_PATTERN_RGB_COUNT;
  541. rgb = &COLOR_PATTERNS[USER_CONFIG.COLOR_PATTERN_INDEX][ci][0];
  542. set_user_led_rgb(i, rgb[0], rgb[1], rgb[2]);
  543. break;
  544. }
  545. }
  546. // could be moved to process_record_user()
  547. if(layer != last_layer){
  548. static uint8_t QWEASDP[] = {
  549. KC_Q, KC_W, KC_E, KC_A, KC_S, KC_D, KC_P,
  550. };
  551. static uint8_t YUIOHJKL[] = {
  552. KC_Y, KC_U, KC_I, KC_O, KC_H, KC_J, KC_K, KC_L,
  553. };
  554. switch(last_layer){
  555. case 1:
  556. for(uint8_t i = 0; i < 7; ++i){
  557. unset_indicator_led_rgb(ktli(QWEASDP[i]), 1);
  558. }
  559. break;
  560. case 2:
  561. for(uint8_t i = 0; i < 6; ++i){
  562. unset_indicator_led_rgb(ktli(QWEASDP[i]), 2);
  563. }
  564. for(uint8_t i = 0; i < 8; ++i){
  565. unset_indicator_led_rgb(ktli(YUIOHJKL[i]), 2);
  566. }
  567. unset_indicator_led_rgb(ktli(KC_TAB), 2);
  568. unset_indicator_led_rgb(ktli(KC_CAPS), 2);
  569. break;
  570. }
  571. switch(layer){
  572. case 1:
  573. for(uint8_t i = 0; i < 7; ++i){
  574. set_indicator_led_rgb(ktli(QWEASDP[i]), 1, 255, 0, 0);
  575. }
  576. break;
  577. case 2:
  578. for(uint8_t i = 0; i < 6; ++i){
  579. set_indicator_led_rgb(ktli(QWEASDP[i]), 2, 0, 255, 0);
  580. }
  581. for(uint8_t i = 0; i < 8; ++i){
  582. set_indicator_led_rgb(ktli(YUIOHJKL[i]), 2, 0, 255, 0);
  583. }
  584. set_indicator_led_rgb(ktli(KC_TAB), 2, 0, 255, 0);
  585. set_indicator_led_rgb(ktli(KC_CAPS), 2, 0, 255, 0);
  586. break;
  587. }
  588. refresh_pattern_indicators();
  589. refresh_color_pattern_indicators();
  590. last_layer = layer;
  591. }
  592. switch(layer){
  593. case 0:
  594. if(timer_elapsed32(scan_timer) > 2000){
  595. scan_timer = timer_read32();
  596. } else if(timer_elapsed32(scan_timer) > 1000){
  597. // set_user_led_rgb(ktli(KC_F5), 255, 255, 255);
  598. }
  599. break;
  600. case 1:
  601. break;
  602. case 2:
  603. break;
  604. }
  605. };
  606. #define MODS_SHIFT (get_mods() & MOD_BIT(KC_LSHIFT) || get_mods() & MOD_BIT(KC_RSHIFT))
  607. #define MODS_CTRL (get_mods() & MOD_BIT(KC_LCTL) || get_mods() & MOD_BIT(KC_RCTRL))
  608. #define MODS_ALT (get_mods() & MOD_BIT(KC_LALT) || get_mods() & MOD_BIT(KC_RALT))
  609. bool process_record_user(uint16_t keycode, keyrecord_t *record) {
  610. static uint32_t key_timer;
  611. switch (keycode) {
  612. case L_BRI:
  613. if (record->event.pressed) {
  614. if (LED_GCR_STEP > LED_GCR_MAX - gcr_desired) gcr_desired = LED_GCR_MAX;
  615. else gcr_desired += LED_GCR_STEP;
  616. if (led_animation_breathing) gcr_breathe = gcr_desired;
  617. }
  618. return false;
  619. case L_BRD:
  620. if (record->event.pressed) {
  621. if (LED_GCR_STEP > gcr_desired) gcr_desired = 0;
  622. else gcr_desired -= LED_GCR_STEP;
  623. if (led_animation_breathing) gcr_breathe = gcr_desired;
  624. }
  625. return false;
  626. case L_PTN:
  627. if (record->event.pressed) {
  628. if (led_animation_id == led_setups_count - 1) led_animation_id = 0;
  629. else led_animation_id++;
  630. }
  631. return false;
  632. case L_PTP:
  633. if (record->event.pressed) {
  634. if (led_animation_id == 0) led_animation_id = led_setups_count - 1;
  635. else led_animation_id--;
  636. }
  637. return false;
  638. case L_PSI:
  639. if (record->event.pressed) {
  640. led_animation_speed += ANIMATION_SPEED_STEP;
  641. }
  642. return false;
  643. case L_PSD:
  644. if (record->event.pressed) {
  645. led_animation_speed -= ANIMATION_SPEED_STEP;
  646. if (led_animation_speed < 0) led_animation_speed = 0;
  647. }
  648. return false;
  649. case L_T_MD:
  650. if (record->event.pressed) {
  651. led_lighting_mode++;
  652. if (led_lighting_mode > LED_MODE_MAX_INDEX) led_lighting_mode = LED_MODE_NORMAL;
  653. }
  654. return false;
  655. case L_T_ONF:
  656. if (record->event.pressed) {
  657. led_enabled = !led_enabled;
  658. I2C3733_Control_Set(led_enabled);
  659. }
  660. return false;
  661. case L_ON:
  662. if (record->event.pressed) {
  663. led_enabled = 1;
  664. I2C3733_Control_Set(led_enabled);
  665. }
  666. return false;
  667. case L_OFF:
  668. if (record->event.pressed) {
  669. led_enabled = 0;
  670. I2C3733_Control_Set(led_enabled);
  671. }
  672. return false;
  673. case L_T_BR:
  674. if (record->event.pressed) {
  675. led_animation_breathing = !led_animation_breathing;
  676. if (led_animation_breathing) {
  677. gcr_breathe = gcr_desired;
  678. led_animation_breathe_cur = BREATHE_MIN_STEP;
  679. breathe_dir = 1;
  680. }
  681. }
  682. return false;
  683. case L_T_PTD:
  684. if (record->event.pressed) {
  685. led_animation_direction = !led_animation_direction;
  686. }
  687. return false;
  688. case U_T_AUTO:
  689. if (record->event.pressed && MODS_SHIFT && MODS_CTRL) {
  690. TOGGLE_FLAG_AND_PRINT(usb_extra_manual, "USB extra port manual mode");
  691. }
  692. return false;
  693. case U_T_AGCR:
  694. if (record->event.pressed && MODS_SHIFT && MODS_CTRL) {
  695. TOGGLE_FLAG_AND_PRINT(usb_gcr_auto, "USB GCR auto mode");
  696. }
  697. return false;
  698. case DBG_TOG:
  699. if (record->event.pressed) {
  700. TOGGLE_FLAG_AND_PRINT(debug_enable, "Debug mode");
  701. }
  702. return false;
  703. case DBG_MTRX:
  704. if (record->event.pressed) {
  705. TOGGLE_FLAG_AND_PRINT(debug_matrix, "Debug matrix");
  706. }
  707. return false;
  708. case DBG_KBD:
  709. if (record->event.pressed) {
  710. TOGGLE_FLAG_AND_PRINT(debug_keyboard, "Debug keyboard");
  711. }
  712. return false;
  713. case DBG_MOU:
  714. if (record->event.pressed) {
  715. TOGGLE_FLAG_AND_PRINT(debug_mouse, "Debug mouse");
  716. }
  717. return false;
  718. case MD_BOOT:
  719. if (record->event.pressed) {
  720. key_timer = timer_read32();
  721. } else {
  722. if (timer_elapsed32(key_timer) >= 500) {
  723. reset_keyboard();
  724. }
  725. }
  726. return false;
  727. case L_SP_PR: // previous dripple pattern
  728. case L_SP_NE: // next dripple pattern
  729. if (record->event.pressed) {
  730. #define PATTERN_COUNT 7
  731. uint8_t incre = keycode == L_SP_PR ? PATTERN_COUNT-1 : 1;
  732. USER_CONFIG.PATTERN_INDEX += incre;
  733. USER_CONFIG.PATTERN_INDEX %= PATTERN_COUNT;
  734. if(USER_CONFIG.PATTERN_INDEX <= 4){
  735. USER_CONFIG.TRAVEL_DISTANCE = 25;
  736. USER_CONFIG.COLOR_PATTERN_INDEX = 0;
  737. USER_CONFIG.WAVE_PERIOD = 50;
  738. }
  739. switch(USER_CONFIG.PATTERN_INDEX){
  740. case 0: // None
  741. break;
  742. case 1: // background off, wave on
  743. USER_CONFIG.WAVE_FRONT_WIDTH = 2;
  744. break;
  745. case 2: // background on, wave off
  746. USER_CONFIG.WAVE_FRONT_WIDTH = 5;
  747. break;
  748. case 3: // background off, rainbow wave
  749. USER_CONFIG.WAVE_FRONT_WIDTH = 10;
  750. break;
  751. case 4: // background on, rainbow wave
  752. USER_CONFIG.WAVE_FRONT_WIDTH = 10;
  753. break;
  754. case 5:
  755. USER_CONFIG.WAVE_FRONT_WIDTH = 10;
  756. USER_CONFIG.COLOR_PATTERN_INDEX = 2;
  757. USER_CONFIG.TRAVEL_DISTANCE = 0;
  758. USER_CONFIG.WAVE_PERIOD = 100;
  759. break;
  760. case 6:
  761. USER_CONFIG.WAVE_FRONT_WIDTH = 25;
  762. USER_CONFIG.COLOR_PATTERN_INDEX = 3;
  763. USER_CONFIG.TRAVEL_DISTANCE = 2;
  764. USER_CONFIG.WAVE_PERIOD = 10;
  765. break;
  766. }
  767. // remove effect after changing pattern
  768. for(int i = 0; i < KEY_STROKES_LENGTH; ++i){
  769. KEY_STROKES[i].alive = 0;
  770. }
  771. refresh_pattern_indicators();
  772. refresh_color_pattern_indicators();
  773. }
  774. return false;
  775. case L_SP_WD:
  776. case L_SP_NW:
  777. if(record->event.pressed){
  778. short incre = keycode == L_SP_WD ? 1 : -1;
  779. USER_CONFIG.WAVE_FRONT_WIDTH += incre;
  780. if(USER_CONFIG.WAVE_FRONT_WIDTH < 1){
  781. USER_CONFIG.WAVE_FRONT_WIDTH = 1;
  782. }
  783. }
  784. return false;
  785. case L_SP_FA:
  786. case L_SP_SL:
  787. if(record->event.pressed){
  788. short incre = keycode == L_SP_FA ? -1 : 1;
  789. USER_CONFIG.WAVE_PERIOD += 10 * incre;
  790. if(USER_CONFIG.WAVE_PERIOD < 10){
  791. USER_CONFIG.WAVE_PERIOD = 10;
  792. }
  793. }
  794. return false;
  795. // these are the keys not in range 0x04 - 0x52
  796. case L_CP_PR:
  797. case L_CP_NX:
  798. if(record->event.pressed){
  799. uint8_t incre = keycode == L_CP_PR ? COLOR_PATTERNS_COUNT - 1 : 1;
  800. USER_CONFIG.COLOR_PATTERN_INDEX += incre;
  801. USER_CONFIG.COLOR_PATTERN_INDEX %= COLOR_PATTERNS_COUNT;
  802. refresh_color_pattern_indicators();
  803. }
  804. return false;
  805. default:
  806. if (record->event.pressed){
  807. uint8_t led_id = ktli(keycode);
  808. if(led_id){
  809. for(int i = 0; i < KEY_STROKES_LENGTH; ++i){
  810. if(!KEY_STROKES[i].alive){
  811. KEY_STROKES[i].alive = 1;
  812. KEY_STROKES[i].led_id = led_id;
  813. KEY_STROKES[i].time = timer_read32();
  814. break;
  815. }
  816. }
  817. }
  818. }
  819. return true; //Process all other keycodes normally
  820. }
  821. }