소스 검색

Move backlight to a separated bit to realize complex ledmap combinations

yuk86
Kai Ryu 10 년 전
부모
커밋
54360bf85b
7개의 변경된 파일119개의 추가작업 그리고 59개의 파일을 삭제
  1. 1
    1
      common/keyboard.c
  2. 71
    45
      common/ledmap.c
  3. 17
    2
      common/ledmap.h
  4. 1
    1
      common/ledmap_in_eeprom.c
  5. 18
    3
      common/softpwm_led.c
  6. 4
    0
      common/softpwm_led.h
  7. 7
    7
      keyboard/gh60/ledmap.c

+ 1
- 1
common/keyboard.c 파일 보기

#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

+ 71
- 45
common/ledmap.c 파일 보기

#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);
/*
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);
}
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);
} }
*/
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);
}
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);
} }
update_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++) {
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) 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 #endif


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 (code == LEDMAP_BACKLIGHT) {
if (backlight_binding & LED_BIT(i)) {
ledmap_led_on(i); ledmap_led_on(i);
} }
} }
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 (code == LEDMAP_BACKLIGHT) {
if (backlight_binding & LED_BIT(i)) {
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 {
} }
} }
} }
led_state_last = led_state;
led_state_last = state;
} }
} }

+ 17
- 2
common/ledmap.h 파일 보기

#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;
#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))
#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_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 { typedef enum {
LEDMAP_DEFAULT_LAYER_0 = 0, LEDMAP_DEFAULT_LAYER_0 = 0,
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);

+ 1
- 1
common/ledmap_in_eeprom.c 파일 보기

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

+ 18
- 3
common/softpwm_led.c 파일 보기

#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;
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) void softpwm_led_init(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)
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)
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

+ 4
- 0
common/softpwm_led.h 파일 보기

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()
#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



+ 7
- 7
keyboard/gh60/ledmap.c 파일 보기



static const uint8_t ledmaps[LED_COUNT] PROGMEM = { static const uint8_t ledmaps[LED_COUNT] PROGMEM = {
#if defined(GH60_REV_CHN) #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 #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 #endif
}; };