quantum.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. /* Copyright 2016-2017 Jack Humbert
  2. *
  3. * This program is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation, either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include "quantum.h"
  17. #ifdef BLUETOOTH_ENABLE
  18. # include "outputselect.h"
  19. #endif
  20. #ifdef BACKLIGHT_ENABLE
  21. # include "backlight.h"
  22. #endif
  23. #ifdef API_ENABLE
  24. # include "api.h"
  25. #endif
  26. #ifdef MIDI_ENABLE
  27. # include "process_midi.h"
  28. #endif
  29. #ifdef VELOCIKEY_ENABLE
  30. # include "velocikey.h"
  31. #endif
  32. #ifdef HAPTIC_ENABLE
  33. # include "haptic.h"
  34. #endif
  35. #ifdef AUDIO_ENABLE
  36. # ifndef GOODBYE_SONG
  37. # define GOODBYE_SONG SONG(GOODBYE_SOUND)
  38. # endif
  39. float goodbye_song[][2] = GOODBYE_SONG;
  40. # ifdef DEFAULT_LAYER_SONGS
  41. float default_layer_songs[][16][2] = DEFAULT_LAYER_SONGS;
  42. # endif
  43. # ifdef SENDSTRING_BELL
  44. float bell_song[][2] = SONG(TERMINAL_SOUND);
  45. # endif
  46. #endif
  47. #ifdef AUTO_SHIFT_ENABLE
  48. # include "process_auto_shift.h"
  49. #endif
  50. static void do_code16(uint16_t code, void (*f)(uint8_t)) {
  51. switch (code) {
  52. case QK_MODS ... QK_MODS_MAX:
  53. break;
  54. default:
  55. return;
  56. }
  57. uint8_t mods_to_send = 0;
  58. if (code & QK_RMODS_MIN) { // Right mod flag is set
  59. if (code & QK_LCTL) mods_to_send |= MOD_BIT(KC_RCTL);
  60. if (code & QK_LSFT) mods_to_send |= MOD_BIT(KC_RSFT);
  61. if (code & QK_LALT) mods_to_send |= MOD_BIT(KC_RALT);
  62. if (code & QK_LGUI) mods_to_send |= MOD_BIT(KC_RGUI);
  63. } else {
  64. if (code & QK_LCTL) mods_to_send |= MOD_BIT(KC_LCTL);
  65. if (code & QK_LSFT) mods_to_send |= MOD_BIT(KC_LSFT);
  66. if (code & QK_LALT) mods_to_send |= MOD_BIT(KC_LALT);
  67. if (code & QK_LGUI) mods_to_send |= MOD_BIT(KC_LGUI);
  68. }
  69. f(mods_to_send);
  70. }
  71. void register_code16(uint16_t code) {
  72. if (IS_MOD(code) || code == KC_NO) {
  73. do_code16(code, register_mods);
  74. } else {
  75. do_code16(code, register_weak_mods);
  76. }
  77. register_code(code);
  78. }
  79. void unregister_code16(uint16_t code) {
  80. unregister_code(code);
  81. if (IS_MOD(code) || code == KC_NO) {
  82. do_code16(code, unregister_mods);
  83. } else {
  84. do_code16(code, unregister_weak_mods);
  85. }
  86. }
  87. void tap_code16(uint16_t code) {
  88. register_code16(code);
  89. #if TAP_CODE_DELAY > 0
  90. wait_ms(TAP_CODE_DELAY);
  91. #endif
  92. unregister_code16(code);
  93. }
  94. __attribute__((weak)) bool process_action_kb(keyrecord_t *record) { return true; }
  95. __attribute__((weak)) bool process_record_kb(uint16_t keycode, keyrecord_t *record) { return process_record_user(keycode, record); }
  96. __attribute__((weak)) bool process_record_user(uint16_t keycode, keyrecord_t *record) { return true; }
  97. __attribute__((weak)) void post_process_record_kb(uint16_t keycode, keyrecord_t *record) { post_process_record_user(keycode, record); }
  98. __attribute__((weak)) void post_process_record_user(uint16_t keycode, keyrecord_t *record) {}
  99. void reset_keyboard(void) {
  100. clear_keyboard();
  101. #if defined(MIDI_ENABLE) && defined(MIDI_BASIC)
  102. process_midi_all_notes_off();
  103. #endif
  104. #ifdef AUDIO_ENABLE
  105. # ifndef NO_MUSIC_MODE
  106. music_all_notes_off();
  107. # endif
  108. uint16_t timer_start = timer_read();
  109. PLAY_SONG(goodbye_song);
  110. shutdown_user();
  111. while (timer_elapsed(timer_start) < 250) wait_ms(1);
  112. stop_all_notes();
  113. #else
  114. shutdown_user();
  115. wait_ms(250);
  116. #endif
  117. #ifdef HAPTIC_ENABLE
  118. haptic_shutdown();
  119. #endif
  120. bootloader_jump();
  121. }
  122. /* Convert record into usable keycode via the contained event. */
  123. uint16_t get_record_keycode(keyrecord_t *record, bool update_layer_cache) { return get_event_keycode(record->event, update_layer_cache); }
  124. /* Convert event into usable keycode. Checks the layer cache to ensure that it
  125. * retains the correct keycode after a layer change, if the key is still pressed.
  126. * "update_layer_cache" is to ensure that it only updates the layer cache when
  127. * appropriate, otherwise, it will update it and cause layer tap (and other keys)
  128. * from triggering properly.
  129. */
  130. uint16_t get_event_keycode(keyevent_t event, bool update_layer_cache) {
  131. #if !defined(NO_ACTION_LAYER) && !defined(STRICT_LAYER_RELEASE)
  132. /* TODO: Use store_or_get_action() or a similar function. */
  133. if (!disable_action_cache) {
  134. uint8_t layer;
  135. if (event.pressed && update_layer_cache) {
  136. layer = layer_switch_get_layer(event.key);
  137. update_source_layers_cache(event.key, layer);
  138. } else {
  139. layer = read_source_layers_cache(event.key);
  140. }
  141. return keymap_key_to_keycode(layer, event.key);
  142. } else
  143. #endif
  144. return keymap_key_to_keycode(layer_switch_get_layer(event.key), event.key);
  145. }
  146. /* Get keycode, and then call keyboard function */
  147. void post_process_record_quantum(keyrecord_t *record) {
  148. uint16_t keycode = get_record_keycode(record, false);
  149. post_process_record_kb(keycode, record);
  150. }
  151. /* Core keycode function, hands off handling to other functions,
  152. then processes internal quantum keycodes, and then processes
  153. ACTIONs. */
  154. bool process_record_quantum(keyrecord_t *record) {
  155. uint16_t keycode = get_record_keycode(record, true);
  156. // This is how you use actions here
  157. // if (keycode == KC_LEAD) {
  158. // action_t action;
  159. // action.code = ACTION_DEFAULT_LAYER_SET(0);
  160. // process_action(record, action);
  161. // return false;
  162. // }
  163. #ifdef VELOCIKEY_ENABLE
  164. if (velocikey_enabled() && record->event.pressed) {
  165. velocikey_accelerate();
  166. }
  167. #endif
  168. #ifdef WPM_ENABLE
  169. if (record->event.pressed) {
  170. update_wpm(keycode);
  171. }
  172. #endif
  173. #ifdef TAP_DANCE_ENABLE
  174. preprocess_tap_dance(keycode, record);
  175. #endif
  176. if (!(
  177. #if defined(KEY_LOCK_ENABLE)
  178. // Must run first to be able to mask key_up events.
  179. process_key_lock(&keycode, record) &&
  180. #endif
  181. #if defined(DYNAMIC_MACRO_ENABLE) && !defined(DYNAMIC_MACRO_USER_CALL)
  182. // Must run asap to ensure all keypresses are recorded.
  183. process_dynamic_macro(keycode, record) &&
  184. #endif
  185. #if defined(AUDIO_ENABLE) && defined(AUDIO_CLICKY)
  186. process_clicky(keycode, record) &&
  187. #endif // AUDIO_CLICKY
  188. #ifdef HAPTIC_ENABLE
  189. process_haptic(keycode, record) &&
  190. #endif // HAPTIC_ENABLE
  191. #if defined(VIA_ENABLE)
  192. process_record_via(keycode, record) &&
  193. #endif
  194. process_record_kb(keycode, record) &&
  195. #if defined(SEQUENCER_ENABLE)
  196. process_sequencer(keycode, record) &&
  197. #endif
  198. #if defined(MIDI_ENABLE) && defined(MIDI_ADVANCED)
  199. process_midi(keycode, record) &&
  200. #endif
  201. #ifdef AUDIO_ENABLE
  202. process_audio(keycode, record) &&
  203. #endif
  204. #ifdef BACKLIGHT_ENABLE
  205. process_backlight(keycode, record) &&
  206. #endif
  207. #ifdef STENO_ENABLE
  208. process_steno(keycode, record) &&
  209. #endif
  210. #if (defined(AUDIO_ENABLE) || (defined(MIDI_ENABLE) && defined(MIDI_BASIC))) && !defined(NO_MUSIC_MODE)
  211. process_music(keycode, record) &&
  212. #endif
  213. #ifdef TAP_DANCE_ENABLE
  214. process_tap_dance(keycode, record) &&
  215. #endif
  216. #if defined(UNICODE_ENABLE) || defined(UNICODEMAP_ENABLE) || defined(UCIS_ENABLE)
  217. process_unicode_common(keycode, record) &&
  218. #endif
  219. #ifdef LEADER_ENABLE
  220. process_leader(keycode, record) &&
  221. #endif
  222. #ifdef COMBO_ENABLE
  223. process_combo(keycode, record) &&
  224. #endif
  225. #ifdef PRINTING_ENABLE
  226. process_printer(keycode, record) &&
  227. #endif
  228. #ifdef AUTO_SHIFT_ENABLE
  229. process_auto_shift(keycode, record) &&
  230. #endif
  231. #ifdef TERMINAL_ENABLE
  232. process_terminal(keycode, record) &&
  233. #endif
  234. #ifdef SPACE_CADET_ENABLE
  235. process_space_cadet(keycode, record) &&
  236. #endif
  237. #ifdef MAGIC_KEYCODE_ENABLE
  238. process_magic(keycode, record) &&
  239. #endif
  240. #ifdef GRAVE_ESC_ENABLE
  241. process_grave_esc(keycode, record) &&
  242. #endif
  243. #if defined(RGBLIGHT_ENABLE) || defined(RGB_MATRIX_ENABLE)
  244. process_rgb(keycode, record) &&
  245. #endif
  246. #ifdef JOYSTICK_ENABLE
  247. process_joystick(keycode, record) &&
  248. #endif
  249. true)) {
  250. return false;
  251. }
  252. if (record->event.pressed) {
  253. switch (keycode) {
  254. #ifndef NO_RESET
  255. case RESET:
  256. reset_keyboard();
  257. return false;
  258. #endif
  259. #ifndef NO_DEBUG
  260. case DEBUG:
  261. debug_enable ^= 1;
  262. if (debug_enable) {
  263. print("DEBUG: enabled.\n");
  264. } else {
  265. print("DEBUG: disabled.\n");
  266. }
  267. #endif
  268. return false;
  269. case EEPROM_RESET:
  270. eeconfig_init();
  271. return false;
  272. #ifdef VELOCIKEY_ENABLE
  273. case VLK_TOG:
  274. velocikey_toggle();
  275. return false;
  276. #endif
  277. #ifdef BLUETOOTH_ENABLE
  278. case OUT_AUTO:
  279. set_output(OUTPUT_AUTO);
  280. return false;
  281. case OUT_USB:
  282. set_output(OUTPUT_USB);
  283. return false;
  284. case OUT_BT:
  285. set_output(OUTPUT_BLUETOOTH);
  286. return false;
  287. #endif
  288. }
  289. }
  290. return process_action_kb(record);
  291. }
  292. void set_single_persistent_default_layer(uint8_t default_layer) {
  293. #if defined(AUDIO_ENABLE) && defined(DEFAULT_LAYER_SONGS)
  294. PLAY_SONG(default_layer_songs[default_layer]);
  295. #endif
  296. eeconfig_update_default_layer(1U << default_layer);
  297. default_layer_set(1U << default_layer);
  298. }
  299. layer_state_t update_tri_layer_state(layer_state_t state, uint8_t layer1, uint8_t layer2, uint8_t layer3) {
  300. layer_state_t mask12 = (1UL << layer1) | (1UL << layer2);
  301. layer_state_t mask3 = 1UL << layer3;
  302. return (state & mask12) == mask12 ? (state | mask3) : (state & ~mask3);
  303. }
  304. void update_tri_layer(uint8_t layer1, uint8_t layer2, uint8_t layer3) { layer_state_set(update_tri_layer_state(layer_state, layer1, layer2, layer3)); }
  305. void tap_random_base64(void) {
  306. #if defined(__AVR_ATmega32U4__)
  307. uint8_t key = (TCNT0 + TCNT1 + TCNT3 + TCNT4) % 64;
  308. #else
  309. uint8_t key = rand() % 64;
  310. #endif
  311. switch (key) {
  312. case 0 ... 25:
  313. send_char(key + 'A');
  314. break;
  315. case 26 ... 51:
  316. send_char(key - 26 + 'a');
  317. break;
  318. case 52:
  319. send_char('0');
  320. break;
  321. case 53 ... 61:
  322. send_char(key - 53 + '1');
  323. break;
  324. case 62:
  325. send_char('+');
  326. break;
  327. case 63:
  328. send_char('/');
  329. break;
  330. }
  331. }
  332. void matrix_init_quantum() {
  333. #ifdef BOOTMAGIC_LITE
  334. bootmagic_lite();
  335. #endif
  336. if (!eeconfig_is_enabled()) {
  337. eeconfig_init();
  338. }
  339. #if defined(LED_NUM_LOCK_PIN) || defined(LED_CAPS_LOCK_PIN) || defined(LED_SCROLL_LOCK_PIN) || defined(LED_COMPOSE_PIN) || defined(LED_KANA_PIN)
  340. // TODO: remove calls to led_init_ports from keyboards and remove ifdef
  341. led_init_ports();
  342. #endif
  343. #ifdef BACKLIGHT_ENABLE
  344. # ifdef LED_MATRIX_ENABLE
  345. led_matrix_init();
  346. # else
  347. backlight_init_ports();
  348. # endif
  349. #endif
  350. #ifdef AUDIO_ENABLE
  351. audio_init();
  352. #endif
  353. #ifdef RGB_MATRIX_ENABLE
  354. rgb_matrix_init();
  355. #endif
  356. #if defined(UNICODE_ENABLE) || defined(UNICODEMAP_ENABLE) || defined(UCIS_ENABLE)
  357. unicode_input_mode_init();
  358. #endif
  359. #ifdef HAPTIC_ENABLE
  360. haptic_init();
  361. #endif
  362. #if defined(BLUETOOTH_ENABLE) && defined(OUTPUT_AUTO_ENABLE)
  363. set_output(OUTPUT_AUTO);
  364. #endif
  365. matrix_init_kb();
  366. }
  367. void matrix_scan_quantum() {
  368. #if defined(AUDIO_ENABLE)
  369. // There are some tasks that need to be run a little bit
  370. // after keyboard startup, or else they will not work correctly
  371. // because of interaction with the USB device state, which
  372. // may still be in flux...
  373. //
  374. // At the moment the only feature that needs this is the
  375. // startup song.
  376. static bool delayed_tasks_run = false;
  377. static uint16_t delayed_task_timer = 0;
  378. if (!delayed_tasks_run) {
  379. if (!delayed_task_timer) {
  380. delayed_task_timer = timer_read();
  381. } else if (timer_elapsed(delayed_task_timer) > 300) {
  382. audio_startup();
  383. delayed_tasks_run = true;
  384. }
  385. }
  386. #endif
  387. #if defined(AUDIO_ENABLE) && !defined(NO_MUSIC_MODE)
  388. matrix_scan_music();
  389. #endif
  390. #ifdef SEQUENCER_ENABLE
  391. matrix_scan_sequencer();
  392. #endif
  393. #ifdef TAP_DANCE_ENABLE
  394. matrix_scan_tap_dance();
  395. #endif
  396. #ifdef COMBO_ENABLE
  397. matrix_scan_combo();
  398. #endif
  399. #ifdef LED_MATRIX_ENABLE
  400. led_matrix_task();
  401. #endif
  402. #ifdef WPM_ENABLE
  403. decay_wpm();
  404. #endif
  405. #ifdef HAPTIC_ENABLE
  406. haptic_task();
  407. #endif
  408. #ifdef DIP_SWITCH_ENABLE
  409. dip_switch_read(false);
  410. #endif
  411. #ifdef AUTO_SHIFT_ENABLE
  412. autoshift_matrix_scan();
  413. #endif
  414. matrix_scan_kb();
  415. }
  416. #ifdef HD44780_ENABLED
  417. # include "hd44780.h"
  418. #endif
  419. // Functions for spitting out values
  420. //
  421. void send_dword(uint32_t number) {
  422. uint16_t word = (number >> 16);
  423. send_word(word);
  424. send_word(number & 0xFFFFUL);
  425. }
  426. void send_word(uint16_t number) {
  427. uint8_t byte = number >> 8;
  428. send_byte(byte);
  429. send_byte(number & 0xFF);
  430. }
  431. void send_byte(uint8_t number) {
  432. uint8_t nibble = number >> 4;
  433. send_nibble(nibble);
  434. send_nibble(number & 0xF);
  435. }
  436. void send_nibble(uint8_t number) { tap_code16(hex_to_keycode(number)); }
  437. __attribute__((weak)) uint16_t hex_to_keycode(uint8_t hex) {
  438. hex = hex & 0xF;
  439. if (hex == 0x0) {
  440. return KC_0;
  441. } else if (hex < 0xA) {
  442. return KC_1 + (hex - 0x1);
  443. } else {
  444. return KC_A + (hex - 0xA);
  445. }
  446. }
  447. void api_send_unicode(uint32_t unicode) {
  448. #ifdef API_ENABLE
  449. uint8_t chunk[4];
  450. dword_to_bytes(unicode, chunk);
  451. MT_SEND_DATA(DT_UNICODE, chunk, 5);
  452. #endif
  453. }
  454. //------------------------------------------------------------------------------
  455. // Override these functions in your keymap file to play different tunes on
  456. // different events such as startup and bootloader jump
  457. __attribute__((weak)) void startup_user() {}
  458. __attribute__((weak)) void shutdown_user() {}