keymap.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208
  1. #include QMK_KEYBOARD_H
  2. #define H_TOP1 ((HASH_TYPE) 1 << 0)
  3. #define H_TOP2 ((HASH_TYPE) 1 << 1)
  4. #define H_TOP3 ((HASH_TYPE) 1 << 2)
  5. #define H_TOP4 ((HASH_TYPE) 1 << 3)
  6. #define H_TOP5 ((HASH_TYPE) 1 << 4)
  7. #define H_TOP6 ((HASH_TYPE) 1 << 5)
  8. #define H_TOP7 ((HASH_TYPE) 1 << 6)
  9. #define H_TOP8 ((HASH_TYPE) 1 << 7)
  10. #define H_TOP9 ((HASH_TYPE) 1 << 8)
  11. #define H_TOP10 ((HASH_TYPE) 1 << 9)
  12. #define H_TOP11 ((HASH_TYPE) 1 << 10)
  13. #define H_TOP12 ((HASH_TYPE) 1 << 11)
  14. #define H_BOT1 ((HASH_TYPE) 1 << 12)
  15. #define H_BOT2 ((HASH_TYPE) 1 << 13)
  16. #define H_BOT3 ((HASH_TYPE) 1 << 14)
  17. #define H_BOT4 ((HASH_TYPE) 1 << 15)
  18. #define H_BOT5 ((HASH_TYPE) 1 << 16)
  19. #define H_BOT6 ((HASH_TYPE) 1 << 17)
  20. #define H_BOT7 ((HASH_TYPE) 1 << 18)
  21. #define H_BOT8 ((HASH_TYPE) 1 << 19)
  22. #define H_BOT9 ((HASH_TYPE) 1 << 20)
  23. #define H_BOT10 ((HASH_TYPE) 1 << 21)
  24. #define H_BOT11 ((HASH_TYPE) 1 << 22)
  25. #define H_BOT12 ((HASH_TYPE) 1 << 23)
  26. #define H_THU1 ((HASH_TYPE) 1 << 24)
  27. #define H_THU2 ((HASH_TYPE) 1 << 25)
  28. #define H_THU3 ((HASH_TYPE) 1 << 26)
  29. #define H_THU4 ((HASH_TYPE) 1 << 27)
  30. #define H_THU5 ((HASH_TYPE) 1 << 28)
  31. #define H_THU6 ((HASH_TYPE) 1 << 29)
  32. enum internal_keycodes {
  33. TOP1 = SAFE_RANGE,
  34. TOP2, TOP3, TOP4, TOP5, TOP6, TOP7, TOP8, TOP9, TOP10, TOP11, TOP12, BOT1, BOT2, BOT3, BOT4, BOT5, BOT6, BOT7, BOT8, BOT9, BOT10, BOT11, BOT12, THU1, THU2, THU3, THU4, THU5, THU6,
  35. FIRST_INTERNAL_KEYCODE = TOP1,
  36. LAST_INTERNAL_KEYCODE = THU6
  37. };
  38. enum pseudolayers {
  39. ALWAYS_ON, QWERTY, NUM, FNC, NAV, MOUSE
  40. };
  41. #define CHORD_TIMEOUT 100
  42. #define DANCE_TIMEOUT 200
  43. #define LEADER_TIMEOUT 750
  44. #define TAP_TIMEOUT 50
  45. #define LONG_PRESS_MULTIPLIER 3
  46. #define DYNAMIC_MACRO_MAX_LENGTH 20
  47. #define COMMAND_MAX_LENGTH 5
  48. #define STRING_MAX_LENGTH 16
  49. #define LEADER_MAX_LENGTH 5
  50. #define HASH_TYPE uint32_t
  51. #define NUMBER_OF_KEYS 30
  52. #define DEFAULT_PSEUDOLAYER QWERTY
  53. const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
  54. [0] = LAYOUT_georgi(TOP1, TOP2, TOP3, TOP4, TOP5, TOP6, TOP7, TOP8, TOP9, TOP10, TOP11, TOP12, BOT1, BOT2, BOT3, BOT4, BOT5, BOT6, BOT7, BOT8, BOT9, BOT10, BOT11, BOT12, THU1, THU2, THU3, THU4, THU5, THU6),
  55. };
  56. size_t keymapsCount = 1;
  57. uint8_t keycodes_buffer_array[] = {
  58. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  59. };
  60. uint8_t command_buffer[] = {
  61. 0, 0, 0, 0, 0
  62. };
  63. uint16_t leader_buffer[] = {
  64. 0, 0, 0, 0, 0
  65. };
  66. uint8_t dynamic_macro_buffer[] = {
  67. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  68. };
  69. enum chord_states {
  70. IDLE,
  71. READY,
  72. ACTIVATED,
  73. DEACTIVATED,
  74. PRESS_FROM_ACTIVE,
  75. FINISHED_FROM_ACTIVE,
  76. IDLE_IN_DANCE,
  77. READY_IN_DANCE,
  78. FINISHED,
  79. LOCKED,
  80. READY_LOCKED,
  81. RESTART,
  82. IN_ONE_SHOT
  83. };
  84. struct Chord {
  85. uint32_t keycodes_hash;
  86. uint8_t pseudolayer;
  87. uint8_t* state;
  88. uint8_t* counter;
  89. uint16_t value1;
  90. uint8_t value2;
  91. void (*function) (const struct Chord*);
  92. };
  93. uint8_t current_pseudolayer = DEFAULT_PSEUDOLAYER;
  94. bool lock_next = false;
  95. uint16_t chord_timer = 0;
  96. uint16_t dance_timer = 0;
  97. bool autoshift_mode = true;
  98. uint8_t keycode_index = 0;
  99. uint8_t command_mode = 0;
  100. uint8_t command_ind = 0;
  101. bool in_leader_mode = false;
  102. uint8_t leader_ind = 0;
  103. uint16_t leader_timer = 0;
  104. uint8_t dynamic_macro_mode = false;
  105. uint8_t dynamic_macro_ind = 0;
  106. bool a_key_went_through = false;
  107. struct Chord* last_chord = NULL;
  108. bool handle_US_ANSI_shifted_keys(int16_t keycode, bool in) {
  109. bool is_US_ANSI_shifted = true;
  110. int16_t regular_keycode = KC_NO;
  111. switch (keycode) {
  112. case KC_TILDE:
  113. regular_keycode = KC_GRAVE;
  114. break;
  115. case KC_EXCLAIM:
  116. regular_keycode = KC_1;
  117. break;
  118. case KC_AT:
  119. regular_keycode = KC_2;
  120. break;
  121. case KC_HASH:
  122. regular_keycode = KC_3;
  123. break;
  124. case KC_DOLLAR:
  125. regular_keycode = KC_4;
  126. break;
  127. case KC_PERCENT:
  128. regular_keycode = KC_5;
  129. break;
  130. case KC_CIRCUMFLEX:
  131. regular_keycode = KC_6;
  132. break;
  133. case KC_AMPERSAND:
  134. regular_keycode = KC_7;
  135. break;
  136. case KC_ASTERISK:
  137. regular_keycode = KC_8;
  138. break;
  139. case KC_LEFT_PAREN:
  140. regular_keycode = KC_9;
  141. break;
  142. case KC_RIGHT_PAREN:
  143. regular_keycode = KC_0;
  144. break;
  145. case KC_UNDERSCORE:
  146. regular_keycode = KC_MINUS;
  147. break;
  148. case KC_PLUS:
  149. regular_keycode = KC_EQUAL;
  150. break;
  151. case KC_LEFT_CURLY_BRACE:
  152. regular_keycode = KC_LEFT_BRACKET;
  153. break;
  154. case KC_RIGHT_CURLY_BRACE:
  155. regular_keycode = KC_RIGHT_BRACKET;
  156. break;
  157. case KC_PIPE:
  158. regular_keycode = KC_BACKSLASH;
  159. break;
  160. case KC_COLON:
  161. regular_keycode = KC_SEMICOLON;
  162. break;
  163. case KC_DOUBLE_QUOTE:
  164. regular_keycode = KC_QUOTE;
  165. break;
  166. case KC_LEFT_ANGLE_BRACKET:
  167. regular_keycode = KC_COMMA;
  168. break;
  169. case KC_RIGHT_ANGLE_BRACKET:
  170. regular_keycode = KC_DOT;
  171. break;
  172. case KC_QUESTION:
  173. regular_keycode = KC_SLASH;
  174. break;
  175. default:
  176. is_US_ANSI_shifted = false;
  177. }
  178. if (is_US_ANSI_shifted) {
  179. if (in) {
  180. register_code(KC_LSFT);
  181. register_code(regular_keycode);
  182. } else {
  183. unregister_code(regular_keycode);
  184. unregister_code(KC_LSFT);
  185. }
  186. }
  187. return is_US_ANSI_shifted;
  188. }
  189. void key_in(int16_t keycode) {
  190. if (command_mode == 1 && command_ind < COMMAND_MAX_LENGTH) {
  191. command_buffer[command_ind] = keycode;
  192. command_ind++;
  193. a_key_went_through = true;
  194. } else if (in_leader_mode && leader_ind < LEADER_MAX_LENGTH) {
  195. leader_buffer[leader_ind] = keycode;
  196. leader_ind++;
  197. a_key_went_through = true;
  198. } else if (dynamic_macro_mode && dynamic_macro_ind < DYNAMIC_MACRO_MAX_LENGTH) {
  199. dynamic_macro_buffer[dynamic_macro_ind] = keycode;
  200. dynamic_macro_ind++;
  201. a_key_went_through = true;
  202. } else {
  203. if (!handle_US_ANSI_shifted_keys(keycode, true)) {
  204. register_code(keycode);
  205. }
  206. send_keyboard_report();
  207. a_key_went_through = true;
  208. }
  209. }
  210. void key_out(int16_t keycode) {
  211. if (command_mode == 0) {
  212. if (!handle_US_ANSI_shifted_keys(keycode, false)) {
  213. if (command_mode == 0 && in_leader_mode == false && dynamic_macro_mode == false) {
  214. unregister_code(keycode);
  215. }
  216. }
  217. send_keyboard_report();
  218. }
  219. }
  220. void tap_key(int16_t keycode) {
  221. key_in(keycode);
  222. wait_ms(TAP_TIMEOUT);
  223. key_out(keycode);
  224. }
  225. const char * const strings[] PROGMEM = {
  226. };
  227. void single_dance(const struct Chord* self) {
  228. switch (*self->state) {
  229. case ACTIVATED:
  230. key_in(self->value1);
  231. break;
  232. case DEACTIVATED:
  233. key_out(self->value1);
  234. *self->state = IDLE;
  235. break;
  236. case RESTART:
  237. key_out(self->value1);
  238. break;
  239. default:
  240. break;
  241. }
  242. }
  243. void key_layer_dance(const struct Chord* self) {
  244. switch (*self->state) {
  245. case ACTIVATED:
  246. current_pseudolayer = self->value2;
  247. a_key_went_through = false;
  248. break;
  249. case DEACTIVATED:
  250. case RESTART:
  251. if (!a_key_went_through) {
  252. tap_key(self->value1);
  253. }
  254. current_pseudolayer = self->pseudolayer;
  255. *self->state = IDLE; // does not have effect if the state was RESTART
  256. break;
  257. default:
  258. break;
  259. }
  260. }
  261. void key_mod_dance(const struct Chord* self) {
  262. switch (*self->state) {
  263. case ACTIVATED:
  264. key_in(self->value2);
  265. a_key_went_through = false;
  266. break;
  267. case DEACTIVATED:
  268. case RESTART:
  269. key_out(self->value2);
  270. if (!a_key_went_through) {
  271. tap_key(self->value1);
  272. }
  273. *self->state = IDLE; // does not have effect if the state was RESTART
  274. break;
  275. default:
  276. break;
  277. }
  278. }
  279. void key_key_dance(const struct Chord* self) {
  280. switch (*self->state) {
  281. case ACTIVATED:
  282. break;
  283. case DEACTIVATED:
  284. tap_key(self->value1);
  285. *self->state = IDLE;
  286. break;
  287. case FINISHED:
  288. case PRESS_FROM_ACTIVE:
  289. key_in(self->value2);
  290. break;
  291. case RESTART:
  292. key_out(self->value2);
  293. break;
  294. default:
  295. break;
  296. }
  297. }
  298. void autoshift_dance_impl(const struct Chord* self) {
  299. switch (*self->state) {
  300. case ACTIVATED:
  301. *self->counter = 0;
  302. break;
  303. case DEACTIVATED:
  304. case RESTART:
  305. tap_key(self->value1);
  306. *self->state = IDLE;
  307. break;
  308. case FINISHED_FROM_ACTIVE:
  309. if (*self->counter == (LONG_PRESS_MULTIPLIER - 2)) {
  310. key_in(KC_LSFT);
  311. tap_key(self->value1);
  312. key_out(KC_LSFT);
  313. *self->state = IDLE;
  314. // the skip to IDLE is usually just a lag optimization,
  315. // in this case it has a logic function, on a short
  316. // press (still longer than a tap) the key does not get shifted
  317. } else {
  318. *self->counter += 1;
  319. *self->state = PRESS_FROM_ACTIVE;
  320. dance_timer = timer_read();
  321. }
  322. break;
  323. default:
  324. break;
  325. }
  326. }
  327. void autoshift_dance(const struct Chord* self) {
  328. if (autoshift_mode) {
  329. autoshift_dance_impl(self);
  330. } else {
  331. single_dance(self);
  332. }
  333. }
  334. void autoshift_toggle(const struct Chord* self){
  335. if (*self->state == ACTIVATED) {
  336. autoshift_mode = !autoshift_mode;
  337. *self->state = IDLE;
  338. }
  339. }
  340. void temp_pseudolayer(const struct Chord* self) {
  341. switch (*self->state) {
  342. case ACTIVATED:
  343. current_pseudolayer = self->value1;
  344. break;
  345. case DEACTIVATED:
  346. current_pseudolayer = self->pseudolayer;
  347. *self->state = IDLE;
  348. break;
  349. case RESTART:
  350. current_pseudolayer = self->pseudolayer;
  351. break;
  352. default:
  353. break;
  354. }
  355. }
  356. void temp_pseudolayer_alt(const struct Chord* self) {
  357. switch (*self->state) {
  358. case ACTIVATED:
  359. current_pseudolayer = self->value1;
  360. break;
  361. case DEACTIVATED:
  362. current_pseudolayer = self->value2;
  363. *self->state = IDLE;
  364. break;
  365. case RESTART:
  366. current_pseudolayer = self->value2;
  367. break;
  368. default:
  369. break;
  370. }
  371. }
  372. void perm_pseudolayer(const struct Chord* self) {
  373. if (*self->state == ACTIVATED) {
  374. current_pseudolayer = self->value1;
  375. *self->state = IDLE;
  376. }
  377. }
  378. void switch_layer(const struct Chord* self) {
  379. if (*self->state == ACTIVATED) {
  380. layer_move(self->value1);
  381. *self->state = IDLE;
  382. }
  383. }
  384. void lock(const struct Chord* self) {
  385. if (*self->state == ACTIVATED) {
  386. lock_next = true;
  387. *self->state = IDLE;
  388. }
  389. }
  390. void one_shot_key(const struct Chord* self) {
  391. switch (*self->state) {
  392. case ACTIVATED:
  393. break;
  394. case DEACTIVATED:
  395. key_in(self->value1);
  396. *self->state = IN_ONE_SHOT;
  397. break;
  398. case FINISHED:
  399. case PRESS_FROM_ACTIVE:
  400. key_in(self->value1);
  401. a_key_went_through = false;
  402. break;
  403. case RESTART:
  404. if (a_key_went_through) {
  405. key_out(self->value1);
  406. } else {
  407. *self->state = IN_ONE_SHOT;
  408. }
  409. default:
  410. break;
  411. }
  412. }
  413. void one_shot_layer(const struct Chord* self) {
  414. switch (*self->state) {
  415. case ACTIVATED:
  416. break;
  417. case DEACTIVATED:
  418. current_pseudolayer = self->value1;
  419. *self->state = IN_ONE_SHOT;
  420. break;
  421. case FINISHED:
  422. case PRESS_FROM_ACTIVE:
  423. current_pseudolayer = self->value1;
  424. a_key_went_through = false;
  425. break;
  426. case RESTART:
  427. if (a_key_went_through) {
  428. current_pseudolayer = self->pseudolayer;
  429. } else {
  430. *self->state = IN_ONE_SHOT;
  431. }
  432. default:
  433. break;
  434. }
  435. }
  436. void command(const struct Chord* self) {
  437. if (*self->state == ACTIVATED) {
  438. command_mode++;
  439. *self->state = IDLE;
  440. }
  441. }
  442. bool identical(uint16_t* buffer1, uint16_t* buffer2) {
  443. bool same = true;
  444. for (int i = 0; i < LEADER_MAX_LENGTH; i++) {
  445. same = same && (buffer1[i] == buffer2[i]);
  446. }
  447. return same;
  448. }
  449. void leader(const struct Chord* self) {
  450. if (*self->state == ACTIVATED) {
  451. in_leader_mode = true;
  452. *self->state = IDLE;
  453. }
  454. }
  455. void dynamic_macro_record(const struct Chord* self) {
  456. if (*self->state == ACTIVATED) {
  457. for (int i = 0; i < DYNAMIC_MACRO_MAX_LENGTH; i++) {
  458. dynamic_macro_buffer[i] = 0;
  459. }
  460. dynamic_macro_mode = true;
  461. *self->state = IDLE;
  462. }
  463. }
  464. void dynamic_macro_next(const struct Chord* self) {
  465. if (*self->state == ACTIVATED) {
  466. if (dynamic_macro_mode && dynamic_macro_ind < DYNAMIC_MACRO_MAX_LENGTH) {
  467. dynamic_macro_buffer[dynamic_macro_ind] = 0;
  468. dynamic_macro_ind++;
  469. }
  470. *self->state = IDLE;
  471. }
  472. }
  473. void dynamic_macro_end(const struct Chord* self) {
  474. if (*self->state == ACTIVATED) {
  475. if (dynamic_macro_mode) {
  476. dynamic_macro_mode = false;
  477. }
  478. *self->state = IDLE;
  479. }
  480. }
  481. void dynamic_macro_play(const struct Chord* self) {
  482. if (*self->state == ACTIVATED) {
  483. int ind_start = 0;
  484. while (ind_start < DYNAMIC_MACRO_MAX_LENGTH) {
  485. for (int i = ind_start; i < DYNAMIC_MACRO_MAX_LENGTH; i++) {
  486. if (dynamic_macro_buffer[i] == 0) {
  487. break;
  488. }
  489. register_code(dynamic_macro_buffer[i]);
  490. }
  491. send_keyboard_report();
  492. wait_ms(TAP_TIMEOUT);
  493. for (int i = ind_start; i < DYNAMIC_MACRO_MAX_LENGTH; i++) {
  494. if (dynamic_macro_buffer[i] == 0) {
  495. ind_start = i + 1;
  496. break;
  497. }
  498. unregister_code(dynamic_macro_buffer[i]);
  499. }
  500. send_keyboard_report();
  501. }
  502. *self->state = IDLE;
  503. }
  504. }
  505. void string_in(const struct Chord* self) {
  506. if (*self->state == ACTIVATED) {
  507. char buffer[STRING_MAX_LENGTH];
  508. strcpy_P(buffer, (char*)pgm_read_word(&(strings[self->value1])));
  509. send_string(buffer);
  510. }
  511. }
  512. void clear(const struct Chord* self);
  513. void reset_keyboard_kb(void){
  514. #ifdef WATCHDOG_ENABLE
  515. MCUSR = 0;
  516. wdt_disable();
  517. wdt_reset();
  518. #endif
  519. reset_keyboard();
  520. }
  521. void reset(const struct Chord* self) {
  522. if (*self->state == ACTIVATED) {
  523. reset_keyboard_kb();
  524. }
  525. }
  526. uint8_t state_0 = IDLE;
  527. const struct Chord chord_0 PROGMEM = {H_TOP1 + H_TOP12 + H_BOT1 + H_BOT12, ALWAYS_ON, &state_0, NULL, 0, 0, clear};
  528. uint8_t state_1 = IDLE;
  529. const struct Chord chord_1 PROGMEM = {H_TOP6 + H_TOP7 + H_BOT6 + H_BOT7, ALWAYS_ON, &state_1, NULL, 0, 0, command};
  530. uint8_t state_2 = IDLE;
  531. const struct Chord chord_2 PROGMEM = {H_TOP1, QWERTY, &state_2, NULL, KC_ESC, 0, single_dance};
  532. uint8_t state_3 = IDLE;
  533. const struct Chord chord_3 PROGMEM = {H_TOP2, QWERTY, &state_3, NULL, KC_Q, 0, single_dance};
  534. uint8_t state_4 = IDLE;
  535. const struct Chord chord_4 PROGMEM = {H_TOP3, QWERTY, &state_4, NULL, KC_W, 0, single_dance};
  536. uint8_t state_5 = IDLE;
  537. const struct Chord chord_5 PROGMEM = {H_TOP4, QWERTY, &state_5, NULL, KC_E, 0, single_dance};
  538. uint8_t state_6 = IDLE;
  539. const struct Chord chord_6 PROGMEM = {H_TOP5, QWERTY, &state_6, NULL, KC_R, 0, single_dance};
  540. uint8_t state_7 = IDLE;
  541. const struct Chord chord_7 PROGMEM = {H_TOP6, QWERTY, &state_7, NULL, KC_T, 0, single_dance};
  542. uint8_t state_8 = IDLE;
  543. const struct Chord chord_8 PROGMEM = {H_TOP7, QWERTY, &state_8, NULL, KC_Y, 0, single_dance};
  544. uint8_t state_9 = IDLE;
  545. const struct Chord chord_9 PROGMEM = {H_TOP8, QWERTY, &state_9, NULL, KC_U, 0, single_dance};
  546. uint8_t state_10 = IDLE;
  547. const struct Chord chord_10 PROGMEM = {H_TOP9, QWERTY, &state_10, NULL, KC_I, 0, single_dance};
  548. uint8_t state_11 = IDLE;
  549. const struct Chord chord_11 PROGMEM = {H_TOP10, QWERTY, &state_11, NULL, KC_O, 0, single_dance};
  550. uint8_t state_12 = IDLE;
  551. const struct Chord chord_12 PROGMEM = {H_TOP11, QWERTY, &state_12, NULL, KC_P, 0, single_dance};
  552. uint8_t state_13 = IDLE;
  553. const struct Chord chord_13 PROGMEM = {H_TOP12, QWERTY, &state_13, NULL, KC_BACKSLASH, 0, single_dance};
  554. uint8_t state_14 = IDLE;
  555. const struct Chord chord_14 PROGMEM = {H_TOP1 + H_BOT1, QWERTY, &state_14, NULL, KC_INS, 0, single_dance};
  556. uint8_t state_15 = IDLE;
  557. const struct Chord chord_15 PROGMEM = {H_TOP2 + H_BOT2, QWERTY, &state_15, NULL, KC_A, 0, single_dance};
  558. uint8_t state_16 = IDLE;
  559. uint8_t counter_16 = 0;
  560. const struct Chord chord_16 PROGMEM = {H_TOP3 + H_BOT3, QWERTY, &state_16, &counter_16, KC_S, KC_LALT, key_key_dance};
  561. uint8_t state_17 = IDLE;
  562. const struct Chord chord_17 PROGMEM = {H_TOP4 + H_BOT4, QWERTY, &state_17, NULL, KC_D, KC_LCTL, key_mod_dance};
  563. uint8_t state_18 = IDLE;
  564. const struct Chord chord_18 PROGMEM = {H_TOP5 + H_BOT5, QWERTY, &state_18, NULL, KC_F, KC_LSFT, key_mod_dance};
  565. uint8_t state_19 = IDLE;
  566. uint8_t counter_19 = 0;
  567. const struct Chord chord_19 PROGMEM = {H_TOP6 + H_BOT6, QWERTY, &state_19, &counter_19, KC_G, KC_LGUI, key_key_dance};
  568. uint8_t state_20 = IDLE;
  569. uint8_t counter_20 = 0;
  570. const struct Chord chord_20 PROGMEM = {H_TOP7 + H_BOT7, QWERTY, &state_20, &counter_20, KC_H, KC_RGUI, key_key_dance};
  571. uint8_t state_21 = IDLE;
  572. const struct Chord chord_21 PROGMEM = {H_TOP8 + H_BOT8, QWERTY, &state_21, NULL, KC_J, KC_RSFT, key_mod_dance};
  573. uint8_t state_22 = IDLE;
  574. const struct Chord chord_22 PROGMEM = {H_TOP9 + H_BOT9, QWERTY, &state_22, NULL, KC_K, KC_RCTL, key_mod_dance};
  575. uint8_t state_23 = IDLE;
  576. uint8_t counter_23 = 0;
  577. const struct Chord chord_23 PROGMEM = {H_TOP10 + H_BOT10, QWERTY, &state_23, &counter_23, KC_L, KC_RALT, key_key_dance};
  578. uint8_t state_24 = IDLE;
  579. const struct Chord chord_24 PROGMEM = {H_TOP11 + H_BOT11, QWERTY, &state_24, NULL, KC_SEMICOLON, 0, single_dance};
  580. uint8_t state_25 = IDLE;
  581. const struct Chord chord_25 PROGMEM = {H_BOT1, QWERTY, &state_25, NULL, KC_TAB, 0, single_dance};
  582. uint8_t state_26 = IDLE;
  583. const struct Chord chord_26 PROGMEM = {H_BOT2, QWERTY, &state_26, NULL, KC_Z, 0, single_dance};
  584. uint8_t state_27 = IDLE;
  585. const struct Chord chord_27 PROGMEM = {H_BOT3, QWERTY, &state_27, NULL, KC_X, 0, single_dance};
  586. uint8_t state_28 = IDLE;
  587. const struct Chord chord_28 PROGMEM = {H_BOT4, QWERTY, &state_28, NULL, KC_C, 0, single_dance};
  588. uint8_t state_29 = IDLE;
  589. const struct Chord chord_29 PROGMEM = {H_BOT5, QWERTY, &state_29, NULL, KC_V, 0, single_dance};
  590. uint8_t state_30 = IDLE;
  591. const struct Chord chord_30 PROGMEM = {H_BOT6, QWERTY, &state_30, NULL, KC_B, 0, single_dance};
  592. uint8_t state_31 = IDLE;
  593. const struct Chord chord_31 PROGMEM = {H_BOT7, QWERTY, &state_31, NULL, KC_N, 0, single_dance};
  594. uint8_t state_32 = IDLE;
  595. const struct Chord chord_32 PROGMEM = {H_BOT8, QWERTY, &state_32, NULL, KC_M, 0, single_dance};
  596. uint8_t state_33 = IDLE;
  597. const struct Chord chord_33 PROGMEM = {H_BOT9, QWERTY, &state_33, NULL, KC_COMMA, 0, single_dance};
  598. uint8_t state_34 = IDLE;
  599. const struct Chord chord_34 PROGMEM = {H_BOT10, QWERTY, &state_34, NULL, KC_DOT, 0, single_dance};
  600. uint8_t state_35 = IDLE;
  601. const struct Chord chord_35 PROGMEM = {H_BOT11, QWERTY, &state_35, NULL, KC_SLASH, 0, single_dance};
  602. uint8_t state_36 = IDLE;
  603. const struct Chord chord_36 PROGMEM = {H_BOT12, QWERTY, &state_36, NULL, KC_QUOTE, 0, single_dance};
  604. uint8_t state_37 = IDLE;
  605. const struct Chord chord_37 PROGMEM = {H_THU1, QWERTY, &state_37, NULL, KC_ENTER, 0, single_dance};
  606. uint8_t state_38 = IDLE;
  607. uint8_t counter_38 = 0;
  608. const struct Chord chord_38 PROGMEM = {H_THU2, QWERTY, &state_38, &counter_38, KC_SPC, NUM, key_layer_dance};
  609. uint8_t state_39 = IDLE;
  610. uint8_t counter_39 = 0;
  611. const struct Chord chord_39 PROGMEM = {H_THU3, QWERTY, &state_39, &counter_39, KC_BSPC, NAV, key_layer_dance};
  612. uint8_t state_40 = IDLE;
  613. const struct Chord chord_40 PROGMEM = {H_THU4, QWERTY, &state_40, NULL, KC_DEL, 0, single_dance};
  614. uint8_t state_41 = IDLE;
  615. uint8_t counter_41 = 0;
  616. const struct Chord chord_41 PROGMEM = {H_THU5, QWERTY, &state_41, &counter_41, KC_SPC, FNC, key_layer_dance};
  617. uint8_t state_42 = IDLE;
  618. const struct Chord chord_42 PROGMEM = {H_THU6, QWERTY, &state_42, NULL, KC_ENTER, 0, single_dance};
  619. uint8_t state_43 = IDLE;
  620. const struct Chord chord_43 PROGMEM = {H_THU2 + H_THU3, QWERTY, &state_43, NULL, MOUSE, 0, temp_pseudolayer};
  621. uint8_t state_44 = IDLE;
  622. const struct Chord chord_44 PROGMEM = {H_TOP1, NUM, &state_44, NULL, KC_GRAVE, 0, single_dance};
  623. uint8_t state_45 = IDLE;
  624. const struct Chord chord_45 PROGMEM = {H_TOP2, NUM, &state_45, NULL, KC_1, 0, single_dance};
  625. uint8_t state_46 = IDLE;
  626. const struct Chord chord_46 PROGMEM = {H_TOP3, NUM, &state_46, NULL, KC_2, 0, single_dance};
  627. uint8_t state_47 = IDLE;
  628. const struct Chord chord_47 PROGMEM = {H_TOP4, NUM, &state_47, NULL, KC_3, 0, single_dance};
  629. uint8_t state_48 = IDLE;
  630. const struct Chord chord_48 PROGMEM = {H_TOP5, NUM, &state_48, NULL, KC_4, 0, single_dance};
  631. uint8_t state_49 = IDLE;
  632. const struct Chord chord_49 PROGMEM = {H_TOP6, NUM, &state_49, NULL, KC_5, 0, single_dance};
  633. uint8_t state_50 = IDLE;
  634. const struct Chord chord_50 PROGMEM = {H_TOP7, NUM, &state_50, NULL, KC_6, 0, single_dance};
  635. uint8_t state_51 = IDLE;
  636. const struct Chord chord_51 PROGMEM = {H_TOP8, NUM, &state_51, NULL, KC_7, 0, single_dance};
  637. uint8_t state_52 = IDLE;
  638. const struct Chord chord_52 PROGMEM = {H_TOP9, NUM, &state_52, NULL, KC_8, 0, single_dance};
  639. uint8_t state_53 = IDLE;
  640. const struct Chord chord_53 PROGMEM = {H_TOP10, NUM, &state_53, NULL, KC_9, 0, single_dance};
  641. uint8_t state_54 = IDLE;
  642. const struct Chord chord_54 PROGMEM = {H_TOP11, NUM, &state_54, NULL, KC_0, 0, single_dance};
  643. uint8_t state_55 = IDLE;
  644. const struct Chord chord_55 PROGMEM = {H_TOP12, NUM, &state_55, NULL, KC_MINUS, 0, single_dance};
  645. uint8_t state_56 = IDLE;
  646. const struct Chord chord_56 PROGMEM = {H_TOP3 + H_BOT3, NUM, &state_56, NULL, KC_LALT, 0, single_dance};
  647. uint8_t state_57 = IDLE;
  648. const struct Chord chord_57 PROGMEM = {H_TOP4 + H_BOT4, NUM, &state_57, NULL, KC_LCTL, 0, single_dance};
  649. uint8_t state_58 = IDLE;
  650. const struct Chord chord_58 PROGMEM = {H_TOP5 + H_BOT5, NUM, &state_58, NULL, KC_LSFT, 0, single_dance};
  651. uint8_t state_59 = IDLE;
  652. const struct Chord chord_59 PROGMEM = {H_TOP6 + H_BOT6, NUM, &state_59, NULL, KC_LGUI, 0, single_dance};
  653. uint8_t state_60 = IDLE;
  654. const struct Chord chord_60 PROGMEM = {H_TOP7 + H_BOT7, NUM, &state_60, NULL, KC_RGUI, 0, single_dance};
  655. uint8_t state_61 = IDLE;
  656. const struct Chord chord_61 PROGMEM = {H_TOP8 + H_BOT8, NUM, &state_61, NULL, KC_RSFT, 0, single_dance};
  657. uint8_t state_62 = IDLE;
  658. const struct Chord chord_62 PROGMEM = {H_TOP9 + H_BOT9, NUM, &state_62, NULL, KC_RCTL, 0, single_dance};
  659. uint8_t state_63 = IDLE;
  660. const struct Chord chord_63 PROGMEM = {H_TOP10 + H_BOT10, NUM, &state_63, NULL, KC_RALT, 0, single_dance};
  661. uint8_t state_64 = IDLE;
  662. const struct Chord chord_64 PROGMEM = {H_BOT12, NUM, &state_64, NULL, KC_EQUAL, 0, single_dance};
  663. uint8_t state_65 = IDLE;
  664. const struct Chord chord_65 PROGMEM = {H_TOP2, FNC, &state_65, NULL, KC_F1, 0, single_dance};
  665. uint8_t state_66 = IDLE;
  666. const struct Chord chord_66 PROGMEM = {H_TOP3, FNC, &state_66, NULL, KC_F2, 0, single_dance};
  667. uint8_t state_67 = IDLE;
  668. const struct Chord chord_67 PROGMEM = {H_TOP4, FNC, &state_67, NULL, KC_F3, 0, single_dance};
  669. uint8_t state_68 = IDLE;
  670. const struct Chord chord_68 PROGMEM = {H_TOP5, FNC, &state_68, NULL, KC_F4, 0, single_dance};
  671. uint8_t state_69 = IDLE;
  672. const struct Chord chord_69 PROGMEM = {H_TOP6, FNC, &state_69, NULL, KC_F5, 0, single_dance};
  673. uint8_t state_70 = IDLE;
  674. const struct Chord chord_70 PROGMEM = {H_TOP7, FNC, &state_70, NULL, KC_F6, 0, single_dance};
  675. uint8_t state_71 = IDLE;
  676. const struct Chord chord_71 PROGMEM = {H_TOP8, FNC, &state_71, NULL, KC_F7, 0, single_dance};
  677. uint8_t state_72 = IDLE;
  678. const struct Chord chord_72 PROGMEM = {H_TOP9, FNC, &state_72, NULL, KC_F8, 0, single_dance};
  679. uint8_t state_73 = IDLE;
  680. const struct Chord chord_73 PROGMEM = {H_TOP10, FNC, &state_73, NULL, KC_F9, 0, single_dance};
  681. uint8_t state_74 = IDLE;
  682. const struct Chord chord_74 PROGMEM = {H_TOP11, FNC, &state_74, NULL, KC_F10, 0, single_dance};
  683. uint8_t state_75 = IDLE;
  684. const struct Chord chord_75 PROGMEM = {H_TOP12, FNC, &state_75, NULL, KC_F11, 0, single_dance};
  685. uint8_t state_76 = IDLE;
  686. const struct Chord chord_76 PROGMEM = {H_TOP3 + H_BOT3, FNC, &state_76, NULL, KC_LALT, 0, single_dance};
  687. uint8_t state_77 = IDLE;
  688. const struct Chord chord_77 PROGMEM = {H_TOP4 + H_BOT4, FNC, &state_77, NULL, KC_LCTL, 0, single_dance};
  689. uint8_t state_78 = IDLE;
  690. const struct Chord chord_78 PROGMEM = {H_TOP5 + H_BOT5, FNC, &state_78, NULL, KC_LSFT, 0, single_dance};
  691. uint8_t state_79 = IDLE;
  692. const struct Chord chord_79 PROGMEM = {H_TOP6 + H_BOT6, FNC, &state_79, NULL, KC_LGUI, 0, single_dance};
  693. uint8_t state_80 = IDLE;
  694. const struct Chord chord_80 PROGMEM = {H_TOP7 + H_BOT7, FNC, &state_80, NULL, KC_RGUI, 0, single_dance};
  695. uint8_t state_81 = IDLE;
  696. const struct Chord chord_81 PROGMEM = {H_TOP8 + H_BOT8, FNC, &state_81, NULL, KC_RSFT, 0, single_dance};
  697. uint8_t state_82 = IDLE;
  698. const struct Chord chord_82 PROGMEM = {H_TOP9 + H_BOT9, FNC, &state_82, NULL, KC_RCTL, 0, single_dance};
  699. uint8_t state_83 = IDLE;
  700. const struct Chord chord_83 PROGMEM = {H_TOP10 + H_BOT10, FNC, &state_83, NULL, KC_RALT, 0, single_dance};
  701. uint8_t state_84 = IDLE;
  702. const struct Chord chord_84 PROGMEM = {H_BOT12, FNC, &state_84, NULL, KC_F12, 0, single_dance};
  703. uint8_t state_85 = IDLE;
  704. const struct Chord chord_85 PROGMEM = {H_TOP8, NAV, &state_85, NULL, KC_HOME, 0, single_dance};
  705. uint8_t state_86 = IDLE;
  706. const struct Chord chord_86 PROGMEM = {H_TOP9, NAV, &state_86, NULL, KC_UP, 0, single_dance};
  707. uint8_t state_87 = IDLE;
  708. const struct Chord chord_87 PROGMEM = {H_TOP10, NAV, &state_87, NULL, KC_END, 0, single_dance};
  709. uint8_t state_88 = IDLE;
  710. const struct Chord chord_88 PROGMEM = {H_TOP11, NAV, &state_88, NULL, KC_PGUP, 0, single_dance};
  711. uint8_t state_89 = IDLE;
  712. const struct Chord chord_89 PROGMEM = {H_TOP3 + H_BOT3, NAV, &state_89, NULL, KC_LALT, 0, single_dance};
  713. uint8_t state_90 = IDLE;
  714. const struct Chord chord_90 PROGMEM = {H_TOP4 + H_BOT4, NAV, &state_90, NULL, KC_LCTL, 0, single_dance};
  715. uint8_t state_91 = IDLE;
  716. const struct Chord chord_91 PROGMEM = {H_TOP5 + H_BOT5, NAV, &state_91, NULL, KC_LSFT, 0, single_dance};
  717. uint8_t state_92 = IDLE;
  718. const struct Chord chord_92 PROGMEM = {H_TOP6 + H_BOT6, NAV, &state_92, NULL, KC_LGUI, 0, single_dance};
  719. uint8_t state_93 = IDLE;
  720. const struct Chord chord_93 PROGMEM = {H_BOT8, NAV, &state_93, NULL, KC_LEFT, 0, single_dance};
  721. uint8_t state_94 = IDLE;
  722. const struct Chord chord_94 PROGMEM = {H_BOT9, NAV, &state_94, NULL, KC_DOWN, 0, single_dance};
  723. uint8_t state_95 = IDLE;
  724. const struct Chord chord_95 PROGMEM = {H_BOT10, NAV, &state_95, NULL, KC_RIGHT, 0, single_dance};
  725. uint8_t state_96 = IDLE;
  726. const struct Chord chord_96 PROGMEM = {H_BOT11, NAV, &state_96, NULL, KC_PGDN, 0, single_dance};
  727. uint8_t state_97 = IDLE;
  728. const struct Chord chord_97 PROGMEM = {H_TOP8, MOUSE, &state_97, NULL, KC_BTN1, 0, single_dance};
  729. uint8_t state_98 = IDLE;
  730. const struct Chord chord_98 PROGMEM = {H_TOP9, MOUSE, &state_98, NULL, KC_MS_U, 0, single_dance};
  731. uint8_t state_99 = IDLE;
  732. const struct Chord chord_99 PROGMEM = {H_TOP10, MOUSE, &state_99, NULL, KC_BTN2, 0, single_dance};
  733. uint8_t state_100 = IDLE;
  734. const struct Chord chord_100 PROGMEM = {H_TOP11, MOUSE, &state_100, NULL, KC_WH_U, 0, single_dance};
  735. uint8_t state_101 = IDLE;
  736. const struct Chord chord_101 PROGMEM = {H_TOP3 + H_BOT3, MOUSE, &state_101, NULL, KC_LALT, 0, single_dance};
  737. uint8_t state_102 = IDLE;
  738. const struct Chord chord_102 PROGMEM = {H_TOP4 + H_BOT4, MOUSE, &state_102, NULL, KC_LCTL, 0, single_dance};
  739. uint8_t state_103 = IDLE;
  740. const struct Chord chord_103 PROGMEM = {H_TOP5 + H_BOT5, MOUSE, &state_103, NULL, KC_LSFT, 0, single_dance};
  741. uint8_t state_104 = IDLE;
  742. const struct Chord chord_104 PROGMEM = {H_TOP6 + H_BOT6, MOUSE, &state_104, NULL, KC_LGUI, 0, single_dance};
  743. uint8_t state_105 = IDLE;
  744. const struct Chord chord_105 PROGMEM = {H_BOT8, MOUSE, &state_105, NULL, KC_MS_L, 0, single_dance};
  745. uint8_t state_106 = IDLE;
  746. const struct Chord chord_106 PROGMEM = {H_BOT9, MOUSE, &state_106, NULL, KC_MS_D, 0, single_dance};
  747. uint8_t state_107 = IDLE;
  748. const struct Chord chord_107 PROGMEM = {H_BOT10, MOUSE, &state_107, NULL, KC_MS_R, 0, single_dance};
  749. uint8_t state_108 = IDLE;
  750. const struct Chord chord_108 PROGMEM = {H_BOT11, MOUSE, &state_108, NULL, KC_WH_D, 0, single_dance};
  751. const struct Chord* const list_of_chords[] PROGMEM = {
  752. &chord_0, &chord_1, &chord_2, &chord_3, &chord_4, &chord_5, &chord_6, &chord_7, &chord_8, &chord_9, &chord_10, &chord_11, &chord_12, &chord_13, &chord_14, &chord_15, &chord_16, &chord_17, &chord_18, &chord_19, &chord_20, &chord_21, &chord_22, &chord_23, &chord_24, &chord_25, &chord_26, &chord_27, &chord_28, &chord_29, &chord_30, &chord_31, &chord_32, &chord_33, &chord_34, &chord_35, &chord_36, &chord_37, &chord_38, &chord_39, &chord_40, &chord_41, &chord_42, &chord_43, &chord_44, &chord_45, &chord_46, &chord_47, &chord_48, &chord_49, &chord_50, &chord_51, &chord_52, &chord_53, &chord_54, &chord_55, &chord_56, &chord_57, &chord_58, &chord_59, &chord_60, &chord_61, &chord_62, &chord_63, &chord_64, &chord_65, &chord_66, &chord_67, &chord_68, &chord_69, &chord_70, &chord_71, &chord_72, &chord_73, &chord_74, &chord_75, &chord_76, &chord_77, &chord_78, &chord_79, &chord_80, &chord_81, &chord_82, &chord_83, &chord_84, &chord_85, &chord_86, &chord_87, &chord_88, &chord_89, &chord_90, &chord_91, &chord_92, &chord_93, &chord_94, &chord_95, &chord_96, &chord_97, &chord_98, &chord_99, &chord_100, &chord_101, &chord_102, &chord_103, &chord_104, &chord_105, &chord_106, &chord_107, &chord_108
  753. };
  754. const uint16_t** const leader_triggers PROGMEM = NULL;
  755. void (*leader_functions[]) (void) = {};
  756. #define NUMBER_OF_CHORDS 109
  757. #define NUMBER_OF_LEADER_COMBOS 0
  758. bool are_hashed_keycodes_in_sound(HASH_TYPE keycodes_hash, HASH_TYPE sound) {
  759. return (keycodes_hash & sound) == keycodes_hash;
  760. }
  761. uint8_t keycode_to_index(uint16_t keycode) {
  762. return keycode - FIRST_INTERNAL_KEYCODE;
  763. }
  764. void sound_keycode_array(uint16_t keycode) {
  765. uint8_t index = keycode_to_index(keycode);
  766. keycode_index++;
  767. keycodes_buffer_array[index] = keycode_index;
  768. }
  769. void silence_keycode_hash_array(HASH_TYPE keycode_hash) {
  770. for (int i = 0; i < NUMBER_OF_KEYS; i++) {
  771. bool index_in_hash = ((HASH_TYPE) 1 << i) & keycode_hash;
  772. if (index_in_hash) {
  773. uint8_t current_val = keycodes_buffer_array[i];
  774. keycodes_buffer_array[i] = 0;
  775. for (int j = 0; j < NUMBER_OF_KEYS; j++) {
  776. if (keycodes_buffer_array[j] > current_val) {
  777. keycodes_buffer_array[j]--;
  778. }
  779. }
  780. keycode_index--;
  781. }
  782. }
  783. }
  784. bool are_hashed_keycodes_in_array(HASH_TYPE keycode_hash) {
  785. for (int i = 0; i < NUMBER_OF_KEYS; i++) {
  786. bool index_in_hash = ((HASH_TYPE) 1 << i) & keycode_hash;
  787. bool index_in_array = (bool) keycodes_buffer_array[i];
  788. if (index_in_hash && !index_in_array) {
  789. return false;
  790. }
  791. }
  792. return true;
  793. }
  794. void kill_one_shots(void) {
  795. struct Chord chord_storage;
  796. struct Chord* chord_ptr;
  797. struct Chord* chord;
  798. for (int i = 0; i < NUMBER_OF_CHORDS; i++) {
  799. chord_ptr = (struct Chord*) pgm_read_word (&list_of_chords[i]);
  800. memcpy_P(&chord_storage, chord_ptr, sizeof(struct Chord));
  801. chord = &chord_storage;
  802. if (*chord->state == IN_ONE_SHOT) {
  803. *chord->state = RESTART;
  804. chord->function(chord);
  805. if (*chord->state == RESTART) {
  806. *chord->state = IDLE;
  807. }
  808. }
  809. }
  810. }
  811. void process_finished_dances(void) {
  812. struct Chord chord_storage;
  813. struct Chord* chord_ptr;
  814. struct Chord* chord;
  815. for (int i = 0; i < NUMBER_OF_CHORDS; i++) {
  816. chord_ptr = (struct Chord*) pgm_read_word (&list_of_chords[i]);
  817. memcpy_P(&chord_storage, chord_ptr, sizeof(struct Chord));
  818. chord = &chord_storage;
  819. if (*chord->state == ACTIVATED) {
  820. *chord->state = PRESS_FROM_ACTIVE;
  821. chord->function(chord);
  822. if (a_key_went_through) {
  823. kill_one_shots();
  824. }
  825. dance_timer = timer_read();
  826. } else if (*chord->state == IDLE_IN_DANCE) {
  827. *chord->state = FINISHED;
  828. chord->function(chord);
  829. if (*chord->state == FINISHED) {
  830. *chord->state = RESTART;
  831. if (*chord->state == RESTART) {
  832. *chord->state = IDLE;
  833. }
  834. }
  835. } else if (*chord->state == PRESS_FROM_ACTIVE) {
  836. *chord->state = FINISHED_FROM_ACTIVE;
  837. chord->function(chord);
  838. if (a_key_went_through) {
  839. kill_one_shots();
  840. }
  841. dance_timer = timer_read();
  842. }
  843. }
  844. }
  845. uint8_t keycodes_buffer_array_min(uint8_t* first_keycode_index) {
  846. for (int i = 0; i < NUMBER_OF_KEYS; i++) {
  847. if (keycodes_buffer_array[i] == 1) {
  848. if (first_keycode_index != NULL) {
  849. *first_keycode_index = (uint8_t) i;
  850. }
  851. return 1;
  852. }
  853. }
  854. return 0;
  855. }
  856. void remove_subchords(void) {
  857. struct Chord chord_storage;
  858. struct Chord* chord_ptr;
  859. struct Chord* chord;
  860. for (int i = 0; i < NUMBER_OF_CHORDS; i++) {
  861. chord_ptr = (struct Chord*) pgm_read_word (&list_of_chords[i]);
  862. memcpy_P(&chord_storage, chord_ptr, sizeof(struct Chord));
  863. chord = &chord_storage;
  864. if (!(*chord->state == READY || *chord->state == READY_IN_DANCE || *chord->state == READY_LOCKED)) {
  865. continue;
  866. }
  867. struct Chord chord_storage_2;
  868. struct Chord* chord_ptr_2;
  869. struct Chord* chord_2;
  870. for (int j = 0; j < NUMBER_OF_CHORDS; j++) {
  871. if (i == j) {continue;}
  872. chord_ptr_2 = (struct Chord*) pgm_read_word (&list_of_chords[j]);
  873. memcpy_P(&chord_storage_2, chord_ptr_2, sizeof(struct Chord));
  874. chord_2 = &chord_storage_2;
  875. if (are_hashed_keycodes_in_sound(chord_2->keycodes_hash, chord->keycodes_hash)) {
  876. if (*chord_2->state == READY) {
  877. *chord_2->state = IDLE;
  878. }
  879. if (*chord_2->state == READY_IN_DANCE) {
  880. *chord_2->state = IDLE_IN_DANCE;
  881. }
  882. if (*chord_2->state == READY_LOCKED) {
  883. *chord_2->state = LOCKED;
  884. }
  885. }
  886. }
  887. }
  888. }
  889. void process_ready_chords(void) {
  890. uint8_t first_keycode_index = 0;
  891. while (keycodes_buffer_array_min(&first_keycode_index)) {
  892. // find ready chords
  893. struct Chord chord_storage;
  894. struct Chord* chord_ptr;
  895. struct Chord* chord;
  896. for (int i = 0; i < NUMBER_OF_CHORDS; i++) {
  897. chord_ptr = (struct Chord*) pgm_read_word (&list_of_chords[i]);
  898. memcpy_P(&chord_storage, chord_ptr, sizeof(struct Chord));
  899. chord = &chord_storage;
  900. // if the chord does not contain the first keycode
  901. bool contains_first_keycode = ((uint32_t) 1 << first_keycode_index) & chord->keycodes_hash;
  902. if (!contains_first_keycode) {
  903. continue;
  904. }
  905. if (!are_hashed_keycodes_in_array(chord->keycodes_hash)){
  906. continue;
  907. }
  908. if (*chord->state == LOCKED) {
  909. *chord->state = READY_LOCKED;
  910. continue;
  911. }
  912. if (!(chord->pseudolayer == current_pseudolayer || chord->pseudolayer == ALWAYS_ON)) {
  913. continue;
  914. }
  915. if (*chord->state == IDLE) {
  916. *chord->state = READY;
  917. continue;
  918. }
  919. if (*chord->state == IDLE_IN_DANCE) {
  920. *chord->state = READY_IN_DANCE;
  921. }
  922. }
  923. // remove subchords
  924. remove_subchords();
  925. // execute logic
  926. // this should be only one chord
  927. for (int i = 0; i < NUMBER_OF_CHORDS; i++) {
  928. chord_ptr = (struct Chord*) pgm_read_word (&list_of_chords[i]);
  929. memcpy_P(&chord_storage, chord_ptr, sizeof(struct Chord));
  930. chord = &chord_storage;
  931. if (*chord->state == READY_LOCKED) {
  932. *chord->state = RESTART;
  933. chord->function(chord);
  934. if (*chord->state == RESTART) {
  935. *chord->state = IDLE;
  936. }
  937. break;
  938. }
  939. if (*chord->state == READY || *chord->state == READY_IN_DANCE) {
  940. if (last_chord && last_chord != chord) {
  941. process_finished_dances();
  942. }
  943. bool lock_next_prev_state = lock_next;
  944. *chord->state = ACTIVATED;
  945. chord->function(chord);
  946. dance_timer = timer_read();
  947. if (lock_next && lock_next == lock_next_prev_state) {
  948. lock_next = false;
  949. *chord->state = PRESS_FROM_ACTIVE;
  950. chord->function(chord);
  951. if (*chord->state == PRESS_FROM_ACTIVE) {
  952. *chord->state = LOCKED;
  953. }
  954. if (a_key_went_through) {
  955. kill_one_shots();
  956. }
  957. }
  958. break;
  959. }
  960. }
  961. // silence notes
  962. silence_keycode_hash_array(chord->keycodes_hash);
  963. }
  964. }
  965. void deactivate_active_chords(uint16_t keycode) {
  966. HASH_TYPE hash = (HASH_TYPE)1 << (keycode - SAFE_RANGE);
  967. bool broken;
  968. struct Chord chord_storage;
  969. struct Chord* chord_ptr;
  970. struct Chord* chord;
  971. for (int i = 0; i < NUMBER_OF_CHORDS; i++) {
  972. chord_ptr = (struct Chord*) pgm_read_word (&list_of_chords[i]);
  973. memcpy_P(&chord_storage, chord_ptr, sizeof(struct Chord));
  974. chord = &chord_storage;
  975. broken = are_hashed_keycodes_in_sound(hash, chord->keycodes_hash);
  976. if (!broken) {
  977. continue;
  978. }
  979. switch (*chord->state) {
  980. case ACTIVATED:
  981. *chord->state = DEACTIVATED;
  982. chord->function(chord);
  983. if (*chord->state == DEACTIVATED) {
  984. dance_timer = timer_read();
  985. *chord->state = IDLE_IN_DANCE;
  986. }
  987. if (*chord->state != IN_ONE_SHOT) {
  988. kill_one_shots();
  989. }
  990. break;
  991. case PRESS_FROM_ACTIVE:
  992. case FINISHED_FROM_ACTIVE:
  993. *chord->state = RESTART;
  994. chord->function(chord);
  995. if (*chord->state == RESTART) {
  996. *chord->state = IDLE;
  997. }
  998. kill_one_shots();
  999. break;
  1000. default:
  1001. break;
  1002. }
  1003. }
  1004. }
  1005. void process_command(void) {
  1006. command_mode = 0;
  1007. for (int i = 0; i < COMMAND_MAX_LENGTH; i++) {
  1008. if (command_buffer[i]) {
  1009. register_code(command_buffer[i]);
  1010. }
  1011. send_keyboard_report();
  1012. }
  1013. wait_ms(TAP_TIMEOUT);
  1014. for (int i = 0; i < COMMAND_MAX_LENGTH; i++) {
  1015. if (command_buffer[i]) {
  1016. unregister_code(command_buffer[i]);
  1017. }
  1018. send_keyboard_report();
  1019. }
  1020. for (int i = 0; i < COMMAND_MAX_LENGTH; i++) {
  1021. command_buffer[i] = 0;
  1022. }
  1023. command_ind = 0;
  1024. }
  1025. void process_leader(void) {
  1026. in_leader_mode = false;
  1027. for (int i = 0; i < NUMBER_OF_LEADER_COMBOS; i++) {
  1028. uint16_t trigger[LEADER_MAX_LENGTH];
  1029. memcpy_P(trigger, leader_triggers[i], LEADER_MAX_LENGTH * sizeof(uint16_t));
  1030. if (identical(leader_buffer, trigger)) {
  1031. (*leader_functions[i])();
  1032. break;
  1033. }
  1034. }
  1035. for (int i = 0; i < LEADER_MAX_LENGTH; i++) {
  1036. leader_buffer[i] = 0;
  1037. }
  1038. }
  1039. bool process_record_user(uint16_t keycode, keyrecord_t *record) {
  1040. if (keycode < FIRST_INTERNAL_KEYCODE || keycode > LAST_INTERNAL_KEYCODE) {
  1041. return true;
  1042. }
  1043. if (record->event.pressed) {
  1044. sound_keycode_array(keycode);
  1045. } else {
  1046. process_ready_chords();
  1047. deactivate_active_chords(keycode);
  1048. }
  1049. chord_timer = timer_read();
  1050. leader_timer = timer_read();
  1051. return false;
  1052. }
  1053. void matrix_scan_user(void) {
  1054. bool chord_timer_expired = timer_elapsed(chord_timer) > CHORD_TIMEOUT;
  1055. if (chord_timer_expired && keycodes_buffer_array_min(NULL)) {
  1056. process_ready_chords();
  1057. }
  1058. bool dance_timer_expired = timer_elapsed(dance_timer) > DANCE_TIMEOUT;
  1059. if (dance_timer_expired) { // would love to have && in_dance but not sure how
  1060. process_finished_dances();
  1061. }
  1062. bool in_command_mode = command_mode == 2;
  1063. if (in_command_mode) {
  1064. process_command();
  1065. }
  1066. bool leader_timer_expired = timer_elapsed(leader_timer) > LEADER_TIMEOUT;
  1067. if (leader_timer_expired && in_leader_mode) {
  1068. process_leader();
  1069. }
  1070. }
  1071. void clear(const struct Chord* self) {
  1072. if (*self->state == ACTIVATED) {
  1073. // kill all chords
  1074. struct Chord chord_storage;
  1075. struct Chord* chord_ptr;
  1076. struct Chord* chord;
  1077. for (int i = 0; i < NUMBER_OF_CHORDS; i++) {
  1078. chord_ptr = (struct Chord*) pgm_read_word (&list_of_chords[i]);
  1079. memcpy_P(&chord_storage, chord_ptr, sizeof(struct Chord));
  1080. chord = &chord_storage;
  1081. *chord->state = IDLE;
  1082. if (chord->counter) {
  1083. *chord->counter = 0;
  1084. }
  1085. }
  1086. // clear keyboard
  1087. clear_keyboard();
  1088. send_keyboard_report();
  1089. // switch to default pseudolayer
  1090. current_pseudolayer = DEFAULT_PSEUDOLAYER;
  1091. // clear all keyboard states
  1092. lock_next = false;
  1093. autoshift_mode = true;
  1094. command_mode = 0;
  1095. in_leader_mode = false;
  1096. leader_ind = 0;
  1097. dynamic_macro_mode = false;
  1098. a_key_went_through = false;
  1099. for (int i = 0; i < DYNAMIC_MACRO_MAX_LENGTH; i++) {
  1100. dynamic_macro_buffer[i] = 0;
  1101. }
  1102. }
  1103. }