From 1eb8523e9506c752fb726208d8853a82f06e6659 Mon Sep 17 00:00:00 2001 From: Wraul Date: Thu, 30 May 2013 20:24:39 +0200 Subject: Add support for backlight --- common/action.c | 21 ++++++++++++++ common/action_code.h | 18 ++++++++++++ common/backlight.c | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++++ common/backlight.h | 41 ++++++++++++++++++++++++++++ common/command.c | 7 +++++ common/eeconfig.c | 4 +++ common/eeconfig.h | 5 ++++ common/keyboard.c | 5 ++++ common/suspend.c | 7 +++++ 9 files changed, 185 insertions(+) create mode 100644 common/backlight.c create mode 100644 common/backlight.h (limited to 'common') diff --git a/common/action.c b/common/action.c index c7c8f71f2c..b1fa0e31cf 100644 --- a/common/action.c +++ b/common/action.c @@ -20,6 +20,7 @@ along with this program. If not, see . #include "mousekey.h" #include "command.h" #include "led.h" +#include "backlight.h" #include "action_layer.h" #include "action_tapping.h" #include "action_oneshot.h" @@ -291,6 +292,26 @@ void process_action(keyrecord_t *record) case ACT_MACRO: action_macro_play(action_get_macro(record, action.func.id, action.func.opt)); break; +#endif +#ifdef BACKLIGHT_ENABLE + case ACT_BACKLIGHT: + if (!event.pressed) { + switch (action.backlight.id) { + case BACKLIGHT_INCREASE: + backlight_increase(); + break; + case BACKLIGHT_DECREASE: + backlight_decrease(); + break; + case BACKLIGHT_TOGGLE: + backlight_toggle(); + break; + case BACKLIGHT_STEP: + backlight_step(); + break; + } + } + break; #endif case ACT_COMMAND: break; diff --git a/common/action_code.h b/common/action_code.h index 0933dce136..d28b74f843 100644 --- a/common/action_code.h +++ b/common/action_code.h @@ -85,6 +85,9 @@ along with this program. If not, see . * 1100|opt | id(8) Macro play? * 1100|1111| id(8) Macro record? * + * ACT_BACKLIGHT(1101): + * 1110|xxxx| id(8) Backlight commands + * * ACT_COMMAND(1110): * 1110|opt | id(8) Built-in Command exec * @@ -109,6 +112,7 @@ enum action_kind_id { ACT_LAYER_TAP1 = 0b1011, /* Extensions */ ACT_MACRO = 0b1100, + ACT_BACKLIGHT = 0b1101, ACT_COMMAND = 0b1110, ACT_FUNCTION = 0b1111 }; @@ -157,6 +161,9 @@ typedef union { uint8_t page :2; uint8_t kind :4; } usage; + struct action_backlight { + uint8_t id :8; + } backlight; struct action_command { uint8_t id :8; uint8_t opt :4; @@ -272,10 +279,21 @@ enum layer_pram_tap_op { /* * Extensions */ +enum backlight_id { + BACKLIGHT_INCREASE = 0, + BACKLIGHT_DECREASE = 1, + BACKLIGHT_TOGGLE = 2, + BACKLIGHT_STEP = 3, +}; /* Macro */ #define ACTION_MACRO(id) ACTION(ACT_MACRO, (id)) #define ACTION_MACRO_TAP(id) ACTION(ACT_MACRO, FUNC_TAP<<8 | (id)) #define ACTION_MACRO_OPT(id, opt) ACTION(ACT_MACRO, (opt)<<8 | (id)) +/* Backlight */ +#define ACTION_BACKLIGHT_INCREASE() ACTION(ACT_BACKLIGHT, BACKLIGHT_INCREASE) +#define ACTION_BACKLIGHT_DECREASE() ACTION(ACT_BACKLIGHT, BACKLIGHT_DECREASE) +#define ACTION_BACKLIGHT_TOGGLE() ACTION(ACT_BACKLIGHT, BACKLIGHT_TOGGLE) +#define ACTION_BACKLIGHT_STEP() ACTION(ACT_BACKLIGHT, BACKLIGHT_STEP) /* Command */ #define ACTION_COMMAND(id, opt) ACTION(ACT_COMMAND, (opt)<<8 | (addr)) /* Function */ diff --git a/common/backlight.c b/common/backlight.c new file mode 100644 index 0000000000..00dc04a075 --- /dev/null +++ b/common/backlight.c @@ -0,0 +1,77 @@ +/* +Copyright 2013 Mathias Andersson + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ + +#include "backlight.h" +#include "eeconfig.h" +#include "debug.h" + +backlight_config_t backlight_config; + +void backlight_init(void) +{ + /* check signature */ + if (!eeconfig_is_enabled()) { + eeconfig_init(); + } + backlight_config.raw = eeconfig_read_backlight(); + backlight_set(backlight_config.enable ? backlight_config.level : 0); +} + +void backlight_increase(void) +{ + if(backlight_config.level < BACKLIGHT_LEVELS) + { + backlight_config.level++; + backlight_config.enable = 1; + eeconfig_write_backlight(backlight_config.raw); + } + dprintf("backlight increase: %u\n", backlight_config.level); + backlight_set(backlight_config.level); +} + +void backlight_decrease(void) +{ + if(backlight_config.level > 0) + { + backlight_config.level--; + backlight_config.enable = !!backlight_config.level; + eeconfig_write_backlight(backlight_config.raw); + } + dprintf("backlight decrease: %u\n", backlight_config.level); + backlight_set(backlight_config.level); +} + +void backlight_toggle(void) +{ + backlight_config.enable ^= 1; + eeconfig_write_backlight(backlight_config.raw); + dprintf("backlight toggle: %u\n", backlight_config.enable); + backlight_set(backlight_config.enable ? backlight_config.level : 0); +} + +void backlight_step(void) +{ + backlight_config.level++; + if(backlight_config.level > BACKLIGHT_LEVELS) + { + backlight_config.level = 0; + } + backlight_config.enable = !!backlight_config.level; + eeconfig_write_backlight(backlight_config.raw); + dprintf("backlight step: %u\n", backlight_config.level); + backlight_set(backlight_config.level); +} diff --git a/common/backlight.h b/common/backlight.h new file mode 100644 index 0000000000..685c422ae9 --- /dev/null +++ b/common/backlight.h @@ -0,0 +1,41 @@ +/* +Copyright 2013 Mathias Andersson + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ + +#ifndef BACKLIGHT_H +#define BACKLIGHT_H + +#include +#include + +typedef union { + uint8_t raw; + struct { + bool enable:1; + uint8_t level:7; + }; +} backlight_config_t; + +void backlight_init(void); + +void backlight_increase(void); +void backlight_decrease(void); +void backlight_toggle(void); +void backlight_step(void); + +void backlight_set(uint8_t level); + +#endif diff --git a/common/command.c b/common/command.c index 73a72b4a1d..bcf1b4937f 100644 --- a/common/command.c +++ b/common/command.c @@ -31,6 +31,7 @@ along with this program. If not, see . #include "sleep_led.h" #include "led.h" #include "command.h" +#include "backlight.h" #ifdef MOUSEKEY_ENABLE #include "mousekey.h" @@ -149,6 +150,12 @@ static void print_eeconfig(void) print(".no_gui: "); print_dec(kc.no_gui); print("\n"); print(".swap_grave_esc: "); print_dec(kc.swap_grave_esc); print("\n"); print(".swap_backslash_backspace: "); print_dec(kc.swap_backslash_backspace); print("\n"); + + backlight_config_t bc; + bc.raw = eeconfig_read_backlight(); + print("backlight_config.raw: "); print_hex8(bc.raw); print("\n"); + print(".enable: "); print_dec(bc.enable); print("\n"); + print(".level: "); print_dec(bc.level); print("\n"); } #endif diff --git a/common/eeconfig.c b/common/eeconfig.c index a833f4db9c..7af29ec10a 100644 --- a/common/eeconfig.c +++ b/common/eeconfig.c @@ -10,6 +10,7 @@ void eeconfig_init(void) eeprom_write_byte(EECONFIG_DEFAULT_LAYER, 0); eeprom_write_byte(EECONFIG_KEYMAP, 0); eeprom_write_byte(EECONFIG_MOUSEKEY_ACCEL, 0); + eeprom_write_byte(EECONFIG_BACKLIGHT, 0); } void eeconfig_enable(void) @@ -35,3 +36,6 @@ void eeconfig_write_default_layer(uint8_t val) { eeprom_write_byte(EECONFIG_DEFA uint8_t eeconfig_read_keymap(void) { return eeprom_read_byte(EECONFIG_KEYMAP); } void eeconfig_write_keymap(uint8_t val) { eeprom_write_byte(EECONFIG_KEYMAP, val); } + +uint8_t eeconfig_read_backlight(void) { return eeprom_read_byte(EECONFIG_BACKLIGHT); } +void eeconfig_write_backlight(uint8_t val) { eeprom_write_byte(EECONFIG_BACKLIGHT, val); } diff --git a/common/eeconfig.h b/common/eeconfig.h index 0b605fc7bf..f0acce01c9 100644 --- a/common/eeconfig.h +++ b/common/eeconfig.h @@ -19,6 +19,7 @@ along with this program. If not, see . #define EECONFIG_H #include +#include #define EECONFIG_MAGIC_NUMBER (uint16_t)0xFEED @@ -29,6 +30,7 @@ along with this program. If not, see . #define EECONFIG_DEFAULT_LAYER (uint8_t *)3 #define EECONFIG_KEYMAP (uint8_t *)4 #define EECONFIG_MOUSEKEY_ACCEL (uint8_t *)5 +#define EECONFIG_BACKLIGHT (uint8_t *)6 /* debug bit */ @@ -64,4 +66,7 @@ void eeconfig_write_default_layer(uint8_t val); uint8_t eeconfig_read_keymap(void); void eeconfig_write_keymap(uint8_t val); +uint8_t eeconfig_read_backlight(void); +void eeconfig_write_backlight(uint8_t val); + #endif diff --git a/common/keyboard.c b/common/keyboard.c index 6bc6fae6e2..d1821a099f 100644 --- a/common/keyboard.c +++ b/common/keyboard.c @@ -31,6 +31,7 @@ along with this program. If not, see . #include "bootmagic.h" #include "eeconfig.h" #include "mousekey.h" +#include "backlight.h" #ifdef MATRIX_HAS_GHOST @@ -65,6 +66,10 @@ void keyboard_init(void) #ifdef BOOTMAGIC_ENABLE bootmagic(); #endif + +#ifdef BACKLIGHT_ENABLE + backlight_init(); +#endif } /* diff --git a/common/suspend.c b/common/suspend.c index 6029fe7ffa..146b96d5cc 100644 --- a/common/suspend.c +++ b/common/suspend.c @@ -1,10 +1,14 @@ #include "suspend.h" #include "matrix.h" #include "action.h" +#include "backlight.h" void suspend_power_down(void) { +#ifdef BACKLIGHT_ENABLE + backlight_set(0); +#endif #ifndef NO_SUSPEND_POWER_DOWN // Enable watchdog to wake from MCU sleep cli(); @@ -50,6 +54,9 @@ void suspend_wakeup_init(void) // clear matrix and keyboard state matrix_init(); clear_keyboard(); +#ifdef BACKLIGHT_ENABLE + backlight_init(); +#endif } #ifndef NO_SUSPEND_POWER_DOWN -- cgit v1.2.3