From a34c3bf088a57ea0045862d08269c6f6b40e9984 Mon Sep 17 00:00:00 2001 From: Kai Ryu Date: Sat, 19 Jul 2014 11:27:00 +0900 Subject: [PATCH 1/7] New branch for LED mapping feature --- common/ledmap.c | 1 + common/ledmap.h | 9 +++++++++ 2 files changed, 10 insertions(+) create mode 100644 common/ledmap.c create mode 100644 common/ledmap.h diff --git a/common/ledmap.c b/common/ledmap.c new file mode 100644 index 00000000..df30af98 --- /dev/null +++ b/common/ledmap.c @@ -0,0 +1 @@ +#include "ledmap.h" diff --git a/common/ledmap.h b/common/ledmap.h new file mode 100644 index 00000000..32c8d733 --- /dev/null +++ b/common/ledmap.h @@ -0,0 +1,9 @@ +#ifndef LEDMAP_H +#define LEDMAP_H + +#ifdef LEDMAP_ENABLE + +#else +#endif + +#endif From cf06612edfdb8bc05a5d5b64179ecafce720b0d6 Mon Sep 17 00:00:00 2001 From: Kai Ryu Date: Sun, 20 Jul 2014 12:52:56 +0900 Subject: [PATCH 2/7] Implement led mapping feature --- common.mk | 33 +++++++++------ common/action_layer.c | 6 +++ common/action_layer.h | 8 ++++ common/ledmap.c | 99 +++++++++++++++++++++++++++++++++++++++++++ common/ledmap.h | 53 ++++++++++++++++++++++- common/softpwm_led.c | 14 +++--- 6 files changed, 191 insertions(+), 22 deletions(-) diff --git a/common.mk b/common.mk index 540d8da4..f4c5c420 100644 --- a/common.mk +++ b/common.mk @@ -1,18 +1,18 @@ COMMON_DIR = common -SRC += $(COMMON_DIR)/host.c \ - $(COMMON_DIR)/keyboard.c \ - $(COMMON_DIR)/action.c \ - $(COMMON_DIR)/action_tapping.c \ - $(COMMON_DIR)/action_macro.c \ - $(COMMON_DIR)/action_layer.c \ - $(COMMON_DIR)/action_util.c \ - $(COMMON_DIR)/keymap.c \ - $(COMMON_DIR)/timer.c \ - $(COMMON_DIR)/print.c \ - $(COMMON_DIR)/bootloader.c \ - $(COMMON_DIR)/suspend.c \ - $(COMMON_DIR)/xprintf.S \ - $(COMMON_DIR)/util.c +SRC += $(COMMON_DIR)/host.c \ + $(COMMON_DIR)/keyboard.c \ + $(COMMON_DIR)/action.c \ + $(COMMON_DIR)/action_tapping.c \ + $(COMMON_DIR)/action_macro.c \ + $(COMMON_DIR)/action_layer.c \ + $(COMMON_DIR)/action_util.c \ + $(COMMON_DIR)/keymap.c \ + $(COMMON_DIR)/timer.c \ + $(COMMON_DIR)/print.c \ + $(COMMON_DIR)/bootloader.c \ + $(COMMON_DIR)/suspend.c \ + $(COMMON_DIR)/xprintf.S \ + $(COMMON_DIR)/util.c # Option modules @@ -76,6 +76,11 @@ ifdef BACKLIGHT_ENABLE OPT_DEFS += -DBACKLIGHT_ENABLE endif +ifdef LEDMAP_ENABLE + SRC += $(COMMON_DIR)/ledmap.c + OPT_DEFS += -DLEDMAP_ENABLE +endif + ifdef KEYMAP_SECTION_ENABLE OPT_DEFS += -DKEYMAP_SECTION_ENABLE EXTRALDFLAGS = -Wl,-L$(TOP_DIR),-Tldscript_keymap_avr5.x diff --git a/common/action_layer.c b/common/action_layer.c index 526e24d5..579950ac 100644 --- a/common/action_layer.c +++ b/common/action_layer.c @@ -22,6 +22,9 @@ static void default_layer_state_set(uint32_t state) default_layer_debug(); debug(" to "); default_layer_state = state; default_layer_debug(); debug("\n"); +#ifdef LEDMAP_ENABLE + default_layer_state_change(state); +#endif clear_keyboard_but_mods(); // To avoid stuck keys } @@ -63,6 +66,9 @@ static void layer_state_set(uint32_t state) layer_debug(); dprint(" to "); layer_state = state; layer_debug(); dprintln(); +#ifdef LEDMAP_ENABLE + layer_state_change(state); +#endif clear_keyboard_but_mods(); // To avoid stuck keys } diff --git a/common/action_layer.h b/common/action_layer.h index 034e0002..9af7b875 100644 --- a/common/action_layer.h +++ b/common/action_layer.h @@ -70,6 +70,14 @@ void layer_xor(uint32_t state); #define layer_debug() #endif +#ifdef LEDMAP_ENABLE +void default_layer_state_change(uint32_t state); +void layer_state_change(uint32_t state); +#else +#define default_layer_state_change() +#define layer_state_change() +#endif + /* return action depending on current layer status */ action_t layer_switch_get_action(key_t key); diff --git a/common/ledmap.c b/common/ledmap.c index df30af98..fccd4218 100644 --- a/common/ledmap.c +++ b/common/ledmap.c @@ -1 +1,100 @@ #include "ledmap.h" +#include "led.h" +#include "softpwm_led.h" +#include "action_layer.h" + +static led_state_t led_state = 0; +static led_state_t led_state_last = 0; + +static void update_led_state(void); + +void led_set(uint8_t usb_led) +{ + 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; + } + } + update_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); + } + } + 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); + } + } + update_led_state(); +} +#endif + +#ifdef SOFTPWM_LED_ENABLE +void softpwm_led_on() +{ + for (uint8_t i = 0; i < LED_COUNT; i++) { + uint8_t code = ledmap_get_code(i); + if (code == LEDMAP_BACKLIGHT) { + ledmap_led_on(i); + } + } +} + +void softpwm_led_off() +{ + for (uint8_t i = 0; i < LED_COUNT; i++) { + uint8_t code = ledmap_get_code(i); + if (code == LEDMAP_BACKLIGHT) { + ledmap_led_off(i); + } + } +} +#endif + +void update_led_state(void) +{ + uint8_t diff = led_state_last ^ led_state; + if (diff) { + for (uint8_t i = 0; i < LED_COUNT; i++) { + if (diff & LED_BIT(i)) { + if (led_state & LED_BIT(i)) { + ledmap_led_on(i); + } + else { + ledmap_led_off(i); + } + } + } + led_state_last = led_state; + } +} diff --git a/common/ledmap.h b/common/ledmap.h index 32c8d733..8a570081 100644 --- a/common/ledmap.h +++ b/common/ledmap.h @@ -1,9 +1,60 @@ #ifndef LEDMAP_H #define LEDMAP_H -#ifdef LEDMAP_ENABLE +#include "stdint.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; + +#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_ON(state, i) ((state) |= LED_BIT(i)) +#define LED_BIT_OFF(state, i) ((state) &= ~LED_BIT(i)) + +typedef enum { + LEDMAP_DEFAULT_LAYER_0 = 0, + LEDMAP_DEFAULT_LAYER_31 = 31, + LEDMAP_LAYER_0 = 32, + LEDMAP_LAYER_31 = 63, + LEDMAP_NUM_LOCK = 64, + LEDMAP_CAPS_LOCK, + LEDMAP_SCROLL_LOCK, + LEDMAP_COMPOSE, + LEDMAP_KANA, + LEDMAP_BACKLIGHT +} ledmap_code_t; + +#define LEDMAP_LAYER(x) (x) +#define LEDMAP_DEFAULT_LAYER(x) (32 + x) + +void ledmap_init(void); + +#ifdef LEDMAP_ENABLE +uint8_t ledmap_get_code(uint8_t index); +void ledmap_led_init(void); +void ledmap_led_on(uint8_t index); +void ledmap_led_off(uint8_t index); +#else +#define ledmaps +#define ledmap_get() +#define ledmap_led_init() +#define ledmap_led_on() +#define ledmap_led_off() #endif #endif diff --git a/common/softpwm_led.c b/common/softpwm_led.c index e2195c2f..d95ab48d 100644 --- a/common/softpwm_led.c +++ b/common/softpwm_led.c @@ -44,10 +44,10 @@ void softpwm_led_enable(void) /* Enable Compare Match Interrupt */ #ifdef SOFTPWM_LED_TIMER3 TIMSK3 |= (1< Date: Wed, 23 Jul 2014 10:02:14 +0900 Subject: [PATCH 3/7] Implement ledmap-in-eeprom feature --- common.mk | 5 +++++ common/ledmap.c | 1 + common/ledmap.h | 3 ++- common/ledmap_in_eeprom.c | 28 ++++++++++++++++++++++++++++ common/ledmap_in_eeprom.h | 20 ++++++++++++++++++++ 5 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 common/ledmap_in_eeprom.c create mode 100644 common/ledmap_in_eeprom.h diff --git a/common.mk b/common.mk index f4c5c420..0930406d 100644 --- a/common.mk +++ b/common.mk @@ -79,7 +79,12 @@ endif ifdef LEDMAP_ENABLE SRC += $(COMMON_DIR)/ledmap.c OPT_DEFS += -DLEDMAP_ENABLE +ifdef LEDMAP_IN_EEPROM_ENABLE + SRC += $(COMMON_DIR)/ledmap_in_eeprom.c + OPT_DEFS += -DLEDMAP_IN_EEPROM_ENABLE endif +endif + ifdef KEYMAP_SECTION_ENABLE OPT_DEFS += -DKEYMAP_SECTION_ENABLE diff --git a/common/ledmap.c b/common/ledmap.c index fccd4218..2002095e 100644 --- a/common/ledmap.c +++ b/common/ledmap.c @@ -1,4 +1,5 @@ #include "ledmap.h" +#include "ledmap_in_eeprom.h" #include "led.h" #include "softpwm_led.h" #include "action_layer.h" diff --git a/common/ledmap.h b/common/ledmap.h index 8a570081..56960edc 100644 --- a/common/ledmap.h +++ b/common/ledmap.h @@ -36,7 +36,8 @@ typedef enum { LEDMAP_SCROLL_LOCK, LEDMAP_COMPOSE, LEDMAP_KANA, - LEDMAP_BACKLIGHT + LEDMAP_BACKLIGHT = 0x80, + LEDMAP_UNCONFIGURED = 0xFF } ledmap_code_t; #define LEDMAP_LAYER(x) (x) diff --git a/common/ledmap_in_eeprom.c b/common/ledmap_in_eeprom.c new file mode 100644 index 00000000..a0c4adb0 --- /dev/null +++ b/common/ledmap_in_eeprom.c @@ -0,0 +1,28 @@ +#include +#include +#include "ledmap.h" +#include "ledmap_in_eeprom.h" + +#ifdef LEDMAP_IN_EEPROM_ENABLE + +#undef ledmap_get_code + +static uint8_t ledmap[LED_COUNT]; + +void ledmap_in_eeprom_init(void) { + // read and check ledmap in eeprom + for (uint8_t i = 0; i < LED_COUNT; i++) { + ledmap[i] = eeprom_read_byte(EECONFIG_LEDMAP + i); + if (ledmap[i] == LEDMAP_UNCONFIGURED) { + ledmap[i] = lemap_get_code(i); + eeprom_write_byte(EECONFIG_LEDMAP + i, ledmap[i]); + } + } +} + +uint8_t ledmap_in_eeprom_get_code(uint8_t i) +{ + return ledmap[i]; +} + +#endif diff --git a/common/ledmap_in_eeprom.h b/common/ledmap_in_eeprom.h new file mode 100644 index 00000000..9adf7547 --- /dev/null +++ b/common/ledmap_in_eeprom.h @@ -0,0 +1,20 @@ +#ifndef LEDMAP_IN_EEPROM_H +#define LEDMAP_IN_EEPROM_H + +#ifndef EECONFIG_LEDMAP_IN_EEPROM +#define EECONFIG_LEDMAP_IN_EEPROM 7 +#endif + +#define EECONFIG_LEDMAP (uint8_t*)EECONFIG_LEDMAP_IN_EEPROM +#define LEDMAP_SIZE (sizeof(uint8_t) * LED_COUNT) + +#ifdef LEDMAP_IN_EEPROM_ENABLE +#define ledmap_get_code ledmap_in_eeprom_get_code +void ledmap_in_eeprom_init(void); +uint8_t ledmap_in_eeprom_get_code(uint8_t index); +#else +#define ledmap_in_eeprom_init() +#define ledmap_in_eeprom_get_code() +#endif + +#endif From 0c5e2e8f22a03509e7fbc407bf283bc409bdecf2 Mon Sep 17 00:00:00 2001 From: Kai Ryu Date: Thu, 24 Jul 2014 15:03:58 +0900 Subject: [PATCH 4/7] Implement ledmap and fix bugs --- common/bootmagic.c | 4 ++++ common/keyboard.c | 9 +++++++++ common/ledmap.c | 22 +++++++++++++++------- common/ledmap.h | 6 ++---- common/ledmap_in_eeprom.c | 14 +++++++++++--- common/ledmap_in_eeprom.h | 2 ++ 6 files changed, 43 insertions(+), 14 deletions(-) diff --git a/common/bootmagic.c b/common/bootmagic.c index 1668ee49..cf71b9d4 100644 --- a/common/bootmagic.c +++ b/common/bootmagic.c @@ -8,6 +8,7 @@ #include "action_layer.h" #include "eeconfig.h" #include "keymap_in_eeprom.h" +#include "ledmap_in_eeprom.h" #include "bootmagic.h" @@ -35,6 +36,9 @@ void bootmagic(void) eeconfig_init(); #ifdef KEYMAP_IN_EEPROM_ENABLE write_keymap_to_eeprom(); +#endif +#ifdef LEDMAP_IN_EEPROM_ENABLE + write_ledmap_to_eeprom(); #endif } diff --git a/common/keyboard.c b/common/keyboard.c index c7793eec..a9db9ebd 100644 --- a/common/keyboard.c +++ b/common/keyboard.c @@ -36,6 +36,8 @@ along with this program. If not, see . #else #include "breathing_led.h" #endif +#include "ledmap.h" +#include "ledmap_in_eeprom.h" #include "keymap_in_eeprom.h" #ifdef MOUSEKEY_ENABLE # include "mousekey.h" @@ -82,6 +84,13 @@ void keyboard_init(void) bootmagic(); #endif +#ifdef LEDMAP_ENABLE + ledmap_led_init(); +#ifdef LEDMAP_IN_EEPROM_ENABLE + ledmap_in_eeprom_init(); +#endif +#endif + #ifdef BACKLIGHT_ENABLE backlight_init(); #endif diff --git a/common/ledmap.c b/common/ledmap.c index 2002095e..f8264a89 100644 --- a/common/ledmap.c +++ b/common/ledmap.c @@ -3,6 +3,7 @@ #include "led.h" #include "softpwm_led.h" #include "action_layer.h" +#include "debug.h" static led_state_t led_state = 0; static led_state_t led_state_last = 0; @@ -13,25 +14,32 @@ void led_set(uint8_t usb_led) { 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); + (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); + (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); + (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); + (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); + (usb_led & (1 << USB_LED_KANA)) ? LED_BIT_ON(led_state, i) : LED_BIT_OFF(led_state, i); break; default: break; } + */ + 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); + } + } } update_led_state(); } @@ -42,7 +50,7 @@ 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); + (state & (1UL << (code - LEDMAP_DEFAULT_LAYER_0))) ? LED_BIT_ON(led_state, i) : LED_BIT_OFF(led_state, i); } } update_led_state(); @@ -53,7 +61,7 @@ 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); + (state & (1UL << (code - LEDMAP_LAYER_0))) ? LED_BIT_ON(led_state, i) : LED_BIT_OFF(led_state, i); } } update_led_state(); diff --git a/common/ledmap.h b/common/ledmap.h index 56960edc..de68558a 100644 --- a/common/ledmap.h +++ b/common/ledmap.h @@ -40,10 +40,8 @@ typedef enum { LEDMAP_UNCONFIGURED = 0xFF } ledmap_code_t; -#define LEDMAP_LAYER(x) (x) -#define LEDMAP_DEFAULT_LAYER(x) (32 + x) - -void ledmap_init(void); +#define LEDMAP_DEFAULT_LAYER(x) (LEDMAP_DEFAULT_LAYER_0 + x) +#define LEDMAP_LAYER(x) (LEDMAP_LAYER_0 + x) #ifdef LEDMAP_ENABLE uint8_t ledmap_get_code(uint8_t index); diff --git a/common/ledmap_in_eeprom.c b/common/ledmap_in_eeprom.c index a0c4adb0..f3f7c20a 100644 --- a/common/ledmap_in_eeprom.c +++ b/common/ledmap_in_eeprom.c @@ -9,17 +9,25 @@ static uint8_t ledmap[LED_COUNT]; -void ledmap_in_eeprom_init(void) { - // read and check ledmap in eeprom +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; if (ledmap[i] == LEDMAP_UNCONFIGURED) { - ledmap[i] = lemap_get_code(i); + ledmap[i] = ledmap_get_code(i); eeprom_write_byte(EECONFIG_LEDMAP + i, ledmap[i]); } } } +void write_ledmap_to_eeprom(void) +{ + for (uint8_t i = 0; i < LED_COUNT; i++) { + eeprom_write_byte(EECONFIG_LEDMAP + i, ledmap_get_code(i)); + } +} + uint8_t ledmap_in_eeprom_get_code(uint8_t i) { return ledmap[i]; diff --git a/common/ledmap_in_eeprom.h b/common/ledmap_in_eeprom.h index 9adf7547..588bd81d 100644 --- a/common/ledmap_in_eeprom.h +++ b/common/ledmap_in_eeprom.h @@ -11,9 +11,11 @@ #ifdef LEDMAP_IN_EEPROM_ENABLE #define ledmap_get_code ledmap_in_eeprom_get_code void ledmap_in_eeprom_init(void); +void write_ledmap_to_eeprom(void); uint8_t ledmap_in_eeprom_get_code(uint8_t index); #else #define ledmap_in_eeprom_init() +#define write_ledmap_to_eeprom() #define ledmap_in_eeprom_get_code() #endif From 1c3b8f810fb61ccb4f4e61f54d23ff7d7541bd73 Mon Sep 17 00:00:00 2001 From: Kai Ryu Date: Fri, 25 Jul 2014 14:24:57 +0900 Subject: [PATCH 5/7] 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< Date: Sat, 2 Aug 2014 10:15:57 +0900 Subject: [PATCH 6/7] 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() From 9c61e8e4f6e6356bcba6a7ee7e05664a5028a9b5 Mon Sep 17 00:00:00 2001 From: Kai Ryu Date: Sun, 3 Aug 2014 11:27:54 +0900 Subject: [PATCH 7/7] Change ledmap code definition --- common/ledmap.h | 14 +++++++------- common/ledmap_in_eeprom.h | 2 ++ 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/common/ledmap.h b/common/ledmap.h index bbd15af6..9a6754f3 100644 --- a/common/ledmap.h +++ b/common/ledmap.h @@ -9,17 +9,17 @@ typedef led_pack_t led_state_t; typedef led_pack_t led_binding_t; typedef enum { - LEDMAP_DEFAULT_LAYER_0 = 0, - LEDMAP_DEFAULT_LAYER_31 = 31, - LEDMAP_LAYER_0 = 32, - LEDMAP_LAYER_31 = 63, - LEDMAP_NUM_LOCK = 64, + LEDMAP_NO = 0, + LEDMAP_DEFAULT_LAYER_0, + LEDMAP_DEFAULT_LAYER_31 = LEDMAP_DEFAULT_LAYER_0 + 31, + LEDMAP_LAYER_0, + LEDMAP_LAYER_31 = LEDMAP_LAYER_0 + 31, + LEDMAP_NUM_LOCK, LEDMAP_CAPS_LOCK, LEDMAP_SCROLL_LOCK, LEDMAP_COMPOSE, LEDMAP_KANA, - LEDMAP_BACKLIGHT = 0x80, - LEDMAP_UNCONFIGURED = 0xFF + LEDMAP_BACKLIGHT = 0x80 } ledmap_code_t; #define LEDMAP_MASK 0x7F diff --git a/common/ledmap_in_eeprom.h b/common/ledmap_in_eeprom.h index 588bd81d..3e6400c3 100644 --- a/common/ledmap_in_eeprom.h +++ b/common/ledmap_in_eeprom.h @@ -8,6 +8,8 @@ #define EECONFIG_LEDMAP (uint8_t*)EECONFIG_LEDMAP_IN_EEPROM #define LEDMAP_SIZE (sizeof(uint8_t) * LED_COUNT) +#define LEDMAP_UNCONFIGURED 0xFF + #ifdef LEDMAP_IN_EEPROM_ENABLE #define ledmap_get_code ledmap_in_eeprom_get_code void ledmap_in_eeprom_init(void);