You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

403 lines
12 KiB

  1. bool are_hashed_keycodes_in_sound(HASH_TYPE keycodes_hash, HASH_TYPE sound) {
  2. return (keycodes_hash & sound) == keycodes_hash;
  3. }
  4. uint8_t keycode_to_index(uint16_t keycode) {
  5. return keycode - FIRST_INTERNAL_KEYCODE;
  6. }
  7. void sound_keycode_array(uint16_t keycode) {
  8. uint8_t index = keycode_to_index(keycode);
  9. keycode_index++;
  10. keycodes_buffer_array[index] = keycode_index;
  11. }
  12. void silence_keycode_hash_array(HASH_TYPE keycode_hash) {
  13. for (int i = 0; i < NUMBER_OF_KEYS; i++) {
  14. bool index_in_hash = ((HASH_TYPE) 1 << i) & keycode_hash;
  15. if (index_in_hash) {
  16. uint8_t current_val = keycodes_buffer_array[i];
  17. keycodes_buffer_array[i] = 0;
  18. for (int j = 0; j < NUMBER_OF_KEYS; j++) {
  19. if (keycodes_buffer_array[j] > current_val) {
  20. keycodes_buffer_array[j]--;
  21. }
  22. }
  23. keycode_index--;
  24. }
  25. }
  26. }
  27. bool are_hashed_keycodes_in_array(HASH_TYPE keycode_hash) {
  28. for (int i = 0; i < NUMBER_OF_KEYS; i++) {
  29. bool index_in_hash = ((HASH_TYPE) 1 << i) & keycode_hash;
  30. bool index_in_array = (bool) keycodes_buffer_array[i];
  31. if (index_in_hash && !index_in_array) {
  32. return false;
  33. }
  34. }
  35. return true;
  36. }
  37. void kill_one_shots(void) {
  38. struct Chord chord_storage;
  39. struct Chord* chord_ptr;
  40. struct Chord* chord;
  41. for (int i = 0; i < NUMBER_OF_CHORDS; i++) {
  42. chord_ptr = (struct Chord*) pgm_read_word (&list_of_chords[i]);
  43. memcpy_P(&chord_storage, chord_ptr, sizeof(struct Chord));
  44. chord = &chord_storage;
  45. if (*chord->state == IN_ONE_SHOT) {
  46. *chord->state = RESTART;
  47. chord->function(chord);
  48. if (*chord->state == RESTART) {
  49. *chord->state = IDLE;
  50. }
  51. }
  52. }
  53. }
  54. void process_finished_dances(void) {
  55. struct Chord chord_storage;
  56. struct Chord* chord_ptr;
  57. struct Chord* chord;
  58. for (int i = 0; i < NUMBER_OF_CHORDS; i++) {
  59. chord_ptr = (struct Chord*) pgm_read_word (&list_of_chords[i]);
  60. memcpy_P(&chord_storage, chord_ptr, sizeof(struct Chord));
  61. chord = &chord_storage;
  62. if (*chord->state == ACTIVATED) {
  63. *chord->state = PRESS_FROM_ACTIVE;
  64. chord->function(chord);
  65. if (a_key_went_through) {
  66. kill_one_shots();
  67. }
  68. dance_timer = timer_read();
  69. } else if (*chord->state == IDLE_IN_DANCE) {
  70. *chord->state = FINISHED;
  71. chord->function(chord);
  72. if (*chord->state == FINISHED) {
  73. *chord->state = RESTART;
  74. if (*chord->state == RESTART) {
  75. *chord->state = IDLE;
  76. }
  77. }
  78. } else if (*chord->state == PRESS_FROM_ACTIVE) {
  79. *chord->state = FINISHED_FROM_ACTIVE;
  80. chord->function(chord);
  81. if (a_key_went_through) {
  82. kill_one_shots();
  83. }
  84. dance_timer = timer_read();
  85. }
  86. }
  87. }
  88. uint8_t keycodes_buffer_array_min(uint8_t* first_keycode_index) {
  89. for (int i = 0; i < NUMBER_OF_KEYS; i++) {
  90. if (keycodes_buffer_array[i] == 1) {
  91. if (first_keycode_index != NULL) {
  92. *first_keycode_index = (uint8_t) i;
  93. }
  94. return 1;
  95. }
  96. }
  97. return 0;
  98. }
  99. void remove_subchords(void) {
  100. struct Chord chord_storage;
  101. struct Chord* chord_ptr;
  102. struct Chord* chord;
  103. for (int i = 0; i < NUMBER_OF_CHORDS; i++) {
  104. chord_ptr = (struct Chord*) pgm_read_word (&list_of_chords[i]);
  105. memcpy_P(&chord_storage, chord_ptr, sizeof(struct Chord));
  106. chord = &chord_storage;
  107. if (!(*chord->state == READY || *chord->state == READY_IN_DANCE || *chord->state == READY_LOCKED)) {
  108. continue;
  109. }
  110. struct Chord chord_storage_2;
  111. struct Chord* chord_ptr_2;
  112. struct Chord* chord_2;
  113. for (int j = 0; j < NUMBER_OF_CHORDS; j++) {
  114. if (i == j) {continue;}
  115. chord_ptr_2 = (struct Chord*) pgm_read_word (&list_of_chords[j]);
  116. memcpy_P(&chord_storage_2, chord_ptr_2, sizeof(struct Chord));
  117. chord_2 = &chord_storage_2;
  118. if (are_hashed_keycodes_in_sound(chord_2->keycodes_hash, chord->keycodes_hash)) {
  119. if (*chord_2->state == READY) {
  120. *chord_2->state = IDLE;
  121. }
  122. if (*chord_2->state == READY_IN_DANCE) {
  123. *chord_2->state = IDLE_IN_DANCE;
  124. }
  125. if (*chord_2->state == READY_LOCKED) {
  126. *chord_2->state = LOCKED;
  127. }
  128. }
  129. }
  130. }
  131. }
  132. void process_ready_chords(void) {
  133. uint8_t first_keycode_index = 0;
  134. while (keycodes_buffer_array_min(&first_keycode_index)) {
  135. // find ready chords
  136. struct Chord chord_storage;
  137. struct Chord* chord_ptr;
  138. struct Chord* chord;
  139. for (int i = 0; i < NUMBER_OF_CHORDS; i++) {
  140. chord_ptr = (struct Chord*) pgm_read_word (&list_of_chords[i]);
  141. memcpy_P(&chord_storage, chord_ptr, sizeof(struct Chord));
  142. chord = &chord_storage;
  143. // if the chord does not contain the first keycode
  144. bool contains_first_keycode = ((uint32_t) 1 << first_keycode_index) & chord->keycodes_hash;
  145. if (!contains_first_keycode) {
  146. continue;
  147. }
  148. if (!are_hashed_keycodes_in_array(chord->keycodes_hash)){
  149. continue;
  150. }
  151. if (*chord->state == LOCKED) {
  152. *chord->state = READY_LOCKED;
  153. continue;
  154. }
  155. if (!(chord->pseudolayer == current_pseudolayer || chord->pseudolayer == ALWAYS_ON)) {
  156. continue;
  157. }
  158. if (*chord->state == IDLE) {
  159. *chord->state = READY;
  160. continue;
  161. }
  162. if (*chord->state == IDLE_IN_DANCE) {
  163. *chord->state = READY_IN_DANCE;
  164. }
  165. }
  166. // remove subchords
  167. remove_subchords();
  168. // execute logic
  169. // this should be only one chord
  170. for (int i = 0; i < NUMBER_OF_CHORDS; i++) {
  171. chord_ptr = (struct Chord*) pgm_read_word (&list_of_chords[i]);
  172. memcpy_P(&chord_storage, chord_ptr, sizeof(struct Chord));
  173. chord = &chord_storage;
  174. if (*chord->state == READY_LOCKED) {
  175. *chord->state = RESTART;
  176. chord->function(chord);
  177. if (*chord->state == RESTART) {
  178. *chord->state = IDLE;
  179. }
  180. break;
  181. }
  182. if (*chord->state == READY || *chord->state == READY_IN_DANCE) {
  183. if (last_chord && last_chord != chord) {
  184. process_finished_dances();
  185. }
  186. bool lock_next_prev_state = lock_next;
  187. *chord->state = ACTIVATED;
  188. chord->function(chord);
  189. dance_timer = timer_read();
  190. if (lock_next && lock_next == lock_next_prev_state) {
  191. lock_next = false;
  192. *chord->state = PRESS_FROM_ACTIVE;
  193. chord->function(chord);
  194. if (*chord->state == PRESS_FROM_ACTIVE) {
  195. *chord->state = LOCKED;
  196. }
  197. if (a_key_went_through) {
  198. kill_one_shots();
  199. }
  200. }
  201. break;
  202. }
  203. }
  204. // silence notes
  205. silence_keycode_hash_array(chord->keycodes_hash);
  206. }
  207. }
  208. void deactivate_active_chords(uint16_t keycode) {
  209. HASH_TYPE hash = (HASH_TYPE)1 << (keycode - SAFE_RANGE);
  210. bool broken;
  211. struct Chord chord_storage;
  212. struct Chord* chord_ptr;
  213. struct Chord* chord;
  214. for (int i = 0; i < NUMBER_OF_CHORDS; i++) {
  215. chord_ptr = (struct Chord*) pgm_read_word (&list_of_chords[i]);
  216. memcpy_P(&chord_storage, chord_ptr, sizeof(struct Chord));
  217. chord = &chord_storage;
  218. broken = are_hashed_keycodes_in_sound(hash, chord->keycodes_hash);
  219. if (!broken) {
  220. continue;
  221. }
  222. switch (*chord->state) {
  223. case ACTIVATED:
  224. *chord->state = DEACTIVATED;
  225. chord->function(chord);
  226. if (*chord->state == DEACTIVATED) {
  227. dance_timer = timer_read();
  228. *chord->state = IDLE_IN_DANCE;
  229. }
  230. if (*chord->state != IN_ONE_SHOT) {
  231. kill_one_shots();
  232. }
  233. break;
  234. case PRESS_FROM_ACTIVE:
  235. case FINISHED_FROM_ACTIVE:
  236. *chord->state = RESTART;
  237. chord->function(chord);
  238. if (*chord->state == RESTART) {
  239. *chord->state = IDLE;
  240. }
  241. kill_one_shots();
  242. break;
  243. default:
  244. break;
  245. }
  246. }
  247. }
  248. void process_command(void) {
  249. command_mode = 0;
  250. for (int i = 0; i < COMMAND_MAX_LENGTH; i++) {
  251. if (command_buffer[i]) {
  252. register_code(command_buffer[i]);
  253. }
  254. send_keyboard_report();
  255. }
  256. wait_ms(TAP_TIMEOUT);
  257. for (int i = 0; i < COMMAND_MAX_LENGTH; i++) {
  258. if (command_buffer[i]) {
  259. unregister_code(command_buffer[i]);
  260. }
  261. send_keyboard_report();
  262. }
  263. for (int i = 0; i < COMMAND_MAX_LENGTH; i++) {
  264. command_buffer[i] = 0;
  265. }
  266. command_ind = 0;
  267. }
  268. void process_leader(void) {
  269. in_leader_mode = false;
  270. for (int i = 0; i < NUMBER_OF_LEADER_COMBOS; i++) {
  271. uint16_t trigger[LEADER_MAX_LENGTH];
  272. memcpy_P(trigger, leader_triggers[i], LEADER_MAX_LENGTH * sizeof(uint16_t));
  273. if (identical(leader_buffer, trigger)) {
  274. (*leader_functions[i])();
  275. break;
  276. }
  277. }
  278. for (int i = 0; i < LEADER_MAX_LENGTH; i++) {
  279. leader_buffer[i] = 0;
  280. }
  281. }
  282. bool process_record_user(uint16_t keycode, keyrecord_t *record) {
  283. if (keycode < FIRST_INTERNAL_KEYCODE || keycode > LAST_INTERNAL_KEYCODE) {
  284. return true;
  285. }
  286. if (record->event.pressed) {
  287. sound_keycode_array(keycode);
  288. } else {
  289. process_ready_chords();
  290. deactivate_active_chords(keycode);
  291. }
  292. chord_timer = timer_read();
  293. leader_timer = timer_read();
  294. return false;
  295. }
  296. void matrix_scan_user(void) {
  297. bool chord_timer_expired = timer_elapsed(chord_timer) > CHORD_TIMEOUT;
  298. if (chord_timer_expired && keycodes_buffer_array_min(NULL)) {
  299. process_ready_chords();
  300. }
  301. bool dance_timer_expired = timer_elapsed(dance_timer) > DANCE_TIMEOUT;
  302. if (dance_timer_expired) { // would love to have && in_dance but not sure how
  303. process_finished_dances();
  304. }
  305. bool in_command_mode = command_mode == 2;
  306. if (in_command_mode) {
  307. process_command();
  308. }
  309. bool leader_timer_expired = timer_elapsed(leader_timer) > LEADER_TIMEOUT;
  310. if (leader_timer_expired && in_leader_mode) {
  311. process_leader();
  312. }
  313. }
  314. void clear(const struct Chord* self) {
  315. if (*self->state == ACTIVATED) {
  316. // kill all chords
  317. struct Chord chord_storage;
  318. struct Chord* chord_ptr;
  319. struct Chord* chord;
  320. for (int i = 0; i < NUMBER_OF_CHORDS; i++) {
  321. chord_ptr = (struct Chord*) pgm_read_word (&list_of_chords[i]);
  322. memcpy_P(&chord_storage, chord_ptr, sizeof(struct Chord));
  323. chord = &chord_storage;
  324. *chord->state = IDLE;
  325. if (chord->counter) {
  326. *chord->counter = 0;
  327. }
  328. }
  329. // clear keyboard
  330. clear_keyboard();
  331. send_keyboard_report();
  332. // switch to default pseudolayer
  333. current_pseudolayer = DEFAULT_PSEUDOLAYER;
  334. // clear all keyboard states
  335. lock_next = false;
  336. autoshift_mode = true;
  337. command_mode = 0;
  338. in_leader_mode = false;
  339. leader_ind = 0;
  340. dynamic_macro_mode = false;
  341. a_key_went_through = false;
  342. for (int i = 0; i < DYNAMIC_MACRO_MAX_LENGTH; i++) {
  343. dynamic_macro_buffer[i] = 0;
  344. }
  345. }
  346. }