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 34886dfffb
commit 54360bf85b
7 changed files with 120 additions and 60 deletions

View File

@ -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

View File

@ -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;
}
}

View File

@ -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);

View File

@ -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]);

View File

@ -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<<OCIE1A);
//dprintf("softpwm led on: %u\n", TIMSK1 & (1<<OCIE1A));
#endif
softpwm_state = 1;
softpwm_led_state_change(softpwm_state);
}
void softpwm_led_disable(void)
@ -61,6 +64,9 @@ void softpwm_led_disable(void)
TIMSK1 &= ~(1<<OCIE1A);
//dprintf("softpwm led off: %u\n", TIMSK1 & (1<<OCIE1A));
#endif
softpwm_state = 0;
softpwm_led_off();
softpwm_led_state_change(softpwm_state);
}
void softpwm_led_toggle(void)
@ -73,12 +79,21 @@ void softpwm_led_toggle(void)
TIMSK1 ^= (1<<OCIE1A);
//dprintf("softpwm led toggle: %u\n", TIMSK1 & (1<<OCIE1A));
#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;
}
inline uint8_t softpwm_led_get_state(void)
{
return softpwm_state;
}
#ifdef BREATHING_LED_ENABLE
/* 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_on(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
#define breathing_led_init()
@ -34,6 +36,8 @@ void breathing_led_set_duration(uint8_t dur);
#define softpwm_led_set()
#define softpwm_led_on()
#define softpwm_led_off()
#define softpwm_led_get_state()
#define softpwm_led_state_change()
#endif

View File

@ -7,14 +7,14 @@
static const uint8_t ledmaps[LED_COUNT] PROGMEM = {
#if defined(GH60_REV_CHN)
[0] = LEDMAP_CAPS_LOCK, // CapsLock - PB2
[1] = LEDMAP_BACKLIGHT, // PWM - PB6
[0] = LEDMAP_CAPS_LOCK | LEDMAP_BACKLIGHT, // CapsLock - PB2
[1] = LEDMAP_BACKLIGHT, // PWM - PB6
#else
[0] = LEDMAP_CAPS_LOCK, // CapsLock - PB2
[1] = LEDMAP_BACKLIGHT, // Esc - GPIO1 - PF6
[2] = LEDMAP_LAYER(1), // WASD - GPIO0 - PF7
[3] = LEDMAP_LAYER(2), // Sh/Al/Ct - GPIO3 - PF4
[4] = LEDMAP_LAYER(1), // Fn - GPIO2 - PF5
[0] = LEDMAP_CAPS_LOCK | LEDMAP_BACKLIGHT, // CapsLock - PB2
[1] = LEDMAP_BACKLIGHT, // Esc - GPIO1 - PF6
[2] = LEDMAP_LAYER(1) | LEDMAP_BACKLIGHT, // WASD - GPIO0 - PF7
[3] = LEDMAP_LAYER(2) | LEDMAP_BACKLIGHT, // Sh/Al/Ct - GPIO3 - PF4
[4] = LEDMAP_LAYER(1) | LEDMAP_BACKLIGHT, // Fn - GPIO2 - PF5
#endif
};