瀏覽代碼

Merge branch 'breathing_led'

akb96
Kai Ryu 10 年之前
父節點
當前提交
f6bfbd0f48
共有 7 個文件被更改,包括 216 次插入70 次删除
  1. 5
    0
      common.mk
  2. 73
    0
      common/breathing_led.c
  3. 25
    0
      common/breathing_led.h
  4. 4
    0
      common/keyboard.c
  5. 1
    1
      keyboard/gh60/Makefile
  6. 102
    67
      keyboard/gh60/backlight.c
  7. 6
    2
      keyboard/gh60/config.h

+ 5
- 0
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

+ 73
- 0
common/breathing_led.c 查看文件

@@ -0,0 +1,73 @@
#include <avr/io.h>
#include <avr/interrupt.h>
#include "led.h"
#include "breathing_led.h"
#include "debug.h"

#define BREATHING_LED_TIMER_TOP F_CPU/256

static uint8_t breathing_led_duration = 0;

void breathing_led_init(void)
{
/* Timer3 setup */
/* CTC mode */
TCCR3B |= (1<<WGM32);
/* Clock selelct: clk/8 */
TCCR3B |= (1<<CS30);
/* Set TOP value */
uint8_t sreg = SREG;
cli();
OCR3AH = (BREATHING_LED_TIMER_TOP>>8)&0xff;
OCR3AL = BREATHING_LED_TIMER_TOP&0xff;
SREG = sreg;
}

void breathing_led_enable(void)
{
/* Enable Compare Match Interrupt */
TIMSK3 |= (1<<OCIE3A);
dprintf("breathing led on: %u\n", TIMSK3 & (1<<OCIE3A));
}

void breathing_led_disable(void)
{
/* Disable Compare Match Interrupt */
TIMSK3 &= ~(1<<OCIE3A);
dprintf("breathing led off: %u\n", TIMSK3 & (1<<OCIE3A));
}

void breathing_led_toggle(void)
{
/* Disable Compare Match Interrupt */
TIMSK3 ^= (1<<OCIE3A);
dprintf("breathing led toggle: %u\n", TIMSK3 & (1<<OCIE3A));
}

void breathing_led_set_duration(uint8_t dur)
{
breathing_led_duration = dur;
dprintf("breathing led set duration: %u\n", breathing_led_duration);
}

/* Breathing LED brighness(PWM On period) table
*
* 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[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)
{
static uint8_t index = 0;
static uint8_t step = 0;
step++;
if (step > breathing_led_duration) {
step = 0;
breathing_led_set_raw(pgm_read_byte(&breathing_table[index]));
index++;
}
}

+ 25
- 0
common/breathing_led.h 查看文件

@@ -0,0 +1,25 @@
#ifndef BREATHING_LED_H
#define BREATHING_LED_H


#ifdef BREATHING_LED_ENABLE

void breathing_led_init(void);
void breathing_led_enable(void);
void breathing_led_disable(void);
void breathing_led_toggle(void);
void breathing_led_set_duration(uint8_t dur);
void breathing_led_set_raw(uint8_t raw);

#else

#define breathing_led_init()
#define breathing_led_enable()
#define breathing_led_disable()
#define breathing_led_toggle()
#define breathing_led_set_duration()
#define breathing_led_set_raw()

#endif

#endif

+ 4
- 0
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

+ 1
- 1
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

+ 102
- 67
keyboard/gh60/backlight.c 查看文件

@@ -19,98 +19,133 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include "backlight.h"
#include "breathing_led.h"

#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(GH60_REV_CHN)
static const uint8_t backlight_table[] PROGMEM = {
0, 16, 128, 255
};

/* Backlight pin configuration
* PWM: PB7
* PWM: PB6 (Rev.CHN)
* GPIO: PF7 PF6 PF5 PF4 (Rev.B)
*/
void backlight_set(uint8_t level)
void backlight_enable(void)
{
if (level > 0) {
// Turn on PWM
cli();
DDRB |= (1<<PB6);
TCCR1A |= ( (1<<WGM10) | (1<<COM1B1) );
TCCR1B |= ( (1<<CS11) | (1<<CS10) );
sei();
// Set PWM
OCR1B = pgm_read_byte(&backlight_table[level]);
}
else {
// Turn off PWM
cli();
DDRB &= ~(1<<PB6);
TCCR1A &= ~( (1<<WGM10) | (1<<COM1B1) );
TCCR1B &= ~( (1<<CS11) | (1<<CS10) );
sei();
// Set PWM
OCR1B = 0;
}
#if defined(GH60_REV_CHN)
// Turn on PWM
DDRB |= (1<<PB6);
cli();
TCCR1A |= ((1<<WGM10) | (1<<COM1B1));
TCCR1B |= ((1<<CS11) | (1<<CS10));
sei();
#else
DDRF |= (1<<PF7 | 1<<PF6 | 1<<PF5 | 1<<PF4);
PORTF |= (1<<PF7 | 1<<PF6 | 1<<PF5 | 1<<PF4);
cli();
TCCR1B |= (1<<WGM12);
TCCR1B |= (1<<CS10);
OCR1AH = (SOFTPWM_TIMER_TOP>>8)&0xff;
OCR1AL = SOFTPWM_TIMER_TOP&0xff;
TIMSK1 |= (1<<OCIE1A);
sei();
#endif
}
#elif defined(GH60_REV_CNY)
static const uint8_t backlight_table[] PROGMEM = {
0, 16, 128, 255
};

void backlight_set(uint8_t level)
void backlight_disable(void)
{
#ifdef LED_MATRIX_ENABLE
if (level > 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 off PWM
cli();
DDRB &= ~(1<<PB6);
TCCR1A &= ~( (1<<WGM10) | (1<<COM1B1) );
TCCR1B &= ~( (1<<CS11) | (1<<CS10) );
sei();
OCR1B = 0;
#else
DDRF &= ~(1<<PF7 | 1<<PF6 | 1<<PF5 | 1<<PF4);
cli();
TCCR1B &= ~(1<<WGM12);
TCCR1B &= ~(1<<CS10);
TIMSK1 &= ~(1<<OCIE1A);
sei();
OCR1A = 0;
#endif
}
#else
static const uint8_t backlight_table[] PROGMEM = {
0, 16, 128, 255
};

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) {
DDRF |= (1<<PF7 | 1<<PF6 | 1<<PF5 | 1<<PF4);
PORTF |= (1<<PF7 | 1<<PF6 | 1<<PF5 | 1<<PF4);
cli();
TCCR1A |= (1<<WGM10);
TCCR1B |= ((1<<CS11) | (1<<CS10));
TIMSK1 |= ((1<<OCIE1A) | (1<<TOIE1));
TIFR1 |= (1<<TOV1);
sei();
OCR1A = pgm_read_byte(&backlight_table[level]);
backlight_enable();
backlight_set_raw(pgm_read_byte(&backlight_table[level]));
}
else {
DDRF &= ~(1<<PF7 | 1<<PF6 | 1<<PF5 | 1<<PF4);
cli();
TCCR1A &= ~(1<<WGM10);
TCCR1B &= ~((1<<CS11) | (1<<CS10));
TIMSK1 |= ((1<<OCIE1A) | (1<<TOIE1));
TIFR1 |= (1<<TOV1);
sei();
OCR1A = 0;
backlight_disable();
}
#endif
}

ISR(TIMER1_COMPA_vect)
#ifdef BREATHING_LED_ENABLE
void breathing_led_set_raw(uint8_t raw)
{
// LED off
PORTF |= (1<<PF7 | 1<<PF6 | 1<<PF5 | 1<<PF4);
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
}
ISR(TIMER1_OVF_vect)

#if defined(GH60_REV_CHN)
#else
ISR(TIMER1_COMPA_vect)
{
static uint8_t pwm = 0;
pwm++;
// LED on
PORTF &= ~(1<<PF7 | 1<<PF6 | 1<<PF5 | 1<<PF4);
if (pwm == 0) {
//PORTB |= (1<<PB6);
PORTF &= ~(1<<PF7 | 1<<PF6 | 1<<PF5 | 1<<PF4);
softpwm_ocr = softpwm_ocr_buff;
}
// LED off
if (pwm == softpwm_ocr) {
//PORTB &= ~(1<<PB6);
PORTF |= (1<<PF7 | 1<<PF6 | 1<<PF5 | 1<<PF4);
}
}
#endif

+ 6
- 2
keyboard/gh60/config.h 查看文件

@@ -42,11 +42,15 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#define DEBOUNCE 5

/* number of backlight levels */
#ifdef BREATHING_LED_ENABLE
#define BACKLIGHT_LEVELS 6
#else
#define BACKLIGHT_LEVELS 3
#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 */