quantum.c 23 KB

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