1
0

Merge branch 'ledmap'

This commit is contained in:
Kai Ryu 2014-08-03 11:33:02 +09:00
commit 22273c5edf
12 changed files with 441 additions and 60 deletions

View File

@ -1,18 +1,18 @@
COMMON_DIR = common
SRC += $(COMMON_DIR)/host.c \
$(COMMON_DIR)/keyboard.c \
$(COMMON_DIR)/action.c \
$(COMMON_DIR)/action_tapping.c \
$(COMMON_DIR)/action_macro.c \
$(COMMON_DIR)/action_layer.c \
$(COMMON_DIR)/action_util.c \
$(COMMON_DIR)/keymap.c \
$(COMMON_DIR)/timer.c \
$(COMMON_DIR)/print.c \
$(COMMON_DIR)/bootloader.c \
$(COMMON_DIR)/suspend.c \
$(COMMON_DIR)/xprintf.S \
$(COMMON_DIR)/util.c
SRC += $(COMMON_DIR)/host.c \
$(COMMON_DIR)/keyboard.c \
$(COMMON_DIR)/action.c \
$(COMMON_DIR)/action_tapping.c \
$(COMMON_DIR)/action_macro.c \
$(COMMON_DIR)/action_layer.c \
$(COMMON_DIR)/action_util.c \
$(COMMON_DIR)/keymap.c \
$(COMMON_DIR)/timer.c \
$(COMMON_DIR)/print.c \
$(COMMON_DIR)/bootloader.c \
$(COMMON_DIR)/suspend.c \
$(COMMON_DIR)/xprintf.S \
$(COMMON_DIR)/util.c
# Option modules
@ -76,6 +76,16 @@ ifdef BACKLIGHT_ENABLE
OPT_DEFS += -DBACKLIGHT_ENABLE
endif
ifdef LEDMAP_ENABLE
SRC += $(COMMON_DIR)/ledmap.c
OPT_DEFS += -DLEDMAP_ENABLE
ifdef LEDMAP_IN_EEPROM_ENABLE
SRC += $(COMMON_DIR)/ledmap_in_eeprom.c
OPT_DEFS += -DLEDMAP_IN_EEPROM_ENABLE
endif
endif
ifdef KEYMAP_SECTION_ENABLE
OPT_DEFS += -DKEYMAP_SECTION_ENABLE
EXTRALDFLAGS = -Wl,-L$(TOP_DIR),-Tldscript_keymap_avr5.x

View File

@ -22,6 +22,9 @@ static void default_layer_state_set(uint32_t state)
default_layer_debug(); debug(" to ");
default_layer_state = state;
default_layer_debug(); debug("\n");
#ifdef LEDMAP_ENABLE
default_layer_state_change(state);
#endif
clear_keyboard_but_mods(); // To avoid stuck keys
}
@ -63,6 +66,9 @@ static void layer_state_set(uint32_t state)
layer_debug(); dprint(" to ");
layer_state = state;
layer_debug(); dprintln();
#ifdef LEDMAP_ENABLE
layer_state_change(state);
#endif
clear_keyboard_but_mods(); // To avoid stuck keys
}

View File

@ -70,6 +70,14 @@ void layer_xor(uint32_t state);
#define layer_debug()
#endif
#ifdef LEDMAP_ENABLE
void default_layer_state_change(uint32_t state);
void layer_state_change(uint32_t state);
#else
#define default_layer_state_change()
#define layer_state_change()
#endif
/* return action depending on current layer status */
action_t layer_switch_get_action(key_t key);

View File

@ -9,6 +9,7 @@
#include "action_layer.h"
#include "eeconfig.h"
#include "keymap_in_eeprom.h"
#include "ledmap_in_eeprom.h"
#include "bootmagic.h"
@ -36,6 +37,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
}

View File

@ -36,6 +36,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#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
#ifdef LEDMAP_IN_EEPROM_ENABLE
ledmap_in_eeprom_init();
#endif
ledmap_init();
#endif
#ifdef BACKLIGHT_ENABLE
backlight_init();
#endif

View File

@ -19,6 +19,32 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#define LED_H
#include "stdint.h"
#ifndef LED_COUNT
#define LED_COUNT 1
#endif
#if (LED_COUNT <= 8)
typedef uint8_t led_pack_t;
#elif (LED_COUNT <= 16)
typedef uint16_t led_pack_t;
#elif (LED_COUNT <= 32)
typedef uint32_t led_pack_t;
#else
#error "LED_COUNT: invalid value"
#endif
#if (LED_COUNT <= 16)
#define LED_BIT(i) (1U<<(i))
#elif (LED_COUNT <= 32)
#define LED_BIT(i) (1UL<<(i))
#else
#error "LED_COUNT: invalid value"
#endif
#define LED_BIT_SET(x, i) ((x) |= LED_BIT(i))
#define LED_BIT_CLEAR(x, i) ((x) &= ~LED_BIT(i))
#define LED_BIT_XOR(x, i) ((x) ^= LED_BIT(i))
#define LED_BIT_IS_SET(x, i) ((x) & LED_BIT(i))
/* keyboard LEDs */
#define USB_LED_NUM_LOCK 0

131
common/ledmap.c Normal file
View File

@ -0,0 +1,131 @@
#include "ledmap.h"
#include "ledmap_in_eeprom.h"
#include "led.h"
#include "softpwm_led.h"
#include "action_layer.h"
#include "debug.h"
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(led_state_t state);
void ledmap_init(void)
{
for (uint8_t i = 0; i < LED_COUNT; i++) {
uint8_t code = ledmap_get_code(i);
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);
}
}
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)
{
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);
}
}
void layer_state_change(uint32_t state)
{
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);
}
}
#endif
#ifdef SOFTPWM_LED_ENABLE
void softpwm_led_on(uint8_t index)
{
if (backlight_binding & LED_BIT(index)) {
ledmap_led_on(index);
}
}
void softpwm_led_off(uint8_t index)
{
if (backlight_binding & LED_BIT(index)) {
ledmap_led_off(index);
}
}
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(led_state_t 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 (state & LED_BIT(i)) {
ledmap_led_on(i);
}
else {
ledmap_led_off(i);
}
}
}
led_state_last = state;
}
}

53
common/ledmap.h Normal file
View File

@ -0,0 +1,53 @@
#ifndef LEDMAP_H
#define LEDMAP_H
#include "stdint.h"
#include "stdbool.h"
#include "led.h"
typedef led_pack_t led_state_t;
typedef led_pack_t led_binding_t;
typedef enum {
LEDMAP_NO = 0,
LEDMAP_DEFAULT_LAYER_0,
LEDMAP_DEFAULT_LAYER_31 = LEDMAP_DEFAULT_LAYER_0 + 31,
LEDMAP_LAYER_0,
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
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);
void ledmap_led_on(uint8_t index);
void ledmap_led_off(uint8_t index);
#else
#define ledmaps
#define ledmap_get()
#define ledmap_led_init()
#define ledmap_led_on()
#define ledmap_led_off()
#endif
#endif

36
common/ledmap_in_eeprom.c Normal file
View File

@ -0,0 +1,36 @@
#include <avr/pgmspace.h>
#include <avr/eeprom.h>
#include "ledmap.h"
#include "ledmap_in_eeprom.h"
#ifdef LEDMAP_IN_EEPROM_ENABLE
#undef ledmap_get_code
static uint8_t ledmap[LED_COUNT];
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] = 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];
}
#endif

24
common/ledmap_in_eeprom.h Normal file
View File

@ -0,0 +1,24 @@
#ifndef LEDMAP_IN_EEPROM_H
#define LEDMAP_IN_EEPROM_H
#ifndef EECONFIG_LEDMAP_IN_EEPROM
#define EECONFIG_LEDMAP_IN_EEPROM 7
#endif
#define EECONFIG_LEDMAP (uint8_t*)EECONFIG_LEDMAP_IN_EEPROM
#define LEDMAP_SIZE (sizeof(uint8_t) * LED_COUNT)
#define LEDMAP_UNCONFIGURED 0xFF
#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
#endif

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_led_state = 0;
static uint8_t softpwm_led_ocr[LED_COUNT] = {0};
static uint8_t softpwm_led_ocr_buff[LED_COUNT] = {0};
void softpwm_led_init(void)
{
@ -44,10 +45,14 @@ void softpwm_led_enable(void)
/* Enable Compare Match Interrupt */
#ifdef SOFTPWM_LED_TIMER3
TIMSK3 |= (1<<OCIE3A);
dprintf("softpwm led on: %u\n", TIMSK3 & (1<<OCIE3A));
//dprintf("softpwm led on: %u\n", TIMSK3 & (1<<OCIE3A));
#else
TIMSK1 |= (1<<OCIE1A);
dprintf("softpwm led on: %u\n", TIMSK1 & (1<<OCIE1A));
//dprintf("softpwm led on: %u\n", TIMSK1 & (1<<OCIE1A));
#endif
softpwm_led_state = 1;
#ifdef LEDMAP_ENABLE
softpwm_led_state_change(softpwm_led_state);
#endif
}
@ -56,27 +61,45 @@ void softpwm_led_disable(void)
/* Disable Compare Match Interrupt */
#ifdef SOFTPWM_LED_TIMER3
TIMSK3 &= ~(1<<OCIE3A);
dprintf("softpwm led off: %u\n", TIMSK3 & (1<<OCIE3A));
//dprintf("softpwm led off: %u\n", TIMSK3 & (1<<OCIE3A));
#else
TIMSK1 &= ~(1<<OCIE1A);
dprintf("softpwm led off: %u\n", TIMSK1 & (1<<OCIE1A));
//dprintf("softpwm led off: %u\n", TIMSK1 & (1<<OCIE1A));
#endif
softpwm_led_state = 0;
for (uint8_t i = 0; i < LED_COUNT; i++) {
softpwm_led_off(i);
}
#ifdef LEDMAP_ENABLE
softpwm_led_state_change(softpwm_led_state);
#endif
}
void softpwm_led_toggle(void)
{
/* Disable Compare Match Interrupt */
#ifdef SOFTPWM_LED_TIMER3
TIMSK3 ^= (1<<OCIE3A);
dprintf("softpwm led toggle: %u\n", TIMSK3 & (1<<OCIE3A));
#else
TIMSK1 ^= (1<<OCIE1A);
dprintf("softpwm led toggle: %u\n", TIMSK1 & (1<<OCIE1A));
#endif
if (softpwm_led_state) {
softpwm_led_disable();
}
else {
softpwm_led_enable();
}
}
void softpwm_led_set(uint8_t val) {
softpwm_ocr_buff = val;
void softpwm_led_set(uint8_t index, uint8_t val)
{
softpwm_led_ocr_buff[index] = val;
}
void softpwm_led_set_all(uint8_t val)
{
for (uint8_t i = 0; i < LED_COUNT; i++) {
softpwm_led_ocr_buff[i] = val;
}
}
inline uint8_t softpwm_led_get_state(void)
{
return softpwm_led_state;
}
#ifdef BREATHING_LED_ENABLE
@ -91,28 +114,54 @@ static const uint8_t breathing_table[256] PROGMEM = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 8, 8, 9, 10, 11, 11, 12, 13, 14, 15, 16, 17, 18, 19, 21, 22, 23, 25, 26, 27, 29, 30, 32, 34, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53, 56, 58, 61, 63, 66, 68, 71, 74, 77, 80, 83, 86, 89, 92, 95, 98, 102, 105, 108, 112, 116, 119, 123, 126, 130, 134, 138, 142, 145, 149, 153, 157, 161, 165, 169, 173, 176, 180, 184, 188, 192, 195, 199, 203, 206, 210, 213, 216, 219, 223, 226, 228, 231, 234, 236, 239, 241, 243, 245, 247, 248, 250, 251, 252, 253, 254, 255, 255, 255, 255, 255, 255, 255, 254, 253, 252, 251, 250, 248, 247, 245, 243, 241, 239, 236, 234, 231, 228, 226, 223, 219, 216, 213, 210, 206, 203, 199, 195, 192, 188, 184, 180, 176, 173, 169, 165, 161, 157, 153, 149, 145, 142, 138, 134, 130, 126, 123, 119, 116, 112, 108, 105, 102, 98, 95, 92, 89, 86, 83, 80, 77, 74, 71, 68, 66, 63, 61, 58, 56, 53, 51, 49, 47, 45, 43, 41, 39, 37, 35, 34, 32, 30, 29, 27, 26, 25, 23, 22, 21, 19, 18, 17, 16, 15, 14, 13, 12, 11, 11, 10, 9, 8, 8, 7, 6, 6, 5, 5, 4, 4, 3, 3, 2, 2, 2, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0
};
static uint8_t breathing_led_state = 0;
static uint8_t breathing_led_duration = 0;
static led_state_t breathing_led_state = 0;
static uint8_t breathing_led_duration[LED_COUNT] = {0};
void breathing_led_enable(void)
void breathing_led_enable(uint8_t index)
{
breathing_led_state = 1;
LED_BIT_SET(breathing_led_state, index);
}
void breathing_led_disable(void)
void breathing_led_enable_all(void)
{
for (uint8_t i = 0; i < LED_COUNT; i++) {
LED_BIT_SET(breathing_led_state, i);
}
}
void breathing_led_disable(uint8_t index)
{
LED_BIT_CLEAR(breathing_led_state, index);
}
void breathing_led_disable_all(void)
{
breathing_led_state = 0;
}
void breathing_led_toggle(void)
void breathing_led_toggle(uint8_t index)
{
breathing_led_state ^= 1;
LED_BIT_XOR(breathing_led_state, index);
}
void breathing_led_set_duration(uint8_t dur)
void breathing_led_toggle_all(void)
{
breathing_led_duration = dur;
dprintf("breathing led set duration: %u\n", breathing_led_duration);
for (uint8_t i = 0; i < LED_COUNT; i++) {
LED_BIT_XOR(breathing_led_state, i);
}
}
void breathing_led_set_duration(uint8_t index, uint8_t dur)
{
breathing_led_duration[index] = dur;
//dprintf("breathing led set duration: %u\n", breathing_led_duration);
}
void breathing_led_set_duration_all(uint8_t dur)
{
for (uint8_t i = 0; i < LED_COUNT; i++) {
breathing_led_duration[i] = dur;
}
}
#endif
@ -127,27 +176,33 @@ ISR(TIMER1_COMPA_vect)
pwm++;
// LED on
if (pwm == 0) {
softpwm_led_on();
softpwm_ocr = softpwm_ocr_buff;
for (uint8_t i = 0; i < LED_COUNT; i++) {
softpwm_led_on(i);
softpwm_led_ocr[i] = softpwm_led_ocr_buff[i];
}
}
// LED off
if (pwm == softpwm_ocr) {
softpwm_led_off();
for (uint8_t i = 0; i < LED_COUNT; i++) {
if (pwm == softpwm_led_ocr[i]) {
softpwm_led_off(i);
}
}
#ifdef BREATHING_LED_ENABLE
static uint8_t count = 0;
static uint8_t index = 0;
static uint8_t step = 0;
static uint8_t index[LED_COUNT] = {0};
static uint8_t step[LED_COUNT] = {0};
if (breathing_led_state) {
count++;
if (count > SOFTPWM_LED_FREQ) {
if (++count > SOFTPWM_LED_FREQ) {
count = 0;
step++;
if (step > breathing_led_duration) {
step = 0;
softpwm_ocr_buff = pgm_read_byte(&breathing_table[index]);
index++;
for (uint8_t i = 0; i < LED_COUNT; i++) {
if (breathing_led_state & LED_BIT(i)) {
if (++step[i] > breathing_led_duration[i]) {
step[i] = 0;
softpwm_led_ocr_buff[i] = pgm_read_byte(&breathing_table[index[i]]);
index[i]++;
}
}
}
}
}

View File

@ -1,28 +1,44 @@
#ifndef SOFTPWM_LED_H
#define SOFTPWM_LED_H
#include "stdint.h"
#include "led.h"
typedef led_pack_t led_state_t;
#ifdef SOFTPWM_LED_ENABLE
void softpwm_led_init(void);
void softpwm_led_enable(void);
void softpwm_led_disable(void);
void softpwm_led_toggle(void);
void softpwm_led_set(uint8_t val);
void softpwm_led_on(void);
void softpwm_led_off(void);
void softpwm_led_set(uint8_t index, uint8_t val);
void softpwm_led_set_all(uint8_t val);
void softpwm_led_on(uint8_t index);
void softpwm_led_off(uint8_t index);
uint8_t softpwm_led_get_state(void);
void softpwm_led_state_change(uint8_t state);
#ifdef BREATHING_LED_ENABLE
#define breathing_led_init()
void breathing_led_enable(void);
void breathing_led_disable(void);
void breathing_led_toggle(void);
void breathing_led_set_duration(uint8_t dur);
void breathing_led_enable(uint8_t index);
void breathing_led_enable_all(void);
void breathing_led_disable(uint8_t index);
void breathing_led_disable_all(void);
void breathing_led_toggle(uint8_t index);
void breathing_led_toggle_all(void);
void breathing_led_set_duration(uint8_t index, uint8_t dur);
void breathing_led_set_duration_all(uint8_t dur);
#else
#define breathing_led_init()
#define breathing_led_enable()
#define breathing_led_enable_all()
#define breathing_led_disable()
#define breathing_led_disable_all()
#define breathing_led_toggle()
#define breathing_led_toggle_all()
#define breathing_led_set_duration()
#define breathing_led_set_duration_all()
#endif
#else
@ -32,8 +48,11 @@ void breathing_led_set_duration(uint8_t dur);
#define softpwm_led_disable()
#define softpwm_led_toggle()
#define softpwm_led_set()
#define softpwm_led_set_all()
#define softpwm_led_on()
#define softpwm_led_off()
#define softpwm_led_get_state()
#define softpwm_led_state_change()
#endif