flesh out wsled interface

This commit is contained in:
2026-02-14 13:19:41 -06:00
parent 99e73d5d6b
commit e3090bf1f5
6 changed files with 49 additions and 44 deletions

View File

@@ -13,8 +13,8 @@ Scripts are in scripts directory, named appropriately.
- [x] Build setup, get working hello-world program.
- [x] Develop 7-segment display driver for the soburg-v2 board, which uses 32-bit shift registers.
Ensures functional hardware interaction and interface design.
- [ ] Develop WS2812b driver, would be nice to have some kind of testing for this.
- [ ] Develop WS2812b interface. I like the FastLed library for Arduino so might have a lot of similar functionality.
- [x] Develop WS2812b driver, would be nice to have some kind of testing for this.
- [x] Develop WS2812b interface. I like the FastLed library for Arduino so might have a lot of similar functionality.
- [ ] Implement proper app structure, with error checking and performance profiling
- [ ] Add a couple LED effects- the random blink and rainbow sweep are easy ones.
- [ ] Create a mock LED panel that executes natively to test LED effects outside of hardware. (SDL2 or something)

View File

@@ -7,8 +7,8 @@
#include "esp_log.h"
#include "SsdInterface.hpp"
#include "WsledInterface.hpp"
#include "pins.hpp"
#include "drivers/wsled.h"
DemoTask::DemoTask() {
@@ -23,11 +23,8 @@ void DemoTask::run() {
SsdInterface ssd(&ssdDev, ssdDigits);
// wsled device
size_t ledCount = 4;
wsled_t wsledDev = { gpio_ws2812b, WS2812B, ledCount};
// TODO: wsled interface
CRGB ledBuffer[4];
wsledInit(&wsledDev, (CRGB**)&ledBuffer);
wsled_t wsledDev = { gpio_ws2812b, WS2812B, 0 /* fixed */};
WsledInterface wsled(&wsledDev);
uint32_t delay = 500; // ms
uint8_t digit = 0;
@@ -37,15 +34,13 @@ void DemoTask::run() {
digit++;
if(digit >= 16) digit = 0;
//ledBuffer[0] = CRGB{100, 0, 80};
wsledFill(CRGB{100, 20, 20});
wsledUpdate();
wsled.fill(CRGB{100, 20, 20});
wsled.flush();
vTaskDelay(delay / portTICK_PERIOD_MS);
//ledBuffer[0] = CRGB{0, 0, 10};
wsledFill(CRGB{20, 20, 100});
wsledUpdate();
wsled.fill(CRGB{20, 20, 100});
wsled.flush();
vTaskDelay(delay / portTICK_PERIOD_MS);
}

View File

@@ -3,19 +3,36 @@
WsledInterface::WsledInterface(const wsled_t* device) {
(void)wsledInit(device);
}
STATUS WsledInterface::writePixel(CRGB pixel, size_t index) {
leds_[index] = pixel;
return OKAY;
}
STATUS WsledInterface::get(CRGB* pixel, size_t index) {
*pixel = leds_[index];
return OKAY;
}
STATUS WsledInterface::flush() {
wsledUpdate(leds_, numLeds_);
return OKAY;
}
STATUS WsledInterface::fill(CRGB color) {
for (size_t i = 0; i < numLeds_; i++) {
leds_[i] = color;
}
return OKAY;
}

View File

@@ -27,6 +27,15 @@ public:
// Returns: execution status
STATUS flush();
// Below are the helper functions for manipulating the led buffer
// Fills the buffer with a single color
// Returns: execution status
STATUS fill(CRGB color);
// getter for numLeds_
size_t ledCount() { return numLeds_; }
private:
const wsled_t* device_;

View File

@@ -8,12 +8,11 @@
extern "C" {
#endif
uint16_t* dmaBuffer;
CRGB* wsledPixels;
static uint32_t ledCount;
static uint32_t resetDelay;
static size_t dmaBufferSize;
WsledType type;
static uint16_t* dmaBuffer;
static CRGB* wsledPixels; // delete
static uint32_t resetDelay; // delete
static size_t dmaBufferSize; // delete
static WsledType type; // delete
static spi_settings_t spiSettings = {
.host = SPI2_HOST,
@@ -41,24 +40,18 @@ static const uint16_t timingBits[16] = {
0x1111, 0x7111, 0x1711, 0x7711, 0x1171, 0x7171, 0x1771, 0x7771,
0x1117, 0x7117, 0x1717, 0x7717, 0x1177, 0x7177, 0x1777, 0x7777};
esp_err_t wsledInit(wsled_t* dev, CRGB** buffer) {
esp_err_t wsledInit(const wsled_t* dev) {
ESP_LOGI(__FILE__, "Initializing wsled device...");
type = dev->type;
ledCount = dev->numLeds;
resetDelay = (dev->type == WS2812B) ? WSLED_12_RESET_TIME : WSLED_15_RESET_TIME;
ESP_LOGI(__FILE__, "mallocing the wsledPixel buffer with size %u bytes", sizeof(CRGB) * ledCount);
ESP_LOGI(__FILE__, "mallocing the wsledPixel buffer with size %u bytes", sizeof(CRGB) * dev->numLeds);
// 12 bytes for each led + bytes for initial zero and reset state
dmaBufferSize = ledCount * 12 + (resetDelay + 1) * 2;
wsledPixels = malloc(sizeof(CRGB) * ledCount);
if (wsledPixels == NULL) {
ESP_LOGI(__FILE__, "Allocating memory failed");
return ESP_ERR_NO_MEM;
}
*buffer = wsledPixels;
dmaBufferSize = dev->numLeds * 12 + (resetDelay + 1) * 2;
spiSettings.buscfg.mosi_io_num = dev->pin;
spiSettings.buscfg.max_transfer_sz = dmaBufferSize;
@@ -86,14 +79,7 @@ esp_err_t wsledInit(wsled_t* dev, CRGB** buffer) {
return ESP_OK;
}
esp_err_t wsledFill(CRGB color) {
for (int i = 0; i < ledCount; i++) {
wsledPixels[i] = color;
}
return 0;
}
esp_err_t wsledUpdate() {
esp_err_t wsledUpdate(const CRGB* pixels, size_t ledCount) {
uint32_t n = 0;
@@ -102,7 +88,7 @@ esp_err_t wsledUpdate() {
for (int i = 0; i < ledCount; i++) {
CRGB currentPixel = wsledPixels[i];
CRGB currentPixel = pixels[i];
uint8_t b0 = (type == WS2812B) ? currentPixel.g : currentPixel.r;
uint8_t b1 = (type == WS2812B) ? currentPixel.r : currentPixel.g;

View File

@@ -38,12 +38,10 @@ typedef struct {
uint32_t numLeds;
} wsled_t;
esp_err_t wsledInit(wsled_t* dev, CRGB** buffer);
// initializes the wsled device provided the device settings
esp_err_t wsledInit(const wsled_t* dev);
// test function
esp_err_t wsledFill(CRGB color);
esp_err_t wsledUpdate();
esp_err_t wsledUpdate(const CRGB* pixels, size_t ledCount);
#ifdef __cplusplus
}