quantum.c 31 KB

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