quantum.c 32 KB

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