quantum.c 21 KB

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