quantum.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  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. 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. #if defined(__AVR_ATmega32U4__)
  287. uint8_t key = (TCNT0 + TCNT1 + TCNT3 + TCNT4) % 64;
  288. #else
  289. uint8_t key = rand() % 64;
  290. #endif
  291. switch (key) {
  292. case 0 ... 25:
  293. register_code(KC_LSFT);
  294. register_code(key + KC_A);
  295. unregister_code(key + KC_A);
  296. unregister_code(KC_LSFT);
  297. break;
  298. case 26 ... 51:
  299. register_code(key - 26 + KC_A);
  300. unregister_code(key - 26 + KC_A);
  301. break;
  302. case 52:
  303. register_code(KC_0);
  304. unregister_code(KC_0);
  305. break;
  306. case 53 ... 61:
  307. register_code(key - 53 + KC_1);
  308. unregister_code(key - 53 + KC_1);
  309. break;
  310. case 62:
  311. register_code(KC_LSFT);
  312. register_code(KC_EQL);
  313. unregister_code(KC_EQL);
  314. unregister_code(KC_LSFT);
  315. break;
  316. case 63:
  317. register_code(KC_SLSH);
  318. unregister_code(KC_SLSH);
  319. break;
  320. }
  321. }
  322. void matrix_init_quantum() {
  323. #ifdef BACKLIGHT_ENABLE
  324. backlight_init_ports();
  325. #endif
  326. matrix_init_kb();
  327. }
  328. void matrix_scan_quantum() {
  329. #ifdef AUDIO_ENABLE
  330. matrix_scan_music();
  331. #endif
  332. #ifdef TAP_DANCE_ENABLE
  333. matrix_scan_tap_dance();
  334. #endif
  335. matrix_scan_kb();
  336. }
  337. #if defined(BACKLIGHT_ENABLE) && defined(BACKLIGHT_PIN)
  338. static const uint8_t backlight_pin = BACKLIGHT_PIN;
  339. #if BACKLIGHT_PIN == B7
  340. # define COM1x1 COM1C1
  341. # define OCR1x OCR1C
  342. #elif BACKLIGHT_PIN == B6
  343. # define COM1x1 COM1B1
  344. # define OCR1x OCR1B
  345. #elif BACKLIGHT_PIN == B5
  346. # define COM1x1 COM1A1
  347. # define OCR1x OCR1A
  348. #else
  349. # error "Backlight pin not supported - use B5, B6, or B7"
  350. #endif
  351. __attribute__ ((weak))
  352. void backlight_init_ports(void)
  353. {
  354. // Setup backlight pin as output and output low.
  355. // DDRx |= n
  356. _SFR_IO8((backlight_pin >> 4) + 1) |= _BV(backlight_pin & 0xF);
  357. // PORTx &= ~n
  358. _SFR_IO8((backlight_pin >> 4) + 2) &= ~_BV(backlight_pin & 0xF);
  359. // Use full 16-bit resolution.
  360. ICR1 = 0xFFFF;
  361. // I could write a wall of text here to explain... but TL;DW
  362. // Go read the ATmega32u4 datasheet.
  363. // And this: http://blog.saikoled.com/post/43165849837/secret-konami-cheat-code-to-high-resolution-pwm-on
  364. // Pin PB7 = OCR1C (Timer 1, Channel C)
  365. // Compare Output Mode = Clear on compare match, Channel C = COM1C1=1 COM1C0=0
  366. // (i.e. start high, go low when counter matches.)
  367. // WGM Mode 14 (Fast PWM) = WGM13=1 WGM12=1 WGM11=1 WGM10=0
  368. // Clock Select = clk/1 (no prescaling) = CS12=0 CS11=0 CS10=1
  369. TCCR1A = _BV(COM1x1) | _BV(WGM11); // = 0b00001010;
  370. TCCR1B = _BV(WGM13) | _BV(WGM12) | _BV(CS10); // = 0b00011001;
  371. backlight_init();
  372. #ifdef BACKLIGHT_BREATHING
  373. breathing_defaults();
  374. #endif
  375. }
  376. __attribute__ ((weak))
  377. void backlight_set(uint8_t level)
  378. {
  379. // Prevent backlight blink on lowest level
  380. // PORTx &= ~n
  381. _SFR_IO8((backlight_pin >> 4) + 2) &= ~_BV(backlight_pin & 0xF);
  382. if ( level == 0 ) {
  383. // Turn off PWM control on backlight pin, revert to output low.
  384. TCCR1A &= ~(_BV(COM1x1));
  385. OCR1x = 0x0;
  386. } else if ( level == BACKLIGHT_LEVELS ) {
  387. // Turn on PWM control of backlight pin
  388. TCCR1A |= _BV(COM1x1);
  389. // Set the brightness
  390. OCR1x = 0xFFFF;
  391. } else {
  392. // Turn on PWM control of backlight pin
  393. TCCR1A |= _BV(COM1x1);
  394. // Set the brightness
  395. OCR1x = 0xFFFF >> ((BACKLIGHT_LEVELS - level) * ((BACKLIGHT_LEVELS + 1) / 2));
  396. }
  397. #ifdef BACKLIGHT_BREATHING
  398. breathing_intensity_default();
  399. #endif
  400. }
  401. #ifdef BACKLIGHT_BREATHING
  402. #define BREATHING_NO_HALT 0
  403. #define BREATHING_HALT_OFF 1
  404. #define BREATHING_HALT_ON 2
  405. static uint8_t breath_intensity;
  406. static uint8_t breath_speed;
  407. static uint16_t breathing_index;
  408. static uint8_t breathing_halt;
  409. void breathing_enable(void)
  410. {
  411. if (get_backlight_level() == 0)
  412. {
  413. breathing_index = 0;
  414. }
  415. else
  416. {
  417. // Set breathing_index to be at the midpoint (brightest point)
  418. breathing_index = 0x20 << breath_speed;
  419. }
  420. breathing_halt = BREATHING_NO_HALT;
  421. // Enable breathing interrupt
  422. TIMSK1 |= _BV(OCIE1A);
  423. }
  424. void breathing_pulse(void)
  425. {
  426. if (get_backlight_level() == 0)
  427. {
  428. breathing_index = 0;
  429. }
  430. else
  431. {
  432. // Set breathing_index to be at the midpoint + 1 (brightest point)
  433. breathing_index = 0x21 << breath_speed;
  434. }
  435. breathing_halt = BREATHING_HALT_ON;
  436. // Enable breathing interrupt
  437. TIMSK1 |= _BV(OCIE1A);
  438. }
  439. void breathing_disable(void)
  440. {
  441. // Disable breathing interrupt
  442. TIMSK1 &= ~_BV(OCIE1A);
  443. backlight_set(get_backlight_level());
  444. }
  445. void breathing_self_disable(void)
  446. {
  447. if (get_backlight_level() == 0)
  448. {
  449. breathing_halt = BREATHING_HALT_OFF;
  450. }
  451. else
  452. {
  453. breathing_halt = BREATHING_HALT_ON;
  454. }
  455. //backlight_set(get_backlight_level());
  456. }
  457. void breathing_toggle(void)
  458. {
  459. if (!is_breathing())
  460. {
  461. if (get_backlight_level() == 0)
  462. {
  463. breathing_index = 0;
  464. }
  465. else
  466. {
  467. // Set breathing_index to be at the midpoint + 1 (brightest point)
  468. breathing_index = 0x21 << breath_speed;
  469. }
  470. breathing_halt = BREATHING_NO_HALT;
  471. }
  472. // Toggle breathing interrupt
  473. TIMSK1 ^= _BV(OCIE1A);
  474. // Restore backlight level
  475. if (!is_breathing())
  476. {
  477. backlight_set(get_backlight_level());
  478. }
  479. }
  480. bool is_breathing(void)
  481. {
  482. return (TIMSK1 && _BV(OCIE1A));
  483. }
  484. void breathing_intensity_default(void)
  485. {
  486. //breath_intensity = (uint8_t)((uint16_t)100 * (uint16_t)get_backlight_level() / (uint16_t)BACKLIGHT_LEVELS);
  487. breath_intensity = ((BACKLIGHT_LEVELS - get_backlight_level()) * ((BACKLIGHT_LEVELS + 1) / 2));
  488. }
  489. void breathing_intensity_set(uint8_t value)
  490. {
  491. breath_intensity = value;
  492. }
  493. void breathing_speed_default(void)
  494. {
  495. breath_speed = 4;
  496. }
  497. void breathing_speed_set(uint8_t value)
  498. {
  499. bool is_breathing_now = is_breathing();
  500. uint8_t old_breath_speed = breath_speed;
  501. if (is_breathing_now)
  502. {
  503. // Disable breathing interrupt
  504. TIMSK1 &= ~_BV(OCIE1A);
  505. }
  506. breath_speed = value;
  507. if (is_breathing_now)
  508. {
  509. // Adjust index to account for new speed
  510. breathing_index = (( (uint8_t)( (breathing_index) >> old_breath_speed ) ) & 0x3F) << breath_speed;
  511. // Enable breathing interrupt
  512. TIMSK1 |= _BV(OCIE1A);
  513. }
  514. }
  515. void breathing_speed_inc(uint8_t value)
  516. {
  517. if ((uint16_t)(breath_speed - value) > 10 )
  518. {
  519. breathing_speed_set(0);
  520. }
  521. else
  522. {
  523. breathing_speed_set(breath_speed - value);
  524. }
  525. }
  526. void breathing_speed_dec(uint8_t value)
  527. {
  528. if ((uint16_t)(breath_speed + value) > 10 )
  529. {
  530. breathing_speed_set(10);
  531. }
  532. else
  533. {
  534. breathing_speed_set(breath_speed + value);
  535. }
  536. }
  537. void breathing_defaults(void)
  538. {
  539. breathing_intensity_default();
  540. breathing_speed_default();
  541. breathing_halt = BREATHING_NO_HALT;
  542. }
  543. /* Breathing Sleep LED brighness(PWM On period) table
  544. * (64[steps] * 4[duration]) / 64[PWM periods/s] = 4 second breath cycle
  545. *
  546. * http://www.wolframalpha.com/input/?i=%28sin%28+x%2F64*pi%29**8+*+255%2C+x%3D0+to+63
  547. * (0..63).each {|x| p ((sin(x/64.0*PI)**8)*255).to_i }
  548. */
  549. static const uint8_t breathing_table[64] PROGMEM = {
  550. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 4, 6, 10,
  551. 15, 23, 32, 44, 58, 74, 93, 113, 135, 157, 179, 199, 218, 233, 245, 252,
  552. 255, 252, 245, 233, 218, 199, 179, 157, 135, 113, 93, 74, 58, 44, 32, 23,
  553. 15, 10, 6, 4, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  554. };
  555. ISR(TIMER1_COMPA_vect)
  556. {
  557. // OCR1x = (pgm_read_byte(&breathing_table[ ( (uint8_t)( (breathing_index++) >> breath_speed ) ) & 0x3F ] )) * breath_intensity;
  558. uint8_t local_index = ( (uint8_t)( (breathing_index++) >> breath_speed ) ) & 0x3F;
  559. if (((breathing_halt == BREATHING_HALT_ON) && (local_index == 0x20)) || ((breathing_halt == BREATHING_HALT_OFF) && (local_index == 0x3F)))
  560. {
  561. // Disable breathing interrupt
  562. TIMSK1 &= ~_BV(OCIE1A);
  563. }
  564. OCR1x = (uint16_t)(((uint16_t)pgm_read_byte(&breathing_table[local_index]) * 257)) >> breath_intensity;
  565. }
  566. #endif // breathing
  567. #else // backlight
  568. __attribute__ ((weak))
  569. void backlight_init_ports(void)
  570. {
  571. }
  572. __attribute__ ((weak))
  573. void backlight_set(uint8_t level)
  574. {
  575. }
  576. #endif // backlight
  577. __attribute__ ((weak))
  578. void led_set_user(uint8_t usb_led) {
  579. }
  580. __attribute__ ((weak))
  581. void led_set_kb(uint8_t usb_led) {
  582. led_set_user(usb_led);
  583. }
  584. __attribute__ ((weak))
  585. void led_init_ports(void)
  586. {
  587. }
  588. __attribute__ ((weak))
  589. void led_set(uint8_t usb_led)
  590. {
  591. // Example LED Code
  592. //
  593. // // Using PE6 Caps Lock LED
  594. // if (usb_led & (1<<USB_LED_CAPS_LOCK))
  595. // {
  596. // // Output high.
  597. // DDRE |= (1<<6);
  598. // PORTE |= (1<<6);
  599. // }
  600. // else
  601. // {
  602. // // Output low.
  603. // DDRE &= ~(1<<6);
  604. // PORTE &= ~(1<<6);
  605. // }
  606. led_set_kb(usb_led);
  607. }
  608. //------------------------------------------------------------------------------
  609. // Override these functions in your keymap file to play different tunes on
  610. // different events such as startup and bootloader jump
  611. __attribute__ ((weak))
  612. void startup_user() {}
  613. __attribute__ ((weak))
  614. void shutdown_user() {}
  615. //------------------------------------------------------------------------------