From f9f3894888b394379c0d9664110d7e4b1b4d4d9e Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Tue, 24 Aug 2021 14:35:11 -0700 Subject: [PATCH] Support multiple status dotstars Only supporting one left a white dotstar stranded. Fixes #5170 --- .../boards/adafruit_funhouse/mpconfigboard.h | 1 + supervisor/shared/status_leds.c | 21 ++++++++++++++----- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/ports/esp32s2/boards/adafruit_funhouse/mpconfigboard.h b/ports/esp32s2/boards/adafruit_funhouse/mpconfigboard.h index a040fe70e0..6379147c30 100644 --- a/ports/esp32s2/boards/adafruit_funhouse/mpconfigboard.h +++ b/ports/esp32s2/boards/adafruit_funhouse/mpconfigboard.h @@ -31,6 +31,7 @@ #define MICROPY_HW_APA102_MOSI (&pin_GPIO14) #define MICROPY_HW_APA102_SCK (&pin_GPIO15) +#define MICROPY_HW_APA102_COUNT (5) #define CIRCUITPY_BOOT_BUTTON (&pin_GPIO0) diff --git a/supervisor/shared/status_leds.c b/supervisor/shared/status_leds.c index 056833a8d3..92c9578051 100644 --- a/supervisor/shared/status_leds.c +++ b/supervisor/shared/status_leds.c @@ -26,6 +26,8 @@ #include "supervisor/shared/status_leds.h" +#include + #include "mphalport.h" #include "shared-bindings/microcontroller/Pin.h" #include "supervisor/shared/tick.h" @@ -52,8 +54,12 @@ static digitalio_digitalinout_obj_t status_neopixel; #elif defined(MICROPY_HW_APA102_MOSI) && defined(MICROPY_HW_APA102_SCK) uint8_t rgb_status_brightness = 50; - #define APA102_BUFFER_LENGTH 12 -static uint8_t status_apa102_color[APA102_BUFFER_LENGTH] = {0, 0, 0, 0, 0xff, 0, 0, 0, 0xff, 0xff, 0xff, 0xff}; +#ifndef MICROPY_HW_APA102_COUNT +#define MICROPY_HW_APA102_COUNT (1) +#endif + + #define APA102_BUFFER_LENGTH (4 + 4 * MICROPY_HW_APA102_COUNT + 4) +static uint8_t status_apa102_color[APA102_BUFFER_LENGTH]; #if CIRCUITPY_BITBANG_APA102 #include "shared-bindings/bitbangio/SPI.h" @@ -142,6 +148,8 @@ void status_led_init() { common_hal_digitalio_digitalinout_construct(&status_neopixel, MICROPY_HW_NEOPIXEL); common_hal_digitalio_digitalinout_switch_to_output(&status_neopixel, false, DRIVE_MODE_PUSH_PULL); #elif defined(MICROPY_HW_APA102_MOSI) && defined(MICROPY_HW_APA102_SCK) + // Set every byte to 0xff except the start 4 bytes that make up the header. + memset(status_apa102_color + 4, 0xff, APA102_BUFFER_LENGTH - 4); #if CIRCUITPY_BITBANG_APA102 shared_module_bitbangio_spi_construct(&status_apa102, MICROPY_HW_APA102_SCK, @@ -259,9 +267,12 @@ void new_status_color(uint32_t rgb) { common_hal_neopixel_write(&status_neopixel, status_neopixel_color, 3 * MICROPY_HW_NEOPIXEL_COUNT); #elif defined(MICROPY_HW_APA102_MOSI) && defined(MICROPY_HW_APA102_SCK) - status_apa102_color[5] = rgb_adjusted & 0xff; - status_apa102_color[6] = (rgb_adjusted >> 8) & 0xff; - status_apa102_color[7] = (rgb_adjusted >> 16) & 0xff; + for (size_t i = 0; i < MICROPY_HW_APA102_COUNT; i++) { + // Skip 4 + offset to skip the header bytes too. + status_apa102_color[4 * i + 4 + 1] = rgb_adjusted & 0xff; + status_apa102_color[4 * i + 4 + 2] = (rgb_adjusted >> 8) & 0xff; + status_apa102_color[4 * i + 4 + 3] = (rgb_adjusted >> 16) & 0xff; + } #if CIRCUITPY_BITBANG_APA102 shared_module_bitbangio_spi_write(&status_apa102, status_apa102_color, APA102_BUFFER_LENGTH);