2
0

quantum.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  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. _delay_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. if (!shift_interrupted[0]) {
  152. register_code(LSPO_KEY);
  153. unregister_code(LSPO_KEY);
  154. }
  155. unregister_mods(MOD_BIT(KC_LSFT));
  156. }
  157. return false;
  158. break;
  159. }
  160. case KC_RSPC: {
  161. if (record->event.pressed) {
  162. shift_interrupted[1] = false;
  163. register_mods(MOD_BIT(KC_RSFT));
  164. }
  165. else {
  166. if (!shift_interrupted[1]) {
  167. register_code(RSPC_KEY);
  168. unregister_code(RSPC_KEY);
  169. }
  170. unregister_mods(MOD_BIT(KC_RSFT));
  171. }
  172. return false;
  173. break;
  174. }
  175. default: {
  176. shift_interrupted[0] = true;
  177. shift_interrupted[1] = true;
  178. break;
  179. }
  180. }
  181. return process_action_kb(record);
  182. }
  183. const bool ascii_to_qwerty_shift_lut[0x80] PROGMEM = {
  184. 0, 0, 0, 0, 0, 0, 0, 0,
  185. 0, 0, 0, 0, 0, 0, 0, 0,
  186. 0, 0, 0, 0, 0, 0, 0, 0,
  187. 0, 0, 0, 0, 0, 0, 0, 0,
  188. 0, 1, 1, 1, 1, 1, 1, 0,
  189. 1, 1, 1, 1, 0, 0, 0, 0,
  190. 0, 0, 0, 0, 0, 0, 0, 0,
  191. 0, 0, 1, 0, 1, 0, 1, 1,
  192. 1, 1, 1, 1, 1, 1, 1, 1,
  193. 1, 1, 1, 1, 1, 1, 1, 1,
  194. 1, 1, 1, 1, 1, 1, 1, 1,
  195. 1, 1, 1, 0, 0, 0, 1, 1,
  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, 1, 1, 1, 1, 0
  200. };
  201. const uint8_t ascii_to_qwerty_keycode_lut[0x80] PROGMEM = {
  202. 0, 0, 0, 0, 0, 0, 0, 0,
  203. KC_BSPC, KC_TAB, KC_ENT, 0, 0, 0, 0, 0,
  204. 0, 0, 0, 0, 0, 0, 0, 0,
  205. 0, 0, 0, KC_ESC, 0, 0, 0, 0,
  206. KC_SPC, KC_1, KC_QUOT, KC_3, KC_4, KC_5, KC_7, KC_QUOT,
  207. KC_9, KC_0, KC_8, KC_EQL, KC_COMM, KC_MINS, KC_DOT, KC_SLSH,
  208. KC_0, KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7,
  209. KC_8, KC_9, KC_SCLN, KC_SCLN, KC_COMM, KC_EQL, KC_DOT, KC_SLSH,
  210. KC_2, KC_A, KC_B, KC_C, KC_D, KC_E, KC_F, KC_G,
  211. KC_H, KC_I, KC_J, KC_K, KC_L, KC_M, KC_N, KC_O,
  212. KC_P, KC_Q, KC_R, KC_S, KC_T, KC_U, KC_V, KC_W,
  213. KC_X, KC_Y, KC_Z, KC_LBRC, KC_BSLS, KC_RBRC, KC_6, KC_MINS,
  214. KC_GRV, KC_A, KC_B, KC_C, KC_D, KC_E, KC_F, KC_G,
  215. KC_H, KC_I, KC_J, KC_K, KC_L, KC_M, KC_N, KC_O,
  216. KC_P, KC_Q, KC_R, KC_S, KC_T, KC_U, KC_V, KC_W,
  217. KC_X, KC_Y, KC_Z, KC_LBRC, KC_BSLS, KC_RBRC, KC_GRV, KC_DEL
  218. };
  219. /* for users whose OSes are set to Colemak */
  220. #if 0
  221. #include "keymap_colemak.h"
  222. const bool ascii_to_colemak_shift_lut[0x80] PROGMEM = {
  223. 0, 0, 0, 0, 0, 0, 0, 0,
  224. 0, 0, 0, 0, 0, 0, 0, 0,
  225. 0, 0, 0, 0, 0, 0, 0, 0,
  226. 0, 0, 0, 0, 0, 0, 0, 0,
  227. 0, 1, 1, 1, 1, 1, 1, 0,
  228. 1, 1, 1, 1, 0, 0, 0, 0,
  229. 0, 0, 0, 0, 0, 0, 0, 0,
  230. 0, 0, 1, 0, 1, 0, 1, 1,
  231. 1, 1, 1, 1, 1, 1, 1, 1,
  232. 1, 1, 1, 1, 1, 1, 1, 1,
  233. 1, 1, 1, 1, 1, 1, 1, 1,
  234. 1, 1, 1, 0, 0, 0, 1, 1,
  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, 1, 1, 1, 1, 0
  239. };
  240. const uint8_t ascii_to_colemak_keycode_lut[0x80] PROGMEM = {
  241. 0, 0, 0, 0, 0, 0, 0, 0,
  242. KC_BSPC, KC_TAB, KC_ENT, 0, 0, 0, 0, 0,
  243. 0, 0, 0, 0, 0, 0, 0, 0,
  244. 0, 0, 0, KC_ESC, 0, 0, 0, 0,
  245. KC_SPC, KC_1, KC_QUOT, KC_3, KC_4, KC_5, KC_7, KC_QUOT,
  246. KC_9, KC_0, KC_8, KC_EQL, KC_COMM, KC_MINS, KC_DOT, KC_SLSH,
  247. KC_0, KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7,
  248. KC_8, KC_9, CM_SCLN, CM_SCLN, KC_COMM, KC_EQL, KC_DOT, KC_SLSH,
  249. KC_2, CM_A, CM_B, CM_C, CM_D, CM_E, CM_F, CM_G,
  250. CM_H, CM_I, CM_J, CM_K, CM_L, CM_M, CM_N, CM_O,
  251. CM_P, CM_Q, CM_R, CM_S, CM_T, CM_U, CM_V, CM_W,
  252. CM_X, CM_Y, CM_Z, KC_LBRC, KC_BSLS, KC_RBRC, KC_6, KC_MINS,
  253. KC_GRV, CM_A, CM_B, CM_C, CM_D, CM_E, CM_F, CM_G,
  254. CM_H, CM_I, CM_J, CM_K, CM_L, CM_M, CM_N, CM_O,
  255. CM_P, CM_Q, CM_R, CM_S, CM_T, CM_U, CM_V, CM_W,
  256. CM_X, CM_Y, CM_Z, KC_LBRC, KC_BSLS, KC_RBRC, KC_GRV, KC_DEL
  257. };
  258. #endif
  259. void send_string(const char *str) {
  260. while (1) {
  261. uint8_t keycode;
  262. uint8_t ascii_code = pgm_read_byte(str);
  263. if (!ascii_code) break;
  264. keycode = pgm_read_byte(&ascii_to_qwerty_keycode_lut[ascii_code]);
  265. if (pgm_read_byte(&ascii_to_qwerty_shift_lut[ascii_code])) {
  266. register_code(KC_LSFT);
  267. register_code(keycode);
  268. unregister_code(keycode);
  269. unregister_code(KC_LSFT);
  270. }
  271. else {
  272. register_code(keycode);
  273. unregister_code(keycode);
  274. }
  275. ++str;
  276. }
  277. }
  278. void update_tri_layer(uint8_t layer1, uint8_t layer2, uint8_t layer3) {
  279. if (IS_LAYER_ON(layer1) && IS_LAYER_ON(layer2)) {
  280. layer_on(layer3);
  281. } else {
  282. layer_off(layer3);
  283. }
  284. }
  285. void tap_random_base64(void) {
  286. uint8_t key = (TCNT0 + TCNT1 + TCNT3 + TCNT4) % 64;
  287. // uint8_t key = rand() % 64;
  288. switch (key) {
  289. case 0 ... 25:
  290. register_code(KC_LSFT);
  291. register_code(key + KC_A);
  292. unregister_code(key + KC_A);
  293. unregister_code(KC_LSFT);
  294. break;
  295. case 26 ... 51:
  296. register_code(key - 26 + KC_A);
  297. unregister_code(key - 26 + KC_A);
  298. break;
  299. case 52:
  300. register_code(KC_0);
  301. unregister_code(KC_0);
  302. break;
  303. case 53 ... 61:
  304. register_code(key - 53 + KC_1);
  305. unregister_code(key - 53 + KC_1);
  306. break;
  307. case 62:
  308. register_code(KC_LSFT);
  309. register_code(KC_EQL);
  310. unregister_code(KC_EQL);
  311. unregister_code(KC_LSFT);
  312. break;
  313. case 63:
  314. register_code(KC_SLSH);
  315. unregister_code(KC_SLSH);
  316. break;
  317. }
  318. }
  319. void matrix_init_quantum() {
  320. #ifdef BACKLIGHT_ENABLE
  321. backlight_init_ports();
  322. #endif
  323. matrix_init_kb();
  324. }
  325. void matrix_scan_quantum() {
  326. #ifdef AUDIO_ENABLE
  327. matrix_scan_music();
  328. #endif
  329. #ifdef TAP_DANCE_ENABLE
  330. matrix_scan_tap_dance();
  331. #endif
  332. matrix_scan_kb();
  333. }
  334. #if defined(BACKLIGHT_ENABLE) && defined(BACKLIGHT_PIN)
  335. static const uint8_t backlight_pin = BACKLIGHT_PIN;
  336. #if BACKLIGHT_PIN == B7
  337. # define COM1x1 COM1C1
  338. # define OCR1x OCR1C
  339. #elif BACKLIGHT_PIN == B6
  340. # define COM1x1 COM1B1
  341. # define OCR1x OCR1B
  342. #elif BACKLIGHT_PIN == B5
  343. # define COM1x1 COM1A1
  344. # define OCR1x OCR1A
  345. #else
  346. # error "Backlight pin not supported - use B5, B6, or B7"
  347. #endif
  348. __attribute__ ((weak))
  349. void backlight_init_ports(void)
  350. {
  351. // Setup backlight pin as output and output low.
  352. // DDRx |= n
  353. _SFR_IO8((backlight_pin >> 4) + 1) |= _BV(backlight_pin & 0xF);
  354. // PORTx &= ~n
  355. _SFR_IO8((backlight_pin >> 4) + 2) &= ~_BV(backlight_pin & 0xF);
  356. // Use full 16-bit resolution.
  357. ICR1 = 0xFFFF;
  358. // I could write a wall of text here to explain... but TL;DW
  359. // Go read the ATmega32u4 datasheet.
  360. // And this: http://blog.saikoled.com/post/43165849837/secret-konami-cheat-code-to-high-resolution-pwm-on
  361. // Pin PB7 = OCR1C (Timer 1, Channel C)
  362. // Compare Output Mode = Clear on compare match, Channel C = COM1C1=1 COM1C0=0
  363. // (i.e. start high, go low when counter matches.)
  364. // WGM Mode 14 (Fast PWM) = WGM13=1 WGM12=1 WGM11=1 WGM10=0
  365. // Clock Select = clk/1 (no prescaling) = CS12=0 CS11=0 CS10=1
  366. TCCR1A = _BV(COM1x1) | _BV(WGM11); // = 0b00001010;
  367. TCCR1B = _BV(WGM13) | _BV(WGM12) | _BV(CS10); // = 0b00011001;
  368. backlight_init();
  369. #ifdef BACKLIGHT_BREATHING
  370. breathing_defaults();
  371. #endif
  372. }
  373. __attribute__ ((weak))
  374. void backlight_set(uint8_t level)
  375. {
  376. // Prevent backlight blink on lowest level
  377. // PORTx &= ~n
  378. _SFR_IO8((backlight_pin >> 4) + 2) &= ~_BV(backlight_pin & 0xF);
  379. if ( level == 0 ) {
  380. // Turn off PWM control on backlight pin, revert to output low.
  381. TCCR1A &= ~(_BV(COM1x1));
  382. OCR1x = 0x0;
  383. } else if ( level == BACKLIGHT_LEVELS ) {
  384. // Turn on PWM control of backlight pin
  385. TCCR1A |= _BV(COM1x1);
  386. // Set the brightness
  387. OCR1x = 0xFFFF;
  388. } else {
  389. // Turn on PWM control of backlight pin
  390. TCCR1A |= _BV(COM1x1);
  391. // Set the brightness
  392. OCR1x = 0xFFFF >> ((BACKLIGHT_LEVELS - level) * ((BACKLIGHT_LEVELS + 1) / 2));
  393. }
  394. #ifdef BACKLIGHT_BREATHING
  395. breathing_intensity_default();
  396. #endif
  397. }
  398. #ifdef BACKLIGHT_BREATHING
  399. #define BREATHING_NO_HALT 0
  400. #define BREATHING_HALT_OFF 1
  401. #define BREATHING_HALT_ON 2
  402. static uint8_t breath_intensity;
  403. static uint8_t breath_speed;
  404. static uint16_t breathing_index;
  405. static uint8_t breathing_halt;
  406. void breathing_enable(void)
  407. {
  408. if (get_backlight_level() == 0)
  409. {
  410. breathing_index = 0;
  411. }
  412. else
  413. {
  414. // Set breathing_index to be at the midpoint (brightest point)
  415. breathing_index = 0x20 << breath_speed;
  416. }
  417. breathing_halt = BREATHING_NO_HALT;
  418. // Enable breathing interrupt
  419. TIMSK1 |= _BV(OCIE1A);
  420. }
  421. void breathing_pulse(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 + 1 (brightest point)
  430. breathing_index = 0x21 << breath_speed;
  431. }
  432. breathing_halt = BREATHING_HALT_ON;
  433. // Enable breathing interrupt
  434. TIMSK1 |= _BV(OCIE1A);
  435. }
  436. void breathing_disable(void)
  437. {
  438. // Disable breathing interrupt
  439. TIMSK1 &= ~_BV(OCIE1A);
  440. backlight_set(get_backlight_level());
  441. }
  442. void breathing_self_disable(void)
  443. {
  444. if (get_backlight_level() == 0)
  445. {
  446. breathing_halt = BREATHING_HALT_OFF;
  447. }
  448. else
  449. {
  450. breathing_halt = BREATHING_HALT_ON;
  451. }
  452. //backlight_set(get_backlight_level());
  453. }
  454. void breathing_toggle(void)
  455. {
  456. if (!is_breathing())
  457. {
  458. if (get_backlight_level() == 0)
  459. {
  460. breathing_index = 0;
  461. }
  462. else
  463. {
  464. // Set breathing_index to be at the midpoint + 1 (brightest point)
  465. breathing_index = 0x21 << breath_speed;
  466. }
  467. breathing_halt = BREATHING_NO_HALT;
  468. }
  469. // Toggle breathing interrupt
  470. TIMSK1 ^= _BV(OCIE1A);
  471. // Restore backlight level
  472. if (!is_breathing())
  473. {
  474. backlight_set(get_backlight_level());
  475. }
  476. }
  477. bool is_breathing(void)
  478. {
  479. return (TIMSK1 && _BV(OCIE1A));
  480. }
  481. void breathing_intensity_default(void)
  482. {
  483. //breath_intensity = (uint8_t)((uint16_t)100 * (uint16_t)get_backlight_level() / (uint16_t)BACKLIGHT_LEVELS);
  484. breath_intensity = ((BACKLIGHT_LEVELS - get_backlight_level()) * ((BACKLIGHT_LEVELS + 1) / 2));
  485. }
  486. void breathing_intensity_set(uint8_t value)
  487. {
  488. breath_intensity = value;
  489. }
  490. void breathing_speed_default(void)
  491. {
  492. breath_speed = 4;
  493. }
  494. void breathing_speed_set(uint8_t value)
  495. {
  496. bool is_breathing_now = is_breathing();
  497. uint8_t old_breath_speed = breath_speed;
  498. if (is_breathing_now)
  499. {
  500. // Disable breathing interrupt
  501. TIMSK1 &= ~_BV(OCIE1A);
  502. }
  503. breath_speed = value;
  504. if (is_breathing_now)
  505. {
  506. // Adjust index to account for new speed
  507. breathing_index = (( (uint8_t)( (breathing_index) >> old_breath_speed ) ) & 0x3F) << breath_speed;
  508. // Enable breathing interrupt
  509. TIMSK1 |= _BV(OCIE1A);
  510. }
  511. }
  512. void breathing_speed_inc(uint8_t value)
  513. {
  514. if ((uint16_t)(breath_speed - value) > 10 )
  515. {
  516. breathing_speed_set(0);
  517. }
  518. else
  519. {
  520. breathing_speed_set(breath_speed - value);
  521. }
  522. }
  523. void breathing_speed_dec(uint8_t value)
  524. {
  525. if ((uint16_t)(breath_speed + value) > 10 )
  526. {
  527. breathing_speed_set(10);
  528. }
  529. else
  530. {
  531. breathing_speed_set(breath_speed + value);
  532. }
  533. }
  534. void breathing_defaults(void)
  535. {
  536. breathing_intensity_default();
  537. breathing_speed_default();
  538. breathing_halt = BREATHING_NO_HALT;
  539. }
  540. /* Breathing Sleep LED brighness(PWM On period) table
  541. * (64[steps] * 4[duration]) / 64[PWM periods/s] = 4 second breath cycle
  542. *
  543. * http://www.wolframalpha.com/input/?i=%28sin%28+x%2F64*pi%29**8+*+255%2C+x%3D0+to+63
  544. * (0..63).each {|x| p ((sin(x/64.0*PI)**8)*255).to_i }
  545. */
  546. static const uint8_t breathing_table[64] PROGMEM = {
  547. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 4, 6, 10,
  548. 15, 23, 32, 44, 58, 74, 93, 113, 135, 157, 179, 199, 218, 233, 245, 252,
  549. 255, 252, 245, 233, 218, 199, 179, 157, 135, 113, 93, 74, 58, 44, 32, 23,
  550. 15, 10, 6, 4, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  551. };
  552. ISR(TIMER1_COMPA_vect)
  553. {
  554. // OCR1x = (pgm_read_byte(&breathing_table[ ( (uint8_t)( (breathing_index++) >> breath_speed ) ) & 0x3F ] )) * breath_intensity;
  555. uint8_t local_index = ( (uint8_t)( (breathing_index++) >> breath_speed ) ) & 0x3F;
  556. if (((breathing_halt == BREATHING_HALT_ON) && (local_index == 0x20)) || ((breathing_halt == BREATHING_HALT_OFF) && (local_index == 0x3F)))
  557. {
  558. // Disable breathing interrupt
  559. TIMSK1 &= ~_BV(OCIE1A);
  560. }
  561. OCR1x = (uint16_t)(((uint16_t)pgm_read_byte(&breathing_table[local_index]) * 257)) >> breath_intensity;
  562. }
  563. #endif // breathing
  564. #else // backlight
  565. __attribute__ ((weak))
  566. void backlight_init_ports(void)
  567. {
  568. }
  569. __attribute__ ((weak))
  570. void backlight_set(uint8_t level)
  571. {
  572. }
  573. #endif // backlight
  574. __attribute__ ((weak))
  575. void led_set_user(uint8_t usb_led) {
  576. }
  577. __attribute__ ((weak))
  578. void led_set_kb(uint8_t usb_led) {
  579. led_set_user(usb_led);
  580. }
  581. __attribute__ ((weak))
  582. void led_init_ports(void)
  583. {
  584. }
  585. __attribute__ ((weak))
  586. void led_set(uint8_t usb_led)
  587. {
  588. // Example LED Code
  589. //
  590. // // Using PE6 Caps Lock LED
  591. // if (usb_led & (1<<USB_LED_CAPS_LOCK))
  592. // {
  593. // // Output high.
  594. // DDRE |= (1<<6);
  595. // PORTE |= (1<<6);
  596. // }
  597. // else
  598. // {
  599. // // Output low.
  600. // DDRE &= ~(1<<6);
  601. // PORTE &= ~(1<<6);
  602. // }
  603. led_set_kb(usb_led);
  604. }
  605. //------------------------------------------------------------------------------
  606. // Override these functions in your keymap file to play different tunes on
  607. // different events such as startup and bootloader jump
  608. __attribute__ ((weak))
  609. void startup_user() {}
  610. __attribute__ ((weak))
  611. void shutdown_user() {}
  612. //------------------------------------------------------------------------------