diff --git a/README.md b/README.md index ab00571..a354ea7 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/src/DemoTask.cpp b/src/DemoTask.cpp index 5be07ba..773ae2f 100644 --- a/src/DemoTask.cpp +++ b/src/DemoTask.cpp @@ -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); } diff --git a/src/WsledInterface.cpp b/src/WsledInterface.cpp index 18827b3..ad845bc 100644 --- a/src/WsledInterface.cpp +++ b/src/WsledInterface.cpp @@ -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; } diff --git a/src/WsledInterface.hpp b/src/WsledInterface.hpp index 0e96fee..59d46f0 100644 --- a/src/WsledInterface.hpp +++ b/src/WsledInterface.hpp @@ -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_; diff --git a/src/drivers/wsled.c b/src/drivers/wsled.c index 9b6282a..01224b9 100644 --- a/src/drivers/wsled.c +++ b/src/drivers/wsled.c @@ -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; diff --git a/src/drivers/wsled.h b/src/drivers/wsled.h index c331451..01921ce 100644 --- a/src/drivers/wsled.h +++ b/src/drivers/wsled.h @@ -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 }