2
0

quantum.c 31 KB

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