Skip to content

Commit

Permalink
apa102: cleanups (#22826)
Browse files Browse the repository at this point in the history
  • Loading branch information
fauxpark authored Jan 7, 2024
1 parent e5203f8 commit b3f55cb
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 51 deletions.
94 changes: 44 additions & 50 deletions drivers/led/apa102.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

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);
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_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?
//
Expand Down Expand Up @@ -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;
}
}
3 changes: 2 additions & 1 deletion drivers/led/apa102.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

#define APA102_MAX_BRIGHTNESS 31

extern uint8_t apa102_led_brightness;
void apa102_init(void);

/* User Interface
*
Expand All @@ -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);

0 comments on commit b3f55cb

Please sign in to comment.