1
0

Move backlight to a separated bit to realize complex ledmap combinations

This commit is contained in:
Kai Ryu 2014-07-25 14:24:57 +09:00
parent 0c5e2e8f22
commit 1c3b8f810f
6 changed files with 113 additions and 53 deletions

View File

@ -85,10 +85,10 @@ void keyboard_init(void)
#endif #endif
#ifdef LEDMAP_ENABLE #ifdef LEDMAP_ENABLE
ledmap_led_init();
#ifdef LEDMAP_IN_EEPROM_ENABLE #ifdef LEDMAP_IN_EEPROM_ENABLE
ledmap_in_eeprom_init(); ledmap_in_eeprom_init();
#endif #endif
ledmap_init();
#endif #endif
#ifdef BACKLIGHT_ENABLE #ifdef BACKLIGHT_ENABLE

View File

@ -5,66 +5,80 @@
#include "action_layer.h" #include "action_layer.h"
#include "debug.h" #include "debug.h"
static led_state_t led_state = 0;
static led_state_t led_state_last = 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++) { for (uint8_t i = 0; i < LED_COUNT; i++) {
uint8_t code = ledmap_get_code(i); uint8_t code = ledmap_get_code(i);
/* if (code & LEDMAP_BACKLIGHT) {
switch (code) { LED_BIT_SET(backlight_binding, i);
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;
} }
*/ code &= LEDMAP_MASK;
for (uint8_t j = USB_LED_NUM_LOCK; j <= USB_LED_KANA; j++) { if (code >= LEDMAP_DEFAULT_LAYER_0 && code <= LEDMAP_DEFAULT_LAYER_31) {
if (code - LEDMAP_NUM_LOCK == j) { LED_BIT_SET(default_layer_binding, i);
(usb_led & (1 << j)) ? LED_BIT_ON(led_state, i) : LED_BIT_OFF(led_state, 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 #ifndef NO_ACTION_LAYER
void default_layer_state_change(uint32_t state) void default_layer_state_change(uint32_t state)
{ {
for (uint8_t i = 0; i < LED_COUNT; i++) { if (default_layer_binding) {
uint8_t code = ledmap_get_code(i); led_state_t led_state = led_state_last;
if (code >= LEDMAP_DEFAULT_LAYER_0 && code < LEDMAP_DEFAULT_LAYER_31) { for (uint8_t i = 0; i < LED_COUNT; i++) {
(state & (1UL << (code - LEDMAP_DEFAULT_LAYER_0))) ? LED_BIT_ON(led_state, i) : LED_BIT_OFF(led_state, 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) void layer_state_change(uint32_t state)
{ {
for (uint8_t i = 0; i < LED_COUNT; i++) { if (layer_binding) {
uint8_t code = ledmap_get_code(i); led_state_t led_state = led_state_last;
if (code >= LEDMAP_LAYER_0 && code < LEDMAP_LAYER_31) { for (uint8_t i = 0; i < LED_COUNT; i++) {
(state & (1UL << (code - LEDMAP_LAYER_0))) ? LED_BIT_ON(led_state, i) : LED_BIT_OFF(led_state, 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 #endif
@ -72,8 +86,7 @@ void layer_state_change(uint32_t state)
void softpwm_led_on() void softpwm_led_on()
{ {
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); if (backlight_binding & LED_BIT(i)) {
if (code == LEDMAP_BACKLIGHT) {
ledmap_led_on(i); ledmap_led_on(i);
} }
} }
@ -82,21 +95,34 @@ void softpwm_led_on()
void softpwm_led_off() void softpwm_led_off()
{ {
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); if (backlight_binding & LED_BIT(i)) {
if (code == LEDMAP_BACKLIGHT) {
ledmap_led_off(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 #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) { if (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))) {
continue;
}
if (diff & LED_BIT(i)) { if (diff & LED_BIT(i)) {
if (led_state & LED_BIT(i)) { if (state & LED_BIT(i)) {
ledmap_led_on(i); ledmap_led_on(i);
} }
else { else {
@ -104,6 +130,6 @@ void update_led_state(void)
} }
} }
} }
led_state_last = led_state; led_state_last = state;
} }
} }

View File

@ -2,6 +2,7 @@
#define LEDMAP_H #define LEDMAP_H
#include "stdint.h" #include "stdint.h"
#include "stdbool.h"
#if (LED_COUNT <= 8) #if (LED_COUNT <= 8)
typedef uint8_t led_pack_t; typedef uint8_t led_pack_t;
@ -14,6 +15,7 @@ typedef uint32_t led_pack_t;
#endif #endif
typedef led_pack_t led_state_t; typedef led_pack_t led_state_t;
typedef led_pack_t led_binding_t;
#if (LED_COUNT <= 16) #if (LED_COUNT <= 16)
#define LED_BIT(i) (1U<<(i)) #define LED_BIT(i) (1U<<(i))
@ -23,8 +25,9 @@ typedef led_pack_t led_state_t;
#error "LED_COUNT: invalid value" #error "LED_COUNT: invalid value"
#endif #endif
#define LED_BIT_ON(state, i) ((state) |= LED_BIT(i)) #define LED_BIT_SET(x, i) ((x) |= LED_BIT(i))
#define LED_BIT_OFF(state, i) ((state) &= ~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 { typedef enum {
LEDMAP_DEFAULT_LAYER_0 = 0, LEDMAP_DEFAULT_LAYER_0 = 0,
@ -40,9 +43,21 @@ typedef enum {
LEDMAP_UNCONFIGURED = 0xFF LEDMAP_UNCONFIGURED = 0xFF
} ledmap_code_t; } 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_DEFAULT_LAYER(x) (LEDMAP_DEFAULT_LAYER_0 + x)
#define LEDMAP_LAYER(x) (LEDMAP_LAYER_0 + x) #define LEDMAP_LAYER(x) (LEDMAP_LAYER_0 + x)
void ledmap_init(void);
#ifdef LEDMAP_ENABLE #ifdef LEDMAP_ENABLE
uint8_t ledmap_get_code(uint8_t index); uint8_t ledmap_get_code(uint8_t index);
void ledmap_led_init(void); void ledmap_led_init(void);

View File

@ -13,7 +13,7 @@ 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] = eeprom_read_byte(EECONFIG_LEDMAP + i);
ledmap[i] = LEDMAP_UNCONFIGURED; //ledmap[i] = LEDMAP_UNCONFIGURED;
if (ledmap[i] == LEDMAP_UNCONFIGURED) { if (ledmap[i] == LEDMAP_UNCONFIGURED) {
ledmap[i] = ledmap_get_code(i); ledmap[i] = ledmap_get_code(i);
eeprom_write_byte(EECONFIG_LEDMAP + i, ledmap[i]); eeprom_write_byte(EECONFIG_LEDMAP + i, ledmap[i]);

View File

@ -7,8 +7,9 @@
#define SOFTPWM_LED_FREQ 64 #define SOFTPWM_LED_FREQ 64
#define SOFTPWM_LED_TIMER_TOP F_CPU / (256 * SOFTPWM_LED_FREQ) #define SOFTPWM_LED_TIMER_TOP F_CPU / (256 * SOFTPWM_LED_FREQ)
uint8_t softpwm_ocr = 0; static uint8_t softpwm_state = 0;
uint8_t softpwm_ocr_buff = 0; static uint8_t softpwm_ocr = 0;
static uint8_t softpwm_ocr_buff = 0;
void softpwm_led_init(void) void softpwm_led_init(void)
{ {
@ -49,6 +50,8 @@ void softpwm_led_enable(void)
TIMSK1 |= (1<<OCIE1A); TIMSK1 |= (1<<OCIE1A);
//dprintf("softpwm led on: %u\n", TIMSK1 & (1<<OCIE1A)); //dprintf("softpwm led on: %u\n", TIMSK1 & (1<<OCIE1A));
#endif #endif
softpwm_state = 1;
softpwm_led_state_change(softpwm_state);
} }
void softpwm_led_disable(void) void softpwm_led_disable(void)
@ -61,6 +64,9 @@ void softpwm_led_disable(void)
TIMSK1 &= ~(1<<OCIE1A); TIMSK1 &= ~(1<<OCIE1A);
//dprintf("softpwm led off: %u\n", TIMSK1 & (1<<OCIE1A)); //dprintf("softpwm led off: %u\n", TIMSK1 & (1<<OCIE1A));
#endif #endif
softpwm_state = 0;
softpwm_led_off();
softpwm_led_state_change(softpwm_state);
} }
void softpwm_led_toggle(void) void softpwm_led_toggle(void)
@ -73,12 +79,21 @@ void softpwm_led_toggle(void)
TIMSK1 ^= (1<<OCIE1A); TIMSK1 ^= (1<<OCIE1A);
//dprintf("softpwm led toggle: %u\n", TIMSK1 & (1<<OCIE1A)); //dprintf("softpwm led toggle: %u\n", TIMSK1 & (1<<OCIE1A));
#endif #endif
softpwm_state ^= 1;
if (!softpwm_state) softpwm_led_off();
softpwm_led_state_change(softpwm_state);
} }
void softpwm_led_set(uint8_t val) { void softpwm_led_set(uint8_t val)
{
softpwm_ocr_buff = val; softpwm_ocr_buff = val;
} }
inline uint8_t softpwm_led_get_state(void)
{
return softpwm_state;
}
#ifdef BREATHING_LED_ENABLE #ifdef BREATHING_LED_ENABLE
/* Breathing LED brighness(PWM On period) table /* Breathing LED brighness(PWM On period) table

View File

@ -10,6 +10,8 @@ void softpwm_led_toggle(void);
void softpwm_led_set(uint8_t val); void softpwm_led_set(uint8_t val);
void softpwm_led_on(void); void softpwm_led_on(void);
void softpwm_led_off(void); void softpwm_led_off(void);
uint8_t softpwm_led_get_state(void);
void softpwm_led_state_change(uint8_t state);
#ifdef BREATHING_LED_ENABLE #ifdef BREATHING_LED_ENABLE
#define breathing_led_init() #define breathing_led_init()
@ -34,6 +36,8 @@ void breathing_led_set_duration(uint8_t dur);
#define softpwm_led_set() #define softpwm_led_set()
#define softpwm_led_on() #define softpwm_led_on()
#define softpwm_led_off() #define softpwm_led_off()
#define softpwm_led_get_state()
#define softpwm_led_state_change()
#endif #endif