1
0

Improve softpwm to control LED respectively

This commit is contained in:
Kai Ryu 2014-08-02 10:15:57 +09:00
parent 83ba4d37ad
commit 49e70286a6
6 changed files with 157 additions and 86 deletions

View File

@ -19,6 +19,32 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#define LED_H #define LED_H
#include "stdint.h" #include "stdint.h"
#ifndef LED_COUNT
#define LED_COUNT 1
#endif
#if (LED_COUNT <= 8)
typedef uint8_t led_pack_t;
#elif (LED_COUNT <= 16)
typedef uint16_t led_pack_t;
#elif (LED_COUNT <= 32)
typedef uint32_t led_pack_t;
#else
#error "LED_COUNT: invalid value"
#endif
#if (LED_COUNT <= 16)
#define LED_BIT(i) (1U<<(i))
#elif (LED_COUNT <= 32)
#define LED_BIT(i) (1UL<<(i))
#else
#error "LED_COUNT: invalid value"
#endif
#define LED_BIT_SET(x, i) ((x) |= LED_BIT(i))
#define LED_BIT_CLEAR(x, i) ((x) &= ~LED_BIT(i))
#define LED_BIT_XOR(x, i) ((x) ^= LED_BIT(i))
#define LED_BIT_IS_SET(x, i) ((x) & LED_BIT(i))
/* keyboard LEDs */ /* keyboard LEDs */
#define USB_LED_NUM_LOCK 0 #define USB_LED_NUM_LOCK 0

View File

@ -83,21 +83,17 @@ void layer_state_change(uint32_t state)
#endif #endif
#ifdef SOFTPWM_LED_ENABLE #ifdef SOFTPWM_LED_ENABLE
void softpwm_led_on() void softpwm_led_on(uint8_t index)
{ {
for (uint8_t i = 0; i < LED_COUNT; i++) { if (backlight_binding & LED_BIT(index)) {
if (backlight_binding & LED_BIT(i)) { ledmap_led_on(index);
ledmap_led_on(i);
}
} }
} }
void softpwm_led_off() void softpwm_led_off(uint8_t index)
{ {
for (uint8_t i = 0; i < LED_COUNT; i++) { if (backlight_binding & LED_BIT(index)) {
if (backlight_binding & LED_BIT(i)) { ledmap_led_off(index);
ledmap_led_off(i);
}
} }
} }

View File

@ -3,32 +3,11 @@
#include "stdint.h" #include "stdint.h"
#include "stdbool.h" #include "stdbool.h"
#include "led.h"
#if (LED_COUNT <= 8)
typedef uint8_t led_pack_t;
#elif (LED_COUNT <= 16)
typedef uint16_t led_pack_t;
#elif (LED_COUNT <= 32)
typedef uint32_t led_pack_t;
#else
#error "LED_COUNT: invalid value"
#endif
typedef led_pack_t led_state_t; typedef led_pack_t led_state_t;
typedef led_pack_t led_binding_t; typedef led_pack_t led_binding_t;
#if (LED_COUNT <= 16)
#define LED_BIT(i) (1U<<(i))
#elif (LED_COUNT <= 32)
#define LED_BIT(i) (1UL<<(i))
#else
#error "LED_COUNT: invalid value"
#endif
#define LED_BIT_SET(x, i) ((x) |= LED_BIT(i))
#define LED_BIT_CLEAR(x, i) ((x) &= ~LED_BIT(i))
#define LED_BIT_IS_SET(x, i) ((x) & LED_BIT(i))
typedef enum { typedef enum {
LEDMAP_DEFAULT_LAYER_0 = 0, LEDMAP_DEFAULT_LAYER_0 = 0,
LEDMAP_DEFAULT_LAYER_31 = 31, LEDMAP_DEFAULT_LAYER_31 = 31,

View File

@ -7,9 +7,9 @@
#define SOFTPWM_LED_FREQ 64 #define SOFTPWM_LED_FREQ 64
#define SOFTPWM_LED_TIMER_TOP F_CPU / (256 * SOFTPWM_LED_FREQ) #define SOFTPWM_LED_TIMER_TOP F_CPU / (256 * SOFTPWM_LED_FREQ)
static uint8_t softpwm_state = 0; static uint8_t softpwm_led_state = 0;
static uint8_t softpwm_ocr = 0; static uint8_t softpwm_led_ocr[LED_COUNT] = {0};
static uint8_t softpwm_ocr_buff = 0; static uint8_t softpwm_led_ocr_buff[LED_COUNT] = {0};
void softpwm_led_init(void) void softpwm_led_init(void)
{ {
@ -50,8 +50,10 @@ void softpwm_led_enable(void)
TIMSK1 |= (1<<OCIE1A); TIMSK1 |= (1<<OCIE1A);
//dprintf("softpwm led on: %u\n", TIMSK1 & (1<<OCIE1A)); //dprintf("softpwm led on: %u\n", TIMSK1 & (1<<OCIE1A));
#endif #endif
softpwm_state = 1; softpwm_led_state = 1;
softpwm_led_state_change(softpwm_state); #ifdef LEDMAP_ENABLE
softpwm_led_state_change(softpwm_led_state);
#endif
} }
void softpwm_led_disable(void) void softpwm_led_disable(void)
@ -64,34 +66,40 @@ void softpwm_led_disable(void)
TIMSK1 &= ~(1<<OCIE1A); TIMSK1 &= ~(1<<OCIE1A);
//dprintf("softpwm led off: %u\n", TIMSK1 & (1<<OCIE1A)); //dprintf("softpwm led off: %u\n", TIMSK1 & (1<<OCIE1A));
#endif #endif
softpwm_state = 0; softpwm_led_state = 0;
softpwm_led_off(); for (uint8_t i = 0; i < LED_COUNT; i++) {
softpwm_led_state_change(softpwm_state); softpwm_led_off(i);
}
#ifdef LEDMAP_ENABLE
softpwm_led_state_change(softpwm_led_state);
#endif
} }
void softpwm_led_toggle(void) void softpwm_led_toggle(void)
{ {
/* Disable Compare Match Interrupt */ if (softpwm_led_state) {
#ifdef SOFTPWM_LED_TIMER3 softpwm_led_disable();
TIMSK3 ^= (1<<OCIE3A); }
//dprintf("softpwm led toggle: %u\n", TIMSK3 & (1<<OCIE3A)); else {
#else softpwm_led_enable();
TIMSK1 ^= (1<<OCIE1A); }
//dprintf("softpwm led toggle: %u\n", TIMSK1 & (1<<OCIE1A));
#endif
softpwm_state ^= 1;
if (!softpwm_state) softpwm_led_off();
softpwm_led_state_change(softpwm_state);
} }
void softpwm_led_set(uint8_t val) void softpwm_led_set(uint8_t index, uint8_t val)
{ {
softpwm_ocr_buff = val; softpwm_led_ocr_buff[index] = val;
}
void softpwm_led_set_all(uint8_t val)
{
for (uint8_t i = 0; i < LED_COUNT; i++) {
softpwm_led_ocr_buff[i] = val;
}
} }
inline uint8_t softpwm_led_get_state(void) inline uint8_t softpwm_led_get_state(void)
{ {
return softpwm_state; return softpwm_led_state;
} }
#ifdef BREATHING_LED_ENABLE #ifdef BREATHING_LED_ENABLE
@ -106,30 +114,56 @@ 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
}; };
static uint8_t breathing_led_state = 0; static led_state_t breathing_led_state = 0;
static uint8_t breathing_led_duration = 0; static uint8_t breathing_led_duration[LED_COUNT] = {0};
void breathing_led_enable(void) void breathing_led_enable(uint8_t index)
{ {
breathing_led_state = 1; LED_BIT_SET(breathing_led_state, index);
} }
void breathing_led_disable(void) void breathing_led_enable_all(void)
{
for (uint8_t i = 0; i < LED_COUNT; i++) {
LED_BIT_SET(breathing_led_state, i);
}
}
void breathing_led_disable(uint8_t index)
{
LED_BIT_CLEAR(breathing_led_state, index);
}
void breathing_led_disable_all(void)
{ {
breathing_led_state = 0; breathing_led_state = 0;
} }
void breathing_led_toggle(void) void breathing_led_toggle(uint8_t index)
{ {
breathing_led_state ^= 1; LED_BIT_XOR(breathing_led_state, index);
} }
void breathing_led_set_duration(uint8_t dur) void breathing_led_toggle_all(void)
{ {
breathing_led_duration = dur; for (uint8_t i = 0; i < LED_COUNT; i++) {
LED_BIT_XOR(breathing_led_state, i);
}
}
void breathing_led_set_duration(uint8_t index, uint8_t dur)
{
breathing_led_duration[index] = dur;
//dprintf("breathing led set duration: %u\n", breathing_led_duration); //dprintf("breathing led set duration: %u\n", breathing_led_duration);
} }
void breathing_led_set_duration_all(uint8_t dur)
{
for (uint8_t i = 0; i < LED_COUNT; i++) {
breathing_led_duration[i] = dur;
}
}
#endif #endif
#ifdef SOFTPWM_LED_TIMER3 #ifdef SOFTPWM_LED_TIMER3
@ -142,27 +176,33 @@ ISR(TIMER1_COMPA_vect)
pwm++; pwm++;
// LED on // LED on
if (pwm == 0) { if (pwm == 0) {
softpwm_led_on(); for (uint8_t i = 0; i < LED_COUNT; i++) {
softpwm_ocr = softpwm_ocr_buff; softpwm_led_on(i);
softpwm_led_ocr[i] = softpwm_led_ocr_buff[i];
}
} }
// LED off // LED off
if (pwm == softpwm_ocr) { for (uint8_t i = 0; i < LED_COUNT; i++) {
softpwm_led_off(); if (pwm == softpwm_led_ocr[i]) {
softpwm_led_off(i);
}
} }
#ifdef BREATHING_LED_ENABLE #ifdef BREATHING_LED_ENABLE
static uint8_t count = 0; static uint8_t count = 0;
static uint8_t index = 0; static uint8_t index[LED_COUNT] = {0};
static uint8_t step = 0; static uint8_t step[LED_COUNT] = {0};
if (breathing_led_state) { if (breathing_led_state) {
count++; if (++count > SOFTPWM_LED_FREQ) {
if (count > SOFTPWM_LED_FREQ) {
count = 0; count = 0;
step++; for (uint8_t i = 0; i < LED_COUNT; i++) {
if (step > breathing_led_duration) { if (breathing_led_state & LED_BIT(i)) {
step = 0; if (++step[i] > breathing_led_duration[i]) {
softpwm_ocr_buff = pgm_read_byte(&breathing_table[index]); step[i] = 0;
index++; softpwm_led_ocr_buff[i] = pgm_read_byte(&breathing_table[index[i]]);
index[i]++;
}
}
} }
} }
} }

View File

@ -1,30 +1,44 @@
#ifndef SOFTPWM_LED_H #ifndef SOFTPWM_LED_H
#define SOFTPWM_LED_H #define SOFTPWM_LED_H
#include "stdint.h"
#include "led.h"
typedef led_pack_t led_state_t;
#ifdef SOFTPWM_LED_ENABLE #ifdef SOFTPWM_LED_ENABLE
void softpwm_led_init(void); void softpwm_led_init(void);
void softpwm_led_enable(void); void softpwm_led_enable(void);
void softpwm_led_disable(void); void softpwm_led_disable(void);
void softpwm_led_toggle(void); void softpwm_led_toggle(void);
void softpwm_led_set(uint8_t val); void softpwm_led_set(uint8_t index, uint8_t val);
void softpwm_led_on(void); void softpwm_led_set_all(uint8_t val);
void softpwm_led_off(void); void softpwm_led_on(uint8_t index);
void softpwm_led_off(uint8_t index);
uint8_t softpwm_led_get_state(void); uint8_t softpwm_led_get_state(void);
void softpwm_led_state_change(uint8_t state); void softpwm_led_state_change(uint8_t state);
#ifdef BREATHING_LED_ENABLE #ifdef BREATHING_LED_ENABLE
#define breathing_led_init() #define breathing_led_init()
void breathing_led_enable(void); void breathing_led_enable(uint8_t index);
void breathing_led_disable(void); void breathing_led_enable_all(void);
void breathing_led_toggle(void); void breathing_led_disable(uint8_t index);
void breathing_led_set_duration(uint8_t dur); void breathing_led_disable_all(void);
void breathing_led_toggle(uint8_t index);
void breathing_led_toggle_all(void);
void breathing_led_set_duration(uint8_t index, uint8_t dur);
void breathing_led_set_duration_all(uint8_t dur);
#else #else
#define breathing_led_init() #define breathing_led_init()
#define breathing_led_enable() #define breathing_led_enable()
#define breathing_led_enable_all()
#define breathing_led_disable() #define breathing_led_disable()
#define breathing_led_disable_all()
#define breathing_led_toggle() #define breathing_led_toggle()
#define breathing_led_toggle_all()
#define breathing_led_set_duration() #define breathing_led_set_duration()
#define breathing_led_set_duration_all()
#endif #endif
#else #else
@ -34,6 +48,7 @@ void breathing_led_set_duration(uint8_t dur);
#define softpwm_led_disable() #define softpwm_led_disable()
#define softpwm_led_toggle() #define softpwm_led_toggle()
#define softpwm_led_set() #define softpwm_led_set()
#define softpwm_led_set_all()
#define softpwm_led_on() #define softpwm_led_on()
#define softpwm_led_off() #define softpwm_led_off()
#define softpwm_led_get_state() #define softpwm_led_get_state()

View File

@ -107,19 +107,32 @@ void backlight_set(uint8_t level)
case 2: case 2:
case 3: case 3:
backlight_enable(); backlight_enable();
#ifdef SOFTPWM_LED_ENABLE
breathing_led_disable_all();
#else
breathing_led_disable(); breathing_led_disable();
#endif
backlight_set_raw(pgm_read_byte(&backlight_table[level])); backlight_set_raw(pgm_read_byte(&backlight_table[level]));
break; break;
case 4: case 4:
case 5: case 5:
case 6: case 6:
backlight_enable(); backlight_enable();
#ifdef SOFTPWM_LED_ENABLE
breathing_led_enable_all();
breathing_led_set_duration_all(6 - level);
#else
breathing_led_enable(); breathing_led_enable();
breathing_led_set_duration(6 - level); breathing_led_set_duration(6 - level);
#endif
break; break;
case 0: case 0:
default: default:
#ifdef SOFTPWM_LED_ENABLE
breathing_led_disable_all();
#else
breathing_led_disable(); breathing_led_disable();
#endif
backlight_disable(); backlight_disable();
break; break;
} }
@ -146,7 +159,9 @@ void breathing_led_set_raw(uint8_t raw)
inline void backlight_set_raw(uint8_t raw) inline void backlight_set_raw(uint8_t raw)
{ {
#ifdef SOFTPWM_LED_ENABLE #ifdef SOFTPWM_LED_ENABLE
softpwm_led_set(raw); for (uint8_t i = 0; i < LED_COUNT; i++) {
softpwm_led_set(i, raw);
}
#else #else
#if defined(GH60_REV_CHN) #if defined(GH60_REV_CHN)
OCR1B = raw; OCR1B = raw;
@ -159,7 +174,7 @@ inline void backlight_set_raw(uint8_t raw)
#ifdef SOFTPWM_LED_ENABLE #ifdef SOFTPWM_LED_ENABLE
#ifndef LEDMAP_ENABLE #ifndef LEDMAP_ENABLE
void softpwm_led_on(void) void softpwm_led_on(uint8_t index)
{ {
#if defined(GH60_REV_CHN) #if defined(GH60_REV_CHN)
PORTB |= (1<<PB6); PORTB |= (1<<PB6);
@ -168,7 +183,7 @@ void softpwm_led_on(void)
#endif #endif
} }
void softpwm_led_off(void) void softpwm_led_off(uint8_t index)
{ {
#if defined(GH60_REV_CHN) #if defined(GH60_REV_CHN)
PORTB &= ~(1<<PB6); PORTB &= ~(1<<PB6);