quantum.c 19 KB

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