Compare commits

..

9 Commits

Author SHA1 Message Date
36788ac5a6 comments and update readme 2026-02-14 21:33:55 -06:00
335452cc6d simplify driver to be stateless 2026-02-14 21:09:15 -06:00
532d93d5d7 reorganize project structure 2026-02-14 19:05:26 -06:00
e3090bf1f5 flesh out wsled interface 2026-02-14 13:19:41 -06:00
99e73d5d6b Merge branch 'feature/ssd-driver' 2026-02-14 12:04:24 -06:00
4f333435ea wsled interface skeleton 2026-02-13 12:29:59 -06:00
9f884b41e7 working wsled driver checkpoint 2026-02-13 12:21:31 -06:00
bb8dcd76b2 fix wsled build 2026-02-11 22:31:03 -06:00
7a2f701653 add untested wsled drivers 2026-02-11 20:57:15 -06:00
27 changed files with 377 additions and 44 deletions

View File

@@ -5,7 +5,11 @@ set(PROJECT_MAIN_COMPONENT ${CMAKE_CURRENT_LIST_DIR}/src)
set(EXTRA_COMPONENT_DIRS
${CMAKE_CURRENT_LIST_DIR}/config
${CMAKE_CURRENT_LIST_DIR}/src
${CMAKE_CURRENT_LIST_DIR}/src/drivers
${CMAKE_CURRENT_LIST_DIR}/src/main
${CMAKE_CURRENT_LIST_DIR}/src/tasks
${CMAKE_CURRENT_LIST_DIR}/src/ssd
${CMAKE_CURRENT_LIST_DIR}/src/wsled
)
# For the esp-idf configuration

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)
@@ -35,3 +35,11 @@ After cloning: $ ./scripts/repo-setup # installs esp-idf, sets up target configu
To build: $ ./scripts/build.sh\
To flash: $ ./scripts/flash.sh # note: flash.sh automatically builds\
To monitor: $ ./scripts/monitor.sh\
## TODOs for developers
- [ ] Move all hardware specifications to config/Kconfig.projbuild file. (things like NUM_LEDS, pins.h, ssd_digits, etc.)
- [ ] Create an effect base class for effects to inherit from
- [ ] Attach interrupts for hardware inputs
- [ ] Human I/O (like from buttons, knobs) needs to be in its own io task. Other tasks will be injected with a general I/O interface
- [ ] Investigate putting the wsled task on a separate core from io and such
- [ ] Wsled interface needs a platform for matrix creation

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

@@ -1,34 +0,0 @@
#include "DemoTask.hpp"
#include "driver/gpio.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_log.h"
#include "SsdInterface.hpp"
#include "pins.hpp"
DemoTask::DemoTask() {
}
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);
uint32_t delay = 500; // ms
uint8_t digit = 0;
while(1) {
ssd.writeRaw(&digitMap[digit], 1);
digit++;
if(digit >= 16) digit = 0;
vTaskDelay(delay / portTICK_PERIOD_MS);
}
}

View File

@@ -2,12 +2,11 @@
file(GLOB SRC_FILES
"*.c"
"*.cpp"
"drivers/*.c"
)
idf_component_register(
SRCS ${SRC_FILES}
PRIV_REQUIRES spi_flash
REQUIRES esp_driver_gpio
REQUIRES esp_driver_gpio esp_driver_spi
INCLUDE_DIRS "."
)

116
src/drivers/wsled.c Normal file
View File

@@ -0,0 +1,116 @@
#include "wsled.h"
#include "esp_heap_caps.h"
#include "esp_log.h"
#ifdef __cplusplus
extern "C" {
#endif
static uint16_t* dmaBuffer;
static size_t dmaBufferSize;
static spi_settings_t spiSettings = {
.host = SPI2_HOST,
.dma_chan = SPI_DMA_CH_AUTO,
.buscfg =
{
.miso_io_num = -1,
.sclk_io_num = -1,
.quadwp_io_num = -1,
.quadhd_io_num = -1,
},
.devcfg =
{
.clock_speed_hz = 3.2 * 1000 * 1000, // Clock out at 3.2 MHz
.mode = 0, // SPI mode 0
.spics_io_num = -1, // CS pin
.queue_size = 1,
.command_bits = 0,
.address_bits = 0,
//.flags = SPI_DEVICE_TXBIT_LSBFIRST,
},
};
// translations from SPI -> ws2812b protocol
// [spi] 0b1110 = [ws2812b]0b1 and [spi] 0b1000 = [ws2812b]0b0
static const uint16_t timingBits[16] = {
0x1111, 0x7111, 0x1711, 0x7711, 0x1171, 0x7171, 0x1771, 0x7771,
0x1117, 0x7117, 0x1717, 0x7717, 0x1177, 0x7177, 0x1777, 0x7777};
// WS2812b can handle shorter reset delays than the WS2815
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) {
// 12 bytes for each led + bytes for initial zero and reset state
dmaBufferSize = dev->numLeds * 12 + (resetDelay(dev) + 1) * 2;
spiSettings.buscfg.mosi_io_num = dev->pin;
spiSettings.buscfg.max_transfer_sz = dmaBufferSize;
if (ESP_OK != spi_bus_initialize(spiSettings.host, &spiSettings.buscfg, spiSettings.dma_chan)) {
ESP_LOGI(__FILE__, "SPI initialization failed");
return -1;
}
if (ESP_OK != spi_bus_add_device(spiSettings.host, &spiSettings.devcfg, &spiSettings.spi)) {
ESP_LOGI(__FILE__, "Failed to add spi bus device");
return -1;
}
dmaBuffer = heap_caps_malloc(dmaBufferSize, MALLOC_CAP_DMA);
if (NULL == dmaBuffer) {
ESP_LOGI(__FILE__, "Failed to heap_caps_malloc");
return -1;
}
return ESP_OK;
}
esp_err_t wsledUpdate(const wsled_t* dev, const CRGB* pixels, size_t ledCount) {
uint32_t n = 0;
memset(dmaBuffer, 0, dmaBufferSize);
dmaBuffer[n++] = 0;
for (int i = 0; i < ledCount; i++) {
CRGB currentPixel = pixels[i];
uint8_t b0 = (dev->type == WS2812B) ? currentPixel.g : currentPixel.r;
uint8_t b1 = (dev->type == WS2812B) ? currentPixel.r : currentPixel.g;
uint8_t b2 = currentPixel.b;
// Green
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[(b2 >> 4) & 0x0F];
dmaBuffer[n++] = timingBits[b2 & 0x0F];
}
// reset pulse
for (int i = 0; i < resetDelay(dev); i++) {
dmaBuffer[n++] = 0;
}
esp_err_t error = spi_device_transmit(spiSettings.spi, &(spi_transaction_t) {
.length = dmaBufferSize * 8,
.tx_buffer = dmaBuffer,
});
return error;
}
#ifdef __cplusplus
}
#endif

49
src/drivers/wsled.h Normal file
View File

@@ -0,0 +1,49 @@
#pragma once
#include "driver/gpio.h"
#include "driver/spi_master.h"
#include <stdio.h>
#include <string.h>
#define WSLED_12_RESET_TIME 5
#define WSLED_15_RESET_TIME 30
#ifdef __cplusplus
extern "C" {
#endif
typedef struct {
uint8_t r;
uint8_t g;
uint8_t b;
} CRGB;
typedef enum {
WS2812B = 0,
WS2815
} WsledType;
typedef struct {
spi_host_device_t host;
spi_device_handle_t spi;
int dma_chan;
spi_device_interface_config_t devcfg;
spi_bus_config_t buscfg;
} spi_settings_t;
typedef struct {
gpio_num_t pin;
WsledType type;
uint32_t numLeds;
} wsled_t;
// initializes the wsled device provided the device settings
esp_err_t wsledInit(const wsled_t* dev);
// 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
}
#endif

View File

@@ -7,7 +7,7 @@
#include "esp_log.h"
#include "sdkconfig.h"
#include "pins.hpp"
#include "shared/pins.h"
#include "DemoTask.hpp"

View File

@@ -3,6 +3,7 @@
#include<stdint.h>
// This class is for managing tasks
class App {
public:
@@ -14,8 +15,6 @@ public:
private:
uint32_t ledState = 0;
uint32_t blinkTime = 250;
const char *TAG = "app"; // TODO: instead of this for logging you can use __FILE__ or __func__
};

10
src/main/CMakeLists.txt Normal file
View File

@@ -0,0 +1,10 @@
file(GLOB SRC_FILES
"*.c"
"*.cpp"
)
idf_component_register(
SRCS ${SRC_FILES}
INCLUDE_DIRS "." ".."
)

12
src/ssd/CMakeLists.txt Normal file
View File

@@ -0,0 +1,12 @@
file(GLOB SRC_FILES
"*.c"
"*.cpp"
)
idf_component_register(
SRCS ${SRC_FILES}
PRIV_REQUIRES spi_flash
REQUIRES esp_driver_gpio esp_driver_spi
INCLUDE_DIRS "." ".."
)

View File

@@ -4,7 +4,7 @@
#include "stdint.h"
#include "drivers/ssd.h"
#include "common.h"
#include "shared/common.h"
class SsdInterface {

12
src/tasks/CMakeLists.txt Normal file
View File

@@ -0,0 +1,12 @@
file(GLOB SRC_FILES
"*.c"
"*.cpp"
)
idf_component_register(
SRCS ${SRC_FILES}
PRIV_REQUIRES spi_flash
REQUIRES esp_driver_gpio esp_driver_spi
INCLUDE_DIRS "." ".."
)

46
src/tasks/DemoTask.cpp Normal file
View File

@@ -0,0 +1,46 @@
#include "DemoTask.hpp"
#include "esp_log.h"
#include "ssd/SsdInterface.hpp"
#include "wsled/WsledInterface.hpp"
#include "shared/pins.h"
#define NUM_LEDS 4
DemoTask::DemoTask() {
}
void DemoTask::run() {
ESP_LOGI(__FILE__, "Demo Task: run()");
// ssd device
ssd_595_t ssdDev = { gpio_ssd_data, gpio_ssd_clk, gpio_ssd_latch, true };
SsdInterface ssd(&ssdDev, ssdDigits);
// wsled device
wsled_t wsledDev = { gpio_ws2812b, WS2812B, NUM_LEDS};
WsledInterface wsled(&wsledDev);
uint32_t delay = 500; // ms
uint8_t digit = 0;
while(1) {
// the SSD demo shifts a single hex digit over
ssd.writeRaw(&digitMap[digit], 1);
digit++;
if(digit >= 16) digit = 0;
// the wsled demo toggles the entire array between red and blue
CRGB color = (digit % 2) ? CRGB{100, 20, 20} : CRGB{20, 20, 100};
wsled.fill(color);
wsled.flush();
vTaskDelay(delay / portTICK_PERIOD_MS);
}
}

12
src/wsled/CMakeLists.txt Normal file
View File

@@ -0,0 +1,12 @@
file(GLOB SRC_FILES
"*.c"
"*.cpp"
)
idf_component_register(
SRCS ${SRC_FILES}
PRIV_REQUIRES spi_flash
REQUIRES esp_driver_gpio esp_driver_spi
INCLUDE_DIRS "." ".."
)

View File

@@ -0,0 +1,50 @@
#include "WsledInterface.hpp"
WsledInterface::WsledInterface(const wsled_t* device) : device_(device), numLeds_(device->numLeds) {
(void)wsledInit(device);
// who cares if its dynamically allocated we have like 4 Mb of ram
// an led strip 1024 long will use 3kb here and 12kb in the driver for dma
leds_.resize(numLeds_);
// turn all leds off
(void)fill(CRGB(0, 0, 0));
(void)flush();
}
STATUS WsledInterface::writePixel(CRGB pixel, size_t index) {
// is it really that easy
leds_[index] = pixel;
return OKAY;
}
STATUS WsledInterface::get(CRGB* pixel, size_t index) {
// I could just return the pixel but im so cool and C coded
*pixel = leds_[index];
return OKAY;
}
STATUS WsledInterface::flush() {
// man I wrote my driver so well that it really is that easy
wsledUpdate(device_, leds_.data(), numLeds_);
return OKAY;
}
STATUS WsledInterface::fill(CRGB color) {
// I HATE ITERATORS YOU CAN NEVER MAKE ME USE THEM !!!!
for (size_t i = 0; i < numLeds_; i++) {
leds_[i] = color;
}
return OKAY;
}

View File

@@ -0,0 +1,47 @@
#pragma once
#include "stdint.h"
#include <vector>
#include "drivers/wsled.h"
#include "shared/common.h"
class WsledInterface {
public:
WsledInterface(const wsled_t* device);
~WsledInterface() = default;
// Sets a pixel in the leds buffer to a particular color
// index: the order in the strip of the pixel
// Returns: execution status
STATUS writePixel(CRGB pixel, size_t index);
// Copies the color currently in a pixel to the pointer
// index: the order in the strip of the pixel
// Returns: execution status
STATUS get(CRGB* pixel, size_t index);
// Writes all the data in the leds buffer to hardware
// 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_;
size_t numLeds_;
std::vector<CRGB> leds_;
};

View File