quantum.c 24 KB

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