]> git.donarmstrong.com Git - qmk_firmware.git/blob - tmk_core/common/backlight.c
12f75b38ac8773c8ea821b1942c8a48bb0137a0b
[qmk_firmware.git] / tmk_core / common / backlight.c
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 void backlight_init(void)
25 {
26     /* check signature */
27     if (!eeconfig_is_enabled()) {
28         eeconfig_init();
29     }
30     backlight_config.raw = eeconfig_read_backlight();
31     if (backlight_config.level > BACKLIGHT_LEVELS) {
32        backlight_config.level = BACKLIGHT_LEVELS;
33     }
34     backlight_set(backlight_config.enable ? backlight_config.level : 0);
35 }
36
37 void backlight_increase(void)
38 {
39     if(backlight_config.level < BACKLIGHT_LEVELS)
40     {
41         backlight_config.level++;
42     }
43     backlight_config.enable = 1;
44     eeconfig_update_backlight(backlight_config.raw);
45     dprintf("backlight increase: %u\n", backlight_config.level);
46     backlight_set(backlight_config.level);
47 }
48
49 void backlight_decrease(void)
50 {
51     if(backlight_config.level > 0)
52     {
53         backlight_config.level--;
54         backlight_config.enable = !!backlight_config.level;
55         eeconfig_update_backlight(backlight_config.raw);
56     }
57     dprintf("backlight decrease: %u\n", backlight_config.level);
58     backlight_set(backlight_config.level);
59 }
60
61 void backlight_toggle(void)
62 {
63     backlight_config.enable ^= 1;
64     if (backlight_config.raw == 1) // enabled but level = 0
65         backlight_config.level = 1;
66     eeconfig_update_backlight(backlight_config.raw);
67     dprintf("backlight toggle: %u\n", backlight_config.enable);
68     backlight_set(backlight_config.enable ? backlight_config.level : 0);
69 }
70
71 void backlight_step(void)
72 {
73     backlight_config.level++;
74     if(backlight_config.level > BACKLIGHT_LEVELS)
75     {
76         backlight_config.level = 0;
77     }
78     backlight_config.enable = !!backlight_config.level;
79     eeconfig_update_backlight(backlight_config.raw);
80     dprintf("backlight step: %u\n", backlight_config.level);
81     backlight_set(backlight_config.level);
82 }
83
84 void backlight_level(uint8_t level)
85 {
86     if (level > BACKLIGHT_LEVELS)
87         level = BACKLIGHT_LEVELS;
88     backlight_config.level = level;
89     backlight_config.enable = !!backlight_config.level;
90     eeconfig_update_backlight(backlight_config.raw);
91     backlight_set(backlight_config.level);
92 }
93
94 uint8_t get_backlight_level(void)
95 {
96     return backlight_config.level;
97 }