quantum.c 23 KB

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