From 44651f4192ab76f078e6cdb38bc9a3e5479dd9d1 Mon Sep 17 00:00:00 2001 From: Kai Ryu Date: Sat, 2 Aug 2014 10:15:57 +0900 Subject: [PATCH] Improve softpwm to control LED respectively --- common/led.h | 26 +++++++++ common/ledmap.c | 16 ++---- common/ledmap.h | 23 +------- common/softpwm_led.c | 128 ++++++++++++++++++++++++++++--------------- common/softpwm_led.h | 29 +++++++--- 5 files changed, 139 insertions(+), 83 deletions(-) diff --git a/common/led.h b/common/led.h index 402a247b..f72d5dc7 100644 --- a/common/led.h +++ b/common/led.h @@ -19,6 +19,32 @@ along with this program. If not, see . #define LED_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 */ #define USB_LED_NUM_LOCK 0 diff --git a/common/ledmap.c b/common/ledmap.c index 757074c8..113db621 100644 --- a/common/ledmap.c +++ b/common/ledmap.c @@ -83,21 +83,17 @@ void layer_state_change(uint32_t state) #endif #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(i)) { - ledmap_led_on(i); - } + if (backlight_binding & LED_BIT(index)) { + ledmap_led_on(index); } } -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(i)) { - ledmap_led_off(i); - } + if (backlight_binding & LED_BIT(index)) { + ledmap_led_off(index); } } diff --git a/common/ledmap.h b/common/ledmap.h index bca421ea..bbd15af6 100644 --- a/common/ledmap.h +++ b/common/ledmap.h @@ -3,32 +3,11 @@ #include "stdint.h" #include "stdbool.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 +#include "led.h" typedef led_pack_t led_state_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 { LEDMAP_DEFAULT_LAYER_0 = 0, LEDMAP_DEFAULT_LAYER_31 = 31, diff --git a/common/softpwm_led.c b/common/softpwm_led.c index 2155d1f2..5fd0e80f 100644 --- a/common/softpwm_led.c +++ b/common/softpwm_led.c @@ -7,9 +7,9 @@ #define SOFTPWM_LED_FREQ 64 #define SOFTPWM_LED_TIMER_TOP F_CPU / (256 * SOFTPWM_LED_FREQ) -static uint8_t softpwm_state = 0; -static uint8_t softpwm_ocr = 0; -static uint8_t softpwm_ocr_buff = 0; +static uint8_t softpwm_led_state = 0; +static uint8_t softpwm_led_ocr[LED_COUNT] = {0}; +static uint8_t softpwm_led_ocr_buff[LED_COUNT] = {0}; void softpwm_led_init(void) { @@ -50,8 +50,10 @@ void softpwm_led_enable(void) TIMSK1 |= (1< SOFTPWM_LED_FREQ) { + if (++count > SOFTPWM_LED_FREQ) { count = 0; - step++; - if (step > breathing_led_duration) { - step = 0; - softpwm_ocr_buff = pgm_read_byte(&breathing_table[index]); - index++; + for (uint8_t i = 0; i < LED_COUNT; i++) { + if (breathing_led_state & LED_BIT(i)) { + if (++step[i] > breathing_led_duration[i]) { + step[i] = 0; + softpwm_led_ocr_buff[i] = pgm_read_byte(&breathing_table[index[i]]); + index[i]++; + } + } } } } diff --git a/common/softpwm_led.h b/common/softpwm_led.h index 0cd12e6a..63e5a133 100644 --- a/common/softpwm_led.h +++ b/common/softpwm_led.h @@ -1,30 +1,44 @@ #ifndef SOFTPWM_LED_H #define SOFTPWM_LED_H +#include "stdint.h" +#include "led.h" + +typedef led_pack_t led_state_t; + #ifdef SOFTPWM_LED_ENABLE void softpwm_led_init(void); void softpwm_led_enable(void); void softpwm_led_disable(void); void softpwm_led_toggle(void); -void softpwm_led_set(uint8_t val); -void softpwm_led_on(void); -void softpwm_led_off(void); +void softpwm_led_set(uint8_t index, uint8_t val); +void softpwm_led_set_all(uint8_t val); +void softpwm_led_on(uint8_t index); +void softpwm_led_off(uint8_t index); uint8_t softpwm_led_get_state(void); void softpwm_led_state_change(uint8_t state); #ifdef BREATHING_LED_ENABLE #define breathing_led_init() -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_enable(uint8_t index); +void breathing_led_enable_all(void); +void breathing_led_disable(uint8_t index); +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 #define breathing_led_init() #define breathing_led_enable() +#define breathing_led_enable_all() #define breathing_led_disable() +#define breathing_led_disable_all() #define breathing_led_toggle() +#define breathing_led_toggle_all() #define breathing_led_set_duration() +#define breathing_led_set_duration_all() #endif #else @@ -34,6 +48,7 @@ void breathing_led_set_duration(uint8_t dur); #define softpwm_led_disable() #define softpwm_led_toggle() #define softpwm_led_set() +#define softpwm_led_set_all() #define softpwm_led_on() #define softpwm_led_off() #define softpwm_led_get_state()