diff options
Diffstat (limited to 'quantum/backlight/backlight_soft.c')
-rw-r--r-- | quantum/backlight/backlight_soft.c | 54 |
1 files changed, 8 insertions, 46 deletions
diff --git a/quantum/backlight/backlight_soft.c b/quantum/backlight/backlight_soft.c index 096b41d91..8552384a4 100644 --- a/quantum/backlight/backlight_soft.c +++ b/quantum/backlight/backlight_soft.c | |||
@@ -9,47 +9,7 @@ | |||
9 | # error "Backlight breathing is not available for software PWM. Please disable." | 9 | # error "Backlight breathing is not available for software PWM. Please disable." |
10 | #endif | 10 | #endif |
11 | 11 | ||
12 | #ifndef BACKLIGHT_ON_STATE | 12 | static uint16_t s_duty_pattern = 0; |
13 | # define BACKLIGHT_ON_STATE 1 | ||
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 uint16_t s_duty_pattern = 0; | ||
24 | static const pin_t backlight_pins[] = BACKLIGHT_PIN_INIT; | ||
25 | #define BACKLIGHT_LED_COUNT (sizeof(backlight_pins) / sizeof(pin_t)) | ||
26 | |||
27 | #define FOR_EACH_LED(x) \ | ||
28 | for (uint8_t i = 0; i < BACKLIGHT_LED_COUNT; i++) { \ | ||
29 | pin_t backlight_pin = backlight_pins[i]; \ | ||
30 | { x } \ | ||
31 | } | ||
32 | |||
33 | void backlight_on(pin_t backlight_pin) { | ||
34 | #if BACKLIGHT_ON_STATE == 0 | ||
35 | writePinLow(backlight_pin); | ||
36 | #else | ||
37 | writePinHigh(backlight_pin); | ||
38 | #endif | ||
39 | } | ||
40 | |||
41 | void backlight_off(pin_t backlight_pin) { | ||
42 | #if BACKLIGHT_ON_STATE == 0 | ||
43 | writePinHigh(backlight_pin); | ||
44 | #else | ||
45 | writePinLow(backlight_pin); | ||
46 | #endif | ||
47 | } | ||
48 | |||
49 | void backlight_init_ports(void) { | ||
50 | // Setup backlight pin as output and output to off state. | ||
51 | FOR_EACH_LED(setPinOutput(backlight_pin); backlight_off(backlight_pin);) | ||
52 | } | ||
53 | 13 | ||
54 | // clang-format off | 14 | // clang-format off |
55 | 15 | ||
@@ -58,7 +18,7 @@ void backlight_init_ports(void) { | |||
58 | * We scale the current backlight level to an index within this array. This allows | 18 | * We scale the current backlight level to an index within this array. This allows |
59 | * backlight_task to focus on just switching LEDs on/off, and we can predict the duty pattern | 19 | * backlight_task to focus on just switching LEDs on/off, and we can predict the duty pattern |
60 | */ | 20 | */ |
61 | static uint16_t backlight_duty_table[] = { | 21 | static const uint16_t backlight_duty_table[] = { |
62 | 0b0000000000000000, | 22 | 0b0000000000000000, |
63 | 0b1000000000000000, | 23 | 0b1000000000000000, |
64 | 0b1000000010000000, | 24 | 0b1000000010000000, |
@@ -75,15 +35,17 @@ static uint16_t backlight_duty_table[] = { | |||
75 | 35 | ||
76 | static uint8_t scale_backlight(uint8_t v) { return v * (backlight_duty_table_size - 1) / BACKLIGHT_LEVELS; } | 36 | static uint8_t scale_backlight(uint8_t v) { return v * (backlight_duty_table_size - 1) / BACKLIGHT_LEVELS; } |
77 | 37 | ||
38 | void backlight_init_ports(void) { backlight_pins_init(); } | ||
39 | |||
40 | void backlight_set(uint8_t level) { s_duty_pattern = backlight_duty_table[scale_backlight(level)]; } | ||
41 | |||
78 | void backlight_task(void) { | 42 | void backlight_task(void) { |
79 | static uint8_t backlight_tick = 0; | 43 | static uint8_t backlight_tick = 0; |
80 | 44 | ||
81 | if (s_duty_pattern & ((uint16_t)1 << backlight_tick)) { | 45 | if (s_duty_pattern & ((uint16_t)1 << backlight_tick)) { |
82 | FOR_EACH_LED(backlight_on(backlight_pin);) | 46 | backlight_pins_on(); |
83 | } else { | 47 | } else { |
84 | FOR_EACH_LED(backlight_off(backlight_pin);) | 48 | backlight_pins_off(); |
85 | } | 49 | } |
86 | backlight_tick = (backlight_tick + 1) % 16; | 50 | backlight_tick = (backlight_tick + 1) % 16; |
87 | } | 51 | } |
88 | |||
89 | void backlight_set(uint8_t level) { s_duty_pattern = backlight_duty_table[scale_backlight(level)]; } | ||