Commit | Line | Data |
---|---|---|
4531cc87 JC |
1 | #include "quantum.h" |
2 | #include "backlight.h" | |
3 | ||
4 | #if !defined(BACKLIGHT_PIN) && !defined(BACKLIGHT_PINS) | |
5 | # error "Backlight pin/pins not defined. Please configure." | |
6 | #endif | |
7 | ||
8 | #ifdef BACKLIGHT_BREATHING | |
9 | # error "Backlight breathing is not available for software PWM. Please disable." | |
10 | #endif | |
11 | ||
12 | #ifndef BACKLIGHT_ON_STATE | |
13 | # define BACKLIGHT_ON_STATE 0 | |
14 | #endif | |
15 | ||
16 | #ifdef BACKLIGHT_PINS | |
17 | # define BACKLIGHT_PIN_INIT BACKLIGHT_PINS | |
18 | #else | |
19 | # define BACKLIGHT_PIN_INIT \ | |
20 | { BACKLIGHT_PIN } | |
21 | #endif | |
22 | ||
23 | static const pin_t backlight_pins[] = BACKLIGHT_PIN_INIT; | |
24 | #define BACKLIGHT_LED_COUNT (sizeof(backlight_pins) / sizeof(pin_t)) | |
25 | ||
26 | #define FOR_EACH_LED(x) \ | |
27 | for (uint8_t i = 0; i < BACKLIGHT_LED_COUNT; i++) { \ | |
28 | pin_t backlight_pin = backlight_pins[i]; \ | |
29 | { x } \ | |
30 | } | |
31 | ||
32 | void backlight_on(pin_t backlight_pin) { | |
33 | #if BACKLIGHT_ON_STATE == 0 | |
34 | writePinLow(backlight_pin); | |
35 | #else | |
36 | writePinHigh(backlight_pin); | |
37 | #endif | |
38 | } | |
39 | ||
40 | void backlight_off(pin_t backlight_pin) { | |
41 | #if BACKLIGHT_ON_STATE == 0 | |
42 | writePinHigh(backlight_pin); | |
43 | #else | |
44 | writePinLow(backlight_pin); | |
45 | #endif | |
46 | } | |
47 | ||
48 | void backlight_init_ports(void) { | |
49 | // Setup backlight pin as output and output to on state. | |
50 | FOR_EACH_LED(setPinOutput(backlight_pin); backlight_on(backlight_pin);) | |
51 | } | |
52 | ||
53 | void backlight_task(void) { | |
54 | static uint8_t backlight_tick = 0; | |
55 | ||
56 | if ((0xFFFF >> (get_backlight_level() * ((BACKLIGHT_LEVELS + 1) / 2))) & (1 << backlight_tick)) { | |
57 | FOR_EACH_LED(backlight_on(backlight_pin);) | |
58 | } else { | |
59 | FOR_EACH_LED(backlight_off(backlight_pin);) | |
60 | } | |
61 | backlight_tick = (backlight_tick + 1) % 16; | |
62 | } | |
63 | ||
64 | void backlight_set(uint8_t level) { | |
65 | // noop as backlight_task uses get_backlight_level() | |
66 | } |