diff options
author | Josh Rahm <joshuarahm@gmail.com> | 2021-11-21 01:43:23 -0700 |
---|---|---|
committer | Josh Rahm <joshuarahm@gmail.com> | 2021-11-21 01:43:23 -0700 |
commit | 47f0d11301c71819ced150deb96b7304ee10bab1 (patch) | |
tree | 8f7dd394bb859d04c3cdb21d9ad4e8143f2ae1c3 /include/drv/ws2812b.h | |
parent | 85d993bcc6363eb20019cd5519784733b1f929c1 (diff) | |
download | esp32-ws2812b-47f0d11301c71819ced150deb96b7304ee10bab1.tar.gz esp32-ws2812b-47f0d11301c71819ced150deb96b7304ee10bab1.tar.bz2 esp32-ws2812b-47f0d11301c71819ced150deb96b7304ee10bab1.zip |
Add crude ws2812b driver.
This driver can run lights! However it is very crude and does not
provide a great API. It does work though! Currently lights up 5 leds of
different colors.
Right now this does synchronous writes, which is not the best. It would
be better to be async, but I think that sholud not be a problem and will
shortly be done.
Diffstat (limited to 'include/drv/ws2812b.h')
-rw-r--r-- | include/drv/ws2812b.h | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/include/drv/ws2812b.h b/include/drv/ws2812b.h new file mode 100644 index 0000000..9cf35e4 --- /dev/null +++ b/include/drv/ws2812b.h @@ -0,0 +1,42 @@ +#pragma once +#ifndef INCLUDE_DRV_WS2812B_H_ +#define INCLUDE_DRV_WS2812B_H_ + +#include <stdint.h> +#include "driver/spi_master.h" + +typedef struct { + uint8_t r; + uint8_t g; + uint8_t b; +} ws2812b_rgb_t; + +typedef struct { + uint8_t* buf_; /* Buffer to hold the value to spi. */ + + uint32_t n_rgb; /* Number of rgb values which exist. */ + ws2812b_rgb_t rgb[]; /* Colors to write. */ +} ws2812b_buffer_t; + +struct WS2812B; +typedef struct WS2812B ws2812b_t; + +ws2812b_buffer_t* ws2812b_new_buffer(uint32_t size); + +ws2812b_t* ws2812b_init(spi_device_handle_t spi); + +esp_err_t ws2812b_write_sync(ws2812b_t* drv, ws2812b_buffer_t* buffer); + +static inline void ws2812b_buffer_set_rgb( + ws2812b_buffer_t* buf, size_t idx, uint8_t r, uint8_t g, uint8_t b) +{ + if (idx >= buf->n_rgb) { + return; + } + + buf->rgb[idx].r = r; + buf->rgb[idx].g = g; + buf->rgb[idx].b = b; +} + +#endif /* INCLUDE_DRV_WS2812B_H_ */ |