quantum.c 17 KB

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