From 91b149f785b5554092e90d238012f161f20a0cff Mon Sep 17 00:00:00 2001 From: Kai Ryu Date: Tue, 15 Apr 2014 16:28:48 +0900 Subject: [PATCH 1/4] New branch for breathing led feature --- common.mk | 5 ++ common/backlight.c | 36 +++++++++++-- common/backlight.h | 3 ++ common/breathing_led.c | 106 ++++++++++++++++++++++++++++++++++++ common/breathing_led.h | 39 ++++++++++++++ common/eeconfig.c | 8 +++ common/eeconfig.h | 6 +++ keyboard/gh60/Makefile | 2 +- keyboard/gh60/backlight.c | 110 ++++++++++++++++---------------------- keyboard/gh60/config.h | 1 + 10 files changed, 246 insertions(+), 70 deletions(-) create mode 100644 common/breathing_led.c create mode 100644 common/breathing_led.h diff --git a/common.mk b/common.mk index bef00a3c..09561cba 100644 --- a/common.mk +++ b/common.mk @@ -54,6 +54,11 @@ ifdef SLEEP_LED_ENABLE OPT_DEFS += -DNO_SUSPEND_POWER_DOWN endif +ifdef BREATHING_LED_ENABLE + SRC += $(COMMON_DIR)/breathing_led.c + OPT_DEFS += -DBREATHING_LED_ENABLE +endif + ifdef BACKLIGHT_ENABLE SRC += $(COMMON_DIR)/backlight.c OPT_DEFS += -DBACKLIGHT_ENABLE diff --git a/common/backlight.c b/common/backlight.c index a0b2ce50..ee8169c6 100644 --- a/common/backlight.c +++ b/common/backlight.c @@ -16,6 +16,7 @@ along with this program. If not, see . */ #include "backlight.h" +#include "breathing_led.h" #include "eeconfig.h" #include "debug.h" @@ -33,7 +34,7 @@ void backlight_init(void) void backlight_increase(void) { -#ifdef BACKLIGHT_CUSTOM +#if defined(BACKLIGHT_CUSTOM) || defined(BREATHING_LED_ENABLE) if (backlight_config.enable) { if (backlight_config.level < BACKLIGHT_LEVELS) { backlight_config.level++; @@ -42,6 +43,9 @@ void backlight_increase(void) dprintf("backlight custom increase: %u\n", backlight_config.level); backlight_set(backlight_config.level); } +#ifdef BREATHING_LED_ENABLE + breathing_led_increase(); +#endif #else if(backlight_config.level < BACKLIGHT_LEVELS) { @@ -56,7 +60,7 @@ void backlight_increase(void) void backlight_decrease(void) { -#ifdef BACKLIGHT_CUSTOM +#if defined(BACKLIGHT_CUSTOM) || defined(BREATHING_LED_ENABLE) if (backlight_config.enable) { if(backlight_config.level > 1) { @@ -66,6 +70,9 @@ void backlight_decrease(void) dprintf("backlight custom decrease: %u\n", backlight_config.level); backlight_set(backlight_config.level); } +#ifdef BREATHING_LED_ENABLE + breathing_led_decrease(); +#endif #else if(backlight_config.level > 0) { @@ -80,16 +87,37 @@ void backlight_decrease(void) void backlight_toggle(void) { +#ifdef BREATHING_LED_ENABLE + if (breathing_led_is_enabled()) { + breathing_led_disable(); + backlight_disable(); + return; + } +#endif backlight_config.enable ^= 1; - if (backlight_config.enable) - { + if (backlight_config.enable) { +#if defined(BACKLIGHT_CUSTOM) || defined(BREATHING_LED_ENABLE) + backlight_enable(); +#endif if (backlight_config.level == 0) { backlight_config.level = 1; } } + else { +#ifndef BREATHING_LED_ENABLE +#ifdef BACKLIGHT_CUSTOM + backlight_disable(); +#endif +#endif + } eeconfig_write_backlight(backlight_config.raw); dprintf("backlight toggle: %u\n", backlight_config.enable); backlight_set(backlight_config.enable ? backlight_config.level : 0); +#ifdef BREATHING_LED_ENABLE + if (!backlight_config.enable) { + breathing_led_enable(); + } +#endif } void backlight_step(void) diff --git a/common/backlight.h b/common/backlight.h index 685c422a..a33c727a 100644 --- a/common/backlight.h +++ b/common/backlight.h @@ -36,6 +36,9 @@ void backlight_decrease(void); void backlight_toggle(void); void backlight_step(void); +void backlight_enable(void); +void backlight_disable(void); void backlight_set(uint8_t level); +void backlight_set_raw(uint8_t raw); #endif diff --git a/common/breathing_led.c b/common/breathing_led.c new file mode 100644 index 00000000..d71bf5b8 --- /dev/null +++ b/common/breathing_led.c @@ -0,0 +1,106 @@ +#include +#include +#include +#include "led.h" +#include "breathing_led.h" +#include "backlight.h" +#include "debug.h" + +#define BREATHING_LED_TIMER_TOP F_CPU/64 + +breathing_led_config_t breathing_led_config; + +void breathing_led_init(void) +{ + /* Timer3 setup */ + /* CTC mode */ + TCCR3B |= _BV(WGM12); + /* Clock selelct: clk/1 */ + TCCR3B |= _BV(CS10); + /* Set TOP value */ + uint8_t sreg = SREG; + cli(); + OCR3AH = (BREATHING_LED_TIMER_TOP>>8)&0xff; + OCR3AL = BREATHING_LED_TIMER_TOP&0xff; + SREG = sreg; +} + +bool breathing_led_is_enabled(void) +{ + return breathing_led_config.enable; +} + +void breathing_led_enable(void) +{ + /* Enable Compare Match Interrupt */ + TIMSK3 |= _BV(OCIE3A); + breathing_led_config.enable = true; + dprintf("breathing led on: %u\n", breathing_led_config.enable); + eeconfig_write_breathing_led(breathing_led_config.raw); +} + +void breathing_led_disable(void) +{ + /* Disable Compare Match Interrupt */ + TIMSK3 &= ~_BV(OCIE3A); + breathing_led_config.enable = false; + dprintf("breathing led off: %u\n", breathing_led_config.enable); + eeconfig_write_breathing_led(breathing_led_config.raw); +} + +void breathing_led_toggle(void) +{ + /* Disable Compare Match Interrupt */ + TIMSK3 ^= _BV(OCIE3A); + breathing_led_config.enable ^= 1; + dprintf("breathing led toggle: %u\n", breathing_led_config.enable); + eeconfig_write_breathing_led(breathing_led_config.raw); +} + +void breathing_led_increase(void) +{ + if (breathing_led_config.enable) { + if (breathing_led_config.level < BREATHING_LED_LEVELS) { + breathing_led_config.level++; + eeconfig_write_breathing_led(breathing_led_config.raw); + } + dprintf("breathing led speed increase: %u\n", breathing_led_config.level); + } +} + +void breathing_led_decrease(void) +{ + if (breathing_led_config.enable) { + if (breathing_led_config.level > 0) + { + breathing_led_config.level--; + eeconfig_write_breathing_led(breathing_led_config.raw); + } + dprintf("breathing led speed decrease: %u\n", breathing_led_config.level); + } +} + +/* Breathing LED brighness(PWM On period) table + * (64[steps] * 4[duration]) / 64[PWM periods/s] = 4 second breath cycle + * + * http://www.wolframalpha.com/input/?i=%28sin%28+x%2F64*pi%29**8+*+255%2C+x%3D0+to+63 + * (0..63).each {|x| p ((sin(x/64.0*PI)**8)*255).to_i } + */ +static const uint8_t breathing_table[64] PROGMEM = { +0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 4, 6, 10, +15, 23, 32, 44, 58, 74, 93, 113, 135, 157, 179, 199, 218, 233, 245, 252, +255, 252, 245, 233, 218, 199, 179, 157, 135, 113, 93, 74, 58, 44, 32, 23, +15, 10, 6, 4, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 +}; + +ISR(TIMER3_COMPA_vect) +{ + uint8_t index = 0; + uint8_t step = 0; + step++; + if (step >= BREATHING_LED_LEVELS - breathing_led_config.level) { + step = 0; + index++; + backlight_set_raw(pgm_read_byte(&breathing_table[index])); + } +} diff --git a/common/breathing_led.h b/common/breathing_led.h new file mode 100644 index 00000000..fb31668f --- /dev/null +++ b/common/breathing_led.h @@ -0,0 +1,39 @@ +#ifndef BREATHING_LED_H +#define BREATHING_LED_H + + +#ifdef BREATHING_LED_ENABLE + +#include +#include + +#ifndef BREATHING_LED_LEVEL +#define BREATHING_LED_LEVEL 1 +#endif + +typedef union { + uint8_t raw; + struct { + bool enable:1; + uint8_t level:7; + }; +} breathing_led_config_t; + +void breathing_led_init(void); +bool breathing_led_is_enabled(void); +void breathing_led_enable(void); +void breathing_led_disable(void); +void breathing_led_toggle(void); +void breathing_led_increase(void); +void breathing_led_decrease(void); + +#else + +#define breathing_led_init() +#define breathing_led_enable() +#define breathing_led_disable() +#define breathing_led_toggle() + +#endif + +#endif diff --git a/common/eeconfig.c b/common/eeconfig.c index 5aadf1c5..ab9bae3c 100644 --- a/common/eeconfig.c +++ b/common/eeconfig.c @@ -14,6 +14,9 @@ void eeconfig_init(void) #ifdef BACKLIGHT_ENABLE eeprom_write_byte(EECONFIG_BACKLIGHT, 0); #endif +#ifdef BREATHING_LED_ENABLE + eeprom_write_byte(EECONFIG_BREATHING_LED, 0); +#endif #ifdef KEYMAP_EX_ENABLE keymap_ex_init(); #endif @@ -50,3 +53,8 @@ void eeconfig_write_keymap(uint8_t val) { eeprom_write_byte(EECONFIG_KEYMAP, val uint8_t eeconfig_read_backlight(void) { return eeprom_read_byte(EECONFIG_BACKLIGHT); } void eeconfig_write_backlight(uint8_t val) { eeprom_write_byte(EECONFIG_BACKLIGHT, val); } #endif + +#ifdef BREATHING_LED_ENABLE +uint8_t eeconfig_read_breathing_led(void) { return eeprom_read_byte(EECONFIG_BREATHING_LED); } +void eeconfig_write_breathing_led(uint8_t val) { eeprom_write_byte(EECONFIG_BREATHING_LED, val); } +#endif diff --git a/common/eeconfig.h b/common/eeconfig.h index e1b5ae28..36c18143 100644 --- a/common/eeconfig.h +++ b/common/eeconfig.h @@ -31,6 +31,7 @@ along with this program. If not, see . #define EECONFIG_KEYMAP (uint8_t *)4 #define EECONFIG_MOUSEKEY_ACCEL (uint8_t *)5 #define EECONFIG_BACKLIGHT (uint8_t *)6 +#define EECONFIG_BREATHING_LED (uint8_t *)7 /* debug bit */ @@ -71,4 +72,9 @@ uint8_t eeconfig_read_backlight(void); void eeconfig_write_backlight(uint8_t val); #endif +#ifdef BREATHING_LED_ENABLE +uint8_t eeconfig_read_breathing_led(void); +void eeconfig_write_breathing_led(uint8_t val); +#endif + #endif diff --git a/keyboard/gh60/Makefile b/keyboard/gh60/Makefile index 5bdd7e46..56da7779 100644 --- a/keyboard/gh60/Makefile +++ b/keyboard/gh60/Makefile @@ -138,7 +138,7 @@ NKRO_ENABLE = yes # USB Nkey Rollover - not yet supported in LUFA BACKLIGHT_ENABLE = yes # Enable keyboard backlight functionality KEYMAP_EX_ENABLE = yes # External keymap in eeprom KEYMAP_SECTION_ENABLE = yes # Fixed address keymap for keymap editor -#LED_MATRIX_ENABLE = yes +BREATHING_LED_ENABLE = yes # Enable breathing backlight # Optimize size but this may cause error "relocation truncated to fit" #EXTRALDFLAGS = -Wl,--relax diff --git a/keyboard/gh60/backlight.c b/keyboard/gh60/backlight.c index bc524f6d..eabb1c34 100644 --- a/keyboard/gh60/backlight.c +++ b/keyboard/gh60/backlight.c @@ -20,7 +20,6 @@ along with this program. If not, see . #include #include "backlight.h" -#if defined(GH60_REV_CHN) static const uint8_t backlight_table[] PROGMEM = { 0, 16, 128, 255 }; @@ -28,81 +27,62 @@ static const uint8_t backlight_table[] PROGMEM = { /* Backlight pin configuration * PWM: PB7 */ -void backlight_set(uint8_t level) +void backlight_enable(void) { - if (level > 0) { - // Turn on PWM - cli(); - DDRB |= (1< 0) { - led_matrix_disable(); - for (uint8_t row = 0; row < LED_MATRIX_ROWS; row++) { - for (uint8_t col = 0; col < LED_MATRIX_COLS; col++) { - led_matrix_set_value(row, col, pgm_read_byte(&backlight_table[level])); - } - } - led_matrix_enable(); - } - else { - led_matrix_disable(); - } +#if defined(GH60_REV_CHN) + // Turn on PWM + cli(); + DDRB |= (1< 0) { - DDRF |= (1<. /* number of backlight levels */ #define BACKLIGHT_LEVELS 3 +#define BREATHING_LED_LEVELS 4 #ifdef GH60_REV_CNY # define LED_MATRIX_ROWS 6 From 3c8412b1e552d6b6a44f223444f9a15dcf8310fd Mon Sep 17 00:00:00 2001 From: Kai Ryu Date: Wed, 16 Apr 2014 12:12:15 +0900 Subject: [PATCH 2/4] Bug fix and optimization of breathing led --- common/breathing_led.c | 25 ++++------ common/keyboard.c | 4 ++ keyboard/gh60/backlight.c | 102 +++++++++++++++++++++++++++++++++++++- keyboard/gh60/config.h | 2 +- 4 files changed, 117 insertions(+), 16 deletions(-) diff --git a/common/breathing_led.c b/common/breathing_led.c index d71bf5b8..80bb9d59 100644 --- a/common/breathing_led.c +++ b/common/breathing_led.c @@ -6,7 +6,7 @@ #include "backlight.h" #include "debug.h" -#define BREATHING_LED_TIMER_TOP F_CPU/64 +#define BREATHING_LED_TIMER_TOP F_CPU/256 breathing_led_config_t breathing_led_config; @@ -14,9 +14,9 @@ void breathing_led_init(void) { /* Timer3 setup */ /* CTC mode */ - TCCR3B |= _BV(WGM12); - /* Clock selelct: clk/1 */ - TCCR3B |= _BV(CS10); + TCCR3B |= _BV(WGM32); + /* Clock selelct: clk/8 */ + TCCR3B |= _BV(CS30); /* Set TOP value */ uint8_t sreg = SREG; cli(); @@ -81,22 +81,19 @@ void breathing_led_decrease(void) } /* Breathing LED brighness(PWM On period) table - * (64[steps] * 4[duration]) / 64[PWM periods/s] = 4 second breath cycle * - * http://www.wolframalpha.com/input/?i=%28sin%28+x%2F64*pi%29**8+*+255%2C+x%3D0+to+63 - * (0..63).each {|x| p ((sin(x/64.0*PI)**8)*255).to_i } + * http://www.wolframalpha.com/input/?i=Table%5Bfloor%28%28exp%28sin%28x%2F256*2*pi%2B3%2F2*pi%29%29-1%2Fe%29*%28256%2F%28e-1%2Fe%29%29%29%2C+%7Bx%2C0%2C255%2C1%7D%5D + * Table[floor((exp(sin(x/256*2*pi+3/2*pi))-1/e)*(256/(e-1/e))), {x,0,255,1}] + * (0..255).each {|x| print ((exp(sin(x/256.0*2*PI+3.0/2*PI))-1/E)*(256/(E-1/E))).to_i, ', ' } */ -static const uint8_t breathing_table[64] PROGMEM = { -0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 4, 6, 10, -15, 23, 32, 44, 58, 74, 93, 113, 135, 157, 179, 199, 218, 233, 245, 252, -255, 252, 245, 233, 218, 199, 179, 157, 135, 113, 93, 74, 58, 44, 32, 23, -15, 10, 6, 4, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 +static const uint8_t breathing_table[256] PROGMEM = { +0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 8, 8, 9, 10, 11, 11, 12, 13, 14, 15, 16, 17, 18, 19, 21, 22, 23, 25, 26, 27, 29, 30, 32, 34, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53, 56, 58, 61, 63, 66, 68, 71, 74, 77, 80, 83, 86, 89, 92, 95, 98, 102, 105, 108, 112, 116, 119, 123, 126, 130, 134, 138, 142, 145, 149, 153, 157, 161, 165, 169, 173, 176, 180, 184, 188, 192, 195, 199, 203, 206, 210, 213, 216, 219, 223, 226, 228, 231, 234, 236, 239, 241, 243, 245, 247, 248, 250, 251, 252, 253, 254, 255, 255, 255, 255, 255, 255, 255, 254, 253, 252, 251, 250, 248, 247, 245, 243, 241, 239, 236, 234, 231, 228, 226, 223, 219, 216, 213, 210, 206, 203, 199, 195, 192, 188, 184, 180, 176, 173, 169, 165, 161, 157, 153, 149, 145, 142, 138, 134, 130, 126, 123, 119, 116, 112, 108, 105, 102, 98, 95, 92, 89, 86, 83, 80, 77, 74, 71, 68, 66, 63, 61, 58, 56, 53, 51, 49, 47, 45, 43, 41, 39, 37, 35, 34, 32, 30, 29, 27, 26, 25, 23, 22, 21, 19, 18, 17, 16, 15, 14, 13, 12, 11, 11, 10, 9, 8, 8, 7, 6, 6, 5, 5, 4, 4, 3, 3, 2, 2, 2, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, }; ISR(TIMER3_COMPA_vect) { - uint8_t index = 0; - uint8_t step = 0; + static uint8_t index = 0; + static uint8_t step = 0; step++; if (step >= BREATHING_LED_LEVELS - breathing_led_config.level) { step = 0; diff --git a/common/keyboard.c b/common/keyboard.c index 933eb780..29155cfe 100644 --- a/common/keyboard.c +++ b/common/keyboard.c @@ -81,6 +81,10 @@ void keyboard_init(void) backlight_init(); #endif +#ifdef BREATHING_LED_ENABLE + breathing_led_init(); +#endif + #ifdef KEYMAP_EX_ENABLE keymap_ex_init(); #endif diff --git a/keyboard/gh60/backlight.c b/keyboard/gh60/backlight.c index eabb1c34..62a6e2ca 100644 --- a/keyboard/gh60/backlight.c +++ b/keyboard/gh60/backlight.c @@ -19,13 +19,15 @@ along with this program. If not, see . #include #include #include "backlight.h" +#include "debug.h" +#if defined(BREATHING_LED_ENABLE) || defined(BACKLIGHT_CUSTOM) static const uint8_t backlight_table[] PROGMEM = { 0, 16, 128, 255 }; /* Backlight pin configuration - * PWM: PB7 + * PWM: PB6 */ void backlight_enable(void) { @@ -79,7 +81,11 @@ void backlight_set(uint8_t level) void backlight_set_raw(uint8_t raw) { +#if defined(GH60_REV_CHN) + OCR1B = raw; +#else OCR1A = raw; +#endif } #ifndef GH60_REV_CHN @@ -94,3 +100,97 @@ ISR(TIMER1_OVF_vect) PORTF &= ~(1< 0) { + // Turn on PWM + cli(); + DDRB |= (1< 0) { + led_matrix_disable(); + for (uint8_t row = 0; row < LED_MATRIX_ROWS; row++) { + for (uint8_t col = 0; col < LED_MATRIX_COLS; col++) { + led_matrix_set_value(row, col, pgm_read_byte(&backlight_table[level])); + } + } + led_matrix_enable(); + } + else { + led_matrix_disable(); + } +} +#else +static const uint8_t backlight_table[] PROGMEM = { + 0, 16, 128, 255 +}; + +void backlight_set(uint8_t level) +{ + if (level > 0) { + DDRF |= (1<. /* number of backlight levels */ #define BACKLIGHT_LEVELS 3 -#define BREATHING_LED_LEVELS 4 +#define BREATHING_LED_LEVELS 5 #ifdef GH60_REV_CNY # define LED_MATRIX_ROWS 6 From e26869d70ac3e78d3c07d312c8f07b190b0c3325 Mon Sep 17 00:00:00 2001 From: Kai Ryu Date: Fri, 18 Apr 2014 12:12:59 +0900 Subject: [PATCH 3/4] Change software pwm method to fix blink issue --- common/breathing_led.c | 2 +- keyboard/gh60/backlight.c | 58 +++++++++++++++++++++++++++++---------- 2 files changed, 44 insertions(+), 16 deletions(-) diff --git a/common/breathing_led.c b/common/breathing_led.c index 80bb9d59..497f9a6d 100644 --- a/common/breathing_led.c +++ b/common/breathing_led.c @@ -97,7 +97,7 @@ ISR(TIMER3_COMPA_vect) step++; if (step >= BREATHING_LED_LEVELS - breathing_led_config.level) { step = 0; - index++; backlight_set_raw(pgm_read_byte(&breathing_table[index])); + index++; } } diff --git a/keyboard/gh60/backlight.c b/keyboard/gh60/backlight.c index 62a6e2ca..9b9c190b 100644 --- a/keyboard/gh60/backlight.c +++ b/keyboard/gh60/backlight.c @@ -21,6 +21,12 @@ along with this program. If not, see . #include "backlight.h" #include "debug.h" +#ifndef GH60_REV_CHN +#define SOFTPWM_TIMER_TOP F_CPU/(256*64) +uint8_t softpwm_ocr = 0; +uint8_t softpwm_ocr_buff = 0; +#endif + #if defined(BREATHING_LED_ENABLE) || defined(BACKLIGHT_CUSTOM) static const uint8_t backlight_table[] PROGMEM = { 0, 16, 128, 255 @@ -33,19 +39,20 @@ void backlight_enable(void) { #if defined(GH60_REV_CHN) // Turn on PWM - cli(); DDRB |= (1<>8)&0xff; + OCR1AL = SOFTPWM_TIMER_TOP&0xff; + TIMSK1 |= (1< 15) { + PORTF &= ~(1< Date: Fri, 18 Apr 2014 14:33:57 +0900 Subject: [PATCH 4/4] Simplifiy breathing led feature and code --- common/backlight.c | 36 +-------- common/backlight.h | 3 - common/breathing_led.c | 60 ++++----------- common/breathing_led.h | 22 +----- common/eeconfig.c | 8 -- common/eeconfig.h | 6 -- keyboard/gh60/backlight.c | 155 ++++++++++---------------------------- keyboard/gh60/config.h | 9 ++- 8 files changed, 70 insertions(+), 229 deletions(-) diff --git a/common/backlight.c b/common/backlight.c index ee8169c6..a0b2ce50 100644 --- a/common/backlight.c +++ b/common/backlight.c @@ -16,7 +16,6 @@ along with this program. If not, see . */ #include "backlight.h" -#include "breathing_led.h" #include "eeconfig.h" #include "debug.h" @@ -34,7 +33,7 @@ void backlight_init(void) void backlight_increase(void) { -#if defined(BACKLIGHT_CUSTOM) || defined(BREATHING_LED_ENABLE) +#ifdef BACKLIGHT_CUSTOM if (backlight_config.enable) { if (backlight_config.level < BACKLIGHT_LEVELS) { backlight_config.level++; @@ -43,9 +42,6 @@ void backlight_increase(void) dprintf("backlight custom increase: %u\n", backlight_config.level); backlight_set(backlight_config.level); } -#ifdef BREATHING_LED_ENABLE - breathing_led_increase(); -#endif #else if(backlight_config.level < BACKLIGHT_LEVELS) { @@ -60,7 +56,7 @@ void backlight_increase(void) void backlight_decrease(void) { -#if defined(BACKLIGHT_CUSTOM) || defined(BREATHING_LED_ENABLE) +#ifdef BACKLIGHT_CUSTOM if (backlight_config.enable) { if(backlight_config.level > 1) { @@ -70,9 +66,6 @@ void backlight_decrease(void) dprintf("backlight custom decrease: %u\n", backlight_config.level); backlight_set(backlight_config.level); } -#ifdef BREATHING_LED_ENABLE - breathing_led_decrease(); -#endif #else if(backlight_config.level > 0) { @@ -87,37 +80,16 @@ void backlight_decrease(void) void backlight_toggle(void) { -#ifdef BREATHING_LED_ENABLE - if (breathing_led_is_enabled()) { - breathing_led_disable(); - backlight_disable(); - return; - } -#endif backlight_config.enable ^= 1; - if (backlight_config.enable) { -#if defined(BACKLIGHT_CUSTOM) || defined(BREATHING_LED_ENABLE) - backlight_enable(); -#endif + if (backlight_config.enable) + { if (backlight_config.level == 0) { backlight_config.level = 1; } } - else { -#ifndef BREATHING_LED_ENABLE -#ifdef BACKLIGHT_CUSTOM - backlight_disable(); -#endif -#endif - } eeconfig_write_backlight(backlight_config.raw); dprintf("backlight toggle: %u\n", backlight_config.enable); backlight_set(backlight_config.enable ? backlight_config.level : 0); -#ifdef BREATHING_LED_ENABLE - if (!backlight_config.enable) { - breathing_led_enable(); - } -#endif } void backlight_step(void) diff --git a/common/backlight.h b/common/backlight.h index a33c727a..685c422a 100644 --- a/common/backlight.h +++ b/common/backlight.h @@ -36,9 +36,6 @@ void backlight_decrease(void); void backlight_toggle(void); void backlight_step(void); -void backlight_enable(void); -void backlight_disable(void); void backlight_set(uint8_t level); -void backlight_set_raw(uint8_t raw); #endif diff --git a/common/breathing_led.c b/common/breathing_led.c index 497f9a6d..4e4822f8 100644 --- a/common/breathing_led.c +++ b/common/breathing_led.c @@ -1,22 +1,20 @@ #include #include -#include #include "led.h" #include "breathing_led.h" -#include "backlight.h" #include "debug.h" #define BREATHING_LED_TIMER_TOP F_CPU/256 -breathing_led_config_t breathing_led_config; +static uint8_t breathing_led_duration = 0; void breathing_led_init(void) { /* Timer3 setup */ /* CTC mode */ - TCCR3B |= _BV(WGM32); + TCCR3B |= (1< 0) - { - breathing_led_config.level--; - eeconfig_write_breathing_led(breathing_led_config.raw); - } - dprintf("breathing led speed decrease: %u\n", breathing_led_config.level); - } + breathing_led_duration = dur; + dprintf("breathing led set duration: %u\n", breathing_led_duration); } /* Breathing LED brighness(PWM On period) table @@ -87,7 +57,7 @@ void breathing_led_decrease(void) * (0..255).each {|x| print ((exp(sin(x/256.0*2*PI+3.0/2*PI))-1/E)*(256/(E-1/E))).to_i, ', ' } */ static const uint8_t breathing_table[256] PROGMEM = { -0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 8, 8, 9, 10, 11, 11, 12, 13, 14, 15, 16, 17, 18, 19, 21, 22, 23, 25, 26, 27, 29, 30, 32, 34, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53, 56, 58, 61, 63, 66, 68, 71, 74, 77, 80, 83, 86, 89, 92, 95, 98, 102, 105, 108, 112, 116, 119, 123, 126, 130, 134, 138, 142, 145, 149, 153, 157, 161, 165, 169, 173, 176, 180, 184, 188, 192, 195, 199, 203, 206, 210, 213, 216, 219, 223, 226, 228, 231, 234, 236, 239, 241, 243, 245, 247, 248, 250, 251, 252, 253, 254, 255, 255, 255, 255, 255, 255, 255, 254, 253, 252, 251, 250, 248, 247, 245, 243, 241, 239, 236, 234, 231, 228, 226, 223, 219, 216, 213, 210, 206, 203, 199, 195, 192, 188, 184, 180, 176, 173, 169, 165, 161, 157, 153, 149, 145, 142, 138, 134, 130, 126, 123, 119, 116, 112, 108, 105, 102, 98, 95, 92, 89, 86, 83, 80, 77, 74, 71, 68, 66, 63, 61, 58, 56, 53, 51, 49, 47, 45, 43, 41, 39, 37, 35, 34, 32, 30, 29, 27, 26, 25, 23, 22, 21, 19, 18, 17, 16, 15, 14, 13, 12, 11, 11, 10, 9, 8, 8, 7, 6, 6, 5, 5, 4, 4, 3, 3, 2, 2, 2, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, +0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 8, 8, 9, 10, 11, 11, 12, 13, 14, 15, 16, 17, 18, 19, 21, 22, 23, 25, 26, 27, 29, 30, 32, 34, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53, 56, 58, 61, 63, 66, 68, 71, 74, 77, 80, 83, 86, 89, 92, 95, 98, 102, 105, 108, 112, 116, 119, 123, 126, 130, 134, 138, 142, 145, 149, 153, 157, 161, 165, 169, 173, 176, 180, 184, 188, 192, 195, 199, 203, 206, 210, 213, 216, 219, 223, 226, 228, 231, 234, 236, 239, 241, 243, 245, 247, 248, 250, 251, 252, 253, 254, 255, 255, 255, 255, 255, 255, 255, 254, 253, 252, 251, 250, 248, 247, 245, 243, 241, 239, 236, 234, 231, 228, 226, 223, 219, 216, 213, 210, 206, 203, 199, 195, 192, 188, 184, 180, 176, 173, 169, 165, 161, 157, 153, 149, 145, 142, 138, 134, 130, 126, 123, 119, 116, 112, 108, 105, 102, 98, 95, 92, 89, 86, 83, 80, 77, 74, 71, 68, 66, 63, 61, 58, 56, 53, 51, 49, 47, 45, 43, 41, 39, 37, 35, 34, 32, 30, 29, 27, 26, 25, 23, 22, 21, 19, 18, 17, 16, 15, 14, 13, 12, 11, 11, 10, 9, 8, 8, 7, 6, 6, 5, 5, 4, 4, 3, 3, 2, 2, 2, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; ISR(TIMER3_COMPA_vect) @@ -95,9 +65,9 @@ ISR(TIMER3_COMPA_vect) static uint8_t index = 0; static uint8_t step = 0; step++; - if (step >= BREATHING_LED_LEVELS - breathing_led_config.level) { + if (step > breathing_led_duration) { step = 0; - backlight_set_raw(pgm_read_byte(&breathing_table[index])); + breathing_led_set_raw(pgm_read_byte(&breathing_table[index])); index++; } } diff --git a/common/breathing_led.h b/common/breathing_led.h index fb31668f..b3a9e775 100644 --- a/common/breathing_led.h +++ b/common/breathing_led.h @@ -4,28 +4,12 @@ #ifdef BREATHING_LED_ENABLE -#include -#include - -#ifndef BREATHING_LED_LEVEL -#define BREATHING_LED_LEVEL 1 -#endif - -typedef union { - uint8_t raw; - struct { - bool enable:1; - uint8_t level:7; - }; -} breathing_led_config_t; - void breathing_led_init(void); -bool breathing_led_is_enabled(void); void breathing_led_enable(void); void breathing_led_disable(void); void breathing_led_toggle(void); -void breathing_led_increase(void); -void breathing_led_decrease(void); +void breathing_led_set_duration(uint8_t dur); +void breathing_led_set_raw(uint8_t raw); #else @@ -33,6 +17,8 @@ void breathing_led_decrease(void); #define breathing_led_enable() #define breathing_led_disable() #define breathing_led_toggle() +#define breathing_led_set_duration() +#define breathing_led_set_raw() #endif diff --git a/common/eeconfig.c b/common/eeconfig.c index ab9bae3c..5aadf1c5 100644 --- a/common/eeconfig.c +++ b/common/eeconfig.c @@ -14,9 +14,6 @@ void eeconfig_init(void) #ifdef BACKLIGHT_ENABLE eeprom_write_byte(EECONFIG_BACKLIGHT, 0); #endif -#ifdef BREATHING_LED_ENABLE - eeprom_write_byte(EECONFIG_BREATHING_LED, 0); -#endif #ifdef KEYMAP_EX_ENABLE keymap_ex_init(); #endif @@ -53,8 +50,3 @@ void eeconfig_write_keymap(uint8_t val) { eeprom_write_byte(EECONFIG_KEYMAP, val uint8_t eeconfig_read_backlight(void) { return eeprom_read_byte(EECONFIG_BACKLIGHT); } void eeconfig_write_backlight(uint8_t val) { eeprom_write_byte(EECONFIG_BACKLIGHT, val); } #endif - -#ifdef BREATHING_LED_ENABLE -uint8_t eeconfig_read_breathing_led(void) { return eeprom_read_byte(EECONFIG_BREATHING_LED); } -void eeconfig_write_breathing_led(uint8_t val) { eeprom_write_byte(EECONFIG_BREATHING_LED, val); } -#endif diff --git a/common/eeconfig.h b/common/eeconfig.h index 36c18143..e1b5ae28 100644 --- a/common/eeconfig.h +++ b/common/eeconfig.h @@ -31,7 +31,6 @@ along with this program. If not, see . #define EECONFIG_KEYMAP (uint8_t *)4 #define EECONFIG_MOUSEKEY_ACCEL (uint8_t *)5 #define EECONFIG_BACKLIGHT (uint8_t *)6 -#define EECONFIG_BREATHING_LED (uint8_t *)7 /* debug bit */ @@ -72,9 +71,4 @@ uint8_t eeconfig_read_backlight(void); void eeconfig_write_backlight(uint8_t val); #endif -#ifdef BREATHING_LED_ENABLE -uint8_t eeconfig_read_breathing_led(void); -void eeconfig_write_breathing_led(uint8_t val); -#endif - #endif diff --git a/keyboard/gh60/backlight.c b/keyboard/gh60/backlight.c index 9b9c190b..4c96d041 100644 --- a/keyboard/gh60/backlight.c +++ b/keyboard/gh60/backlight.c @@ -19,21 +19,22 @@ along with this program. If not, see . #include #include #include "backlight.h" -#include "debug.h" +#include "breathing_led.h" -#ifndef GH60_REV_CHN +#ifdef GH60_REV_CHN +#else #define SOFTPWM_TIMER_TOP F_CPU/(256*64) uint8_t softpwm_ocr = 0; uint8_t softpwm_ocr_buff = 0; #endif -#if defined(BREATHING_LED_ENABLE) || defined(BACKLIGHT_CUSTOM) static const uint8_t backlight_table[] PROGMEM = { 0, 16, 128, 255 }; /* Backlight pin configuration - * PWM: PB6 + * PWM: PB6 (Rev.CHN) + * GPIO: PF7 PF6 PF5 PF4 (Rev.B) */ void backlight_enable(void) { @@ -80,16 +81,50 @@ void backlight_disable(void) void backlight_set(uint8_t level) { +#ifdef BREATHING_LED_ENABLE + switch (level) { + case 1: + case 2: + case 3: + backlight_enable(); + breathing_led_disable(); + backlight_set_raw(pgm_read_byte(&backlight_table[level])); + break; + case 4: + case 5: + case 6: + backlight_enable(); + breathing_led_enable(); + breathing_led_set_duration(6 - level); + break; + case 0: + default: + breathing_led_disable(); + backlight_disable(); + break; + } +#else if (level > 0) { + backlight_enable(); backlight_set_raw(pgm_read_byte(&backlight_table[level])); } + else { + backlight_disable(); + } +#endif } -void backlight_set_raw(uint8_t raw) +#ifdef BREATHING_LED_ENABLE +void breathing_led_set_raw(uint8_t raw) +{ + backlight_set_raw(raw); +} +#endif + +inline void backlight_set_raw(uint8_t raw) { #if defined(GH60_REV_CHN) OCR1B = raw; - #else softpwm_ocr_buff = raw; #endif @@ -113,112 +148,4 @@ ISR(TIMER1_COMPA_vect) PORTF |= (1< 15) { - PORTF &= ~(1< 0) { - // Turn on PWM - cli(); - DDRB |= (1< 0) { - led_matrix_disable(); - for (uint8_t row = 0; row < LED_MATRIX_ROWS; row++) { - for (uint8_t col = 0; col < LED_MATRIX_COLS; col++) { - led_matrix_set_value(row, col, pgm_read_byte(&backlight_table[level])); - } - } - led_matrix_enable(); - } - else { - led_matrix_disable(); - } -} -#else -static const uint8_t backlight_table[] PROGMEM = { - 0, 16, 128, 255 -}; - -void backlight_set(uint8_t level) -{ - if (level > 0) { - DDRF |= (1<. #define DEBOUNCE 5 /* number of backlight levels */ +#ifdef BREATHING_LED_ENABLE +#define BACKLIGHT_LEVELS 6 +#else #define BACKLIGHT_LEVELS 3 -#define BREATHING_LED_LEVELS 5 +#endif #ifdef GH60_REV_CNY -# define LED_MATRIX_ROWS 6 -# define LED_MATRIX_COLS 14 +#define LED_MATRIX_ROWS 6 +#define LED_MATRIX_COLS 14 #endif /* Mechanical locking support. Use KC_LCAP, KC_LNUM or KC_LSCR instead in keymap */