From b3f55cb5b2a1490c2bbe3c6ab147c943223b3608 Mon Sep 17 00:00:00 2001 From: Ryan Date: Sun, 7 Jan 2024 17:48:14 +1100 Subject: [PATCH] apa102: cleanups (#22826) --- drivers/led/apa102.c | 94 +++++++++++++++++++++----------------------- drivers/led/apa102.h | 3 +- 2 files changed, 46 insertions(+), 51 deletions(-) diff --git a/drivers/led/apa102.c b/drivers/led/apa102.c index 4d8f69cdcd..548b8f094e 100644 --- a/drivers/led/apa102.c +++ b/drivers/led/apa102.c @@ -55,55 +55,25 @@ uint8_t apa102_led_brightness = APA102_DEFAULT_BRIGHTNESS; -void static apa102_start_frame(void); -void static apa102_end_frame(uint16_t num_leds); - -void static apa102_send_frame(uint8_t red, uint8_t green, uint8_t blue, uint8_t brightness); -void static apa102_send_byte(uint8_t byte); - -void apa102_setleds(rgb_led_t *start_led, uint16_t num_leds) { - rgb_led_t *end = start_led + num_leds; - - apa102_start_frame(); - for (rgb_led_t *led = start_led; led < end; led++) { - apa102_send_frame(led->r, led->g, led->b, apa102_led_brightness); - } - apa102_end_frame(num_leds); +static void apa102_send_byte(uint8_t byte) { + APA102_SEND_BIT(byte, 7); + APA102_SEND_BIT(byte, 6); + APA102_SEND_BIT(byte, 5); + APA102_SEND_BIT(byte, 4); + APA102_SEND_BIT(byte, 3); + APA102_SEND_BIT(byte, 2); + APA102_SEND_BIT(byte, 1); + APA102_SEND_BIT(byte, 0); } -void static apa102_init(void) { - setPinOutput(APA102_DI_PIN); - setPinOutput(APA102_CI_PIN); - - writePinLow(APA102_DI_PIN); - writePinLow(APA102_CI_PIN); -} - -void apa102_set_brightness(uint8_t brightness) { - if (brightness > APA102_MAX_BRIGHTNESS) { - apa102_led_brightness = APA102_MAX_BRIGHTNESS; - } else if (brightness < 0) { - apa102_led_brightness = 0; - } else { - apa102_led_brightness = brightness; - } -} - -void static apa102_send_frame(uint8_t red, uint8_t green, uint8_t blue, uint8_t brightness) { - apa102_send_byte(0b11100000 | brightness); - apa102_send_byte(blue); - apa102_send_byte(green); - apa102_send_byte(red); -} - -void static apa102_start_frame(void) { +static void apa102_start_frame(void) { apa102_init(); for (uint16_t i = 0; i < 4; i++) { apa102_send_byte(0); } } -void static apa102_end_frame(uint16_t num_leds) { +static void apa102_end_frame(uint16_t num_leds) { // This function has been taken from: https://github.com/pololu/apa102-arduino/blob/master/APA102.h // and adapted. The code is MIT licensed. I think thats compatible? // @@ -136,13 +106,37 @@ void static apa102_end_frame(uint16_t num_leds) { apa102_init(); } -void static apa102_send_byte(uint8_t byte) { - APA102_SEND_BIT(byte, 7); - APA102_SEND_BIT(byte, 6); - APA102_SEND_BIT(byte, 5); - APA102_SEND_BIT(byte, 4); - APA102_SEND_BIT(byte, 3); - APA102_SEND_BIT(byte, 2); - APA102_SEND_BIT(byte, 1); - APA102_SEND_BIT(byte, 0); +static void apa102_send_frame(uint8_t red, uint8_t green, uint8_t blue, uint8_t brightness) { + apa102_send_byte(0b11100000 | brightness); + apa102_send_byte(blue); + apa102_send_byte(green); + apa102_send_byte(red); +} + +void apa102_init(void) { + setPinOutput(APA102_DI_PIN); + setPinOutput(APA102_CI_PIN); + + writePinLow(APA102_DI_PIN); + writePinLow(APA102_CI_PIN); +} + +void apa102_setleds(rgb_led_t *start_led, uint16_t num_leds) { + rgb_led_t *end = start_led + num_leds; + + apa102_start_frame(); + for (rgb_led_t *led = start_led; led < end; led++) { + apa102_send_frame(led->r, led->g, led->b, apa102_led_brightness); + } + apa102_end_frame(num_leds); +} + +void apa102_set_brightness(uint8_t brightness) { + if (brightness > APA102_MAX_BRIGHTNESS) { + apa102_led_brightness = APA102_MAX_BRIGHTNESS; + } else if (brightness < 0) { + apa102_led_brightness = 0; + } else { + apa102_led_brightness = brightness; + } } diff --git a/drivers/led/apa102.h b/drivers/led/apa102.h index e3b269883d..5e2f78658b 100644 --- a/drivers/led/apa102.h +++ b/drivers/led/apa102.h @@ -31,7 +31,7 @@ #define APA102_MAX_BRIGHTNESS 31 -extern uint8_t apa102_led_brightness; +void apa102_init(void); /* User Interface * @@ -44,4 +44,5 @@ extern uint8_t apa102_led_brightness; * - Send out the LED data */ void apa102_setleds(rgb_led_t *start_led, uint16_t num_leds); + void apa102_set_brightness(uint8_t brightness);