quantum.c 26 KB

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