quantum.c 27 KB

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