quantum.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230
  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 PROTOCOL_LUFA
  18. #include "outputselect.h"
  19. #endif
  20. #ifndef TAPPING_TERM
  21. #define TAPPING_TERM 200
  22. #endif
  23. #ifndef BREATHING_PERIOD
  24. #define BREATHING_PERIOD 6
  25. #endif
  26. #include "backlight.h"
  27. extern backlight_config_t backlight_config;
  28. #ifdef FAUXCLICKY_ENABLE
  29. #include "fauxclicky.h"
  30. #endif
  31. #ifdef AUDIO_ENABLE
  32. #ifndef GOODBYE_SONG
  33. #define GOODBYE_SONG SONG(GOODBYE_SOUND)
  34. #endif
  35. #ifndef AG_NORM_SONG
  36. #define AG_NORM_SONG SONG(AG_NORM_SOUND)
  37. #endif
  38. #ifndef AG_SWAP_SONG
  39. #define AG_SWAP_SONG SONG(AG_SWAP_SOUND)
  40. #endif
  41. float goodbye_song[][2] = GOODBYE_SONG;
  42. float ag_norm_song[][2] = AG_NORM_SONG;
  43. float ag_swap_song[][2] = AG_SWAP_SONG;
  44. #ifdef DEFAULT_LAYER_SONGS
  45. float default_layer_songs[][16][2] = DEFAULT_LAYER_SONGS;
  46. #endif
  47. #endif
  48. static void do_code16 (uint16_t code, void (*f) (uint8_t)) {
  49. switch (code) {
  50. case QK_MODS ... QK_MODS_MAX:
  51. break;
  52. default:
  53. return;
  54. }
  55. if (code & QK_LCTL)
  56. f(KC_LCTL);
  57. if (code & QK_LSFT)
  58. f(KC_LSFT);
  59. if (code & QK_LALT)
  60. f(KC_LALT);
  61. if (code & QK_LGUI)
  62. f(KC_LGUI);
  63. if (code < QK_RMODS_MIN) return;
  64. if (code & QK_RCTL)
  65. f(KC_RCTL);
  66. if (code & QK_RSFT)
  67. f(KC_RSFT);
  68. if (code & QK_RALT)
  69. f(KC_RALT);
  70. if (code & QK_RGUI)
  71. f(KC_RGUI);
  72. }
  73. static inline void qk_register_weak_mods(uint8_t kc) {
  74. add_weak_mods(MOD_BIT(kc));
  75. send_keyboard_report();
  76. }
  77. static inline void qk_unregister_weak_mods(uint8_t kc) {
  78. del_weak_mods(MOD_BIT(kc));
  79. send_keyboard_report();
  80. }
  81. static inline void qk_register_mods(uint8_t kc) {
  82. add_weak_mods(MOD_BIT(kc));
  83. send_keyboard_report();
  84. }
  85. static inline void qk_unregister_mods(uint8_t kc) {
  86. del_weak_mods(MOD_BIT(kc));
  87. send_keyboard_report();
  88. }
  89. void register_code16 (uint16_t code) {
  90. if (IS_MOD(code) || code == KC_NO) {
  91. do_code16 (code, qk_register_mods);
  92. } else {
  93. do_code16 (code, qk_register_weak_mods);
  94. }
  95. register_code (code);
  96. }
  97. void unregister_code16 (uint16_t code) {
  98. unregister_code (code);
  99. if (IS_MOD(code) || code == KC_NO) {
  100. do_code16 (code, qk_unregister_mods);
  101. } else {
  102. do_code16 (code, qk_unregister_weak_mods);
  103. }
  104. }
  105. __attribute__ ((weak))
  106. bool process_action_kb(keyrecord_t *record) {
  107. return true;
  108. }
  109. __attribute__ ((weak))
  110. bool process_record_kb(uint16_t keycode, keyrecord_t *record) {
  111. return process_record_user(keycode, record);
  112. }
  113. __attribute__ ((weak))
  114. bool process_record_user(uint16_t keycode, keyrecord_t *record) {
  115. return true;
  116. }
  117. void reset_keyboard(void) {
  118. clear_keyboard();
  119. #if defined(MIDI_ENABLE) && defined(MIDI_BASIC)
  120. process_midi_all_notes_off();
  121. #endif
  122. #if defined(AUDIO_ENABLE)
  123. music_all_notes_off();
  124. uint16_t timer_start = timer_read();
  125. PLAY_SONG(goodbye_song);
  126. shutdown_user();
  127. while(timer_elapsed(timer_start) < 250)
  128. wait_ms(1);
  129. stop_all_notes();
  130. #else
  131. wait_ms(250);
  132. #endif
  133. // this is also done later in bootloader.c - not sure if it's neccesary here
  134. #ifdef BOOTLOADER_CATERINA
  135. *(uint16_t *)0x0800 = 0x7777; // these two are a-star-specific
  136. #endif
  137. bootloader_jump();
  138. }
  139. // Shift / paren setup
  140. #ifndef LSPO_KEY
  141. #define LSPO_KEY KC_9
  142. #endif
  143. #ifndef RSPC_KEY
  144. #define RSPC_KEY KC_0
  145. #endif
  146. // Shift / Enter setup
  147. #ifndef SFTENT_KEY
  148. #define SFTENT_KEY KC_ENT
  149. #endif
  150. static bool shift_interrupted[2] = {0, 0};
  151. static uint16_t scs_timer[2] = {0, 0};
  152. /* true if the last press of GRAVE_ESC was shifted (i.e. GUI or SHIFT were pressed), false otherwise.
  153. * Used to ensure that the correct keycode is released if the key is released.
  154. */
  155. static bool grave_esc_was_shifted = false;
  156. bool process_record_quantum(keyrecord_t *record) {
  157. /* This gets the keycode from the key pressed */
  158. keypos_t key = record->event.key;
  159. uint16_t keycode;
  160. #if !defined(NO_ACTION_LAYER) && defined(PREVENT_STUCK_MODIFIERS)
  161. /* TODO: Use store_or_get_action() or a similar function. */
  162. if (!disable_action_cache) {
  163. uint8_t layer;
  164. if (record->event.pressed) {
  165. layer = layer_switch_get_layer(key);
  166. update_source_layers_cache(key, layer);
  167. } else {
  168. layer = read_source_layers_cache(key);
  169. }
  170. keycode = keymap_key_to_keycode(layer, key);
  171. } else
  172. #endif
  173. keycode = keymap_key_to_keycode(layer_switch_get_layer(key), key);
  174. // This is how you use actions here
  175. // if (keycode == KC_LEAD) {
  176. // action_t action;
  177. // action.code = ACTION_DEFAULT_LAYER_SET(0);
  178. // process_action(record, action);
  179. // return false;
  180. // }
  181. if (!(
  182. #if defined(KEY_LOCK_ENABLE)
  183. // Must run first to be able to mask key_up events.
  184. process_key_lock(&keycode, record) &&
  185. #endif
  186. process_record_kb(keycode, record) &&
  187. #if defined(MIDI_ENABLE) && defined(MIDI_ADVANCED)
  188. process_midi(keycode, record) &&
  189. #endif
  190. #ifdef AUDIO_ENABLE
  191. process_audio(keycode, record) &&
  192. #endif
  193. #ifdef STENO_ENABLE
  194. process_steno(keycode, record) &&
  195. #endif
  196. #if defined(AUDIO_ENABLE) || (defined(MIDI_ENABLE) && defined(MIDI_BASIC))
  197. process_music(keycode, record) &&
  198. #endif
  199. #ifdef TAP_DANCE_ENABLE
  200. process_tap_dance(keycode, record) &&
  201. #endif
  202. #ifndef DISABLE_LEADER
  203. process_leader(keycode, record) &&
  204. #endif
  205. #ifndef DISABLE_CHORDING
  206. process_chording(keycode, record) &&
  207. #endif
  208. #ifdef COMBO_ENABLE
  209. process_combo(keycode, record) &&
  210. #endif
  211. #ifdef UNICODE_ENABLE
  212. process_unicode(keycode, record) &&
  213. #endif
  214. #ifdef UCIS_ENABLE
  215. process_ucis(keycode, record) &&
  216. #endif
  217. #ifdef PRINTING_ENABLE
  218. process_printer(keycode, record) &&
  219. #endif
  220. #ifdef AUTO_SHIFT_ENABLE
  221. process_auto_shift(keycode, record) &&
  222. #endif
  223. #ifdef UNICODEMAP_ENABLE
  224. process_unicode_map(keycode, record) &&
  225. #endif
  226. #ifdef TERMINAL_ENABLE
  227. process_terminal(keycode, record) &&
  228. #endif
  229. true)) {
  230. return false;
  231. }
  232. // Shift / paren setup
  233. switch(keycode) {
  234. case RESET:
  235. if (record->event.pressed) {
  236. reset_keyboard();
  237. }
  238. return false;
  239. case DEBUG:
  240. if (record->event.pressed) {
  241. debug_enable = true;
  242. print("DEBUG: enabled.\n");
  243. }
  244. return false;
  245. #ifdef FAUXCLICKY_ENABLE
  246. case FC_TOG:
  247. if (record->event.pressed) {
  248. FAUXCLICKY_TOGGLE;
  249. }
  250. return false;
  251. case FC_ON:
  252. if (record->event.pressed) {
  253. FAUXCLICKY_ON;
  254. }
  255. return false;
  256. case FC_OFF:
  257. if (record->event.pressed) {
  258. FAUXCLICKY_OFF;
  259. }
  260. return false;
  261. #endif
  262. #ifdef RGBLIGHT_ENABLE
  263. case RGB_TOG:
  264. if (record->event.pressed) {
  265. rgblight_toggle();
  266. }
  267. return false;
  268. case RGB_MODE_FORWARD:
  269. if (record->event.pressed) {
  270. uint8_t shifted = get_mods() & (MOD_BIT(KC_LSHIFT)|MOD_BIT(KC_RSHIFT));
  271. if(shifted) {
  272. rgblight_step_reverse();
  273. }
  274. else {
  275. rgblight_step();
  276. }
  277. }
  278. return false;
  279. case RGB_MODE_REVERSE:
  280. if (record->event.pressed) {
  281. uint8_t shifted = get_mods() & (MOD_BIT(KC_LSHIFT)|MOD_BIT(KC_RSHIFT));
  282. if(shifted) {
  283. rgblight_step();
  284. }
  285. else {
  286. rgblight_step_reverse();
  287. }
  288. }
  289. return false;
  290. case RGB_HUI:
  291. if (record->event.pressed) {
  292. rgblight_increase_hue();
  293. }
  294. return false;
  295. case RGB_HUD:
  296. if (record->event.pressed) {
  297. rgblight_decrease_hue();
  298. }
  299. return false;
  300. case RGB_SAI:
  301. if (record->event.pressed) {
  302. rgblight_increase_sat();
  303. }
  304. return false;
  305. case RGB_SAD:
  306. if (record->event.pressed) {
  307. rgblight_decrease_sat();
  308. }
  309. return false;
  310. case RGB_VAI:
  311. if (record->event.pressed) {
  312. rgblight_increase_val();
  313. }
  314. return false;
  315. case RGB_VAD:
  316. if (record->event.pressed) {
  317. rgblight_decrease_val();
  318. }
  319. return false;
  320. case RGB_MODE_PLAIN:
  321. if (record->event.pressed) {
  322. rgblight_mode(1);
  323. }
  324. return false;
  325. case RGB_MODE_BREATHE:
  326. if (record->event.pressed) {
  327. if ((2 <= rgblight_get_mode()) && (rgblight_get_mode() < 5)) {
  328. rgblight_step();
  329. } else {
  330. rgblight_mode(2);
  331. }
  332. }
  333. return false;
  334. case RGB_MODE_RAINBOW:
  335. if (record->event.pressed) {
  336. if ((6 <= rgblight_get_mode()) && (rgblight_get_mode() < 8)) {
  337. rgblight_step();
  338. } else {
  339. rgblight_mode(6);
  340. }
  341. }
  342. return false;
  343. case RGB_MODE_SWIRL:
  344. if (record->event.pressed) {
  345. if ((9 <= rgblight_get_mode()) && (rgblight_get_mode() < 14)) {
  346. rgblight_step();
  347. } else {
  348. rgblight_mode(9);
  349. }
  350. }
  351. return false;
  352. case RGB_MODE_SNAKE:
  353. if (record->event.pressed) {
  354. if ((15 <= rgblight_get_mode()) && (rgblight_get_mode() < 20)) {
  355. rgblight_step();
  356. } else {
  357. rgblight_mode(15);
  358. }
  359. }
  360. return false;
  361. case RGB_MODE_KNIGHT:
  362. if (record->event.pressed) {
  363. if ((21 <= rgblight_get_mode()) && (rgblight_get_mode() < 23)) {
  364. rgblight_step();
  365. } else {
  366. rgblight_mode(21);
  367. }
  368. }
  369. return false;
  370. case RGB_MODE_XMAS:
  371. if (record->event.pressed) {
  372. rgblight_mode(24);
  373. }
  374. return false;
  375. case RGB_MODE_GRADIENT:
  376. if (record->event.pressed) {
  377. if ((25 <= rgblight_get_mode()) && (rgblight_get_mode() < 34)) {
  378. rgblight_step();
  379. } else {
  380. rgblight_mode(25);
  381. }
  382. }
  383. return false;
  384. #endif
  385. #ifdef PROTOCOL_LUFA
  386. case OUT_AUTO:
  387. if (record->event.pressed) {
  388. set_output(OUTPUT_AUTO);
  389. }
  390. return false;
  391. case OUT_USB:
  392. if (record->event.pressed) {
  393. set_output(OUTPUT_USB);
  394. }
  395. return false;
  396. #ifdef BLUETOOTH_ENABLE
  397. case OUT_BT:
  398. if (record->event.pressed) {
  399. set_output(OUTPUT_BLUETOOTH);
  400. }
  401. return false;
  402. #endif
  403. #endif
  404. case MAGIC_SWAP_CONTROL_CAPSLOCK ... MAGIC_TOGGLE_NKRO:
  405. if (record->event.pressed) {
  406. // MAGIC actions (BOOTMAGIC without the boot)
  407. if (!eeconfig_is_enabled()) {
  408. eeconfig_init();
  409. }
  410. /* keymap config */
  411. keymap_config.raw = eeconfig_read_keymap();
  412. switch (keycode)
  413. {
  414. case MAGIC_SWAP_CONTROL_CAPSLOCK:
  415. keymap_config.swap_control_capslock = true;
  416. break;
  417. case MAGIC_CAPSLOCK_TO_CONTROL:
  418. keymap_config.capslock_to_control = true;
  419. break;
  420. case MAGIC_SWAP_LALT_LGUI:
  421. keymap_config.swap_lalt_lgui = true;
  422. break;
  423. case MAGIC_SWAP_RALT_RGUI:
  424. keymap_config.swap_ralt_rgui = true;
  425. break;
  426. case MAGIC_NO_GUI:
  427. keymap_config.no_gui = true;
  428. break;
  429. case MAGIC_SWAP_GRAVE_ESC:
  430. keymap_config.swap_grave_esc = true;
  431. break;
  432. case MAGIC_SWAP_BACKSLASH_BACKSPACE:
  433. keymap_config.swap_backslash_backspace = true;
  434. break;
  435. case MAGIC_HOST_NKRO:
  436. keymap_config.nkro = true;
  437. break;
  438. case MAGIC_SWAP_ALT_GUI:
  439. keymap_config.swap_lalt_lgui = true;
  440. keymap_config.swap_ralt_rgui = true;
  441. #ifdef AUDIO_ENABLE
  442. PLAY_SONG(ag_swap_song);
  443. #endif
  444. break;
  445. case MAGIC_UNSWAP_CONTROL_CAPSLOCK:
  446. keymap_config.swap_control_capslock = false;
  447. break;
  448. case MAGIC_UNCAPSLOCK_TO_CONTROL:
  449. keymap_config.capslock_to_control = false;
  450. break;
  451. case MAGIC_UNSWAP_LALT_LGUI:
  452. keymap_config.swap_lalt_lgui = false;
  453. break;
  454. case MAGIC_UNSWAP_RALT_RGUI:
  455. keymap_config.swap_ralt_rgui = false;
  456. break;
  457. case MAGIC_UNNO_GUI:
  458. keymap_config.no_gui = false;
  459. break;
  460. case MAGIC_UNSWAP_GRAVE_ESC:
  461. keymap_config.swap_grave_esc = false;
  462. break;
  463. case MAGIC_UNSWAP_BACKSLASH_BACKSPACE:
  464. keymap_config.swap_backslash_backspace = false;
  465. break;
  466. case MAGIC_UNHOST_NKRO:
  467. keymap_config.nkro = false;
  468. break;
  469. case MAGIC_UNSWAP_ALT_GUI:
  470. keymap_config.swap_lalt_lgui = false;
  471. keymap_config.swap_ralt_rgui = false;
  472. #ifdef AUDIO_ENABLE
  473. PLAY_SONG(ag_norm_song);
  474. #endif
  475. break;
  476. case MAGIC_TOGGLE_NKRO:
  477. keymap_config.nkro = !keymap_config.nkro;
  478. break;
  479. default:
  480. break;
  481. }
  482. eeconfig_update_keymap(keymap_config.raw);
  483. clear_keyboard(); // clear to prevent stuck keys
  484. return false;
  485. }
  486. break;
  487. case KC_LSPO: {
  488. if (record->event.pressed) {
  489. shift_interrupted[0] = false;
  490. scs_timer[0] = timer_read ();
  491. register_mods(MOD_BIT(KC_LSFT));
  492. }
  493. else {
  494. #ifdef DISABLE_SPACE_CADET_ROLLOVER
  495. if (get_mods() & MOD_BIT(KC_RSFT)) {
  496. shift_interrupted[0] = true;
  497. shift_interrupted[1] = true;
  498. }
  499. #endif
  500. if (!shift_interrupted[0] && timer_elapsed(scs_timer[0]) < TAPPING_TERM) {
  501. register_code(LSPO_KEY);
  502. unregister_code(LSPO_KEY);
  503. }
  504. unregister_mods(MOD_BIT(KC_LSFT));
  505. }
  506. return false;
  507. }
  508. case KC_RSPC: {
  509. if (record->event.pressed) {
  510. shift_interrupted[1] = false;
  511. scs_timer[1] = timer_read ();
  512. register_mods(MOD_BIT(KC_RSFT));
  513. }
  514. else {
  515. #ifdef DISABLE_SPACE_CADET_ROLLOVER
  516. if (get_mods() & MOD_BIT(KC_LSFT)) {
  517. shift_interrupted[0] = true;
  518. shift_interrupted[1] = true;
  519. }
  520. #endif
  521. if (!shift_interrupted[1] && timer_elapsed(scs_timer[1]) < TAPPING_TERM) {
  522. register_code(RSPC_KEY);
  523. unregister_code(RSPC_KEY);
  524. }
  525. unregister_mods(MOD_BIT(KC_RSFT));
  526. }
  527. return false;
  528. }
  529. case KC_SFTENT: {
  530. if (record->event.pressed) {
  531. shift_interrupted[1] = false;
  532. scs_timer[1] = timer_read ();
  533. register_mods(MOD_BIT(KC_RSFT));
  534. }
  535. else if (!shift_interrupted[1] && timer_elapsed(scs_timer[1]) < TAPPING_TERM) {
  536. unregister_mods(MOD_BIT(KC_RSFT));
  537. register_code(SFTENT_KEY);
  538. unregister_code(SFTENT_KEY);
  539. }
  540. else {
  541. unregister_mods(MOD_BIT(KC_RSFT));
  542. }
  543. return false;
  544. }
  545. case GRAVE_ESC: {
  546. uint8_t shifted = get_mods() & ((MOD_BIT(KC_LSHIFT)|MOD_BIT(KC_RSHIFT)
  547. |MOD_BIT(KC_LGUI)|MOD_BIT(KC_RGUI)));
  548. #ifdef GRAVE_ESC_ALT_OVERRIDE
  549. // if ALT is pressed, ESC is always sent
  550. // this is handy for the cmd+opt+esc shortcut on macOS, among other things.
  551. if (get_mods() & (MOD_BIT(KC_LALT) | MOD_BIT(KC_RALT))) {
  552. shifted = 0;
  553. }
  554. #endif
  555. #ifdef GRAVE_ESC_CTRL_OVERRIDE
  556. // if CTRL is pressed, ESC is always sent
  557. // this is handy for the ctrl+shift+esc shortcut on windows, among other things.
  558. if (get_mods() & (MOD_BIT(KC_LCTL) | MOD_BIT(KC_RCTL))) {
  559. shifted = 0;
  560. }
  561. #endif
  562. #ifdef GRAVE_ESC_GUI_OVERRIDE
  563. // if GUI is pressed, ESC is always sent
  564. if (get_mods() & (MOD_BIT(KC_LGUI) | MOD_BIT(KC_RGUI))) {
  565. shifted = 0;
  566. }
  567. #endif
  568. #ifdef GRAVE_ESC_SHIFT_OVERRIDE
  569. // if SHIFT is pressed, ESC is always sent
  570. if (get_mods() & (MOD_BIT(KC_LSHIFT) | MOD_BIT(KC_RSHIFT))) {
  571. shifted = 0;
  572. }
  573. #endif
  574. if (record->event.pressed) {
  575. grave_esc_was_shifted = shifted;
  576. add_key(shifted ? KC_GRAVE : KC_ESCAPE);
  577. }
  578. else {
  579. del_key(grave_esc_was_shifted ? KC_GRAVE : KC_ESCAPE);
  580. }
  581. send_keyboard_report();
  582. return false;
  583. }
  584. #if defined(BACKLIGHT_ENABLE) && defined(BACKLIGHT_BREATHING)
  585. case BL_BRTG: {
  586. if (record->event.pressed)
  587. breathing_toggle();
  588. return false;
  589. }
  590. #endif
  591. default: {
  592. shift_interrupted[0] = true;
  593. shift_interrupted[1] = true;
  594. break;
  595. }
  596. }
  597. return process_action_kb(record);
  598. }
  599. __attribute__ ((weak))
  600. const bool ascii_to_shift_lut[0x80] PROGMEM = {
  601. 0, 0, 0, 0, 0, 0, 0, 0,
  602. 0, 0, 0, 0, 0, 0, 0, 0,
  603. 0, 0, 0, 0, 0, 0, 0, 0,
  604. 0, 0, 0, 0, 0, 0, 0, 0,
  605. 0, 1, 1, 1, 1, 1, 1, 0,
  606. 1, 1, 1, 1, 0, 0, 0, 0,
  607. 0, 0, 0, 0, 0, 0, 0, 0,
  608. 0, 0, 1, 0, 1, 0, 1, 1,
  609. 1, 1, 1, 1, 1, 1, 1, 1,
  610. 1, 1, 1, 1, 1, 1, 1, 1,
  611. 1, 1, 1, 1, 1, 1, 1, 1,
  612. 1, 1, 1, 0, 0, 0, 1, 1,
  613. 0, 0, 0, 0, 0, 0, 0, 0,
  614. 0, 0, 0, 0, 0, 0, 0, 0,
  615. 0, 0, 0, 0, 0, 0, 0, 0,
  616. 0, 0, 0, 1, 1, 1, 1, 0
  617. };
  618. __attribute__ ((weak))
  619. const uint8_t ascii_to_keycode_lut[0x80] PROGMEM = {
  620. 0, 0, 0, 0, 0, 0, 0, 0,
  621. KC_BSPC, KC_TAB, KC_ENT, 0, 0, 0, 0, 0,
  622. 0, 0, 0, 0, 0, 0, 0, 0,
  623. 0, 0, 0, KC_ESC, 0, 0, 0, 0,
  624. KC_SPC, KC_1, KC_QUOT, KC_3, KC_4, KC_5, KC_7, KC_QUOT,
  625. KC_9, KC_0, KC_8, KC_EQL, KC_COMM, KC_MINS, KC_DOT, KC_SLSH,
  626. KC_0, KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7,
  627. KC_8, KC_9, KC_SCLN, KC_SCLN, KC_COMM, KC_EQL, KC_DOT, KC_SLSH,
  628. KC_2, KC_A, KC_B, KC_C, KC_D, KC_E, KC_F, KC_G,
  629. KC_H, KC_I, KC_J, KC_K, KC_L, KC_M, KC_N, KC_O,
  630. KC_P, KC_Q, KC_R, KC_S, KC_T, KC_U, KC_V, KC_W,
  631. KC_X, KC_Y, KC_Z, KC_LBRC, KC_BSLS, KC_RBRC, KC_6, KC_MINS,
  632. KC_GRV, KC_A, KC_B, KC_C, KC_D, KC_E, KC_F, KC_G,
  633. KC_H, KC_I, KC_J, KC_K, KC_L, KC_M, KC_N, KC_O,
  634. KC_P, KC_Q, KC_R, KC_S, KC_T, KC_U, KC_V, KC_W,
  635. KC_X, KC_Y, KC_Z, KC_LBRC, KC_BSLS, KC_RBRC, KC_GRV, KC_DEL
  636. };
  637. void send_string(const char *str) {
  638. send_string_with_delay(str, 0);
  639. }
  640. void send_string_P(const char *str) {
  641. send_string_with_delay_P(str, 0);
  642. }
  643. void send_string_with_delay(const char *str, uint8_t interval) {
  644. while (1) {
  645. char ascii_code = *str;
  646. if (!ascii_code) break;
  647. if (ascii_code == 1) {
  648. // tap
  649. uint8_t keycode = *(++str);
  650. register_code(keycode);
  651. unregister_code(keycode);
  652. } else if (ascii_code == 2) {
  653. // down
  654. uint8_t keycode = *(++str);
  655. register_code(keycode);
  656. } else if (ascii_code == 3) {
  657. // up
  658. uint8_t keycode = *(++str);
  659. unregister_code(keycode);
  660. } else {
  661. send_char(ascii_code);
  662. }
  663. ++str;
  664. // interval
  665. { uint8_t ms = interval; while (ms--) wait_ms(1); }
  666. }
  667. }
  668. void send_string_with_delay_P(const char *str, uint8_t interval) {
  669. while (1) {
  670. char ascii_code = pgm_read_byte(str);
  671. if (!ascii_code) break;
  672. if (ascii_code == 1) {
  673. // tap
  674. uint8_t keycode = pgm_read_byte(++str);
  675. register_code(keycode);
  676. unregister_code(keycode);
  677. } else if (ascii_code == 2) {
  678. // down
  679. uint8_t keycode = pgm_read_byte(++str);
  680. register_code(keycode);
  681. } else if (ascii_code == 3) {
  682. // up
  683. uint8_t keycode = pgm_read_byte(++str);
  684. unregister_code(keycode);
  685. } else {
  686. send_char(ascii_code);
  687. }
  688. ++str;
  689. // interval
  690. { uint8_t ms = interval; while (ms--) wait_ms(1); }
  691. }
  692. }
  693. void send_char(char ascii_code) {
  694. uint8_t keycode;
  695. keycode = pgm_read_byte(&ascii_to_keycode_lut[(uint8_t)ascii_code]);
  696. if (pgm_read_byte(&ascii_to_shift_lut[(uint8_t)ascii_code])) {
  697. register_code(KC_LSFT);
  698. register_code(keycode);
  699. unregister_code(keycode);
  700. unregister_code(KC_LSFT);
  701. } else {
  702. register_code(keycode);
  703. unregister_code(keycode);
  704. }
  705. }
  706. void set_single_persistent_default_layer(uint8_t default_layer) {
  707. #if defined(AUDIO_ENABLE) && defined(DEFAULT_LAYER_SONGS)
  708. PLAY_SONG(default_layer_songs[default_layer]);
  709. #endif
  710. eeconfig_update_default_layer(1U<<default_layer);
  711. default_layer_set(1U<<default_layer);
  712. }
  713. void update_tri_layer(uint8_t layer1, uint8_t layer2, uint8_t layer3) {
  714. if (IS_LAYER_ON(layer1) && IS_LAYER_ON(layer2)) {
  715. layer_on(layer3);
  716. } else {
  717. layer_off(layer3);
  718. }
  719. }
  720. void tap_random_base64(void) {
  721. #if defined(__AVR_ATmega32U4__)
  722. uint8_t key = (TCNT0 + TCNT1 + TCNT3 + TCNT4) % 64;
  723. #else
  724. uint8_t key = rand() % 64;
  725. #endif
  726. switch (key) {
  727. case 0 ... 25:
  728. register_code(KC_LSFT);
  729. register_code(key + KC_A);
  730. unregister_code(key + KC_A);
  731. unregister_code(KC_LSFT);
  732. break;
  733. case 26 ... 51:
  734. register_code(key - 26 + KC_A);
  735. unregister_code(key - 26 + KC_A);
  736. break;
  737. case 52:
  738. register_code(KC_0);
  739. unregister_code(KC_0);
  740. break;
  741. case 53 ... 61:
  742. register_code(key - 53 + KC_1);
  743. unregister_code(key - 53 + KC_1);
  744. break;
  745. case 62:
  746. register_code(KC_LSFT);
  747. register_code(KC_EQL);
  748. unregister_code(KC_EQL);
  749. unregister_code(KC_LSFT);
  750. break;
  751. case 63:
  752. register_code(KC_SLSH);
  753. unregister_code(KC_SLSH);
  754. break;
  755. }
  756. }
  757. void matrix_init_quantum() {
  758. #ifdef BACKLIGHT_ENABLE
  759. backlight_init_ports();
  760. #endif
  761. #ifdef AUDIO_ENABLE
  762. audio_init();
  763. #endif
  764. matrix_init_kb();
  765. }
  766. void matrix_scan_quantum() {
  767. #ifdef AUDIO_ENABLE
  768. matrix_scan_music();
  769. #endif
  770. #ifdef TAP_DANCE_ENABLE
  771. matrix_scan_tap_dance();
  772. #endif
  773. #ifdef COMBO_ENABLE
  774. matrix_scan_combo();
  775. #endif
  776. #if defined(BACKLIGHT_ENABLE) && defined(BACKLIGHT_PIN)
  777. backlight_task();
  778. #endif
  779. matrix_scan_kb();
  780. }
  781. #if defined(BACKLIGHT_ENABLE) && defined(BACKLIGHT_PIN)
  782. static const uint8_t backlight_pin = BACKLIGHT_PIN;
  783. // depending on the pin, we use a different output compare unit
  784. #if BACKLIGHT_PIN == B7
  785. # define COM1x1 COM1C1
  786. # define OCR1x OCR1C
  787. #elif BACKLIGHT_PIN == B6
  788. # define COM1x1 COM1B1
  789. # define OCR1x OCR1B
  790. #elif BACKLIGHT_PIN == B5
  791. # define COM1x1 COM1A1
  792. # define OCR1x OCR1A
  793. #else
  794. # define NO_HARDWARE_PWM
  795. #endif
  796. #ifndef BACKLIGHT_ON_STATE
  797. #define BACKLIGHT_ON_STATE 0
  798. #endif
  799. #ifdef NO_HARDWARE_PWM // pwm through software
  800. __attribute__ ((weak))
  801. void backlight_init_ports(void)
  802. {
  803. // Setup backlight pin as output and output to on state.
  804. // DDRx |= n
  805. _SFR_IO8((backlight_pin >> 4) + 1) |= _BV(backlight_pin & 0xF);
  806. #if BACKLIGHT_ON_STATE == 0
  807. // PORTx &= ~n
  808. _SFR_IO8((backlight_pin >> 4) + 2) &= ~_BV(backlight_pin & 0xF);
  809. #else
  810. // PORTx |= n
  811. _SFR_IO8((backlight_pin >> 4) + 2) |= _BV(backlight_pin & 0xF);
  812. #endif
  813. }
  814. __attribute__ ((weak))
  815. void backlight_set(uint8_t level) {}
  816. uint8_t backlight_tick = 0;
  817. void backlight_task(void) {
  818. if ((0xFFFF >> ((BACKLIGHT_LEVELS - get_backlight_level()) * ((BACKLIGHT_LEVELS + 1) / 2))) & (1 << backlight_tick)) {
  819. #if BACKLIGHT_ON_STATE == 0
  820. // PORTx &= ~n
  821. _SFR_IO8((backlight_pin >> 4) + 2) &= ~_BV(backlight_pin & 0xF);
  822. #else
  823. // PORTx |= n
  824. _SFR_IO8((backlight_pin >> 4) + 2) |= _BV(backlight_pin & 0xF);
  825. #endif
  826. } else {
  827. #if BACKLIGHT_ON_STATE == 0
  828. // PORTx |= n
  829. _SFR_IO8((backlight_pin >> 4) + 2) |= _BV(backlight_pin & 0xF);
  830. #else
  831. // PORTx &= ~n
  832. _SFR_IO8((backlight_pin >> 4) + 2) &= ~_BV(backlight_pin & 0xF);
  833. #endif
  834. }
  835. backlight_tick = backlight_tick + 1 % 16;
  836. }
  837. #ifdef BACKLIGHT_BREATHING
  838. #error "Backlight breathing only available with hardware PWM. Please disable."
  839. #endif
  840. #else // pwm through timer
  841. #define TIMER_TOP 0xFFFFU
  842. // See http://jared.geek.nz/2013/feb/linear-led-pwm
  843. static uint16_t cie_lightness(uint16_t v) {
  844. if (v <= 5243) // if below 8% of max
  845. return v / 9; // same as dividing by 900%
  846. else {
  847. uint32_t y = (((uint32_t) v + 10486) << 8) / (10486 + 0xFFFFUL); // add 16% of max and compare
  848. // to get a useful result with integer division, we shift left in the expression above
  849. // and revert what we've done again after squaring.
  850. y = y * y * y >> 8;
  851. if (y > 0xFFFFUL) // prevent overflow
  852. return 0xFFFFU;
  853. else
  854. return (uint16_t) y;
  855. }
  856. }
  857. // range for val is [0..TIMER_TOP]. PWM pin is high while the timer count is below val.
  858. static inline void set_pwm(uint16_t val) {
  859. OCR1x = val;
  860. }
  861. __attribute__ ((weak))
  862. void backlight_set(uint8_t level) {
  863. if (level > BACKLIGHT_LEVELS)
  864. level = BACKLIGHT_LEVELS;
  865. if (level == 0) {
  866. // Turn off PWM control on backlight pin
  867. TCCR1A &= ~(_BV(COM1x1));
  868. } else {
  869. // Turn on PWM control of backlight pin
  870. TCCR1A |= _BV(COM1x1);
  871. }
  872. // Set the brightness
  873. set_pwm(cie_lightness(TIMER_TOP * (uint32_t)level / BACKLIGHT_LEVELS));
  874. }
  875. void backlight_task(void) {}
  876. #ifdef BACKLIGHT_BREATHING
  877. #define BREATHING_NO_HALT 0
  878. #define BREATHING_HALT_OFF 1
  879. #define BREATHING_HALT_ON 2
  880. #define BREATHING_STEPS 128
  881. static uint8_t breathing_period = BREATHING_PERIOD;
  882. static uint8_t breathing_halt = BREATHING_NO_HALT;
  883. static uint16_t breathing_counter = 0;
  884. bool is_breathing(void) {
  885. return !!(TIMSK1 & _BV(TOIE1));
  886. }
  887. #define breathing_interrupt_enable() do {TIMSK1 |= _BV(TOIE1);} while (0)
  888. #define breathing_interrupt_disable() do {TIMSK1 &= ~_BV(TOIE1);} while (0)
  889. #define breathing_min() do {breathing_counter = 0;} while (0)
  890. #define breathing_max() do {breathing_counter = breathing_period * 244 / 2;} while (0)
  891. void breathing_enable(void)
  892. {
  893. breathing_counter = 0;
  894. breathing_halt = BREATHING_NO_HALT;
  895. breathing_interrupt_enable();
  896. }
  897. void breathing_pulse(void)
  898. {
  899. if (get_backlight_level() == 0)
  900. breathing_min();
  901. else
  902. breathing_max();
  903. breathing_halt = BREATHING_HALT_ON;
  904. breathing_interrupt_enable();
  905. }
  906. void breathing_disable(void)
  907. {
  908. breathing_interrupt_disable();
  909. // Restore backlight level
  910. backlight_set(get_backlight_level());
  911. }
  912. void breathing_self_disable(void)
  913. {
  914. if (get_backlight_level() == 0)
  915. breathing_halt = BREATHING_HALT_OFF;
  916. else
  917. breathing_halt = BREATHING_HALT_ON;
  918. }
  919. void breathing_toggle(void) {
  920. if (is_breathing())
  921. breathing_disable();
  922. else
  923. breathing_enable();
  924. }
  925. void breathing_period_set(uint8_t value)
  926. {
  927. if (!value)
  928. value = 1;
  929. breathing_period = value;
  930. }
  931. void breathing_period_default(void) {
  932. breathing_period_set(BREATHING_PERIOD);
  933. }
  934. void breathing_period_inc(void)
  935. {
  936. breathing_period_set(breathing_period+1);
  937. }
  938. void breathing_period_dec(void)
  939. {
  940. breathing_period_set(breathing_period-1);
  941. }
  942. /* To generate breathing curve in python:
  943. * from math import sin, pi; [int(sin(x/128.0*pi)**4*255) for x in range(128)]
  944. */
  945. static const uint8_t breathing_table[BREATHING_STEPS] PROGMEM = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 2, 3, 4, 5, 6, 8, 10, 12, 15, 17, 20, 24, 28, 32, 36, 41, 46, 51, 57, 63, 70, 76, 83, 91, 98, 106, 113, 121, 129, 138, 146, 154, 162, 170, 178, 185, 193, 200, 207, 213, 220, 225, 231, 235, 240, 244, 247, 250, 252, 253, 254, 255, 254, 253, 252, 250, 247, 244, 240, 235, 231, 225, 220, 213, 207, 200, 193, 185, 178, 170, 162, 154, 146, 138, 129, 121, 113, 106, 98, 91, 83, 76, 70, 63, 57, 51, 46, 41, 36, 32, 28, 24, 20, 17, 15, 12, 10, 8, 6, 5, 4, 3, 2, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  946. // Use this before the cie_lightness function.
  947. static inline uint16_t scale_backlight(uint16_t v) {
  948. return v / BACKLIGHT_LEVELS * get_backlight_level();
  949. }
  950. /* Assuming a 16MHz CPU clock and a timer that resets at 64k (ICR1), the following interrupt handler will run
  951. * about 244 times per second.
  952. */
  953. ISR(TIMER1_OVF_vect)
  954. {
  955. uint16_t interval = (uint16_t) breathing_period * 244 / BREATHING_STEPS;
  956. // resetting after one period to prevent ugly reset at overflow.
  957. breathing_counter = (breathing_counter + 1) % (breathing_period * 244);
  958. uint8_t index = breathing_counter / interval % BREATHING_STEPS;
  959. if (((breathing_halt == BREATHING_HALT_ON) && (index == BREATHING_STEPS / 2)) ||
  960. ((breathing_halt == BREATHING_HALT_OFF) && (index == BREATHING_STEPS - 1)))
  961. {
  962. breathing_interrupt_disable();
  963. }
  964. set_pwm(cie_lightness(scale_backlight((uint16_t) pgm_read_byte(&breathing_table[index]) * 0x0101U)));
  965. }
  966. #endif // BACKLIGHT_BREATHING
  967. __attribute__ ((weak))
  968. void backlight_init_ports(void)
  969. {
  970. // Setup backlight pin as output and output to on state.
  971. // DDRx |= n
  972. _SFR_IO8((backlight_pin >> 4) + 1) |= _BV(backlight_pin & 0xF);
  973. #if BACKLIGHT_ON_STATE == 0
  974. // PORTx &= ~n
  975. _SFR_IO8((backlight_pin >> 4) + 2) &= ~_BV(backlight_pin & 0xF);
  976. #else
  977. // PORTx |= n
  978. _SFR_IO8((backlight_pin >> 4) + 2) |= _BV(backlight_pin & 0xF);
  979. #endif
  980. // I could write a wall of text here to explain... but TL;DW
  981. // Go read the ATmega32u4 datasheet.
  982. // And this: http://blog.saikoled.com/post/43165849837/secret-konami-cheat-code-to-high-resolution-pwm-on
  983. // Pin PB7 = OCR1C (Timer 1, Channel C)
  984. // Compare Output Mode = Clear on compare match, Channel C = COM1C1=1 COM1C0=0
  985. // (i.e. start high, go low when counter matches.)
  986. // WGM Mode 14 (Fast PWM) = WGM13=1 WGM12=1 WGM11=1 WGM10=0
  987. // Clock Select = clk/1 (no prescaling) = CS12=0 CS11=0 CS10=1
  988. /*
  989. 14.8.3:
  990. "In fast PWM mode, the compare units allow generation of PWM waveforms on the OCnx pins. Setting the COMnx1:0 bits to two will produce a non-inverted PWM [..]."
  991. "In fast PWM mode the counter is incremented until the counter value matches either one of the fixed values 0x00FF, 0x01FF, or 0x03FF (WGMn3:0 = 5, 6, or 7), the value in ICRn (WGMn3:0 = 14), or the value in OCRnA (WGMn3:0 = 15)."
  992. */
  993. TCCR1A = _BV(COM1x1) | _BV(WGM11); // = 0b00001010;
  994. TCCR1B = _BV(WGM13) | _BV(WGM12) | _BV(CS10); // = 0b00011001;
  995. // Use full 16-bit resolution. Counter counts to ICR1 before reset to 0.
  996. ICR1 = TIMER_TOP;
  997. backlight_init();
  998. #ifdef BACKLIGHT_BREATHING
  999. breathing_enable();
  1000. #endif
  1001. }
  1002. #endif // NO_HARDWARE_PWM
  1003. #else // backlight
  1004. __attribute__ ((weak))
  1005. void backlight_init_ports(void) {}
  1006. __attribute__ ((weak))
  1007. void backlight_set(uint8_t level) {}
  1008. #endif // backlight
  1009. // Functions for spitting out values
  1010. //
  1011. void send_dword(uint32_t number) { // this might not actually work
  1012. uint16_t word = (number >> 16);
  1013. send_word(word);
  1014. send_word(number & 0xFFFFUL);
  1015. }
  1016. void send_word(uint16_t number) {
  1017. uint8_t byte = number >> 8;
  1018. send_byte(byte);
  1019. send_byte(number & 0xFF);
  1020. }
  1021. void send_byte(uint8_t number) {
  1022. uint8_t nibble = number >> 4;
  1023. send_nibble(nibble);
  1024. send_nibble(number & 0xF);
  1025. }
  1026. void send_nibble(uint8_t number) {
  1027. switch (number) {
  1028. case 0:
  1029. register_code(KC_0);
  1030. unregister_code(KC_0);
  1031. break;
  1032. case 1 ... 9:
  1033. register_code(KC_1 + (number - 1));
  1034. unregister_code(KC_1 + (number - 1));
  1035. break;
  1036. case 0xA ... 0xF:
  1037. register_code(KC_A + (number - 0xA));
  1038. unregister_code(KC_A + (number - 0xA));
  1039. break;
  1040. }
  1041. }
  1042. __attribute__((weak))
  1043. uint16_t hex_to_keycode(uint8_t hex)
  1044. {
  1045. hex = hex & 0xF;
  1046. if (hex == 0x0) {
  1047. return KC_0;
  1048. } else if (hex < 0xA) {
  1049. return KC_1 + (hex - 0x1);
  1050. } else {
  1051. return KC_A + (hex - 0xA);
  1052. }
  1053. }
  1054. void api_send_unicode(uint32_t unicode) {
  1055. #ifdef API_ENABLE
  1056. uint8_t chunk[4];
  1057. dword_to_bytes(unicode, chunk);
  1058. MT_SEND_DATA(DT_UNICODE, chunk, 5);
  1059. #endif
  1060. }
  1061. __attribute__ ((weak))
  1062. void led_set_user(uint8_t usb_led) {
  1063. }
  1064. __attribute__ ((weak))
  1065. void led_set_kb(uint8_t usb_led) {
  1066. led_set_user(usb_led);
  1067. }
  1068. __attribute__ ((weak))
  1069. void led_init_ports(void)
  1070. {
  1071. }
  1072. __attribute__ ((weak))
  1073. void led_set(uint8_t usb_led)
  1074. {
  1075. // Example LED Code
  1076. //
  1077. // // Using PE6 Caps Lock LED
  1078. // if (usb_led & (1<<USB_LED_CAPS_LOCK))
  1079. // {
  1080. // // Output high.
  1081. // DDRE |= (1<<6);
  1082. // PORTE |= (1<<6);
  1083. // }
  1084. // else
  1085. // {
  1086. // // Output low.
  1087. // DDRE &= ~(1<<6);
  1088. // PORTE &= ~(1<<6);
  1089. // }
  1090. led_set_kb(usb_led);
  1091. }
  1092. //------------------------------------------------------------------------------
  1093. // Override these functions in your keymap file to play different tunes on
  1094. // different events such as startup and bootloader jump
  1095. __attribute__ ((weak))
  1096. void startup_user() {}
  1097. __attribute__ ((weak))
  1098. void shutdown_user() {}
  1099. //------------------------------------------------------------------------------