quantum.c 19 KB

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