From 1c3b8f810fb61ccb4f4e61f54d23ff7d7541bd73 Mon Sep 17 00:00:00 2001 From: Kai Ryu Date: Fri, 25 Jul 2014 14:24:57 +0900 Subject: [PATCH] Move backlight to a separated bit to realize complex ledmap combinations --- common/keyboard.c | 2 +- common/ledmap.c | 118 +++++++++++++++++++++++--------------- common/ledmap.h | 19 +++++- common/ledmap_in_eeprom.c | 2 +- common/softpwm_led.c | 21 ++++++- common/softpwm_led.h | 4 ++ 6 files changed, 113 insertions(+), 53 deletions(-) diff --git a/common/keyboard.c b/common/keyboard.c index a9db9ebd..1fedc8c8 100644 --- a/common/keyboard.c +++ b/common/keyboard.c @@ -85,10 +85,10 @@ void keyboard_init(void) #endif #ifdef LEDMAP_ENABLE - ledmap_led_init(); #ifdef LEDMAP_IN_EEPROM_ENABLE ledmap_in_eeprom_init(); #endif + ledmap_init(); #endif #ifdef BACKLIGHT_ENABLE diff --git a/common/ledmap.c b/common/ledmap.c index f8264a89..757074c8 100644 --- a/common/ledmap.c +++ b/common/ledmap.c @@ -5,66 +5,80 @@ #include "action_layer.h" #include "debug.h" -static led_state_t led_state = 0; static led_state_t led_state_last = 0; +static led_binding_t usb_led_binding = 0; +static led_binding_t default_layer_binding = 0; +static led_binding_t layer_binding = 0; +static led_binding_t backlight_binding = 0; -static void update_led_state(void); +static void update_led_state(led_state_t state); -void led_set(uint8_t usb_led) +void ledmap_init(void) { for (uint8_t i = 0; i < LED_COUNT; i++) { uint8_t code = ledmap_get_code(i); - /* - switch (code) { - case LEDMAP_NUM_LOCK: - (usb_led & (1 << USB_LED_NUM_LOCK)) ? LED_BIT_ON(led_state, i) : LED_BIT_OFF(led_state, i); - break; - case LEDMAP_CAPS_LOCK: - (usb_led & (1 << USB_LED_CAPS_LOCK)) ? LED_BIT_ON(led_state, i) : LED_BIT_OFF(led_state, i); - break; - case LEDMAP_SCROLL_LOCK: - (usb_led & (1 << USB_LED_SCROLL_LOCK)) ? LED_BIT_ON(led_state, i) : LED_BIT_OFF(led_state, i); - break; - case LEDMAP_COMPOSE: - (usb_led & (1 << USB_LED_COMPOSE)) ? LED_BIT_ON(led_state, i) : LED_BIT_OFF(led_state, i); - break; - case LEDMAP_KANA: - (usb_led & (1 << USB_LED_KANA)) ? LED_BIT_ON(led_state, i) : LED_BIT_OFF(led_state, i); - break; - default: - break; + if (code & LEDMAP_BACKLIGHT) { + LED_BIT_SET(backlight_binding, i); } - */ - for (uint8_t j = USB_LED_NUM_LOCK; j <= USB_LED_KANA; j++) { - if (code - LEDMAP_NUM_LOCK == j) { - (usb_led & (1 << j)) ? LED_BIT_ON(led_state, i) : LED_BIT_OFF(led_state, i); - } + code &= LEDMAP_MASK; + if (code >= LEDMAP_DEFAULT_LAYER_0 && code <= LEDMAP_DEFAULT_LAYER_31) { + LED_BIT_SET(default_layer_binding, i); + } + else if (code >= LEDMAP_LAYER_0 && code <= LEDMAP_LAYER_31) { + LED_BIT_SET(layer_binding, i); + } + else if (code >= LEDMAP_NUM_LOCK && code <= LEDMAP_KANA) { + LED_BIT_SET(usb_led_binding, i); } } - update_led_state(); + ledmap_led_init(); +} + +void led_set(uint8_t usb_led) +{ + if (usb_led_binding) { + led_state_t led_state = led_state_last; + for (uint8_t i = 0; i < LED_COUNT; i++) { + if (usb_led_binding & LED_BIT(i)) { + uint8_t code = ledmap_get_code(i) & LEDMAP_MASK; + for (uint8_t j = USB_LED_NUM_LOCK; j <= USB_LED_KANA; j++) { + if (code - LEDMAP_NUM_LOCK == j) { + (usb_led & (1 << j)) ? LED_BIT_SET(led_state, i) : LED_BIT_CLEAR(led_state, i); + } + } + } + } + update_led_state(led_state); + } } #ifndef NO_ACTION_LAYER void default_layer_state_change(uint32_t state) { - for (uint8_t i = 0; i < LED_COUNT; i++) { - uint8_t code = ledmap_get_code(i); - if (code >= LEDMAP_DEFAULT_LAYER_0 && code < LEDMAP_DEFAULT_LAYER_31) { - (state & (1UL << (code - LEDMAP_DEFAULT_LAYER_0))) ? LED_BIT_ON(led_state, i) : LED_BIT_OFF(led_state, i); + if (default_layer_binding) { + led_state_t led_state = led_state_last; + for (uint8_t i = 0; i < LED_COUNT; i++) { + if (default_layer_binding & LED_BIT(i)) { + uint8_t code = ledmap_get_code(i) & LEDMAP_MASK; + (state & (1UL << (code - LEDMAP_DEFAULT_LAYER_0))) ? LED_BIT_SET(led_state, i) : LED_BIT_CLEAR(led_state, i); + } } + update_led_state(led_state); } - update_led_state(); } void layer_state_change(uint32_t state) { - for (uint8_t i = 0; i < LED_COUNT; i++) { - uint8_t code = ledmap_get_code(i); - if (code >= LEDMAP_LAYER_0 && code < LEDMAP_LAYER_31) { - (state & (1UL << (code - LEDMAP_LAYER_0))) ? LED_BIT_ON(led_state, i) : LED_BIT_OFF(led_state, i); + if (layer_binding) { + led_state_t led_state = led_state_last; + for (uint8_t i = 0; i < LED_COUNT; i++) { + if (layer_binding & LED_BIT(i)) { + uint8_t code = ledmap_get_code(i) & LEDMAP_MASK; + (state & (1UL << (code - LEDMAP_LAYER_0))) ? LED_BIT_SET(led_state, i) : LED_BIT_CLEAR(led_state, i); + } } + update_led_state(led_state); } - update_led_state(); } #endif @@ -72,8 +86,7 @@ void layer_state_change(uint32_t state) void softpwm_led_on() { for (uint8_t i = 0; i < LED_COUNT; i++) { - uint8_t code = ledmap_get_code(i); - if (code == LEDMAP_BACKLIGHT) { + if (backlight_binding & LED_BIT(i)) { ledmap_led_on(i); } } @@ -82,21 +95,34 @@ void softpwm_led_on() void softpwm_led_off() { for (uint8_t i = 0; i < LED_COUNT; i++) { - uint8_t code = ledmap_get_code(i); - if (code == LEDMAP_BACKLIGHT) { + if (backlight_binding & LED_BIT(i)) { ledmap_led_off(i); } } } + +void softpwm_led_state_change(uint8_t state) +{ + if (state) { + } + else { + led_state_t led_state = led_state_last; + led_state_last &= ~(backlight_binding); + update_led_state(led_state); + } +} #endif -void update_led_state(void) +void update_led_state(led_state_t state) { - uint8_t diff = led_state_last ^ led_state; + uint8_t diff = led_state_last ^ state; if (diff) { for (uint8_t i = 0; i < LED_COUNT; i++) { + if (softpwm_led_get_state() && (backlight_binding & LED_BIT(i))) { + continue; + } if (diff & LED_BIT(i)) { - if (led_state & LED_BIT(i)) { + if (state & LED_BIT(i)) { ledmap_led_on(i); } else { @@ -104,6 +130,6 @@ void update_led_state(void) } } } - led_state_last = led_state; + led_state_last = state; } } diff --git a/common/ledmap.h b/common/ledmap.h index de68558a..bca421ea 100644 --- a/common/ledmap.h +++ b/common/ledmap.h @@ -2,6 +2,7 @@ #define LEDMAP_H #include "stdint.h" +#include "stdbool.h" #if (LED_COUNT <= 8) typedef uint8_t led_pack_t; @@ -14,6 +15,7 @@ typedef uint32_t led_pack_t; #endif typedef led_pack_t led_state_t; +typedef led_pack_t led_binding_t; #if (LED_COUNT <= 16) #define LED_BIT(i) (1U<<(i)) @@ -23,8 +25,9 @@ typedef led_pack_t led_state_t; #error "LED_COUNT: invalid value" #endif -#define LED_BIT_ON(state, i) ((state) |= LED_BIT(i)) -#define LED_BIT_OFF(state, i) ((state) &= ~LED_BIT(i)) +#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, @@ -40,9 +43,21 @@ typedef enum { LEDMAP_UNCONFIGURED = 0xFF } ledmap_code_t; +#define LEDMAP_MASK 0x7F + +typedef union { + uint8_t raw; + struct { + uint8_t binding : 7; + bool backlight : 1; + }; +} ledmap_t; + #define LEDMAP_DEFAULT_LAYER(x) (LEDMAP_DEFAULT_LAYER_0 + x) #define LEDMAP_LAYER(x) (LEDMAP_LAYER_0 + x) +void ledmap_init(void); + #ifdef LEDMAP_ENABLE uint8_t ledmap_get_code(uint8_t index); void ledmap_led_init(void); diff --git a/common/ledmap_in_eeprom.c b/common/ledmap_in_eeprom.c index f3f7c20a..9876fc72 100644 --- a/common/ledmap_in_eeprom.c +++ b/common/ledmap_in_eeprom.c @@ -13,7 +13,7 @@ void ledmap_in_eeprom_init(void) { for (uint8_t i = 0; i < LED_COUNT; i++) { ledmap[i] = eeprom_read_byte(EECONFIG_LEDMAP + i); - ledmap[i] = LEDMAP_UNCONFIGURED; + //ledmap[i] = LEDMAP_UNCONFIGURED; if (ledmap[i] == LEDMAP_UNCONFIGURED) { ledmap[i] = ledmap_get_code(i); eeprom_write_byte(EECONFIG_LEDMAP + i, ledmap[i]); diff --git a/common/softpwm_led.c b/common/softpwm_led.c index d95ab48d..2155d1f2 100644 --- a/common/softpwm_led.c +++ b/common/softpwm_led.c @@ -7,8 +7,9 @@ #define SOFTPWM_LED_FREQ 64 #define SOFTPWM_LED_TIMER_TOP F_CPU / (256 * SOFTPWM_LED_FREQ) -uint8_t softpwm_ocr = 0; -uint8_t softpwm_ocr_buff = 0; +static uint8_t softpwm_state = 0; +static uint8_t softpwm_ocr = 0; +static uint8_t softpwm_ocr_buff = 0; void softpwm_led_init(void) { @@ -49,6 +50,8 @@ void softpwm_led_enable(void) TIMSK1 |= (1<