simplify driver to be stateless

This commit is contained in:
2026-02-14 21:09:15 -06:00
parent 532d93d5d7
commit 335452cc6d
5 changed files with 29 additions and 29 deletions

View File

@@ -9,10 +9,7 @@ extern "C" {
#endif #endif
static uint16_t* dmaBuffer; static uint16_t* dmaBuffer;
static CRGB* wsledPixels; // delete static size_t dmaBufferSize;
static uint32_t resetDelay; // delete
static size_t dmaBufferSize; // delete
static WsledType type; // delete
static spi_settings_t spiSettings = { static spi_settings_t spiSettings = {
.host = SPI2_HOST, .host = SPI2_HOST,
@@ -40,31 +37,30 @@ static const uint16_t timingBits[16] = {
0x1111, 0x7111, 0x1711, 0x7711, 0x1171, 0x7171, 0x1771, 0x7771, 0x1111, 0x7111, 0x1711, 0x7711, 0x1171, 0x7171, 0x1771, 0x7771,
0x1117, 0x7117, 0x1717, 0x7717, 0x1177, 0x7177, 0x1777, 0x7777}; 0x1117, 0x7117, 0x1717, 0x7717, 0x1177, 0x7177, 0x1777, 0x7777};
static inline uint32_t resetDelay(const wsled_t* dev) {
return (dev->type == WS2812B) ? WSLED_12_RESET_TIME : WSLED_15_RESET_TIME;
}
esp_err_t wsledInit(const wsled_t* dev) { esp_err_t wsledInit(const wsled_t* dev) {
ESP_LOGI(__FILE__, "Initializing wsled device..."); ESP_LOGI(__FILE__, "Initializing wsled device...");
type = dev->type;
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) * dev->numLeds); 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 // 12 bytes for each led + bytes for initial zero and reset state
dmaBufferSize = dev->numLeds * 12 + (resetDelay + 1) * 2; dmaBufferSize = dev->numLeds * 12 + (resetDelay(dev) + 1) * 2;
spiSettings.buscfg.mosi_io_num = dev->pin; spiSettings.buscfg.mosi_io_num = dev->pin;
spiSettings.buscfg.max_transfer_sz = dmaBufferSize; spiSettings.buscfg.max_transfer_sz = dmaBufferSize;
ESP_LOGI(__FILE__, "Initializing spi interface..."); ESP_LOGI(__FILE__, "Initializing spi interface...");
if (ESP_OK != spi_bus_initialize(spiSettings.host, &spiSettings.buscfg, spiSettings.dma_chan)) { if (ESP_OK != spi_bus_initialize(spiSettings.host, &spiSettings.buscfg, spiSettings.dma_chan)) {
free(wsledPixels);
ESP_LOGI(__FILE__, "SPI initialization failed"); ESP_LOGI(__FILE__, "SPI initialization failed");
return -1; return -1;
} }
ESP_LOGI(__FILE__, "Adding spi bus device..."); ESP_LOGI(__FILE__, "Adding spi bus device...");
if (ESP_OK != spi_bus_add_device(spiSettings.host, &spiSettings.devcfg, &spiSettings.spi)) { if (ESP_OK != spi_bus_add_device(spiSettings.host, &spiSettings.devcfg, &spiSettings.spi)) {
free(wsledPixels);
ESP_LOGI(__FILE__, "Failed to add spi bus device"); ESP_LOGI(__FILE__, "Failed to add spi bus device");
return -1; return -1;
} }
@@ -72,14 +68,13 @@ esp_err_t wsledInit(const wsled_t* dev) {
ESP_LOGI(__FILE__, "heap_caps_malloc() with dmaBufferSize=%u...", dmaBufferSize); ESP_LOGI(__FILE__, "heap_caps_malloc() with dmaBufferSize=%u...", dmaBufferSize);
dmaBuffer = heap_caps_malloc(dmaBufferSize, MALLOC_CAP_DMA); dmaBuffer = heap_caps_malloc(dmaBufferSize, MALLOC_CAP_DMA);
if (NULL == dmaBuffer) { if (NULL == dmaBuffer) {
free(wsledPixels);
ESP_LOGI(__FILE__, "Failed to heap_caps_malloc"); ESP_LOGI(__FILE__, "Failed to heap_caps_malloc");
return -1; return -1;
} }
return ESP_OK; return ESP_OK;
} }
esp_err_t wsledUpdate(const CRGB* pixels, size_t ledCount) { esp_err_t wsledUpdate(const wsled_t* dev, const CRGB* pixels, size_t ledCount) {
uint32_t n = 0; uint32_t n = 0;
@@ -90,8 +85,8 @@ esp_err_t wsledUpdate(const CRGB* pixels, size_t ledCount) {
CRGB currentPixel = pixels[i]; CRGB currentPixel = pixels[i];
uint8_t b0 = (type == WS2812B) ? currentPixel.g : currentPixel.r; uint8_t b0 = (dev->type == WS2812B) ? currentPixel.g : currentPixel.r;
uint8_t b1 = (type == WS2812B) ? currentPixel.r : currentPixel.g; uint8_t b1 = (dev->type == WS2812B) ? currentPixel.r : currentPixel.g;
uint8_t b2 = currentPixel.b; uint8_t b2 = currentPixel.b;
// Green // Green
@@ -109,7 +104,7 @@ esp_err_t wsledUpdate(const CRGB* pixels, size_t ledCount) {
} }
// reset pulse // reset pulse
for (int i = 0; i < resetDelay; i++) { for (int i = 0; i < resetDelay(dev); i++) {
dmaBuffer[n++] = 0; dmaBuffer[n++] = 0;
} }

View File

@@ -6,7 +6,7 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#define WSLED_12_RESET_TIME 30 #define WSLED_12_RESET_TIME 5
#define WSLED_15_RESET_TIME 30 #define WSLED_15_RESET_TIME 30
#ifdef __cplusplus #ifdef __cplusplus
@@ -41,7 +41,8 @@ typedef struct {
// initializes the wsled device provided the device settings // initializes the wsled device provided the device settings
esp_err_t wsledInit(const wsled_t* dev); esp_err_t wsledInit(const wsled_t* dev);
esp_err_t wsledUpdate(const CRGB* pixels, size_t ledCount); // outputs the pixel data in pixels to the device
esp_err_t wsledUpdate(const wsled_t* dev, const CRGB* pixels, size_t ledCount);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@@ -7,6 +7,8 @@
#include "wsled/WsledInterface.hpp" #include "wsled/WsledInterface.hpp"
#include "shared/pins.h" #include "shared/pins.h"
#define NUM_LEDS 4
DemoTask::DemoTask() { DemoTask::DemoTask() {
} }
@@ -20,7 +22,7 @@ void DemoTask::run() {
SsdInterface ssd(&ssdDev, ssdDigits); SsdInterface ssd(&ssdDev, ssdDigits);
// wsled device // wsled device
wsled_t wsledDev = { gpio_ws2812b, WS2812B, 0 /* fixed */}; wsled_t wsledDev = { gpio_ws2812b, WS2812B, NUM_LEDS};
WsledInterface wsled(&wsledDev); WsledInterface wsled(&wsledDev);
uint32_t delay = 500; // ms uint32_t delay = 500; // ms
@@ -31,12 +33,8 @@ void DemoTask::run() {
digit++; digit++;
if(digit >= 16) digit = 0; if(digit >= 16) digit = 0;
wsled.fill(CRGB{100, 20, 20}); CRGB color = (digit % 2) ? CRGB{100, 20, 20} : CRGB{20, 20, 100};
wsled.flush(); wsled.fill(color);
vTaskDelay(delay / portTICK_PERIOD_MS);
wsled.fill(CRGB{20, 20, 100});
wsled.flush(); wsled.flush();
vTaskDelay(delay / portTICK_PERIOD_MS); vTaskDelay(delay / portTICK_PERIOD_MS);

View File

@@ -1,10 +1,16 @@
#include "WsledInterface.hpp" #include "WsledInterface.hpp"
WsledInterface::WsledInterface(const wsled_t* device) { WsledInterface::WsledInterface(const wsled_t* device) : device_(device), numLeds_(device->numLeds) {
(void)wsledInit(device); (void)wsledInit(device);
leds_.resize(numLeds_);
// turn all leds off
(void)fill(CRGB(0, 0, 0));
(void)flush();
} }
STATUS WsledInterface::writePixel(CRGB pixel, size_t index) { STATUS WsledInterface::writePixel(CRGB pixel, size_t index) {
@@ -23,7 +29,7 @@ STATUS WsledInterface::get(CRGB* pixel, size_t index) {
STATUS WsledInterface::flush() { STATUS WsledInterface::flush() {
wsledUpdate(leds_, numLeds_); wsledUpdate(device_, leds_.data(), numLeds_);
return OKAY; return OKAY;
} }

View File

@@ -2,6 +2,7 @@
#pragma once #pragma once
#include "stdint.h" #include "stdint.h"
#include <vector>
#include "drivers/wsled.h" #include "drivers/wsled.h"
#include "shared/common.h" #include "shared/common.h"
@@ -39,9 +40,8 @@ public:
private: private:
const wsled_t* device_; const wsled_t* device_;
size_t numLeds_;
static constexpr size_t numLeds_ = 4; std::vector<CRGB> leds_;
CRGB leds_[numLeds_];
}; };