quantum.c 27 KB

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