quantum.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. #include "quantum.h"
  2. __attribute__ ((weak))
  3. void matrix_init_kb(void) {}
  4. __attribute__ ((weak))
  5. void matrix_scan_kb(void) {}
  6. __attribute__ ((weak))
  7. bool process_action_kb(keyrecord_t *record) {
  8. return true;
  9. }
  10. __attribute__ ((weak))
  11. void leader_start(void) {}
  12. __attribute__ ((weak))
  13. void leader_end(void) {}
  14. #ifdef AUDIO_ENABLE
  15. uint8_t starting_note = 0x0C;
  16. int offset = 0;
  17. bool music_activated = false;
  18. #endif
  19. // Leader key stuff
  20. bool leading = false;
  21. uint16_t leader_time = 0;
  22. uint16_t leader_sequence[3] = {0, 0, 0};
  23. uint8_t leader_sequence_size = 0;
  24. // Chording stuff
  25. #define CHORDING_MAX 4
  26. bool chording = false;
  27. uint8_t chord_keys[CHORDING_MAX] = {0};
  28. uint8_t chord_key_count = 0;
  29. uint8_t chord_key_down = 0;
  30. bool keys_chord(uint8_t keys[]) {
  31. uint8_t keys_size = sizeof(keys)/sizeof(keys[0]);
  32. bool pass = true;
  33. uint8_t in = 0;
  34. for (uint8_t i = 0; i < chord_key_count; i++) {
  35. bool found = false;
  36. for (uint8_t j = 0; j < keys_size; j++) {
  37. if (chord_keys[i] == (keys[j] & 0xFF)) {
  38. in++; // detects key in chord
  39. found = true;
  40. break;
  41. }
  42. }
  43. if (found)
  44. continue;
  45. if (chord_keys[i] != 0) {
  46. pass = false; // makes sure rest are blank
  47. }
  48. }
  49. return (pass && (in == keys_size));
  50. }
  51. bool process_action_quantum(keyrecord_t *record) {
  52. /* This gets the keycode from the key pressed */
  53. keypos_t key = record->event.key;
  54. uint16_t keycode;
  55. #if !defined(NO_ACTION_LAYER) && defined(PREVENT_STUCK_MODIFIERS)
  56. uint8_t layer;
  57. if (record->event.pressed) {
  58. layer = layer_switch_get_layer(key);
  59. update_source_layers_cache(key, layer);
  60. } else {
  61. layer = read_source_layers_cache(key);
  62. }
  63. keycode = keymap_key_to_keycode(layer, key);
  64. #else
  65. keycode = keymap_key_to_keycode(layer_switch_get_layer(key), key);
  66. #endif
  67. #ifdef AUDIO_ENABLE
  68. if (music_activated) {
  69. if (record->event.pressed) {
  70. play_note(((double)220.0)*pow(2.0, -4.0)*pow(2.0,(starting_note + SCALE[record->event.key.col + offset])/12.0+(MATRIX_ROWS - record->event.key.row)), 0xF);
  71. } else {
  72. stop_note(((double)220.0)*pow(2.0, -4.0)*pow(2.0,(starting_note + SCALE[record->event.key.col + offset])/12.0+(MATRIX_ROWS - record->event.key.row)));
  73. }
  74. if (keycode < 0xFF) // ignores all normal keycodes, but lets RAISE, LOWER, etc through
  75. return false;
  76. }
  77. #endif
  78. #ifndef DISABLE_LEADER
  79. // Leader key set-up
  80. if (record->event.pressed) {
  81. if (!leading && keycode == KC_LEAD) {
  82. leader_start();
  83. leading = true;
  84. leader_time = timer_read();
  85. leader_sequence_size = 0;
  86. leader_sequence[0] = 0;
  87. leader_sequence[1] = 0;
  88. leader_sequence[2] = 0;
  89. return false;
  90. }
  91. if (leading && timer_elapsed(leader_time) < LEADER_TIMEOUT) {
  92. leader_sequence[leader_sequence_size] = keycode;
  93. leader_sequence_size++;
  94. return false;
  95. }
  96. }
  97. #endif
  98. #define DISABLE_CHORDING
  99. #ifndef DISABLE_CHORDING
  100. if (keycode >= 0x5700 && keycode <= 0x57FF) {
  101. if (record->event.pressed) {
  102. if (!chording) {
  103. chording = true;
  104. for (uint8_t i = 0; i < CHORDING_MAX; i++)
  105. chord_keys[i] = 0;
  106. chord_key_count = 0;
  107. chord_key_down = 0;
  108. }
  109. chord_keys[chord_key_count] = (keycode & 0xFF);
  110. chord_key_count++;
  111. chord_key_down++;
  112. return false;
  113. } else {
  114. if (chording) {
  115. chord_key_down--;
  116. if (chord_key_down == 0) {
  117. chording = false;
  118. // Chord Dictionary
  119. if (keys_chord((uint8_t[]){KC_ENTER, KC_SPACE})) {
  120. register_code(KC_A);
  121. unregister_code(KC_A);
  122. return false;
  123. }
  124. for (uint8_t i = 0; i < chord_key_count; i++) {
  125. register_code(chord_keys[i]);
  126. unregister_code(chord_keys[i]);
  127. return false;
  128. }
  129. }
  130. }
  131. }
  132. }
  133. #endif
  134. return process_action_kb(record);
  135. }
  136. void matrix_init_quantum() {
  137. matrix_init_kb();
  138. }
  139. void matrix_scan_quantum() {
  140. matrix_scan_kb();
  141. }