qmk

QMK Firmware
git clone git://git.z3bra.org/qmk.git
Log | Files | Refs | Submodules | LICENSE

backlight.c (5258B)


      1 /*
      2 Copyright 2013 Mathias Andersson <wraul@dbox.se>
      3 
      4 This program is free software: you can redistribute it and/or modify
      5 it under the terms of the GNU General Public License as published by
      6 the Free Software Foundation, either version 2 of the License, or
      7 (at your option) any later version.
      8 
      9 This program is distributed in the hope that it will be useful,
     10 but WITHOUT ANY WARRANTY; without even the implied warranty of
     11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     12 GNU General Public License for more details.
     13 
     14 You should have received a copy of the GNU General Public License
     15 along with this program.  If not, see <http://www.gnu.org/licenses/>.
     16 */
     17 
     18 #include "backlight.h"
     19 #include "eeconfig.h"
     20 #include "debug.h"
     21 
     22 backlight_config_t backlight_config;
     23 
     24 /** \brief Backlight initialization
     25  *
     26  * FIXME: needs doc
     27  */
     28 void backlight_init(void) {
     29     /* check signature */
     30     if (!eeconfig_is_enabled()) {
     31         eeconfig_init();
     32     }
     33     backlight_config.raw = eeconfig_read_backlight();
     34     if (backlight_config.level > BACKLIGHT_LEVELS) {
     35         backlight_config.level = BACKLIGHT_LEVELS;
     36     }
     37     backlight_set(backlight_config.enable ? backlight_config.level : 0);
     38 }
     39 
     40 /** \brief Backlight increase
     41  *
     42  * FIXME: needs doc
     43  */
     44 void backlight_increase(void) {
     45     if (backlight_config.level < BACKLIGHT_LEVELS) {
     46         backlight_config.level++;
     47     }
     48     backlight_config.enable = 1;
     49     eeconfig_update_backlight(backlight_config.raw);
     50     dprintf("backlight increase: %u\n", backlight_config.level);
     51     backlight_set(backlight_config.level);
     52 }
     53 
     54 /** \brief Backlight decrease
     55  *
     56  * FIXME: needs doc
     57  */
     58 void backlight_decrease(void) {
     59     if (backlight_config.level > 0) {
     60         backlight_config.level--;
     61         backlight_config.enable = !!backlight_config.level;
     62         eeconfig_update_backlight(backlight_config.raw);
     63     }
     64     dprintf("backlight decrease: %u\n", backlight_config.level);
     65     backlight_set(backlight_config.level);
     66 }
     67 
     68 /** \brief Backlight toggle
     69  *
     70  * FIXME: needs doc
     71  */
     72 void backlight_toggle(void) {
     73     bool enabled = backlight_config.enable;
     74     dprintf("backlight toggle: %u\n", enabled);
     75     if (enabled)
     76         backlight_disable();
     77     else
     78         backlight_enable();
     79 }
     80 
     81 /** \brief Enable backlight
     82  *
     83  * FIXME: needs doc
     84  */
     85 void backlight_enable(void) {
     86     if (backlight_config.enable) return;  // do nothing if backlight is already on
     87 
     88     backlight_config.enable = true;
     89     if (backlight_config.raw == 1)  // enabled but level == 0
     90         backlight_config.level = 1;
     91     eeconfig_update_backlight(backlight_config.raw);
     92     dprintf("backlight enable\n");
     93     backlight_set(backlight_config.level);
     94 }
     95 
     96 /** \brief Disable backlight
     97  *
     98  * FIXME: needs doc
     99  */
    100 void backlight_disable(void) {
    101     if (!backlight_config.enable) return;  // do nothing if backlight is already off
    102 
    103     backlight_config.enable = false;
    104     eeconfig_update_backlight(backlight_config.raw);
    105     dprintf("backlight disable\n");
    106     backlight_set(0);
    107 }
    108 
    109 /** /brief Get the backlight status
    110  *
    111  * FIXME: needs doc
    112  */
    113 bool is_backlight_enabled(void) { return backlight_config.enable; }
    114 
    115 /** \brief Backlight step through levels
    116  *
    117  * FIXME: needs doc
    118  */
    119 void backlight_step(void) {
    120     backlight_config.level++;
    121     if (backlight_config.level > BACKLIGHT_LEVELS) {
    122         backlight_config.level = 0;
    123     }
    124     backlight_config.enable = !!backlight_config.level;
    125     eeconfig_update_backlight(backlight_config.raw);
    126     dprintf("backlight step: %u\n", backlight_config.level);
    127     backlight_set(backlight_config.level);
    128 }
    129 
    130 /** \brief Backlight set level
    131  *
    132  * FIXME: needs doc
    133  */
    134 void backlight_level(uint8_t level) {
    135     if (level > BACKLIGHT_LEVELS) level = BACKLIGHT_LEVELS;
    136     backlight_config.level  = level;
    137     backlight_config.enable = !!backlight_config.level;
    138     eeconfig_update_backlight(backlight_config.raw);
    139     backlight_set(backlight_config.level);
    140 }
    141 
    142 /** \brief Get backlight level
    143  *
    144  * FIXME: needs doc
    145  */
    146 uint8_t get_backlight_level(void) { return backlight_config.level; }
    147 
    148 #ifdef BACKLIGHT_BREATHING
    149 /** \brief Backlight breathing toggle
    150  *
    151  * FIXME: needs doc
    152  */
    153 void backlight_toggle_breathing(void) {
    154     bool breathing = backlight_config.breathing;
    155     dprintf("backlight breathing toggle: %u\n", breathing);
    156     if (breathing)
    157         backlight_disable_breathing();
    158     else
    159         backlight_enable_breathing();
    160 }
    161 
    162 /** \brief Enable backlight breathing
    163  *
    164  * FIXME: needs doc
    165  */
    166 void backlight_enable_breathing(void) {
    167     if (backlight_config.breathing) return;  // do nothing if breathing is already on
    168 
    169     backlight_config.breathing = true;
    170     eeconfig_update_backlight(backlight_config.raw);
    171     dprintf("backlight breathing enable\n");
    172     breathing_enable();
    173 }
    174 
    175 /** \brief Disable backlight breathing
    176  *
    177  * FIXME: needs doc
    178  */
    179 void backlight_disable_breathing(void) {
    180     if (!backlight_config.breathing) return;  // do nothing if breathing is already off
    181 
    182     backlight_config.breathing = false;
    183     eeconfig_update_backlight(backlight_config.raw);
    184     dprintf("backlight breathing disable\n");
    185     breathing_disable();
    186 }
    187 
    188 /** \brief Get the backlight breathing status
    189  *
    190  * FIXME: needs doc
    191  */
    192 bool is_backlight_breathing(void) { return backlight_config.breathing; }
    193 #endif