quantum.c 29 KB

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