quantum.c 19 KB

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