Skip to content

Commit

Permalink
Relocate common backlight functionally (qmk#7273)
Browse files Browse the repository at this point in the history
  • Loading branch information
zvecr authored and fdidron committed Feb 14, 2020
1 parent 655d2fb commit 5bd553e
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 81 deletions.
8 changes: 3 additions & 5 deletions common_features.mk
Original file line number Diff line number Diff line change
Expand Up @@ -257,13 +257,11 @@ ifeq ($(strip $(BACKLIGHT_ENABLE)), yes)
SRC += $(QUANTUM_DIR)/backlight/backlight.c
OPT_DEFS += -DBACKLIGHT_ENABLE

ifeq ($(strip $(BACKLIGHT_DRIVER)), software)
ifeq ($(strip $(BACKLIGHT_DRIVER)), custom)
OPT_DEFS += -DBACKLIGHT_CUSTOM_DRIVER
else ifeq ($(strip $(BACKLIGHT_DRIVER)), software)
SRC += $(QUANTUM_DIR)/backlight/backlight_soft.c
else
ifeq ($(strip $(BACKLIGHT_DRIVER)), custom)
OPT_DEFS += -DBACKLIGHT_CUSTOM_DRIVER
endif

ifeq ($(PLATFORM),AVR)
SRC += $(QUANTUM_DIR)/backlight/backlight_avr.c
else
Expand Down
21 changes: 21 additions & 0 deletions quantum/backlight/backlight.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.

backlight_config_t backlight_config;

// TODO: migrate to backlight_config_t
static uint8_t breathing_period = BREATHING_PERIOD;

/** \brief Backlight initialization
*
* FIXME: needs doc
Expand Down Expand Up @@ -191,3 +194,21 @@ void backlight_disable_breathing(void) {
*/
bool is_backlight_breathing(void) { return backlight_config.breathing; }
#endif

// following are marked as weak purely for backwards compatibility
__attribute__((weak)) void breathing_period_set(uint8_t value) { breathing_period = value ? value : 1; }

__attribute__((weak)) uint8_t get_breathing_period(void) { return breathing_period; }

__attribute__((weak)) void breathing_period_default(void) { breathing_period_set(BREATHING_PERIOD); }

__attribute__((weak)) void breathing_period_inc(void) { breathing_period_set(breathing_period + 1); }

__attribute__((weak)) void breathing_period_dec(void) { breathing_period_set(breathing_period - 1); }

// defaults for backlight api
__attribute__((weak)) void backlight_init_ports(void) {}

__attribute__((weak)) void backlight_set(uint8_t level) {}

__attribute__((weak)) void backlight_task(void) {}
23 changes: 20 additions & 3 deletions quantum/backlight/backlight.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,39 @@ typedef union {
} backlight_config_t;

void backlight_init(void);
void backlight_increase(void);
void backlight_decrease(void);
void backlight_toggle(void);
void backlight_enable(void);
void backlight_disable(void);
bool is_backlight_enabled(void);
void backlight_step(void);
void backlight_set(uint8_t level);
void backlight_increase(void);
void backlight_decrease(void);
void backlight_level(uint8_t level);
uint8_t get_backlight_level(void);

// implementation specific
void backlight_init_ports(void);
void backlight_set(uint8_t level);
void backlight_task(void);

#ifdef BACKLIGHT_BREATHING

void backlight_toggle_breathing(void);
void backlight_enable_breathing(void);
void backlight_disable_breathing(void);
bool is_backlight_breathing(void);

void breathing_period_set(uint8_t value);
uint8_t get_breathing_period(void);
void breathing_period_default(void);
void breathing_period_inc(void);
void breathing_period_dec(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
25 changes: 3 additions & 22 deletions quantum/backlight/backlight_arm.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,14 @@ void backlight_task(void) {}
# define BREATHING_HALT_ON 2
# define BREATHING_STEPS 128

static uint8_t breathing_period = BREATHING_PERIOD;
static uint8_t breathing_halt = BREATHING_NO_HALT;
static uint16_t breathing_counter = 0;

bool is_breathing(void) { return BACKLIGHT_PWM_DRIVER.config == &pwmCFG_breathing; }

static inline void breathing_min(void) { breathing_counter = 0; }

static inline void breathing_max(void) { breathing_counter = breathing_period * 256 / 2; }
static inline void breathing_max(void) { breathing_counter = get_breathing_period() * 256 / 2; }

void breathing_interrupt_enable(void) {
pwmStop(&BACKLIGHT_PWM_DRIVER);
Expand Down Expand Up @@ -170,17 +169,6 @@ void breathing_toggle(void) {
breathing_enable();
}

void breathing_period_set(uint8_t value) {
if (!value) value = 1;
breathing_period = value;
}

void breathing_period_default(void) { breathing_period_set(BREATHING_PERIOD); }

void breathing_period_inc(void) { breathing_period_set(breathing_period + 1); }

void breathing_period_dec(void) { breathing_period_set(breathing_period - 1); }

/* To generate breathing curve in python:
* from math import sin, pi; [int(sin(x/128.0*pi)**4*255) for x in range(128)]
*/
Expand All @@ -191,7 +179,8 @@ static inline uint16_t scale_backlight(uint16_t v) { return v / BACKLIGHT_LEVELS

static void breathing_callback(PWMDriver *pwmp) {
(void)pwmp;
uint16_t interval = (uint16_t)breathing_period * 256 / BREATHING_STEPS;
uint8_t breathing_period = get_breathing_period();
uint16_t interval = (uint16_t)breathing_period * 256 / BREATHING_STEPS;
// resetting after one period to prevent ugly reset at overflow.
breathing_counter = (breathing_counter + 1) % (breathing_period * 256);
uint8_t index = breathing_counter / interval % BREATHING_STEPS;
Expand All @@ -207,12 +196,4 @@ static void breathing_callback(PWMDriver *pwmp) {
chSysUnlockFromISR();
}

#else

__attribute__((weak)) void backlight_init_ports(void) {}

__attribute__((weak)) void backlight_set(uint8_t level) {}

__attribute__((weak)) void backlight_task(void) {}

#endif
35 changes: 8 additions & 27 deletions quantum/backlight/backlight_avr.c
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ static const pin_t backlight_pin = BACKLIGHT_PIN;
# endif

# ifdef NO_HARDWARE_PWM
__attribute__((weak)) void backlight_init_ports(void) {
void backlight_init_ports(void) {
// Setup backlight pin as output and output to on state.
FOR_EACH_LED(setPinOutput(backlight_pin); backlight_on(backlight_pin);)

Expand All @@ -217,8 +217,6 @@ __attribute__((weak)) void backlight_init_ports(void) {
# endif
}

__attribute__((weak)) void backlight_set(uint8_t level) {}

uint8_t backlight_tick = 0;

# ifndef BACKLIGHT_CUSTOM_DRIVER
Expand Down Expand Up @@ -303,7 +301,7 @@ static uint16_t cie_lightness(uint16_t v) {
static inline void set_pwm(uint16_t val) { OCRxx = val; }

# ifndef BACKLIGHT_CUSTOM_DRIVER
__attribute__((weak)) void backlight_set(uint8_t level) {
void backlight_set(uint8_t level) {
if (level > BACKLIGHT_LEVELS) level = BACKLIGHT_LEVELS;

if (level == 0) {
Expand Down Expand Up @@ -342,7 +340,6 @@ void backlight_task(void) {}
# define BREATHING_HALT_ON 2
# define BREATHING_STEPS 128

static uint8_t breathing_period = BREATHING_PERIOD;
static uint8_t breathing_halt = BREATHING_NO_HALT;
static uint16_t breathing_counter = 0;

Expand Down Expand Up @@ -377,9 +374,9 @@ bool is_breathing(void) { return !!(TIMSKx & _BV(TOIEx)); }
do { \
breathing_counter = 0; \
} while (0)
# define breathing_max() \
do { \
breathing_counter = breathing_period * 244 / 2; \
# define breathing_max() \
do { \
breathing_counter = get_breathing_period() * 244 / 2; \
} while (0)

void breathing_enable(void) {
Expand Down Expand Up @@ -417,17 +414,6 @@ void breathing_toggle(void) {
breathing_enable();
}

void breathing_period_set(uint8_t value) {
if (!value) value = 1;
breathing_period = value;
}

void breathing_period_default(void) { breathing_period_set(BREATHING_PERIOD); }

void breathing_period_inc(void) { breathing_period_set(breathing_period + 1); }

void breathing_period_dec(void) { breathing_period_set(breathing_period - 1); }

/* To generate breathing curve in python:
* from math import sin, pi; [int(sin(x/128.0*pi)**4*255) for x in range(128)]
*/
Expand All @@ -445,6 +431,7 @@ void breathing_task(void)
ISR(TIMERx_OVF_vect)
# endif
{
uint8_t breathing_period = get_breathing_period();
uint16_t interval = (uint16_t)breathing_period * 244 / BREATHING_STEPS;
// resetting after one period to prevent ugly reset at overflow.
breathing_counter = (breathing_counter + 1) % (breathing_period * 244);
Expand All @@ -459,7 +446,7 @@ ISR(TIMERx_OVF_vect)

# endif // BACKLIGHT_BREATHING

__attribute__((weak)) void backlight_init_ports(void) {
void backlight_init_ports(void) {
// Setup backlight pin as output and output to on state.
FOR_EACH_LED(setPinOutput(backlight_pin); backlight_on(backlight_pin);)

Expand Down Expand Up @@ -500,10 +487,4 @@ __attribute__((weak)) void backlight_init_ports(void) {

# endif // hardware backlight

#else // no backlight

__attribute__((weak)) void backlight_init_ports(void) {}

__attribute__((weak)) void backlight_set(uint8_t level) {}

#endif // backlight
#endif // backlight
24 changes: 0 additions & 24 deletions quantum/quantum.h
Original file line number Diff line number Diff line change
Expand Up @@ -259,30 +259,6 @@ void register_code16(uint16_t code);
void unregister_code16(uint16_t code);
void tap_code16(uint16_t code);

#ifdef BACKLIGHT_ENABLE
void backlight_init_ports(void);
void backlight_task(void);
void backlight_task_internal(void);
void backlight_on(pin_t backlight_pin);
void backlight_off(pin_t backlight_pin);

# ifdef BACKLIGHT_BREATHING
void breathing_task(void);
void breathing_enable(void);
void breathing_pulse(void);
void breathing_disable(void);
void breathing_self_disable(void);
void breathing_toggle(void);
bool is_breathing(void);

void breathing_intensity_default(void);
void breathing_period_default(void);
void breathing_period_set(uint8_t value);
void breathing_period_inc(void);
void breathing_period_dec(void);
# endif
#endif

void send_dword(uint32_t number);
void send_word(uint16_t number);
void send_byte(uint8_t number);
Expand Down

0 comments on commit 5bd553e

Please sign in to comment.