diff options
Diffstat (limited to 'quantum/quantum.c')
| -rw-r--r-- | quantum/quantum.c | 204 |
1 files changed, 170 insertions, 34 deletions
diff --git a/quantum/quantum.c b/quantum/quantum.c index 63ffe2074..4a6d0355f 100644 --- a/quantum/quantum.c +++ b/quantum/quantum.c | |||
| @@ -1,9 +1,16 @@ | |||
| 1 | #include "quantum.h" | 1 | #include "quantum.h" |
| 2 | #ifdef PROTOCOL_LUFA | ||
| 3 | #include "outputselect.h" | ||
| 4 | #endif | ||
| 2 | 5 | ||
| 3 | #ifndef TAPPING_TERM | 6 | #ifndef TAPPING_TERM |
| 4 | #define TAPPING_TERM 200 | 7 | #define TAPPING_TERM 200 |
| 5 | #endif | 8 | #endif |
| 6 | 9 | ||
| 10 | #ifdef FAUXCLICKY_ENABLE | ||
| 11 | #include "fauxclicky.h" | ||
| 12 | #endif | ||
| 13 | |||
| 7 | static void do_code16 (uint16_t code, void (*f) (uint8_t)) { | 14 | static void do_code16 (uint16_t code, void (*f) (uint8_t)) { |
| 8 | switch (code) { | 15 | switch (code) { |
| 9 | case QK_MODS ... QK_MODS_MAX: | 16 | case QK_MODS ... QK_MODS_MAX: |
| @@ -33,14 +40,42 @@ static void do_code16 (uint16_t code, void (*f) (uint8_t)) { | |||
| 33 | f(KC_RGUI); | 40 | f(KC_RGUI); |
| 34 | } | 41 | } |
| 35 | 42 | ||
| 43 | static inline void qk_register_weak_mods(uint8_t kc) { | ||
| 44 | add_weak_mods(MOD_BIT(kc)); | ||
| 45 | send_keyboard_report(); | ||
| 46 | } | ||
| 47 | |||
| 48 | static inline void qk_unregister_weak_mods(uint8_t kc) { | ||
| 49 | del_weak_mods(MOD_BIT(kc)); | ||
| 50 | send_keyboard_report(); | ||
| 51 | } | ||
| 52 | |||
| 53 | static inline void qk_register_mods(uint8_t kc) { | ||
| 54 | add_weak_mods(MOD_BIT(kc)); | ||
| 55 | send_keyboard_report(); | ||
| 56 | } | ||
| 57 | |||
| 58 | static inline void qk_unregister_mods(uint8_t kc) { | ||
| 59 | del_weak_mods(MOD_BIT(kc)); | ||
| 60 | send_keyboard_report(); | ||
| 61 | } | ||
| 62 | |||
| 36 | void register_code16 (uint16_t code) { | 63 | void register_code16 (uint16_t code) { |
| 37 | do_code16 (code, register_code); | 64 | if (IS_MOD(code) || code == KC_NO) { |
| 65 | do_code16 (code, qk_register_mods); | ||
| 66 | } else { | ||
| 67 | do_code16 (code, qk_register_weak_mods); | ||
| 68 | } | ||
| 38 | register_code (code); | 69 | register_code (code); |
| 39 | } | 70 | } |
| 40 | 71 | ||
| 41 | void unregister_code16 (uint16_t code) { | 72 | void unregister_code16 (uint16_t code) { |
| 42 | unregister_code (code); | 73 | unregister_code (code); |
| 43 | do_code16 (code, unregister_code); | 74 | if (IS_MOD(code) || code == KC_NO) { |
| 75 | do_code16 (code, qk_unregister_mods); | ||
| 76 | } else { | ||
| 77 | do_code16 (code, qk_unregister_weak_mods); | ||
| 78 | } | ||
| 44 | } | 79 | } |
| 45 | 80 | ||
| 46 | __attribute__ ((weak)) | 81 | __attribute__ ((weak)) |
| @@ -130,6 +165,9 @@ bool process_record_quantum(keyrecord_t *record) { | |||
| 130 | #ifndef DISABLE_CHORDING | 165 | #ifndef DISABLE_CHORDING |
| 131 | process_chording(keycode, record) && | 166 | process_chording(keycode, record) && |
| 132 | #endif | 167 | #endif |
| 168 | #ifdef COMBO_ENABLE | ||
| 169 | process_combo(keycode, record) && | ||
| 170 | #endif | ||
| 133 | #ifdef UNICODE_ENABLE | 171 | #ifdef UNICODE_ENABLE |
| 134 | process_unicode(keycode, record) && | 172 | process_unicode(keycode, record) && |
| 135 | #endif | 173 | #endif |
| @@ -162,6 +200,26 @@ bool process_record_quantum(keyrecord_t *record) { | |||
| 162 | } | 200 | } |
| 163 | return false; | 201 | return false; |
| 164 | break; | 202 | break; |
| 203 | #ifdef FAUXCLICKY_ENABLE | ||
| 204 | case FC_TOG: | ||
| 205 | if (record->event.pressed) { | ||
| 206 | FAUXCLICKY_TOGGLE; | ||
| 207 | } | ||
| 208 | return false; | ||
| 209 | break; | ||
| 210 | case FC_ON: | ||
| 211 | if (record->event.pressed) { | ||
| 212 | FAUXCLICKY_ON; | ||
| 213 | } | ||
| 214 | return false; | ||
| 215 | break; | ||
| 216 | case FC_OFF: | ||
| 217 | if (record->event.pressed) { | ||
| 218 | FAUXCLICKY_OFF; | ||
| 219 | } | ||
| 220 | return false; | ||
| 221 | break; | ||
| 222 | #endif | ||
| 165 | #ifdef RGBLIGHT_ENABLE | 223 | #ifdef RGBLIGHT_ENABLE |
| 166 | case RGB_TOG: | 224 | case RGB_TOG: |
| 167 | if (record->event.pressed) { | 225 | if (record->event.pressed) { |
| @@ -212,6 +270,36 @@ bool process_record_quantum(keyrecord_t *record) { | |||
| 212 | return false; | 270 | return false; |
| 213 | break; | 271 | break; |
| 214 | #endif | 272 | #endif |
| 273 | #ifdef PROTOCOL_LUFA | ||
| 274 | case OUT_AUTO: | ||
| 275 | if (record->event.pressed) { | ||
| 276 | set_output(OUTPUT_AUTO); | ||
| 277 | } | ||
| 278 | return false; | ||
| 279 | break; | ||
| 280 | case OUT_USB: | ||
| 281 | if (record->event.pressed) { | ||
| 282 | set_output(OUTPUT_USB); | ||
| 283 | } | ||
| 284 | return false; | ||
| 285 | break; | ||
| 286 | #ifdef BLUETOOTH_ENABLE | ||
| 287 | case OUT_BT: | ||
| 288 | if (record->event.pressed) { | ||
| 289 | set_output(OUTPUT_BLUETOOTH); | ||
| 290 | } | ||
| 291 | return false; | ||
| 292 | break; | ||
| 293 | #endif | ||
| 294 | #ifdef ADAFRUIT_BLE_ENABLE | ||
| 295 | case OUT_BLE: | ||
| 296 | if (record->event.pressed) { | ||
| 297 | set_output(OUTPUT_ADAFRUIT_BLE); | ||
| 298 | } | ||
| 299 | return false; | ||
| 300 | break; | ||
| 301 | #endif | ||
| 302 | #endif | ||
| 215 | case MAGIC_SWAP_CONTROL_CAPSLOCK ... MAGIC_TOGGLE_NKRO: | 303 | case MAGIC_SWAP_CONTROL_CAPSLOCK ... MAGIC_TOGGLE_NKRO: |
| 216 | if (record->event.pressed) { | 304 | if (record->event.pressed) { |
| 217 | // MAGIC actions (BOOTMAGIC without the boot) | 305 | // MAGIC actions (BOOTMAGIC without the boot) |
| @@ -508,6 +596,11 @@ void matrix_scan_quantum() { | |||
| 508 | #ifdef TAP_DANCE_ENABLE | 596 | #ifdef TAP_DANCE_ENABLE |
| 509 | matrix_scan_tap_dance(); | 597 | matrix_scan_tap_dance(); |
| 510 | #endif | 598 | #endif |
| 599 | |||
| 600 | #ifdef COMBO_ENABLE | ||
| 601 | matrix_scan_combo(); | ||
| 602 | #endif | ||
| 603 | |||
| 511 | matrix_scan_kb(); | 604 | matrix_scan_kb(); |
| 512 | } | 605 | } |
| 513 | 606 | ||
| @@ -525,34 +618,45 @@ static const uint8_t backlight_pin = BACKLIGHT_PIN; | |||
| 525 | # define COM1x1 COM1A1 | 618 | # define COM1x1 COM1A1 |
| 526 | # define OCR1x OCR1A | 619 | # define OCR1x OCR1A |
| 527 | #else | 620 | #else |
| 528 | # error "Backlight pin not supported - use B5, B6, or B7" | 621 | # define NO_BACKLIGHT_CLOCK |
| 622 | #endif | ||
| 623 | |||
| 624 | #ifndef BACKLIGHT_ON_STATE | ||
| 625 | #define BACKLIGHT_ON_STATE 0 | ||
| 529 | #endif | 626 | #endif |
| 530 | 627 | ||
| 531 | __attribute__ ((weak)) | 628 | __attribute__ ((weak)) |
| 532 | void backlight_init_ports(void) | 629 | void backlight_init_ports(void) |
| 533 | { | 630 | { |
| 534 | 631 | ||
| 535 | // Setup backlight pin as output and output low. | 632 | // Setup backlight pin as output and output to on state. |
| 536 | // DDRx |= n | 633 | // DDRx |= n |
| 537 | _SFR_IO8((backlight_pin >> 4) + 1) |= _BV(backlight_pin & 0xF); | 634 | _SFR_IO8((backlight_pin >> 4) + 1) |= _BV(backlight_pin & 0xF); |
| 538 | // PORTx &= ~n | 635 | #if BACKLIGHT_ON_STATE == 0 |
| 539 | _SFR_IO8((backlight_pin >> 4) + 2) &= ~_BV(backlight_pin & 0xF); | 636 | // PORTx &= ~n |
| 637 | _SFR_IO8((backlight_pin >> 4) + 2) &= ~_BV(backlight_pin & 0xF); | ||
| 638 | #else | ||
| 639 | // PORTx |= n | ||
| 640 | _SFR_IO8((backlight_pin >> 4) + 2) |= _BV(backlight_pin & 0xF); | ||
| 641 | #endif | ||
| 540 | 642 | ||
| 541 | // Use full 16-bit resolution. | 643 | #ifndef NO_BACKLIGHT_CLOCK |
| 542 | ICR1 = 0xFFFF; | 644 | // Use full 16-bit resolution. |
| 645 | ICR1 = 0xFFFF; | ||
| 543 | 646 | ||
| 544 | // I could write a wall of text here to explain... but TL;DW | 647 | // I could write a wall of text here to explain... but TL;DW |
| 545 | // Go read the ATmega32u4 datasheet. | 648 | // Go read the ATmega32u4 datasheet. |
| 546 | // And this: http://blog.saikoled.com/post/43165849837/secret-konami-cheat-code-to-high-resolution-pwm-on | 649 | // And this: http://blog.saikoled.com/post/43165849837/secret-konami-cheat-code-to-high-resolution-pwm-on |
| 547 | 650 | ||
| 548 | // Pin PB7 = OCR1C (Timer 1, Channel C) | 651 | // Pin PB7 = OCR1C (Timer 1, Channel C) |
| 549 | // Compare Output Mode = Clear on compare match, Channel C = COM1C1=1 COM1C0=0 | 652 | // Compare Output Mode = Clear on compare match, Channel C = COM1C1=1 COM1C0=0 |
| 550 | // (i.e. start high, go low when counter matches.) | 653 | // (i.e. start high, go low when counter matches.) |
| 551 | // WGM Mode 14 (Fast PWM) = WGM13=1 WGM12=1 WGM11=1 WGM10=0 | 654 | // WGM Mode 14 (Fast PWM) = WGM13=1 WGM12=1 WGM11=1 WGM10=0 |
| 552 | // Clock Select = clk/1 (no prescaling) = CS12=0 CS11=0 CS10=1 | 655 | // Clock Select = clk/1 (no prescaling) = CS12=0 CS11=0 CS10=1 |
| 553 | 656 | ||
| 554 | TCCR1A = _BV(COM1x1) | _BV(WGM11); // = 0b00001010; | 657 | TCCR1A = _BV(COM1x1) | _BV(WGM11); // = 0b00001010; |
| 555 | TCCR1B = _BV(WGM13) | _BV(WGM12) | _BV(CS10); // = 0b00011001; | 658 | TCCR1B = _BV(WGM13) | _BV(WGM12) | _BV(CS10); // = 0b00011001; |
| 659 | #endif | ||
| 556 | 660 | ||
| 557 | backlight_init(); | 661 | backlight_init(); |
| 558 | #ifdef BACKLIGHT_BREATHING | 662 | #ifdef BACKLIGHT_BREATHING |
| @@ -564,24 +668,43 @@ __attribute__ ((weak)) | |||
| 564 | void backlight_set(uint8_t level) | 668 | void backlight_set(uint8_t level) |
| 565 | { | 669 | { |
| 566 | // Prevent backlight blink on lowest level | 670 | // Prevent backlight blink on lowest level |
| 567 | // PORTx &= ~n | 671 | #if BACKLIGHT_ON_STATE == 0 |
| 568 | _SFR_IO8((backlight_pin >> 4) + 2) &= ~_BV(backlight_pin & 0xF); | 672 | // PORTx &= ~n |
| 673 | _SFR_IO8((backlight_pin >> 4) + 2) &= ~_BV(backlight_pin & 0xF); | ||
| 674 | #else | ||
| 675 | // PORTx |= n | ||
| 676 | _SFR_IO8((backlight_pin >> 4) + 2) |= _BV(backlight_pin & 0xF); | ||
| 677 | #endif | ||
| 569 | 678 | ||
| 570 | if ( level == 0 ) { | 679 | if ( level == 0 ) { |
| 571 | // Turn off PWM control on backlight pin, revert to output low. | 680 | #ifndef NO_BACKLIGHT_CLOCK |
| 572 | TCCR1A &= ~(_BV(COM1x1)); | 681 | // Turn off PWM control on backlight pin, revert to output low. |
| 573 | OCR1x = 0x0; | 682 | TCCR1A &= ~(_BV(COM1x1)); |
| 574 | } else if ( level == BACKLIGHT_LEVELS ) { | 683 | OCR1x = 0x0; |
| 575 | // Turn on PWM control of backlight pin | 684 | #else |
| 576 | TCCR1A |= _BV(COM1x1); | 685 | #if BACKLIGHT_ON_STATE == 0 |
| 577 | // Set the brightness | 686 | // PORTx |= n |
| 578 | OCR1x = 0xFFFF; | 687 | _SFR_IO8((backlight_pin >> 4) + 2) |= _BV(backlight_pin & 0xF); |
| 579 | } else { | 688 | #else |
| 580 | // Turn on PWM control of backlight pin | 689 | // PORTx &= ~n |
| 581 | TCCR1A |= _BV(COM1x1); | 690 | _SFR_IO8((backlight_pin >> 4) + 2) &= ~_BV(backlight_pin & 0xF); |
| 582 | // Set the brightness | 691 | #endif |
| 583 | OCR1x = 0xFFFF >> ((BACKLIGHT_LEVELS - level) * ((BACKLIGHT_LEVELS + 1) / 2)); | 692 | #endif |
| 584 | } | 693 | } |
| 694 | #ifndef NO_BACKLIGHT_CLOCK | ||
| 695 | else if ( level == BACKLIGHT_LEVELS ) { | ||
| 696 | // Turn on PWM control of backlight pin | ||
| 697 | TCCR1A |= _BV(COM1x1); | ||
| 698 | // Set the brightness | ||
| 699 | OCR1x = 0xFFFF; | ||
| 700 | } | ||
| 701 | else { | ||
| 702 | // Turn on PWM control of backlight pin | ||
| 703 | TCCR1A |= _BV(COM1x1); | ||
| 704 | // Set the brightness | ||
| 705 | OCR1x = 0xFFFF >> ((BACKLIGHT_LEVELS - level) * ((BACKLIGHT_LEVELS + 1) / 2)); | ||
| 706 | } | ||
| 707 | #endif | ||
| 585 | 708 | ||
| 586 | #ifdef BACKLIGHT_BREATHING | 709 | #ifdef BACKLIGHT_BREATHING |
| 587 | breathing_intensity_default(); | 710 | breathing_intensity_default(); |
| @@ -849,6 +972,19 @@ void send_nibble(uint8_t number) { | |||
| 849 | } | 972 | } |
| 850 | } | 973 | } |
| 851 | 974 | ||
| 975 | |||
| 976 | __attribute__((weak)) | ||
| 977 | uint16_t hex_to_keycode(uint8_t hex) | ||
| 978 | { | ||
| 979 | if (hex == 0x0) { | ||
| 980 | return KC_0; | ||
| 981 | } else if (hex < 0xA) { | ||
| 982 | return KC_1 + (hex - 0x1); | ||
| 983 | } else { | ||
| 984 | return KC_A + (hex - 0xA); | ||
| 985 | } | ||
| 986 | } | ||
| 987 | |||
| 852 | void api_send_unicode(uint32_t unicode) { | 988 | void api_send_unicode(uint32_t unicode) { |
| 853 | #ifdef API_ENABLE | 989 | #ifdef API_ENABLE |
| 854 | uint8_t chunk[4]; | 990 | uint8_t chunk[4]; |
