quantum.c 31 KB

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