quantum.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113
  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. #ifdef GRAVE_ESC_CTRL_OVERRIDE
  450. if (get_mods() & (MOD_BIT(KC_LCTL) | MOD_BIT(KC_RCTL)))
  451. shifted = 0;
  452. #endif
  453. method(shifted ? KC_GRAVE : KC_ESCAPE);
  454. send_keyboard_report();
  455. }
  456. default: {
  457. shift_interrupted[0] = true;
  458. shift_interrupted[1] = true;
  459. break;
  460. }
  461. }
  462. return process_action_kb(record);
  463. }
  464. __attribute__ ((weak))
  465. const bool ascii_to_shift_lut[0x80] PROGMEM = {
  466. 0, 0, 0, 0, 0, 0, 0, 0,
  467. 0, 0, 0, 0, 0, 0, 0, 0,
  468. 0, 0, 0, 0, 0, 0, 0, 0,
  469. 0, 0, 0, 0, 0, 0, 0, 0,
  470. 0, 1, 1, 1, 1, 1, 1, 0,
  471. 1, 1, 1, 1, 0, 0, 0, 0,
  472. 0, 0, 0, 0, 0, 0, 0, 0,
  473. 0, 0, 1, 0, 1, 0, 1, 1,
  474. 1, 1, 1, 1, 1, 1, 1, 1,
  475. 1, 1, 1, 1, 1, 1, 1, 1,
  476. 1, 1, 1, 1, 1, 1, 1, 1,
  477. 1, 1, 1, 0, 0, 0, 1, 1,
  478. 0, 0, 0, 0, 0, 0, 0, 0,
  479. 0, 0, 0, 0, 0, 0, 0, 0,
  480. 0, 0, 0, 0, 0, 0, 0, 0,
  481. 0, 0, 0, 1, 1, 1, 1, 0
  482. };
  483. __attribute__ ((weak))
  484. const uint8_t ascii_to_keycode_lut[0x80] PROGMEM = {
  485. 0, 0, 0, 0, 0, 0, 0, 0,
  486. KC_BSPC, KC_TAB, KC_ENT, 0, 0, 0, 0, 0,
  487. 0, 0, 0, 0, 0, 0, 0, 0,
  488. 0, 0, 0, KC_ESC, 0, 0, 0, 0,
  489. KC_SPC, KC_1, KC_QUOT, KC_3, KC_4, KC_5, KC_7, KC_QUOT,
  490. KC_9, KC_0, KC_8, KC_EQL, KC_COMM, KC_MINS, KC_DOT, KC_SLSH,
  491. KC_0, KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7,
  492. KC_8, KC_9, KC_SCLN, KC_SCLN, KC_COMM, KC_EQL, KC_DOT, KC_SLSH,
  493. KC_2, 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_6, KC_MINS,
  497. KC_GRV, KC_A, KC_B, KC_C, KC_D, KC_E, KC_F, KC_G,
  498. KC_H, KC_I, KC_J, KC_K, KC_L, KC_M, KC_N, KC_O,
  499. KC_P, KC_Q, KC_R, KC_S, KC_T, KC_U, KC_V, KC_W,
  500. KC_X, KC_Y, KC_Z, KC_LBRC, KC_BSLS, KC_RBRC, KC_GRV, KC_DEL
  501. };
  502. void send_string(const char *str) {
  503. send_string_with_delay(str, 0);
  504. }
  505. void send_string_with_delay(const char *str, uint8_t interval) {
  506. while (1) {
  507. uint8_t keycode;
  508. uint8_t ascii_code = pgm_read_byte(str);
  509. if (!ascii_code) break;
  510. keycode = pgm_read_byte(&ascii_to_keycode_lut[ascii_code]);
  511. if (pgm_read_byte(&ascii_to_shift_lut[ascii_code])) {
  512. register_code(KC_LSFT);
  513. register_code(keycode);
  514. unregister_code(keycode);
  515. unregister_code(KC_LSFT);
  516. }
  517. else {
  518. register_code(keycode);
  519. unregister_code(keycode);
  520. }
  521. ++str;
  522. // interval
  523. { uint8_t ms = interval; while (ms--) wait_ms(1); }
  524. }
  525. }
  526. void set_single_persistent_default_layer(uint8_t default_layer) {
  527. #if defined(AUDIO_ENABLE) && defined(DEFAULT_LAYER_SONGS)
  528. PLAY_SONG(default_layer_songs[default_layer]);
  529. #endif
  530. eeconfig_update_default_layer(1U<<default_layer);
  531. default_layer_set(1U<<default_layer);
  532. }
  533. void update_tri_layer(uint8_t layer1, uint8_t layer2, uint8_t layer3) {
  534. if (IS_LAYER_ON(layer1) && IS_LAYER_ON(layer2)) {
  535. layer_on(layer3);
  536. } else {
  537. layer_off(layer3);
  538. }
  539. }
  540. void tap_random_base64(void) {
  541. #if defined(__AVR_ATmega32U4__)
  542. uint8_t key = (TCNT0 + TCNT1 + TCNT3 + TCNT4) % 64;
  543. #else
  544. uint8_t key = rand() % 64;
  545. #endif
  546. switch (key) {
  547. case 0 ... 25:
  548. register_code(KC_LSFT);
  549. register_code(key + KC_A);
  550. unregister_code(key + KC_A);
  551. unregister_code(KC_LSFT);
  552. break;
  553. case 26 ... 51:
  554. register_code(key - 26 + KC_A);
  555. unregister_code(key - 26 + KC_A);
  556. break;
  557. case 52:
  558. register_code(KC_0);
  559. unregister_code(KC_0);
  560. break;
  561. case 53 ... 61:
  562. register_code(key - 53 + KC_1);
  563. unregister_code(key - 53 + KC_1);
  564. break;
  565. case 62:
  566. register_code(KC_LSFT);
  567. register_code(KC_EQL);
  568. unregister_code(KC_EQL);
  569. unregister_code(KC_LSFT);
  570. break;
  571. case 63:
  572. register_code(KC_SLSH);
  573. unregister_code(KC_SLSH);
  574. break;
  575. }
  576. }
  577. void matrix_init_quantum() {
  578. #ifdef BACKLIGHT_ENABLE
  579. backlight_init_ports();
  580. #endif
  581. #ifdef AUDIO_ENABLE
  582. audio_init();
  583. #endif
  584. matrix_init_kb();
  585. }
  586. void matrix_scan_quantum() {
  587. #ifdef AUDIO_ENABLE
  588. matrix_scan_music();
  589. #endif
  590. #ifdef TAP_DANCE_ENABLE
  591. matrix_scan_tap_dance();
  592. #endif
  593. #ifdef COMBO_ENABLE
  594. matrix_scan_combo();
  595. #endif
  596. #if defined(BACKLIGHT_ENABLE) && defined(BACKLIGHT_PIN)
  597. backlight_task();
  598. #endif
  599. matrix_scan_kb();
  600. }
  601. #if defined(BACKLIGHT_ENABLE) && defined(BACKLIGHT_PIN)
  602. static const uint8_t backlight_pin = BACKLIGHT_PIN;
  603. #if BACKLIGHT_PIN == B7
  604. # define COM1x1 COM1C1
  605. # define OCR1x OCR1C
  606. #elif BACKLIGHT_PIN == B6
  607. # define COM1x1 COM1B1
  608. # define OCR1x OCR1B
  609. #elif BACKLIGHT_PIN == B5
  610. # define COM1x1 COM1A1
  611. # define OCR1x OCR1A
  612. #else
  613. # define NO_BACKLIGHT_CLOCK
  614. #endif
  615. #ifndef BACKLIGHT_ON_STATE
  616. #define BACKLIGHT_ON_STATE 0
  617. #endif
  618. __attribute__ ((weak))
  619. void backlight_init_ports(void)
  620. {
  621. // Setup backlight pin as output and output to on state.
  622. // DDRx |= n
  623. _SFR_IO8((backlight_pin >> 4) + 1) |= _BV(backlight_pin & 0xF);
  624. #if BACKLIGHT_ON_STATE == 0
  625. // PORTx &= ~n
  626. _SFR_IO8((backlight_pin >> 4) + 2) &= ~_BV(backlight_pin & 0xF);
  627. #else
  628. // PORTx |= n
  629. _SFR_IO8((backlight_pin >> 4) + 2) |= _BV(backlight_pin & 0xF);
  630. #endif
  631. #ifndef NO_BACKLIGHT_CLOCK
  632. // Use full 16-bit resolution.
  633. ICR1 = 0xFFFF;
  634. // I could write a wall of text here to explain... but TL;DW
  635. // Go read the ATmega32u4 datasheet.
  636. // And this: http://blog.saikoled.com/post/43165849837/secret-konami-cheat-code-to-high-resolution-pwm-on
  637. // Pin PB7 = OCR1C (Timer 1, Channel C)
  638. // Compare Output Mode = Clear on compare match, Channel C = COM1C1=1 COM1C0=0
  639. // (i.e. start high, go low when counter matches.)
  640. // WGM Mode 14 (Fast PWM) = WGM13=1 WGM12=1 WGM11=1 WGM10=0
  641. // Clock Select = clk/1 (no prescaling) = CS12=0 CS11=0 CS10=1
  642. TCCR1A = _BV(COM1x1) | _BV(WGM11); // = 0b00001010;
  643. TCCR1B = _BV(WGM13) | _BV(WGM12) | _BV(CS10); // = 0b00011001;
  644. #endif
  645. backlight_init();
  646. #ifdef BACKLIGHT_BREATHING
  647. breathing_defaults();
  648. #endif
  649. }
  650. __attribute__ ((weak))
  651. void backlight_set(uint8_t level)
  652. {
  653. // Prevent backlight blink on lowest level
  654. // #if BACKLIGHT_ON_STATE == 0
  655. // // PORTx &= ~n
  656. // _SFR_IO8((backlight_pin >> 4) + 2) &= ~_BV(backlight_pin & 0xF);
  657. // #else
  658. // // PORTx |= n
  659. // _SFR_IO8((backlight_pin >> 4) + 2) |= _BV(backlight_pin & 0xF);
  660. // #endif
  661. if ( level == 0 ) {
  662. #ifndef NO_BACKLIGHT_CLOCK
  663. // Turn off PWM control on backlight pin, revert to output low.
  664. TCCR1A &= ~(_BV(COM1x1));
  665. OCR1x = 0x0;
  666. #else
  667. // #if BACKLIGHT_ON_STATE == 0
  668. // // PORTx |= n
  669. // _SFR_IO8((backlight_pin >> 4) + 2) |= _BV(backlight_pin & 0xF);
  670. // #else
  671. // // PORTx &= ~n
  672. // _SFR_IO8((backlight_pin >> 4) + 2) &= ~_BV(backlight_pin & 0xF);
  673. // #endif
  674. #endif
  675. }
  676. #ifndef NO_BACKLIGHT_CLOCK
  677. else if ( level == BACKLIGHT_LEVELS ) {
  678. // Turn on PWM control of backlight pin
  679. TCCR1A |= _BV(COM1x1);
  680. // Set the brightness
  681. OCR1x = 0xFFFF;
  682. }
  683. else {
  684. // Turn on PWM control of backlight pin
  685. TCCR1A |= _BV(COM1x1);
  686. // Set the brightness
  687. OCR1x = 0xFFFF >> ((BACKLIGHT_LEVELS - level) * ((BACKLIGHT_LEVELS + 1) / 2));
  688. }
  689. #endif
  690. #ifdef BACKLIGHT_BREATHING
  691. breathing_intensity_default();
  692. #endif
  693. }
  694. uint8_t backlight_tick = 0;
  695. void backlight_task(void) {
  696. #ifdef NO_BACKLIGHT_CLOCK
  697. if ((0xFFFF >> ((BACKLIGHT_LEVELS - backlight_config.level) * ((BACKLIGHT_LEVELS + 1) / 2))) & (1 << backlight_tick)) {
  698. #if BACKLIGHT_ON_STATE == 0
  699. // PORTx &= ~n
  700. _SFR_IO8((backlight_pin >> 4) + 2) &= ~_BV(backlight_pin & 0xF);
  701. #else
  702. // PORTx |= n
  703. _SFR_IO8((backlight_pin >> 4) + 2) |= _BV(backlight_pin & 0xF);
  704. #endif
  705. } else {
  706. #if BACKLIGHT_ON_STATE == 0
  707. // PORTx |= n
  708. _SFR_IO8((backlight_pin >> 4) + 2) |= _BV(backlight_pin & 0xF);
  709. #else
  710. // PORTx &= ~n
  711. _SFR_IO8((backlight_pin >> 4) + 2) &= ~_BV(backlight_pin & 0xF);
  712. #endif
  713. }
  714. backlight_tick = (backlight_tick + 1) % 16;
  715. #endif
  716. }
  717. #ifdef BACKLIGHT_BREATHING
  718. #define BREATHING_NO_HALT 0
  719. #define BREATHING_HALT_OFF 1
  720. #define BREATHING_HALT_ON 2
  721. static uint8_t breath_intensity;
  722. static uint8_t breath_speed;
  723. static uint16_t breathing_index;
  724. static uint8_t breathing_halt;
  725. void breathing_enable(void)
  726. {
  727. if (get_backlight_level() == 0)
  728. {
  729. breathing_index = 0;
  730. }
  731. else
  732. {
  733. // Set breathing_index to be at the midpoint (brightest point)
  734. breathing_index = 0x20 << breath_speed;
  735. }
  736. breathing_halt = BREATHING_NO_HALT;
  737. // Enable breathing interrupt
  738. TIMSK1 |= _BV(OCIE1A);
  739. }
  740. void breathing_pulse(void)
  741. {
  742. if (get_backlight_level() == 0)
  743. {
  744. breathing_index = 0;
  745. }
  746. else
  747. {
  748. // Set breathing_index to be at the midpoint + 1 (brightest point)
  749. breathing_index = 0x21 << breath_speed;
  750. }
  751. breathing_halt = BREATHING_HALT_ON;
  752. // Enable breathing interrupt
  753. TIMSK1 |= _BV(OCIE1A);
  754. }
  755. void breathing_disable(void)
  756. {
  757. // Disable breathing interrupt
  758. TIMSK1 &= ~_BV(OCIE1A);
  759. backlight_set(get_backlight_level());
  760. }
  761. void breathing_self_disable(void)
  762. {
  763. if (get_backlight_level() == 0)
  764. {
  765. breathing_halt = BREATHING_HALT_OFF;
  766. }
  767. else
  768. {
  769. breathing_halt = BREATHING_HALT_ON;
  770. }
  771. //backlight_set(get_backlight_level());
  772. }
  773. void breathing_toggle(void)
  774. {
  775. if (!is_breathing())
  776. {
  777. if (get_backlight_level() == 0)
  778. {
  779. breathing_index = 0;
  780. }
  781. else
  782. {
  783. // Set breathing_index to be at the midpoint + 1 (brightest point)
  784. breathing_index = 0x21 << breath_speed;
  785. }
  786. breathing_halt = BREATHING_NO_HALT;
  787. }
  788. // Toggle breathing interrupt
  789. TIMSK1 ^= _BV(OCIE1A);
  790. // Restore backlight level
  791. if (!is_breathing())
  792. {
  793. backlight_set(get_backlight_level());
  794. }
  795. }
  796. bool is_breathing(void)
  797. {
  798. return (TIMSK1 && _BV(OCIE1A));
  799. }
  800. void breathing_intensity_default(void)
  801. {
  802. //breath_intensity = (uint8_t)((uint16_t)100 * (uint16_t)get_backlight_level() / (uint16_t)BACKLIGHT_LEVELS);
  803. breath_intensity = ((BACKLIGHT_LEVELS - get_backlight_level()) * ((BACKLIGHT_LEVELS + 1) / 2));
  804. }
  805. void breathing_intensity_set(uint8_t value)
  806. {
  807. breath_intensity = value;
  808. }
  809. void breathing_speed_default(void)
  810. {
  811. breath_speed = 4;
  812. }
  813. void breathing_speed_set(uint8_t value)
  814. {
  815. bool is_breathing_now = is_breathing();
  816. uint8_t old_breath_speed = breath_speed;
  817. if (is_breathing_now)
  818. {
  819. // Disable breathing interrupt
  820. TIMSK1 &= ~_BV(OCIE1A);
  821. }
  822. breath_speed = value;
  823. if (is_breathing_now)
  824. {
  825. // Adjust index to account for new speed
  826. breathing_index = (( (uint8_t)( (breathing_index) >> old_breath_speed ) ) & 0x3F) << breath_speed;
  827. // Enable breathing interrupt
  828. TIMSK1 |= _BV(OCIE1A);
  829. }
  830. }
  831. void breathing_speed_inc(uint8_t value)
  832. {
  833. if ((uint16_t)(breath_speed - value) > 10 )
  834. {
  835. breathing_speed_set(0);
  836. }
  837. else
  838. {
  839. breathing_speed_set(breath_speed - value);
  840. }
  841. }
  842. void breathing_speed_dec(uint8_t value)
  843. {
  844. if ((uint16_t)(breath_speed + value) > 10 )
  845. {
  846. breathing_speed_set(10);
  847. }
  848. else
  849. {
  850. breathing_speed_set(breath_speed + value);
  851. }
  852. }
  853. void breathing_defaults(void)
  854. {
  855. breathing_intensity_default();
  856. breathing_speed_default();
  857. breathing_halt = BREATHING_NO_HALT;
  858. }
  859. /* Breathing Sleep LED brighness(PWM On period) table
  860. * (64[steps] * 4[duration]) / 64[PWM periods/s] = 4 second breath cycle
  861. *
  862. * http://www.wolframalpha.com/input/?i=%28sin%28+x%2F64*pi%29**8+*+255%2C+x%3D0+to+63
  863. * (0..63).each {|x| p ((sin(x/64.0*PI)**8)*255).to_i }
  864. */
  865. static const uint8_t breathing_table[64] PROGMEM = {
  866. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 4, 6, 10,
  867. 15, 23, 32, 44, 58, 74, 93, 113, 135, 157, 179, 199, 218, 233, 245, 252,
  868. 255, 252, 245, 233, 218, 199, 179, 157, 135, 113, 93, 74, 58, 44, 32, 23,
  869. 15, 10, 6, 4, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  870. };
  871. ISR(TIMER1_COMPA_vect)
  872. {
  873. // OCR1x = (pgm_read_byte(&breathing_table[ ( (uint8_t)( (breathing_index++) >> breath_speed ) ) & 0x3F ] )) * breath_intensity;
  874. uint8_t local_index = ( (uint8_t)( (breathing_index++) >> breath_speed ) ) & 0x3F;
  875. if (((breathing_halt == BREATHING_HALT_ON) && (local_index == 0x20)) || ((breathing_halt == BREATHING_HALT_OFF) && (local_index == 0x3F)))
  876. {
  877. // Disable breathing interrupt
  878. TIMSK1 &= ~_BV(OCIE1A);
  879. }
  880. OCR1x = (uint16_t)(((uint16_t)pgm_read_byte(&breathing_table[local_index]) * 257)) >> breath_intensity;
  881. }
  882. #endif // breathing
  883. #else // backlight
  884. __attribute__ ((weak))
  885. void backlight_init_ports(void)
  886. {
  887. }
  888. __attribute__ ((weak))
  889. void backlight_set(uint8_t level)
  890. {
  891. }
  892. #endif // backlight
  893. // Functions for spitting out values
  894. //
  895. void send_dword(uint32_t number) { // this might not actually work
  896. uint16_t word = (number >> 16);
  897. send_word(word);
  898. send_word(number & 0xFFFFUL);
  899. }
  900. void send_word(uint16_t number) {
  901. uint8_t byte = number >> 8;
  902. send_byte(byte);
  903. send_byte(number & 0xFF);
  904. }
  905. void send_byte(uint8_t number) {
  906. uint8_t nibble = number >> 4;
  907. send_nibble(nibble);
  908. send_nibble(number & 0xF);
  909. }
  910. void send_nibble(uint8_t number) {
  911. switch (number) {
  912. case 0:
  913. register_code(KC_0);
  914. unregister_code(KC_0);
  915. break;
  916. case 1 ... 9:
  917. register_code(KC_1 + (number - 1));
  918. unregister_code(KC_1 + (number - 1));
  919. break;
  920. case 0xA ... 0xF:
  921. register_code(KC_A + (number - 0xA));
  922. unregister_code(KC_A + (number - 0xA));
  923. break;
  924. }
  925. }
  926. __attribute__((weak))
  927. uint16_t hex_to_keycode(uint8_t hex)
  928. {
  929. if (hex == 0x0) {
  930. return KC_0;
  931. } else if (hex < 0xA) {
  932. return KC_1 + (hex - 0x1);
  933. } else {
  934. return KC_A + (hex - 0xA);
  935. }
  936. }
  937. void api_send_unicode(uint32_t unicode) {
  938. #ifdef API_ENABLE
  939. uint8_t chunk[4];
  940. dword_to_bytes(unicode, chunk);
  941. MT_SEND_DATA(DT_UNICODE, chunk, 5);
  942. #endif
  943. }
  944. __attribute__ ((weak))
  945. void led_set_user(uint8_t usb_led) {
  946. }
  947. __attribute__ ((weak))
  948. void led_set_kb(uint8_t usb_led) {
  949. led_set_user(usb_led);
  950. }
  951. __attribute__ ((weak))
  952. void led_init_ports(void)
  953. {
  954. }
  955. __attribute__ ((weak))
  956. void led_set(uint8_t usb_led)
  957. {
  958. // Example LED Code
  959. //
  960. // // Using PE6 Caps Lock LED
  961. // if (usb_led & (1<<USB_LED_CAPS_LOCK))
  962. // {
  963. // // Output high.
  964. // DDRE |= (1<<6);
  965. // PORTE |= (1<<6);
  966. // }
  967. // else
  968. // {
  969. // // Output low.
  970. // DDRE &= ~(1<<6);
  971. // PORTE &= ~(1<<6);
  972. // }
  973. led_set_kb(usb_led);
  974. }
  975. //------------------------------------------------------------------------------
  976. // Override these functions in your keymap file to play different tunes on
  977. // different events such as startup and bootloader jump
  978. __attribute__ ((weak))
  979. void startup_user() {}
  980. __attribute__ ((weak))
  981. void shutdown_user() {}
  982. //------------------------------------------------------------------------------