quantum.c 26 KB

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