backlight_driver_common.c (1628B)
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) 11 static 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 { \ 20 x \ 21 } \ 22 } 23 #else 24 // we support only one backlight pin 25 static const pin_t backlight_pin = BACKLIGHT_PIN; 26 # define FOR_EACH_LED(x) x 27 #endif 28 29 static inline void backlight_on(pin_t backlight_pin) { 30 #if BACKLIGHT_ON_STATE == 0 31 gpio_write_pin_low(backlight_pin); 32 #else 33 gpio_write_pin_high(backlight_pin); 34 #endif 35 } 36 37 static inline void backlight_off(pin_t backlight_pin) { 38 #if BACKLIGHT_ON_STATE == 0 39 gpio_write_pin_high(backlight_pin); 40 #else 41 gpio_write_pin_low(backlight_pin); 42 #endif 43 } 44 45 void backlight_pins_init(void) { 46 // Setup backlight pin as output and output to off state. 47 FOR_EACH_LED(gpio_set_pin_output(backlight_pin); backlight_off(backlight_pin);) 48 } 49 50 void backlight_pins_on(void) { 51 FOR_EACH_LED(backlight_on(backlight_pin);) 52 } 53 54 void backlight_pins_off(void) { 55 FOR_EACH_LED(backlight_off(backlight_pin);) 56 }