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