1
0

Simplifiy breathing led feature and code

This commit is contained in:
Kai Ryu 2014-04-18 14:33:57 +09:00
parent 5cc4c69fae
commit 1b2685f374
6 changed files with 23 additions and 112 deletions

View File

@ -16,7 +16,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include "backlight.h" #include "backlight.h"
#include "breathing_led.h"
#include "eeconfig.h" #include "eeconfig.h"
#include "debug.h" #include "debug.h"
@ -34,7 +33,7 @@ void backlight_init(void)
void backlight_increase(void) void backlight_increase(void)
{ {
#if defined(BACKLIGHT_CUSTOM) || defined(BREATHING_LED_ENABLE) #ifdef BACKLIGHT_CUSTOM
if (backlight_config.enable) { if (backlight_config.enable) {
if (backlight_config.level < BACKLIGHT_LEVELS) { if (backlight_config.level < BACKLIGHT_LEVELS) {
backlight_config.level++; backlight_config.level++;
@ -43,9 +42,6 @@ void backlight_increase(void)
dprintf("backlight custom increase: %u\n", backlight_config.level); dprintf("backlight custom increase: %u\n", backlight_config.level);
backlight_set(backlight_config.level); backlight_set(backlight_config.level);
} }
#ifdef BREATHING_LED_ENABLE
breathing_led_increase();
#endif
#else #else
if(backlight_config.level < BACKLIGHT_LEVELS) if(backlight_config.level < BACKLIGHT_LEVELS)
{ {
@ -60,7 +56,7 @@ void backlight_increase(void)
void backlight_decrease(void) void backlight_decrease(void)
{ {
#if defined(BACKLIGHT_CUSTOM) || defined(BREATHING_LED_ENABLE) #ifdef BACKLIGHT_CUSTOM
if (backlight_config.enable) { if (backlight_config.enable) {
if(backlight_config.level > 1) if(backlight_config.level > 1)
{ {
@ -70,9 +66,6 @@ void backlight_decrease(void)
dprintf("backlight custom decrease: %u\n", backlight_config.level); dprintf("backlight custom decrease: %u\n", backlight_config.level);
backlight_set(backlight_config.level); backlight_set(backlight_config.level);
} }
#ifdef BREATHING_LED_ENABLE
breathing_led_decrease();
#endif
#else #else
if(backlight_config.level > 0) if(backlight_config.level > 0)
{ {
@ -87,37 +80,16 @@ void backlight_decrease(void)
void backlight_toggle(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; 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) { if (backlight_config.level == 0) {
backlight_config.level = 1; backlight_config.level = 1;
} }
} }
else {
#ifndef BREATHING_LED_ENABLE
#ifdef BACKLIGHT_CUSTOM
backlight_disable();
#endif
#endif
}
eeconfig_write_backlight(backlight_config.raw); eeconfig_write_backlight(backlight_config.raw);
dprintf("backlight toggle: %u\n", backlight_config.enable); dprintf("backlight toggle: %u\n", backlight_config.enable);
backlight_set(backlight_config.enable ? backlight_config.level : 0); 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) void backlight_step(void)

View File

@ -36,9 +36,6 @@ void backlight_decrease(void);
void backlight_toggle(void); void backlight_toggle(void);
void backlight_step(void); void backlight_step(void);
void backlight_enable(void);
void backlight_disable(void);
void backlight_set(uint8_t level); void backlight_set(uint8_t level);
void backlight_set_raw(uint8_t raw);
#endif #endif

View File

@ -1,22 +1,20 @@
#include <avr/io.h> #include <avr/io.h>
#include <avr/interrupt.h> #include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include "led.h" #include "led.h"
#include "breathing_led.h" #include "breathing_led.h"
#include "backlight.h"
#include "debug.h" #include "debug.h"
#define BREATHING_LED_TIMER_TOP F_CPU/256 #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) void breathing_led_init(void)
{ {
/* Timer3 setup */ /* Timer3 setup */
/* CTC mode */ /* CTC mode */
TCCR3B |= _BV(WGM32); TCCR3B |= (1<<WGM32);
/* Clock selelct: clk/8 */ /* Clock selelct: clk/8 */
TCCR3B |= _BV(CS30); TCCR3B |= (1<<CS30);
/* Set TOP value */ /* Set TOP value */
uint8_t sreg = SREG; uint8_t sreg = SREG;
cli(); cli();
@ -25,59 +23,31 @@ void breathing_led_init(void)
SREG = sreg; SREG = sreg;
} }
bool breathing_led_is_enabled(void)
{
return breathing_led_config.enable;
}
void breathing_led_enable(void) void breathing_led_enable(void)
{ {
/* Enable Compare Match Interrupt */ /* Enable Compare Match Interrupt */
TIMSK3 |= _BV(OCIE3A); TIMSK3 |= (1<<OCIE3A);
breathing_led_config.enable = true; dprintf("breathing led on: %u\n", TIMSK3 & (1<<OCIE3A));
dprintf("breathing led on: %u\n", breathing_led_config.enable);
eeconfig_write_breathing_led(breathing_led_config.raw);
} }
void breathing_led_disable(void) void breathing_led_disable(void)
{ {
/* Disable Compare Match Interrupt */ /* Disable Compare Match Interrupt */
TIMSK3 &= ~_BV(OCIE3A); TIMSK3 &= ~(1<<OCIE3A);
breathing_led_config.enable = false; dprintf("breathing led off: %u\n", TIMSK3 & (1<<OCIE3A));
dprintf("breathing led off: %u\n", breathing_led_config.enable);
eeconfig_write_breathing_led(breathing_led_config.raw);
} }
void breathing_led_toggle(void) void breathing_led_toggle(void)
{ {
/* Disable Compare Match Interrupt */ /* Disable Compare Match Interrupt */
TIMSK3 ^= _BV(OCIE3A); TIMSK3 ^= (1<<OCIE3A);
breathing_led_config.enable ^= 1; dprintf("breathing led toggle: %u\n", TIMSK3 & (1<<OCIE3A));
dprintf("breathing led toggle: %u\n", breathing_led_config.enable);
eeconfig_write_breathing_led(breathing_led_config.raw);
} }
void breathing_led_increase(void) void breathing_led_set_duration(uint8_t dur)
{ {
if (breathing_led_config.enable) { breathing_led_duration = dur;
if (breathing_led_config.level < BREATHING_LED_LEVELS) { dprintf("breathing led set duration: %u\n", breathing_led_duration);
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 /* 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, ', ' } * (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 = { 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) ISR(TIMER3_COMPA_vect)
@ -95,9 +65,9 @@ ISR(TIMER3_COMPA_vect)
static uint8_t index = 0; static uint8_t index = 0;
static uint8_t step = 0; static uint8_t step = 0;
step++; step++;
if (step >= BREATHING_LED_LEVELS - breathing_led_config.level) { if (step > breathing_led_duration) {
step = 0; step = 0;
backlight_set_raw(pgm_read_byte(&breathing_table[index])); breathing_led_set_raw(pgm_read_byte(&breathing_table[index]));
index++; index++;
} }
} }

View File

@ -4,28 +4,12 @@
#ifdef BREATHING_LED_ENABLE #ifdef BREATHING_LED_ENABLE
#include <stdint.h>
#include <stdbool.h>
#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); void breathing_led_init(void);
bool breathing_led_is_enabled(void);
void breathing_led_enable(void); void breathing_led_enable(void);
void breathing_led_disable(void); void breathing_led_disable(void);
void breathing_led_toggle(void); void breathing_led_toggle(void);
void breathing_led_increase(void); void breathing_led_set_duration(uint8_t dur);
void breathing_led_decrease(void); void breathing_led_set_raw(uint8_t raw);
#else #else
@ -33,6 +17,8 @@ void breathing_led_decrease(void);
#define breathing_led_enable() #define breathing_led_enable()
#define breathing_led_disable() #define breathing_led_disable()
#define breathing_led_toggle() #define breathing_led_toggle()
#define breathing_led_set_duration()
#define breathing_led_set_raw()
#endif #endif

View File

@ -14,9 +14,6 @@ void eeconfig_init(void)
#ifdef BACKLIGHT_ENABLE #ifdef BACKLIGHT_ENABLE
eeprom_write_byte(EECONFIG_BACKLIGHT, 0); eeprom_write_byte(EECONFIG_BACKLIGHT, 0);
#endif #endif
#ifdef BREATHING_LED_ENABLE
eeprom_write_byte(EECONFIG_BREATHING_LED, 0);
#endif
#ifdef KEYMAP_EX_ENABLE #ifdef KEYMAP_EX_ENABLE
keymap_ex_init(); keymap_ex_init();
#endif #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); } 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); } void eeconfig_write_backlight(uint8_t val) { eeprom_write_byte(EECONFIG_BACKLIGHT, val); }
#endif #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

View File

@ -31,7 +31,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#define EECONFIG_KEYMAP (uint8_t *)4 #define EECONFIG_KEYMAP (uint8_t *)4
#define EECONFIG_MOUSEKEY_ACCEL (uint8_t *)5 #define EECONFIG_MOUSEKEY_ACCEL (uint8_t *)5
#define EECONFIG_BACKLIGHT (uint8_t *)6 #define EECONFIG_BACKLIGHT (uint8_t *)6
#define EECONFIG_BREATHING_LED (uint8_t *)7
/* debug bit */ /* debug bit */
@ -72,9 +71,4 @@ uint8_t eeconfig_read_backlight(void);
void eeconfig_write_backlight(uint8_t val); void eeconfig_write_backlight(uint8_t val);
#endif #endif
#ifdef BREATHING_LED_ENABLE
uint8_t eeconfig_read_breathing_led(void);
void eeconfig_write_breathing_led(uint8_t val);
#endif
#endif #endif