From 0c5e2e8f22a03509e7fbc407bf283bc409bdecf2 Mon Sep 17 00:00:00 2001 From: Kai Ryu Date: Thu, 24 Jul 2014 15:03:58 +0900 Subject: [PATCH] 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