quantum.c 26 KB

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