quantum.c 19 KB

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