aboutsummaryrefslogtreecommitdiff
path: root/quantum/quantum.c
diff options
context:
space:
mode:
authorluc wastiaux <luc.wastiaux@airpost.net>2017-02-13 07:44:28 +0800
committerluc wastiaux <luc.wastiaux@airpost.net>2017-02-13 07:44:28 +0800
commit352d79e1fbbd7eea65793abb261dd544fec7bb1c (patch)
treee006ee8098826054d93939b2a59521f6c032e10d /quantum/quantum.c
parent5f8535b356e66975e20f1a573ab104db75f8b33c (diff)
parent4505db5d0e0c2423645cc9117f29b942d4841c5b (diff)
downloadqmk_firmware-352d79e1fbbd7eea65793abb261dd544fec7bb1c.tar.gz
qmk_firmware-352d79e1fbbd7eea65793abb261dd544fec7bb1c.zip
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'quantum/quantum.c')
-rw-r--r--quantum/quantum.c167
1 files changed, 133 insertions, 34 deletions
diff --git a/quantum/quantum.c b/quantum/quantum.c
index 63ffe2074..45ea8cb73 100644
--- a/quantum/quantum.c
+++ b/quantum/quantum.c
@@ -1,4 +1,7 @@
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
@@ -33,14 +36,42 @@ static void do_code16 (uint16_t code, void (*f) (uint8_t)) {
33 f(KC_RGUI); 36 f(KC_RGUI);
34} 37}
35 38
39static inline void qk_register_weak_mods(uint8_t kc) {
40 add_weak_mods(MOD_BIT(kc));
41 send_keyboard_report();
42}
43
44static inline void qk_unregister_weak_mods(uint8_t kc) {
45 del_weak_mods(MOD_BIT(kc));
46 send_keyboard_report();
47}
48
49static inline void qk_register_mods(uint8_t kc) {
50 add_weak_mods(MOD_BIT(kc));
51 send_keyboard_report();
52}
53
54static inline void qk_unregister_mods(uint8_t kc) {
55 del_weak_mods(MOD_BIT(kc));
56 send_keyboard_report();
57}
58
36void register_code16 (uint16_t code) { 59void register_code16 (uint16_t code) {
37 do_code16 (code, register_code); 60 if (IS_MOD(code) || code == KC_NO) {
61 do_code16 (code, qk_register_mods);
62 } else {
63 do_code16 (code, qk_register_weak_mods);
64 }
38 register_code (code); 65 register_code (code);
39} 66}
40 67
41void unregister_code16 (uint16_t code) { 68void unregister_code16 (uint16_t code) {
42 unregister_code (code); 69 unregister_code (code);
43 do_code16 (code, unregister_code); 70 if (IS_MOD(code) || code == KC_NO) {
71 do_code16 (code, qk_unregister_mods);
72 } else {
73 do_code16 (code, qk_unregister_weak_mods);
74 }
44} 75}
45 76
46__attribute__ ((weak)) 77__attribute__ ((weak))
@@ -130,6 +161,9 @@ bool process_record_quantum(keyrecord_t *record) {
130 #ifndef DISABLE_CHORDING 161 #ifndef DISABLE_CHORDING
131 process_chording(keycode, record) && 162 process_chording(keycode, record) &&
132 #endif 163 #endif
164 #ifdef COMBO_ENABLE
165 process_combo(keycode, record) &&
166 #endif
133 #ifdef UNICODE_ENABLE 167 #ifdef UNICODE_ENABLE
134 process_unicode(keycode, record) && 168 process_unicode(keycode, record) &&
135 #endif 169 #endif
@@ -212,6 +246,36 @@ bool process_record_quantum(keyrecord_t *record) {
212 return false; 246 return false;
213 break; 247 break;
214 #endif 248 #endif
249 #ifdef PROTOCOL_LUFA
250 case OUT_AUTO:
251 if (record->event.pressed) {
252 set_output(OUTPUT_AUTO);
253 }
254 return false;
255 break;
256 case OUT_USB:
257 if (record->event.pressed) {
258 set_output(OUTPUT_USB);
259 }
260 return false;
261 break;
262 #ifdef BLUETOOTH_ENABLE
263 case OUT_BT:
264 if (record->event.pressed) {
265 set_output(OUTPUT_BLUETOOTH);
266 }
267 return false;
268 break;
269 #endif
270 #ifdef ADAFRUIT_BLE_ENABLE
271 case OUT_BLE:
272 if (record->event.pressed) {
273 set_output(OUTPUT_ADAFRUIT_BLE);
274 }
275 return false;
276 break;
277 #endif
278 #endif
215 case MAGIC_SWAP_CONTROL_CAPSLOCK ... MAGIC_TOGGLE_NKRO: 279 case MAGIC_SWAP_CONTROL_CAPSLOCK ... MAGIC_TOGGLE_NKRO:
216 if (record->event.pressed) { 280 if (record->event.pressed) {
217 // MAGIC actions (BOOTMAGIC without the boot) 281 // MAGIC actions (BOOTMAGIC without the boot)
@@ -508,6 +572,11 @@ void matrix_scan_quantum() {
508 #ifdef TAP_DANCE_ENABLE 572 #ifdef TAP_DANCE_ENABLE
509 matrix_scan_tap_dance(); 573 matrix_scan_tap_dance();
510 #endif 574 #endif
575
576 #ifdef COMBO_ENABLE
577 matrix_scan_combo();
578 #endif
579
511 matrix_scan_kb(); 580 matrix_scan_kb();
512} 581}
513 582
@@ -525,34 +594,45 @@ static const uint8_t backlight_pin = BACKLIGHT_PIN;
525# define COM1x1 COM1A1 594# define COM1x1 COM1A1
526# define OCR1x OCR1A 595# define OCR1x OCR1A
527#else 596#else
528# error "Backlight pin not supported - use B5, B6, or B7" 597# define NO_BACKLIGHT_CLOCK
598#endif
599
600#ifndef BACKLIGHT_ON_STATE
601#define BACKLIGHT_ON_STATE 0
529#endif 602#endif
530 603
531__attribute__ ((weak)) 604__attribute__ ((weak))
532void backlight_init_ports(void) 605void backlight_init_ports(void)
533{ 606{
534 607
535 // Setup backlight pin as output and output low. 608 // Setup backlight pin as output and output to on state.
536 // DDRx |= n 609 // DDRx |= n
537 _SFR_IO8((backlight_pin >> 4) + 1) |= _BV(backlight_pin & 0xF); 610 _SFR_IO8((backlight_pin >> 4) + 1) |= _BV(backlight_pin & 0xF);
538 // PORTx &= ~n 611 #if BACKLIGHT_ON_STATE == 0
539 _SFR_IO8((backlight_pin >> 4) + 2) &= ~_BV(backlight_pin & 0xF); 612 // PORTx &= ~n
613 _SFR_IO8((backlight_pin >> 4) + 2) &= ~_BV(backlight_pin & 0xF);
614 #else
615 // PORTx |= n
616 _SFR_IO8((backlight_pin >> 4) + 2) |= _BV(backlight_pin & 0xF);
617 #endif
540 618
541 // Use full 16-bit resolution. 619 #ifndef NO_BACKLIGHT_CLOCK
542 ICR1 = 0xFFFF; 620 // Use full 16-bit resolution.
621 ICR1 = 0xFFFF;
543 622
544 // I could write a wall of text here to explain... but TL;DW 623 // I could write a wall of text here to explain... but TL;DW
545 // Go read the ATmega32u4 datasheet. 624 // Go read the ATmega32u4 datasheet.
546 // And this: http://blog.saikoled.com/post/43165849837/secret-konami-cheat-code-to-high-resolution-pwm-on 625 // And this: http://blog.saikoled.com/post/43165849837/secret-konami-cheat-code-to-high-resolution-pwm-on
547 626
548 // Pin PB7 = OCR1C (Timer 1, Channel C) 627 // Pin PB7 = OCR1C (Timer 1, Channel C)
549 // Compare Output Mode = Clear on compare match, Channel C = COM1C1=1 COM1C0=0 628 // Compare Output Mode = Clear on compare match, Channel C = COM1C1=1 COM1C0=0
550 // (i.e. start high, go low when counter matches.) 629 // (i.e. start high, go low when counter matches.)
551 // WGM Mode 14 (Fast PWM) = WGM13=1 WGM12=1 WGM11=1 WGM10=0 630 // 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 631 // Clock Select = clk/1 (no prescaling) = CS12=0 CS11=0 CS10=1
553 632
554 TCCR1A = _BV(COM1x1) | _BV(WGM11); // = 0b00001010; 633 TCCR1A = _BV(COM1x1) | _BV(WGM11); // = 0b00001010;
555 TCCR1B = _BV(WGM13) | _BV(WGM12) | _BV(CS10); // = 0b00011001; 634 TCCR1B = _BV(WGM13) | _BV(WGM12) | _BV(CS10); // = 0b00011001;
635 #endif
556 636
557 backlight_init(); 637 backlight_init();
558 #ifdef BACKLIGHT_BREATHING 638 #ifdef BACKLIGHT_BREATHING
@@ -564,24 +644,43 @@ __attribute__ ((weak))
564void backlight_set(uint8_t level) 644void backlight_set(uint8_t level)
565{ 645{
566 // Prevent backlight blink on lowest level 646 // Prevent backlight blink on lowest level
567 // PORTx &= ~n 647 #if BACKLIGHT_ON_STATE == 0
568 _SFR_IO8((backlight_pin >> 4) + 2) &= ~_BV(backlight_pin & 0xF); 648 // PORTx &= ~n
649 _SFR_IO8((backlight_pin >> 4) + 2) &= ~_BV(backlight_pin & 0xF);
650 #else
651 // PORTx |= n
652 _SFR_IO8((backlight_pin >> 4) + 2) |= _BV(backlight_pin & 0xF);
653 #endif
569 654
570 if ( level == 0 ) { 655 if ( level == 0 ) {
571 // Turn off PWM control on backlight pin, revert to output low. 656 #ifndef NO_BACKLIGHT_CLOCK
572 TCCR1A &= ~(_BV(COM1x1)); 657 // Turn off PWM control on backlight pin, revert to output low.
573 OCR1x = 0x0; 658 TCCR1A &= ~(_BV(COM1x1));
574 } else if ( level == BACKLIGHT_LEVELS ) { 659 OCR1x = 0x0;
575 // Turn on PWM control of backlight pin 660 #else
576 TCCR1A |= _BV(COM1x1); 661 #if BACKLIGHT_ON_STATE == 0
577 // Set the brightness 662 // PORTx |= n
578 OCR1x = 0xFFFF; 663 _SFR_IO8((backlight_pin >> 4) + 2) |= _BV(backlight_pin & 0xF);
579 } else { 664 #else
580 // Turn on PWM control of backlight pin 665 // PORTx &= ~n
581 TCCR1A |= _BV(COM1x1); 666 _SFR_IO8((backlight_pin >> 4) + 2) &= ~_BV(backlight_pin & 0xF);
582 // Set the brightness 667 #endif
583 OCR1x = 0xFFFF >> ((BACKLIGHT_LEVELS - level) * ((BACKLIGHT_LEVELS + 1) / 2)); 668 #endif
584 } 669 }
670 #ifndef NO_BACKLIGHT_CLOCK
671 else if ( level == BACKLIGHT_LEVELS ) {
672 // Turn on PWM control of backlight pin
673 TCCR1A |= _BV(COM1x1);
674 // Set the brightness
675 OCR1x = 0xFFFF;
676 }
677 else {
678 // Turn on PWM control of backlight pin
679 TCCR1A |= _BV(COM1x1);
680 // Set the brightness
681 OCR1x = 0xFFFF >> ((BACKLIGHT_LEVELS - level) * ((BACKLIGHT_LEVELS + 1) / 2));
682 }
683 #endif
585 684
586 #ifdef BACKLIGHT_BREATHING 685 #ifdef BACKLIGHT_BREATHING
587 breathing_intensity_default(); 686 breathing_intensity_default();