2019-11-02 21:20:03 +00:00
|
|
|
#include "backlight.h"
|
2020-03-07 12:09:49 +00:00
|
|
|
#include "backlight_driver_common.h"
|
2023-07-08 13:13:10 +00:00
|
|
|
#include "util.h"
|
2019-11-02 21:20:03 +00:00
|
|
|
|
|
|
|
#ifdef BACKLIGHT_BREATHING
|
|
|
|
# error "Backlight breathing is not available for software PWM. Please disable."
|
|
|
|
#endif
|
|
|
|
|
2020-03-06 12:49:45 +00:00
|
|
|
static uint16_t s_duty_pattern = 0;
|
2019-11-02 21:20:03 +00:00
|
|
|
|
2019-10-29 23:57:21 +00:00
|
|
|
// clang-format off
|
|
|
|
|
|
|
|
/** \brief PWM duty patterns
|
|
|
|
*
|
|
|
|
* We scale the current backlight level to an index within this array. This allows
|
|
|
|
* backlight_task to focus on just switching LEDs on/off, and we can predict the duty pattern
|
|
|
|
*/
|
2020-03-06 12:49:45 +00:00
|
|
|
static const uint16_t backlight_duty_table[] = {
|
2019-10-29 23:57:21 +00:00
|
|
|
0b0000000000000000,
|
|
|
|
0b1000000000000000,
|
|
|
|
0b1000000010000000,
|
|
|
|
0b1000001000010000,
|
|
|
|
0b1000100010001000,
|
|
|
|
0b1001001001001000,
|
|
|
|
0b1010101010101010,
|
|
|
|
0b1110111011101110,
|
|
|
|
0b1111111111111111,
|
|
|
|
};
|
2022-08-30 08:20:04 +00:00
|
|
|
#define backlight_duty_table_size ARRAY_SIZE(backlight_duty_table)
|
2019-10-29 23:57:21 +00:00
|
|
|
|
|
|
|
// clang-format on
|
|
|
|
|
2022-02-12 18:29:31 +00:00
|
|
|
static uint8_t scale_backlight(uint8_t v) {
|
|
|
|
return v * (backlight_duty_table_size - 1) / BACKLIGHT_LEVELS;
|
|
|
|
}
|
2019-10-29 23:57:21 +00:00
|
|
|
|
2022-02-12 18:29:31 +00:00
|
|
|
void backlight_init_ports(void) {
|
|
|
|
backlight_pins_init();
|
|
|
|
}
|
2020-03-06 12:49:45 +00:00
|
|
|
|
2022-02-12 18:29:31 +00:00
|
|
|
void backlight_set(uint8_t level) {
|
|
|
|
s_duty_pattern = backlight_duty_table[scale_backlight(level)];
|
|
|
|
}
|
2020-03-06 12:49:45 +00:00
|
|
|
|
2019-11-02 21:20:03 +00:00
|
|
|
void backlight_task(void) {
|
|
|
|
static uint8_t backlight_tick = 0;
|
|
|
|
|
2019-10-29 23:57:21 +00:00
|
|
|
if (s_duty_pattern & ((uint16_t)1 << backlight_tick)) {
|
2020-03-06 12:49:45 +00:00
|
|
|
backlight_pins_on();
|
2019-11-02 21:20:03 +00:00
|
|
|
} else {
|
2020-03-06 12:49:45 +00:00
|
|
|
backlight_pins_off();
|
2019-11-02 21:20:03 +00:00
|
|
|
}
|
|
|
|
backlight_tick = (backlight_tick + 1) % 16;
|
|
|
|
}
|