diff options
Diffstat (limited to 'quantum/backlight/backlight_software.c')
| -rw-r--r-- | quantum/backlight/backlight_software.c | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/quantum/backlight/backlight_software.c b/quantum/backlight/backlight_software.c new file mode 100644 index 000000000..709304f55 --- /dev/null +++ b/quantum/backlight/backlight_software.c | |||
| @@ -0,0 +1,48 @@ | |||
| 1 | #include "quantum.h" | ||
| 2 | #include "backlight.h" | ||
| 3 | #include "backlight_driver_common.h" | ||
| 4 | |||
| 5 | #ifdef BACKLIGHT_BREATHING | ||
| 6 | # error "Backlight breathing is not available for software PWM. Please disable." | ||
| 7 | #endif | ||
| 8 | |||
| 9 | static uint16_t s_duty_pattern = 0; | ||
| 10 | |||
| 11 | // clang-format off | ||
| 12 | |||
| 13 | /** \brief PWM duty patterns | ||
| 14 | * | ||
| 15 | * We scale the current backlight level to an index within this array. This allows | ||
| 16 | * backlight_task to focus on just switching LEDs on/off, and we can predict the duty pattern | ||
| 17 | */ | ||
| 18 | static const uint16_t backlight_duty_table[] = { | ||
| 19 | 0b0000000000000000, | ||
| 20 | 0b1000000000000000, | ||
| 21 | 0b1000000010000000, | ||
| 22 | 0b1000001000010000, | ||
| 23 | 0b1000100010001000, | ||
| 24 | 0b1001001001001000, | ||
| 25 | 0b1010101010101010, | ||
| 26 | 0b1110111011101110, | ||
| 27 | 0b1111111111111111, | ||
| 28 | }; | ||
| 29 | #define backlight_duty_table_size (sizeof(backlight_duty_table) / sizeof(backlight_duty_table[0])) | ||
| 30 | |||
| 31 | // clang-format on | ||
| 32 | |||
| 33 | static uint8_t scale_backlight(uint8_t v) { return v * (backlight_duty_table_size - 1) / BACKLIGHT_LEVELS; } | ||
| 34 | |||
| 35 | void backlight_init_ports(void) { backlight_pins_init(); } | ||
| 36 | |||
| 37 | void backlight_set(uint8_t level) { s_duty_pattern = backlight_duty_table[scale_backlight(level)]; } | ||
| 38 | |||
| 39 | void backlight_task(void) { | ||
| 40 | static uint8_t backlight_tick = 0; | ||
| 41 | |||
| 42 | if (s_duty_pattern & ((uint16_t)1 << backlight_tick)) { | ||
| 43 | backlight_pins_on(); | ||
| 44 | } else { | ||
| 45 | backlight_pins_off(); | ||
| 46 | } | ||
| 47 | backlight_tick = (backlight_tick + 1) % 16; | ||
| 48 | } | ||
