keymap.c 35 KB

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