quantum.c 21 KB

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