quantum.c 27 KB

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