backlight_driver_common.c
1504 bytes
1#include "backlight.h"
2#include "backlight_driver_common.h"
3#include "gpio.h"
4#include "util.h"
5
6#if !defined(BACKLIGHT_PIN) && !defined(BACKLIGHT_PINS)
7# error "Backlight pin/pins not defined. Please configure."
8#endif
9
10#if defined(BACKLIGHT_PINS)
11static const pin_t backlight_pins[] = BACKLIGHT_PINS;
12# ifndef BACKLIGHT_LED_COUNT
13# define BACKLIGHT_LED_COUNT ARRAY_SIZE(backlight_pins)
14# endif
15
16# define FOR_EACH_LED(x) \
17 for (uint8_t i = 0; i < BACKLIGHT_LED_COUNT; i++) { \
18 pin_t backlight_pin = backlight_pins[i]; \
19 { x } \
20 }
21#else
22// we support only one backlight pin
23static const pin_t backlight_pin = BACKLIGHT_PIN;
24# define FOR_EACH_LED(x) x
25#endif
26
27static inline void backlight_on(pin_t backlight_pin) {
28#if BACKLIGHT_ON_STATE == 0
29 gpio_write_pin_low(backlight_pin);
30#else
31 gpio_write_pin_high(backlight_pin);
32#endif
33}
34
35static inline void backlight_off(pin_t backlight_pin) {
36#if BACKLIGHT_ON_STATE == 0
37 gpio_write_pin_high(backlight_pin);
38#else
39 gpio_write_pin_low(backlight_pin);
40#endif
41}
42
43void backlight_pins_init(void) {
44 // Setup backlight pin as output and output to off state.
45 FOR_EACH_LED(gpio_set_pin_output(backlight_pin); backlight_off(backlight_pin);)
46}
47
48void backlight_pins_on(void) {
49 FOR_EACH_LED(backlight_on(backlight_pin);)
50}
51
52void backlight_pins_off(void) {
53 FOR_EACH_LED(backlight_off(backlight_pin);)
54}