quantum.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979
  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. # error "Backlight pin not supported - use B5, B6, or B7"
  552. #endif
  553. __attribute__ ((weak))
  554. void backlight_init_ports(void)
  555. {
  556. // Setup backlight pin as output and output low.
  557. // DDRx |= n
  558. _SFR_IO8((backlight_pin >> 4) + 1) |= _BV(backlight_pin & 0xF);
  559. // PORTx &= ~n
  560. _SFR_IO8((backlight_pin >> 4) + 2) &= ~_BV(backlight_pin & 0xF);
  561. // Use full 16-bit resolution.
  562. ICR1 = 0xFFFF;
  563. // I could write a wall of text here to explain... but TL;DW
  564. // Go read the ATmega32u4 datasheet.
  565. // And this: http://blog.saikoled.com/post/43165849837/secret-konami-cheat-code-to-high-resolution-pwm-on
  566. // Pin PB7 = OCR1C (Timer 1, Channel C)
  567. // Compare Output Mode = Clear on compare match, Channel C = COM1C1=1 COM1C0=0
  568. // (i.e. start high, go low when counter matches.)
  569. // WGM Mode 14 (Fast PWM) = WGM13=1 WGM12=1 WGM11=1 WGM10=0
  570. // Clock Select = clk/1 (no prescaling) = CS12=0 CS11=0 CS10=1
  571. TCCR1A = _BV(COM1x1) | _BV(WGM11); // = 0b00001010;
  572. TCCR1B = _BV(WGM13) | _BV(WGM12) | _BV(CS10); // = 0b00011001;
  573. backlight_init();
  574. #ifdef BACKLIGHT_BREATHING
  575. breathing_defaults();
  576. #endif
  577. }
  578. __attribute__ ((weak))
  579. void backlight_set(uint8_t level)
  580. {
  581. // Prevent backlight blink on lowest level
  582. // PORTx &= ~n
  583. _SFR_IO8((backlight_pin >> 4) + 2) &= ~_BV(backlight_pin & 0xF);
  584. if ( level == 0 ) {
  585. // Turn off PWM control on backlight pin, revert to output low.
  586. TCCR1A &= ~(_BV(COM1x1));
  587. OCR1x = 0x0;
  588. } else if ( level == BACKLIGHT_LEVELS ) {
  589. // Turn on PWM control of backlight pin
  590. TCCR1A |= _BV(COM1x1);
  591. // Set the brightness
  592. OCR1x = 0xFFFF;
  593. } else {
  594. // Turn on PWM control of backlight pin
  595. TCCR1A |= _BV(COM1x1);
  596. // Set the brightness
  597. OCR1x = 0xFFFF >> ((BACKLIGHT_LEVELS - level) * ((BACKLIGHT_LEVELS + 1) / 2));
  598. }
  599. #ifdef BACKLIGHT_BREATHING
  600. breathing_intensity_default();
  601. #endif
  602. }
  603. #ifdef BACKLIGHT_BREATHING
  604. #define BREATHING_NO_HALT 0
  605. #define BREATHING_HALT_OFF 1
  606. #define BREATHING_HALT_ON 2
  607. static uint8_t breath_intensity;
  608. static uint8_t breath_speed;
  609. static uint16_t breathing_index;
  610. static uint8_t breathing_halt;
  611. void breathing_enable(void)
  612. {
  613. if (get_backlight_level() == 0)
  614. {
  615. breathing_index = 0;
  616. }
  617. else
  618. {
  619. // Set breathing_index to be at the midpoint (brightest point)
  620. breathing_index = 0x20 << breath_speed;
  621. }
  622. breathing_halt = BREATHING_NO_HALT;
  623. // Enable breathing interrupt
  624. TIMSK1 |= _BV(OCIE1A);
  625. }
  626. void breathing_pulse(void)
  627. {
  628. if (get_backlight_level() == 0)
  629. {
  630. breathing_index = 0;
  631. }
  632. else
  633. {
  634. // Set breathing_index to be at the midpoint + 1 (brightest point)
  635. breathing_index = 0x21 << breath_speed;
  636. }
  637. breathing_halt = BREATHING_HALT_ON;
  638. // Enable breathing interrupt
  639. TIMSK1 |= _BV(OCIE1A);
  640. }
  641. void breathing_disable(void)
  642. {
  643. // Disable breathing interrupt
  644. TIMSK1 &= ~_BV(OCIE1A);
  645. backlight_set(get_backlight_level());
  646. }
  647. void breathing_self_disable(void)
  648. {
  649. if (get_backlight_level() == 0)
  650. {
  651. breathing_halt = BREATHING_HALT_OFF;
  652. }
  653. else
  654. {
  655. breathing_halt = BREATHING_HALT_ON;
  656. }
  657. //backlight_set(get_backlight_level());
  658. }
  659. void breathing_toggle(void)
  660. {
  661. if (!is_breathing())
  662. {
  663. if (get_backlight_level() == 0)
  664. {
  665. breathing_index = 0;
  666. }
  667. else
  668. {
  669. // Set breathing_index to be at the midpoint + 1 (brightest point)
  670. breathing_index = 0x21 << breath_speed;
  671. }
  672. breathing_halt = BREATHING_NO_HALT;
  673. }
  674. // Toggle breathing interrupt
  675. TIMSK1 ^= _BV(OCIE1A);
  676. // Restore backlight level
  677. if (!is_breathing())
  678. {
  679. backlight_set(get_backlight_level());
  680. }
  681. }
  682. bool is_breathing(void)
  683. {
  684. return (TIMSK1 && _BV(OCIE1A));
  685. }
  686. void breathing_intensity_default(void)
  687. {
  688. //breath_intensity = (uint8_t)((uint16_t)100 * (uint16_t)get_backlight_level() / (uint16_t)BACKLIGHT_LEVELS);
  689. breath_intensity = ((BACKLIGHT_LEVELS - get_backlight_level()) * ((BACKLIGHT_LEVELS + 1) / 2));
  690. }
  691. void breathing_intensity_set(uint8_t value)
  692. {
  693. breath_intensity = value;
  694. }
  695. void breathing_speed_default(void)
  696. {
  697. breath_speed = 4;
  698. }
  699. void breathing_speed_set(uint8_t value)
  700. {
  701. bool is_breathing_now = is_breathing();
  702. uint8_t old_breath_speed = breath_speed;
  703. if (is_breathing_now)
  704. {
  705. // Disable breathing interrupt
  706. TIMSK1 &= ~_BV(OCIE1A);
  707. }
  708. breath_speed = value;
  709. if (is_breathing_now)
  710. {
  711. // Adjust index to account for new speed
  712. breathing_index = (( (uint8_t)( (breathing_index) >> old_breath_speed ) ) & 0x3F) << breath_speed;
  713. // Enable breathing interrupt
  714. TIMSK1 |= _BV(OCIE1A);
  715. }
  716. }
  717. void breathing_speed_inc(uint8_t value)
  718. {
  719. if ((uint16_t)(breath_speed - value) > 10 )
  720. {
  721. breathing_speed_set(0);
  722. }
  723. else
  724. {
  725. breathing_speed_set(breath_speed - value);
  726. }
  727. }
  728. void breathing_speed_dec(uint8_t value)
  729. {
  730. if ((uint16_t)(breath_speed + value) > 10 )
  731. {
  732. breathing_speed_set(10);
  733. }
  734. else
  735. {
  736. breathing_speed_set(breath_speed + value);
  737. }
  738. }
  739. void breathing_defaults(void)
  740. {
  741. breathing_intensity_default();
  742. breathing_speed_default();
  743. breathing_halt = BREATHING_NO_HALT;
  744. }
  745. /* Breathing Sleep LED brighness(PWM On period) table
  746. * (64[steps] * 4[duration]) / 64[PWM periods/s] = 4 second breath cycle
  747. *
  748. * http://www.wolframalpha.com/input/?i=%28sin%28+x%2F64*pi%29**8+*+255%2C+x%3D0+to+63
  749. * (0..63).each {|x| p ((sin(x/64.0*PI)**8)*255).to_i }
  750. */
  751. static const uint8_t breathing_table[64] PROGMEM = {
  752. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 4, 6, 10,
  753. 15, 23, 32, 44, 58, 74, 93, 113, 135, 157, 179, 199, 218, 233, 245, 252,
  754. 255, 252, 245, 233, 218, 199, 179, 157, 135, 113, 93, 74, 58, 44, 32, 23,
  755. 15, 10, 6, 4, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  756. };
  757. ISR(TIMER1_COMPA_vect)
  758. {
  759. // OCR1x = (pgm_read_byte(&breathing_table[ ( (uint8_t)( (breathing_index++) >> breath_speed ) ) & 0x3F ] )) * breath_intensity;
  760. uint8_t local_index = ( (uint8_t)( (breathing_index++) >> breath_speed ) ) & 0x3F;
  761. if (((breathing_halt == BREATHING_HALT_ON) && (local_index == 0x20)) || ((breathing_halt == BREATHING_HALT_OFF) && (local_index == 0x3F)))
  762. {
  763. // Disable breathing interrupt
  764. TIMSK1 &= ~_BV(OCIE1A);
  765. }
  766. OCR1x = (uint16_t)(((uint16_t)pgm_read_byte(&breathing_table[local_index]) * 257)) >> breath_intensity;
  767. }
  768. #endif // breathing
  769. #else // backlight
  770. __attribute__ ((weak))
  771. void backlight_init_ports(void)
  772. {
  773. }
  774. __attribute__ ((weak))
  775. void backlight_set(uint8_t level)
  776. {
  777. }
  778. #endif // backlight
  779. // Functions for spitting out values
  780. //
  781. void send_dword(uint32_t number) { // this might not actually work
  782. uint16_t word = (number >> 16);
  783. send_word(word);
  784. send_word(number & 0xFFFFUL);
  785. }
  786. void send_word(uint16_t number) {
  787. uint8_t byte = number >> 8;
  788. send_byte(byte);
  789. send_byte(number & 0xFF);
  790. }
  791. void send_byte(uint8_t number) {
  792. uint8_t nibble = number >> 4;
  793. send_nibble(nibble);
  794. send_nibble(number & 0xF);
  795. }
  796. void send_nibble(uint8_t number) {
  797. switch (number) {
  798. case 0:
  799. register_code(KC_0);
  800. unregister_code(KC_0);
  801. break;
  802. case 1 ... 9:
  803. register_code(KC_1 + (number - 1));
  804. unregister_code(KC_1 + (number - 1));
  805. break;
  806. case 0xA ... 0xF:
  807. register_code(KC_A + (number - 0xA));
  808. unregister_code(KC_A + (number - 0xA));
  809. break;
  810. }
  811. }
  812. void api_send_unicode(uint32_t unicode) {
  813. #ifdef API_ENABLE
  814. uint8_t chunk[4];
  815. dword_to_bytes(unicode, chunk);
  816. MT_SEND_DATA(DT_UNICODE, chunk, 5);
  817. #endif
  818. }
  819. __attribute__ ((weak))
  820. void led_set_user(uint8_t usb_led) {
  821. }
  822. __attribute__ ((weak))
  823. void led_set_kb(uint8_t usb_led) {
  824. led_set_user(usb_led);
  825. }
  826. __attribute__ ((weak))
  827. void led_init_ports(void)
  828. {
  829. }
  830. __attribute__ ((weak))
  831. void led_set(uint8_t usb_led)
  832. {
  833. // Example LED Code
  834. //
  835. // // Using PE6 Caps Lock LED
  836. // if (usb_led & (1<<USB_LED_CAPS_LOCK))
  837. // {
  838. // // Output high.
  839. // DDRE |= (1<<6);
  840. // PORTE |= (1<<6);
  841. // }
  842. // else
  843. // {
  844. // // Output low.
  845. // DDRE &= ~(1<<6);
  846. // PORTE &= ~(1<<6);
  847. // }
  848. led_set_kb(usb_led);
  849. }
  850. //------------------------------------------------------------------------------
  851. // Override these functions in your keymap file to play different tunes on
  852. // different events such as startup and bootloader jump
  853. __attribute__ ((weak))
  854. void startup_user() {}
  855. __attribute__ ((weak))
  856. void shutdown_user() {}
  857. //------------------------------------------------------------------------------