Browse Source

Bug fix and optimization of breathing led

yuk86
Kai Ryu 10 years ago
parent
commit
3c8412b1e5
4 changed files with 117 additions and 16 deletions
  1. 11
    14
      common/breathing_led.c
  2. 4
    0
      common/keyboard.c
  3. 101
    1
      keyboard/gh60/backlight.c
  4. 1
    1
      keyboard/gh60/config.h

+ 11
- 14
common/breathing_led.c View File

@@ -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;

+ 4
- 0
common/keyboard.c View File

@@ -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

+ 101
- 1
keyboard/gh60/backlight.c View File

@@ -19,13 +19,15 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#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<<PF7 | 1<<PF6 | 1<<PF5 | 1<<PF4);
}
#endif
#else
#if defined(GH60_REV_CHN)
static const uint8_t backlight_table[] PROGMEM = {
0, 16, 128, 255
};

/* Backlight pin configuration
* PWM: PB7
*/
void backlight_set(uint8_t level)
{
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;
}
}
#elif #defined(GH60_REV_CNY)
static const uint8_t backlight_table[] PROGMEM = {
0, 16, 128, 255
};

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

void backlight_set(uint8_t level)
{
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]);
}
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;
}
}

ISR(TIMER1_COMPA_vect)
{
// LED off
PORTF |= (1<<PF7 | 1<<PF6 | 1<<PF5 | 1<<PF4);
}
ISR(TIMER1_OVF_vect)
{
// LED on
PORTF &= ~(1<<PF7 | 1<<PF6 | 1<<PF5 | 1<<PF4);
}
#endif
#endif

+ 1
- 1
keyboard/gh60/config.h View File

@@ -43,7 +43,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.

/* 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