quantum.c 20 KB

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