quantum.c 27 KB

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