From 3a303bd2aec57fd2d4d0f6d3e5583d746367d6e1 Mon Sep 17 00:00:00 2001 From: Joel Challis Date: Sat, 7 Mar 2020 12:09:49 +0000 Subject: Backlight - Carve out a better location for private driver functionality (#8329) * rename backlight_soft to match rules.mk * rename backlight_soft to match rules.mk - update common_features * Carve out a better location for private driver backlight functionality --- quantum/backlight/backlight.c | 49 +++------------------------ quantum/backlight/backlight.h | 8 ++--- quantum/backlight/backlight_arm.c | 4 --- quantum/backlight/backlight_avr.c | 5 +-- quantum/backlight/backlight_driver_common.c | 49 +++++++++++++++++++++++++++ quantum/backlight/backlight_driver_common.h | 7 ++++ quantum/backlight/backlight_soft.c | 51 ----------------------------- quantum/backlight/backlight_software.c | 48 +++++++++++++++++++++++++++ 8 files changed, 111 insertions(+), 110 deletions(-) create mode 100644 quantum/backlight/backlight_driver_common.c create mode 100644 quantum/backlight/backlight_driver_common.h delete mode 100644 quantum/backlight/backlight_soft.c create mode 100644 quantum/backlight/backlight_software.c (limited to 'quantum/backlight') diff --git a/quantum/backlight/backlight.c b/quantum/backlight/backlight.c index 4a0eac64c6..34dd8cbdb5 100644 --- a/quantum/backlight/backlight.c +++ b/quantum/backlight/backlight.c @@ -20,6 +20,10 @@ along with this program. If not, see . #include "eeconfig.h" #include "debug.h" +#if defined(STM32F0XX) || defined(STM32F0xx) +# pragma message("Backlight support for STMF072 has had limited testing, YMMV. If unsure, set 'BACKLIGHT_ENABLE = no' in your rules.mk") +#endif + backlight_config_t backlight_config; #ifdef BACKLIGHT_BREATHING @@ -27,51 +31,6 @@ backlight_config_t backlight_config; static uint8_t breathing_period = BREATHING_PERIOD; #endif -#ifndef BACKLIGHT_CUSTOM_DRIVER -# if defined(BACKLIGHT_PINS) -static const pin_t backlight_pins[] = BACKLIGHT_PINS; -# ifndef BACKLIGHT_LED_COUNT -# define BACKLIGHT_LED_COUNT (sizeof(backlight_pins) / sizeof(pin_t)) -# endif - -# define FOR_EACH_LED(x) \ - for (uint8_t i = 0; i < BACKLIGHT_LED_COUNT; i++) { \ - pin_t backlight_pin = backlight_pins[i]; \ - { x } \ - } -# else -// we support only one backlight pin -static const pin_t backlight_pin = BACKLIGHT_PIN; -# define FOR_EACH_LED(x) x -# endif - -static inline void backlight_on(pin_t backlight_pin) { -# if BACKLIGHT_ON_STATE == 0 - writePinLow(backlight_pin); -# else - writePinHigh(backlight_pin); -# endif -} - -static inline void backlight_off(pin_t backlight_pin) { -# if BACKLIGHT_ON_STATE == 0 - writePinHigh(backlight_pin); -# else - writePinLow(backlight_pin); -# endif -} - -void backlight_pins_init(void) { - // Setup backlight pin as output and output to off state. - FOR_EACH_LED(setPinOutput(backlight_pin); backlight_off(backlight_pin);) -} - -void backlight_pins_on(void) { FOR_EACH_LED(backlight_on(backlight_pin);) } - -void backlight_pins_off(void) { FOR_EACH_LED(backlight_off(backlight_pin);) } - -#endif - /** \brief Backlight initialization * * FIXME: needs doc diff --git a/quantum/backlight/backlight.h b/quantum/backlight/backlight.h index 07a4880e9f..3e506737d4 100644 --- a/quantum/backlight/backlight.h +++ b/quantum/backlight/backlight.h @@ -44,10 +44,6 @@ typedef union { }; } backlight_config_t; -void backlight_pins_init(void); -void backlight_pins_on(void); -void backlight_pins_off(void); - void backlight_init(void); void backlight_toggle(void); void backlight_enable(void); @@ -79,11 +75,11 @@ void breathing_period_default(void); void breathing_period_inc(void); void breathing_period_dec(void); +void breathing_toggle(void); + // implementation specific void breathing_enable(void); void breathing_disable(void); -void breathing_toggle(void); bool is_breathing(void); void breathing_pulse(void); -void breathing_task(void); #endif diff --git a/quantum/backlight/backlight_arm.c b/quantum/backlight/backlight_arm.c index a6d38a1a02..723544adb9 100644 --- a/quantum/backlight/backlight_arm.c +++ b/quantum/backlight/backlight_arm.c @@ -6,10 +6,6 @@ // TODO: remove short term bodge when refactoring BACKLIGHT_CUSTOM_DRIVER out #ifdef BACKLIGHT_PIN -# if defined(STM32F0XX) || defined(STM32F0xx) -# pragma message("Backlight support for STMF072 has had limited testing, YMMV. If unsure, set 'BACKLIGHT_ENABLE = no' in your rules.mk") -# endif - // GPIOV2 && GPIOV3 # ifndef BACKLIGHT_PAL_MODE # define BACKLIGHT_PAL_MODE 2 diff --git a/quantum/backlight/backlight_avr.c b/quantum/backlight/backlight_avr.c index 40291d3821..ce13f11184 100644 --- a/quantum/backlight/backlight_avr.c +++ b/quantum/backlight/backlight_avr.c @@ -1,11 +1,8 @@ #include "quantum.h" #include "backlight.h" +#include "backlight_driver_common.h" #include "debug.h" -#if !defined(BACKLIGHT_PIN) && !defined(BACKLIGHT_PINS) -# error "Backlight pin/pins not defined. Please configure." -#endif - // This logic is a bit complex, we support 3 setups: // // 1. Hardware PWM when backlight is wired to a PWM pin. diff --git a/quantum/backlight/backlight_driver_common.c b/quantum/backlight/backlight_driver_common.c new file mode 100644 index 0000000000..270a43c67e --- /dev/null +++ b/quantum/backlight/backlight_driver_common.c @@ -0,0 +1,49 @@ +#include "quantum.h" +#include "backlight.h" +#include "backlight_driver_common.h" + +#if !defined(BACKLIGHT_PIN) && !defined(BACKLIGHT_PINS) +# error "Backlight pin/pins not defined. Please configure." +#endif + +#if defined(BACKLIGHT_PINS) +static const pin_t backlight_pins[] = BACKLIGHT_PINS; +# ifndef BACKLIGHT_LED_COUNT +# define BACKLIGHT_LED_COUNT (sizeof(backlight_pins) / sizeof(pin_t)) +# endif + +# define FOR_EACH_LED(x) \ + for (uint8_t i = 0; i < BACKLIGHT_LED_COUNT; i++) { \ + pin_t backlight_pin = backlight_pins[i]; \ + { x } \ + } +#else +// we support only one backlight pin +static const pin_t backlight_pin = BACKLIGHT_PIN; +# define FOR_EACH_LED(x) x +#endif + +static inline void backlight_on(pin_t backlight_pin) { +#if BACKLIGHT_ON_STATE == 0 + writePinLow(backlight_pin); +#else + writePinHigh(backlight_pin); +#endif +} + +static inline void backlight_off(pin_t backlight_pin) { +#if BACKLIGHT_ON_STATE == 0 + writePinHigh(backlight_pin); +#else + writePinLow(backlight_pin); +#endif +} + +void backlight_pins_init(void) { + // Setup backlight pin as output and output to off state. + FOR_EACH_LED(setPinOutput(backlight_pin); backlight_off(backlight_pin);) +} + +void backlight_pins_on(void) { FOR_EACH_LED(backlight_on(backlight_pin);) } + +void backlight_pins_off(void) { FOR_EACH_LED(backlight_off(backlight_pin);) } diff --git a/quantum/backlight/backlight_driver_common.h b/quantum/backlight/backlight_driver_common.h new file mode 100644 index 0000000000..36e8a5fa6b --- /dev/null +++ b/quantum/backlight/backlight_driver_common.h @@ -0,0 +1,7 @@ +#pragma once + +void backlight_pins_init(void); +void backlight_pins_on(void); +void backlight_pins_off(void); + +void breathing_task(void); diff --git a/quantum/backlight/backlight_soft.c b/quantum/backlight/backlight_soft.c deleted file mode 100644 index 8552384a42..0000000000 --- a/quantum/backlight/backlight_soft.c +++ /dev/null @@ -1,51 +0,0 @@ -#include "quantum.h" -#include "backlight.h" - -#if !defined(BACKLIGHT_PIN) && !defined(BACKLIGHT_PINS) -# error "Backlight pin/pins not defined. Please configure." -#endif - -#ifdef BACKLIGHT_BREATHING -# error "Backlight breathing is not available for software PWM. Please disable." -#endif - -static uint16_t s_duty_pattern = 0; - -// 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 - */ -static const uint16_t backlight_duty_table[] = { - 0b0000000000000000, - 0b1000000000000000, - 0b1000000010000000, - 0b1000001000010000, - 0b1000100010001000, - 0b1001001001001000, - 0b1010101010101010, - 0b1110111011101110, - 0b1111111111111111, -}; -#define backlight_duty_table_size (sizeof(backlight_duty_table) / sizeof(backlight_duty_table[0])) - -// clang-format on - -static uint8_t scale_backlight(uint8_t v) { return v * (backlight_duty_table_size - 1) / BACKLIGHT_LEVELS; } - -void backlight_init_ports(void) { backlight_pins_init(); } - -void backlight_set(uint8_t level) { s_duty_pattern = backlight_duty_table[scale_backlight(level)]; } - -void backlight_task(void) { - static uint8_t backlight_tick = 0; - - if (s_duty_pattern & ((uint16_t)1 << backlight_tick)) { - backlight_pins_on(); - } else { - backlight_pins_off(); - } - backlight_tick = (backlight_tick + 1) % 16; -} diff --git a/quantum/backlight/backlight_software.c b/quantum/backlight/backlight_software.c new file mode 100644 index 0000000000..709304f559 --- /dev/null +++ b/quantum/backlight/backlight_software.c @@ -0,0 +1,48 @@ +#include "quantum.h" +#include "backlight.h" +#include "backlight_driver_common.h" + +#ifdef BACKLIGHT_BREATHING +# error "Backlight breathing is not available for software PWM. Please disable." +#endif + +static uint16_t s_duty_pattern = 0; + +// 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 + */ +static const uint16_t backlight_duty_table[] = { + 0b0000000000000000, + 0b1000000000000000, + 0b1000000010000000, + 0b1000001000010000, + 0b1000100010001000, + 0b1001001001001000, + 0b1010101010101010, + 0b1110111011101110, + 0b1111111111111111, +}; +#define backlight_duty_table_size (sizeof(backlight_duty_table) / sizeof(backlight_duty_table[0])) + +// clang-format on + +static uint8_t scale_backlight(uint8_t v) { return v * (backlight_duty_table_size - 1) / BACKLIGHT_LEVELS; } + +void backlight_init_ports(void) { backlight_pins_init(); } + +void backlight_set(uint8_t level) { s_duty_pattern = backlight_duty_table[scale_backlight(level)]; } + +void backlight_task(void) { + static uint8_t backlight_tick = 0; + + if (s_duty_pattern & ((uint16_t)1 << backlight_tick)) { + backlight_pins_on(); + } else { + backlight_pins_off(); + } + backlight_tick = (backlight_tick + 1) % 16; +} -- cgit v1.2.3