quantum.c 27 KB

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