working wsled driver checkpoint

This commit is contained in:
2026-02-13 12:21:31 -06:00
parent bb8dcd76b2
commit 9f884b41e7
6 changed files with 74 additions and 25 deletions

1
scripts/build.sh Normal file → Executable file
View File

@@ -8,6 +8,7 @@ export IDF_PATH=${PWD}/lib/esp-idf
export SDKCONFIG=${PWD}/config/sdkconfig
export SDKCONFIG_DEFAULTS=${PWD}/config/sdkconfig.defaults
export IDF_PATH_FORCE=1
. ${IDF_PATH}/export.sh
idf.py set-target esp32s3

1
scripts/flash.sh Normal file → Executable file
View File

@@ -8,6 +8,7 @@ export IDF_PATH=${PWD}/lib/esp-idf
export SDKCONFIG=${PWD}/config/sdkconfig
export SDKCONFIG_DEFAULTS=${PWD}/config/sdkconfig.defaults
export IDF_PATH_FORCE=1
. ${IDF_PATH}/export.sh
idf.py fullclean

1
scripts/monitor.sh Normal file → Executable file
View File

@@ -5,6 +5,7 @@ set -e
export IDF_TOOLS_PATH=${PWD}/lib/idf-tools
export IDF_PATH=${PWD}/lib/esp-idf
export IDF_PATH_FORCE=1
. ${IDF_PATH}/export.sh
idf.py -b 115200 monitor

View File

@@ -8,6 +8,7 @@
#include "SsdInterface.hpp"
#include "pins.hpp"
#include "drivers/wsled.h"
DemoTask::DemoTask() {
@@ -17,8 +18,16 @@ void DemoTask::run() {
ESP_LOGI(__FILE__, "Demo Task: run()");
ssd_595_t dev = { gpio_ssd_data, gpio_ssd_clk, gpio_ssd_latch, true };
SsdInterface ssd(&dev, ssdDigits);
// ssd device
ssd_595_t ssdDev = { gpio_ssd_data, gpio_ssd_clk, gpio_ssd_latch, true };
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);
uint32_t delay = 500; // ms
uint8_t digit = 0;
@@ -28,6 +37,16 @@ void DemoTask::run() {
digit++;
if(digit >= 16) digit = 0;
//ledBuffer[0] = CRGB{100, 0, 80};
wsledFill(CRGB{100, 20, 20});
wsledUpdate();
vTaskDelay(delay / portTICK_PERIOD_MS);
//ledBuffer[0] = CRGB{0, 0, 10};
wsledFill(CRGB{20, 20, 100});
wsledUpdate();
vTaskDelay(delay / portTICK_PERIOD_MS);
}

View File

@@ -2,10 +2,17 @@
#include "wsled.h"
#include "esp_heap_caps.h"
#include "esp_log.h"
#ifdef __cplusplus
extern "C" {
#endif
uint16_t* dmaBuffer;
CRGB* wsledPixels;
static int ledCount, resetDelay, dmaBufferSize;
static uint32_t ledCount;
static uint32_t resetDelay;
static size_t dmaBufferSize;
WsledType type;
static spi_settings_t spiSettings = {
@@ -26,7 +33,7 @@ static spi_settings_t spiSettings = {
.queue_size = 1,
.command_bits = 0,
.address_bits = 0,
.flags = SPI_DEVICE_TXBIT_LSBFIRST,
//.flags = SPI_DEVICE_TXBIT_LSBFIRST,
},
};
@@ -36,41 +43,54 @@ static const uint16_t timingBits[16] = {
esp_err_t wsledInit(wsled_t* dev, CRGB** buffer) {
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);
// 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;
spiSettings.buscfg.mosi_io_num = dev->pin;
spiSettings.buscfg.max_transfer_sz = dmaBufferSize;
ESP_LOGI(__FILE__, "Initializing spi interface...");
if (ESP_OK != spi_bus_initialize(spiSettings.host, &spiSettings.buscfg, spiSettings.dma_chan)) {
free(wsledPixels);
ESP_LOGI(__FILE__, "SPI initialization failed");
return -1;
}
ESP_LOGI(__FILE__, "Adding spi bus device...");
if (ESP_OK != spi_bus_add_device(spiSettings.host, &spiSettings.devcfg, &spiSettings.spi)) {
free(wsledPixels);
ESP_LOGI(__FILE__, "Failed to add spi bus device");
return -1;
}
if (NULL == heap_caps_malloc(dmaBufferSize, MALLOC_CAP_DMA)) {
ESP_LOGI(__FILE__, "heap_caps_malloc() with dmaBufferSize=%u...", dmaBufferSize);
dmaBuffer = heap_caps_malloc(dmaBufferSize, MALLOC_CAP_DMA);
if (NULL == dmaBuffer) {
free(wsledPixels);
ESP_LOGI(__FILE__, "Failed to heap_caps_malloc");
return -1;
}
return ESP_OK;
}
void wsledFill(CRGB color) {
esp_err_t wsledFill(CRGB color) {
for (int i = 0; i < ledCount; i++) {
wsledPixels[i] = color;
}
return 0;
}
esp_err_t wsledUpdate() {
@@ -82,28 +102,23 @@ esp_err_t wsledUpdate() {
for (int i = 0; i < ledCount; i++) {
// Data you want to write to each LEDs
CRGB currentPixel = wsledPixels[i];
uint32_t packedData;
if(type == WS2812B) { // different models have different color orders
packedData = currentPixel.g | ((uint32_t)currentPixel.r << 8) | ((uint32_t)currentPixel.b << 16) | ((uint32_t)(0x00) << 24);
} else {
packedData = currentPixel.r | ((uint32_t)currentPixel.g << 8) | ((uint32_t)currentPixel.b << 16) | ((uint32_t)(0x00) << 24);
}
// Red
dmaBuffer[n++] = timingBits[0x0f & (packedData >> 4)];
dmaBuffer[n++] = timingBits[0x0f & (packedData)];
uint8_t b0 = (type == WS2812B) ? currentPixel.g : currentPixel.r;
uint8_t b1 = (type == WS2812B) ? currentPixel.r : currentPixel.g;
uint8_t b2 = currentPixel.b;
// Green
dmaBuffer[n++] = timingBits[0x0f & (packedData >> 12)];
dmaBuffer[n++] = timingBits[0x0f & (packedData) >> 8];
dmaBuffer[n++] = timingBits[(b0 >> 4) & 0x0F];
dmaBuffer[n++] = timingBits[b0 & 0x0F];
// Red
dmaBuffer[n++] = timingBits[(b1 >> 4) & 0x0F];
dmaBuffer[n++] = timingBits[b1 & 0x0F];
// Blue
dmaBuffer[n++] = timingBits[0x0f & (packedData >> 20)];
dmaBuffer[n++] = timingBits[0x0f & (packedData) >> 16];
dmaBuffer[n++] = timingBits[(b2 >> 4) & 0x0F];
dmaBuffer[n++] = timingBits[b2 & 0x0F];
}
@@ -117,4 +132,8 @@ esp_err_t wsledUpdate() {
.tx_buffer = dmaBuffer,
});
return error;
}
}
#ifdef __cplusplus
}
#endif

View File

@@ -6,9 +6,13 @@
#include <stdio.h>
#include <string.h>
#define WSLED_12_RESET_TIME 3
#define WSLED_12_RESET_TIME 30
#define WSLED_15_RESET_TIME 30
#ifdef __cplusplus
extern "C" {
#endif
typedef struct {
uint8_t r;
uint8_t g;
@@ -37,6 +41,10 @@ typedef struct {
esp_err_t wsledInit(wsled_t* dev, CRGB** buffer);
// test function
esp_err_t wsledFillAll(CRGB color);
esp_err_t wsledFill(CRGB color);
esp_err_t wsledUpdate();
#ifdef __cplusplus
}
#endif