quantum.c 19 KB

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