1
0

Improve ledmap feature

- Change date type of ledmap from byte to word
- New reverse bit, reversing binding stats
- Fix minor bugs
This commit is contained in:
Kai Ryu 2014-11-25 18:23:16 +09:00
parent 4cf7e9fd7b
commit 1ac468aa5d
4 changed files with 74 additions and 67 deletions

View File

@ -10,28 +10,34 @@ static led_binding_t usb_led_binding = 0;
static led_binding_t default_layer_binding = 0; static led_binding_t default_layer_binding = 0;
static led_binding_t layer_binding = 0; static led_binding_t layer_binding = 0;
static led_binding_t backlight_binding = 0; static led_binding_t backlight_binding = 0;
static led_binding_t reverse_binding = 0;
static void update_led_state(led_state_t state); static void update_led_state(led_state_t state, uint8_t force);
void ledmap_init(void) void ledmap_init(void)
{ {
for (uint8_t i = 0; i < LED_COUNT; i++) { for (uint8_t i = 0; i < LED_COUNT; i++) {
uint8_t code = ledmap_get_code(i); ledmap_t ledmap = ledmap_get_code(i);
if (code & LEDMAP_BACKLIGHT) { if (ledmap.reverse) {
LED_BIT_SET(reverse_binding, i);
}
if (ledmap.backlight) {
LED_BIT_SET(backlight_binding, i); LED_BIT_SET(backlight_binding, i);
} }
code &= LEDMAP_MASK; switch (ledmap.binding) {
if (code >= LEDMAP_DEFAULT_LAYER_0 && code <= LEDMAP_DEFAULT_LAYER_31) { case LEDMAP_BINDING_DEFAULT_LAYER:
LED_BIT_SET(default_layer_binding, i); LED_BIT_SET(default_layer_binding, i);
} break;
else if (code >= LEDMAP_LAYER_0 && code <= LEDMAP_LAYER_31) { case LEDMAP_BINDING_LAYER:
LED_BIT_SET(layer_binding, i); LED_BIT_SET(layer_binding, i);
} break;
else if (code >= LEDMAP_NUM_LOCK && code <= LEDMAP_KANA) { case LEDMAP_BINDING_USB_LED:
LED_BIT_SET(usb_led_binding, i); LED_BIT_SET(usb_led_binding, i);
break;
} }
} }
ledmap_led_init(); ledmap_led_init();
update_led_state(0, 1);
} }
void led_set(uint8_t usb_led) void led_set(uint8_t usb_led)
@ -40,15 +46,11 @@ void led_set(uint8_t usb_led)
led_state_t led_state = led_state_last; led_state_t led_state = led_state_last;
for (uint8_t i = 0; i < LED_COUNT; i++) { for (uint8_t i = 0; i < LED_COUNT; i++) {
if (usb_led_binding & LED_BIT(i)) { if (usb_led_binding & LED_BIT(i)) {
uint8_t code = ledmap_get_code(i) & LEDMAP_MASK; uint8_t param = ledmap_get_code(i).param;
for (uint8_t j = USB_LED_NUM_LOCK; j <= USB_LED_KANA; j++) { (usb_led & (1 << param)) ? LED_BIT_SET(led_state, i) : LED_BIT_CLEAR(led_state, i);
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, 0);
}
update_led_state(led_state);
} }
} }
@ -59,11 +61,11 @@ void default_layer_state_change(uint32_t state)
led_state_t led_state = led_state_last; led_state_t led_state = led_state_last;
for (uint8_t i = 0; i < LED_COUNT; i++) { for (uint8_t i = 0; i < LED_COUNT; i++) {
if (default_layer_binding & LED_BIT(i)) { if (default_layer_binding & LED_BIT(i)) {
uint8_t code = ledmap_get_code(i) & LEDMAP_MASK; uint8_t param = ledmap_get_code(i).param;
(state & (1UL << (code - LEDMAP_DEFAULT_LAYER_0))) ? LED_BIT_SET(led_state, i) : LED_BIT_CLEAR(led_state, i); (state & (1UL << param)) ? LED_BIT_SET(led_state, i) : LED_BIT_CLEAR(led_state, i);
} }
} }
update_led_state(led_state); update_led_state(led_state, 0);
} }
} }
@ -73,11 +75,11 @@ void layer_state_change(uint32_t state)
led_state_t led_state = led_state_last; led_state_t led_state = led_state_last;
for (uint8_t i = 0; i < LED_COUNT; i++) { for (uint8_t i = 0; i < LED_COUNT; i++) {
if (layer_binding & LED_BIT(i)) { if (layer_binding & LED_BIT(i)) {
uint8_t code = ledmap_get_code(i) & LEDMAP_MASK; uint8_t param = ledmap_get_code(i).param;
(state & (1UL << (code - LEDMAP_LAYER_0))) ? LED_BIT_SET(led_state, i) : LED_BIT_CLEAR(led_state, i); (state & (1UL << param)) ? LED_BIT_SET(led_state, i) : LED_BIT_CLEAR(led_state, i);
} }
} }
update_led_state(led_state); update_led_state(led_state, 0);
} }
} }
#endif #endif
@ -106,23 +108,21 @@ void softpwm_led_state_change(uint8_t state)
if (state) { if (state) {
} }
else { else {
led_state_t led_state = led_state_last; update_led_state(led_state_last, 1);
led_state_last &= ~(backlight_binding);
update_led_state(led_state);
} }
} }
#endif #endif
void update_led_state(led_state_t state) void update_led_state(led_state_t state, uint8_t force)
{ {
uint8_t diff = led_state_last ^ state; led_state_t diff = led_state_last ^ state;
if (diff) { if (force || diff) {
for (uint8_t i = 0; i < LED_COUNT; i++) { for (uint8_t i = 0; i < LED_COUNT; i++) {
if (softpwm_led_get_state() && (backlight_binding & LED_BIT(i))) { if (softpwm_led_get_state() && (backlight_binding & LED_BIT(i))) {
continue; continue;
} }
if (diff & LED_BIT(i)) { if (force || diff & LED_BIT(i)) {
if (state & LED_BIT(i)) { if ((state ^ reverse_binding) & LED_BIT(i)) {
ledmap_led_on(i); ledmap_led_on(i);
} }
else { else {

View File

@ -8,43 +8,49 @@
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;
typedef enum { enum ledmap_binding {
LEDMAP_NO = 0, LEDMAP_BINDING_NO = 0,
LEDMAP_DEFAULT_LAYER_0, LEDMAP_BINDING_DEFAULT_LAYER,
LEDMAP_DEFAULT_LAYER_31 = LEDMAP_DEFAULT_LAYER_0 + 31, LEDMAP_BINDING_LAYER,
LEDMAP_LAYER_0, LEDMAP_BINDING_USB_LED
LEDMAP_LAYER_31 = LEDMAP_LAYER_0 + 31, };
LEDMAP_NUM_LOCK,
LEDMAP_CAPS_LOCK,
LEDMAP_SCROLL_LOCK,
LEDMAP_COMPOSE,
LEDMAP_KANA,
LEDMAP_BACKLIGHT = 0x80
} ledmap_code_t;
#define LEDMAP_MASK 0x7F enum ledmap_usb_led {
LEDMAP_USB_LED_NUM_LOCK = 0,
LEDMAP_USB_LED_CAPS_LOCK,
LEDMAP_USB_LED_SCROLL_LOCK,
LEDMAP_USB_LED_COMPOSE,
LEDMAP_USB_LED_KANA,
};
typedef union { typedef union {
uint8_t raw; uint16_t code;
struct { struct {
uint8_t binding : 7; uint8_t param: 8;
bool backlight : 1; uint8_t binding: 4;
bool reverse: 1;
bool backlight: 1;
uint8_t reserved: 2;
}; };
} ledmap_t; } ledmap_t;
#define LEDMAP_DEFAULT_LAYER(x) (LEDMAP_DEFAULT_LAYER_0 + x) #define LEDMAP_DEFAULT_LAYER(layer) (LEDMAP_BINDING_DEFAULT_LAYER<<8 | layer)
#define LEDMAP_LAYER(x) (LEDMAP_LAYER_0 + x) #define LEDMAP_LAYER(layer) (LEDMAP_BINDING_LAYER<<8 | layer )
#define LEDMAP_NUM_LOCK (LEDMAP_BINDING_USB_LED<<8 | LEDMAP_USB_LED_NUM_LOCK)
#define LEDMAP_CAPS_LOCK (LEDMAP_BINDING_USB_LED<<8 | LEDMAP_USB_LED_CAPS_LOCK)
#define LEDMAP_SCROLL_LOCK (LEDMAP_BINDING_USB_LED<<8 | LEDMAP_USB_LED_SCROLL_LOCK)
#define LEDMAP_REVERSE (1<<12)
#define LEDMAP_BACKLIGHT (1<<13)
void ledmap_init(void); void ledmap_init(void);
#ifdef LEDMAP_ENABLE #ifdef LEDMAP_ENABLE
uint8_t ledmap_get_code(uint8_t index); ledmap_t ledmap_get_code(uint8_t index);
void ledmap_led_init(void); void ledmap_led_init(void);
void ledmap_led_on(uint8_t index); void ledmap_led_on(uint8_t index);
void ledmap_led_off(uint8_t index); void ledmap_led_off(uint8_t index);
#else #else
#define ledmaps #define ledmap_get_code()
#define ledmap_get()
#define ledmap_led_init() #define ledmap_led_init()
#define ledmap_led_on() #define ledmap_led_on()
#define ledmap_led_off() #define ledmap_led_off()

View File

@ -1,22 +1,21 @@
#include <avr/pgmspace.h> #include <avr/pgmspace.h>
#include <avr/eeprom.h> #include <avr/eeprom.h>
#include "ledmap.h"
#include "ledmap_in_eeprom.h" #include "ledmap_in_eeprom.h"
#ifdef LEDMAP_IN_EEPROM_ENABLE #ifdef LEDMAP_IN_EEPROM_ENABLE
#undef ledmap_get_code #undef ledmap_get_code
static uint8_t ledmap[LED_COUNT]; static ledmap_t ledmap[LED_COUNT];
void ledmap_in_eeprom_init(void) void ledmap_in_eeprom_init(void)
{ {
for (uint8_t i = 0; i < LED_COUNT; i++) { for (uint8_t i = 0; i < LED_COUNT; i++) {
ledmap[i] = eeprom_read_byte(EECONFIG_LEDMAP + i); ledmap[i].code = eeprom_read_word(EECONFIG_LEDMAP + i);
//ledmap[i] = LEDMAP_UNCONFIGURED; /* ledmap[i].code = LEDMAP_UNCONFIGURED; */
if (ledmap[i] == LEDMAP_UNCONFIGURED) { if (ledmap[i].code == LEDMAP_UNCONFIGURED) {
ledmap[i] = ledmap_get_code(i); ledmap[i] = ledmap_get_code(i);
eeprom_write_byte(EECONFIG_LEDMAP + i, ledmap[i]); eeprom_write_word(EECONFIG_LEDMAP + i, ledmap[i].code);
} }
} }
} }
@ -24,11 +23,11 @@ void ledmap_in_eeprom_init(void)
void write_ledmap_to_eeprom(void) void write_ledmap_to_eeprom(void)
{ {
for (uint8_t i = 0; i < LED_COUNT; i++) { for (uint8_t i = 0; i < LED_COUNT; i++) {
eeprom_write_byte(EECONFIG_LEDMAP + i, ledmap_get_code(i)); eeprom_write_word(EECONFIG_LEDMAP + i, ledmap_get_code(i).code);
} }
} }
uint8_t ledmap_in_eeprom_get_code(uint8_t i) ledmap_t ledmap_in_eeprom_get_code(uint8_t i)
{ {
return ledmap[i]; return ledmap[i];
} }

View File

@ -1,20 +1,22 @@
#ifndef LEDMAP_IN_EEPROM_H #ifndef LEDMAP_IN_EEPROM_H
#define LEDMAP_IN_EEPROM_H #define LEDMAP_IN_EEPROM_H
#include "ledmap.h"
#ifndef EECONFIG_LEDMAP_IN_EEPROM #ifndef EECONFIG_LEDMAP_IN_EEPROM
#define EECONFIG_LEDMAP_IN_EEPROM 7 #define EECONFIG_LEDMAP_IN_EEPROM 7
#endif #endif
#define EECONFIG_LEDMAP (uint8_t*)EECONFIG_LEDMAP_IN_EEPROM #define EECONFIG_LEDMAP (uint16_t*)EECONFIG_LEDMAP_IN_EEPROM
#define LEDMAP_SIZE (sizeof(uint8_t) * LED_COUNT) #define LEDMAP_SIZE (sizeof(uint16_t) * LED_COUNT)
#define LEDMAP_UNCONFIGURED 0xFF #define LEDMAP_UNCONFIGURED 0xFFFF
#ifdef LEDMAP_IN_EEPROM_ENABLE #ifdef LEDMAP_IN_EEPROM_ENABLE
#define ledmap_get_code ledmap_in_eeprom_get_code #define ledmap_get_code ledmap_in_eeprom_get_code
void ledmap_in_eeprom_init(void); void ledmap_in_eeprom_init(void);
void write_ledmap_to_eeprom(void); void write_ledmap_to_eeprom(void);
uint8_t ledmap_in_eeprom_get_code(uint8_t index); ledmap_t ledmap_in_eeprom_get_code(uint8_t index);
#else #else
#define ledmap_in_eeprom_init() #define ledmap_in_eeprom_init()
#define write_ledmap_to_eeprom() #define write_ledmap_to_eeprom()