quantum.c 20 KB

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