libDaisy
Hardware Library for Daisy
Loading...
Searching...
No Matches
dotstar.h
Go to the documentation of this file.
1#pragma once
2#ifndef DSY_DOTSTAR_H
3#define DSY_DOTSTAR_H
4
5#include "per/i2c.h"
6#include "per/spi.h"
7
8namespace daisy
9{
14{
15 public:
31
32 inline void Init(Config &config)
33 {
34 SpiHandle::Config spi_cfg;
35 spi_cfg.periph = config.periph;
40 spi_cfg.datasize = 8;
42 spi_cfg.baud_prescaler = config.baud_prescaler;
43 spi_cfg.pin_config.sclk = config.clk_pin;
44 spi_cfg.pin_config.mosi = config.data_pin;
45 spi_cfg.pin_config.miso = Pin();
46 spi_cfg.pin_config.nss = Pin();
47
48 spi_.Init(spi_cfg);
49 };
50
51 bool Write(uint8_t *data, size_t size)
52 {
53 return spi_.BlockingTransmit(data, size) == SpiHandle::Result::OK;
54 };
55
56 private:
57 SpiHandle spi_;
58};
59
60
65template <typename Transport>
67{
68 public:
69 enum class Result
70 {
71 OK,
74 };
75
76 struct Config
77 {
78 enum ColorOrder : uint8_t
79 {
80 // R G B
81 RGB = ((0 << 4) | (1 << 2) | (2)),
82 RBG = ((0 << 4) | (2 << 2) | (1)),
83 GRB = ((1 << 4) | (0 << 2) | (2)),
84 GBR = ((2 << 4) | (0 << 2) | (1)),
85 BRG = ((1 << 4) | (2 << 2) | (0)),
86 BGR = ((2 << 4) | (1 << 2) | (0)),
87 };
88
89 typename Transport::Config
92 uint16_t num_pixels;
94 void Defaults()
95 {
96 transport_config.Defaults();
98 num_pixels = 1;
99 };
100 };
101
104
106 {
107 if(config.num_pixels > kMaxNumPixels)
108 {
110 }
111 transport_.Init(config.transport_config);
112 num_pixels_ = config.num_pixels;
113 // first color byte is always global brightness (hence +1 offset)
114 r_offset_ = ((config.color_order >> 4) & 0b11) + 1;
115 g_offset_ = ((config.color_order >> 2) & 0b11) + 1;
116 b_offset_ = (config.color_order & 0b11) + 1;
117 // Minimum brightness by default. These LEDs can
118 // be very current hungry. See datasheet for details.
120 Clear();
121 return Result::OK;
122 };
123
135 void SetAllGlobalBrightness(uint16_t b)
136 {
137 for(uint16_t i = 0; i < num_pixels_; i++)
138 {
140 }
141 };
142
154 Result SetPixelGlobalBrightness(uint16_t idx, uint16_t b)
155 {
156 if(idx >= num_pixels_)
157 {
159 }
160 uint8_t *pixel = (uint8_t *)(&pixels_[idx]);
161 pixel[0] = 0xE0 | std::min(b, (uint16_t)31);
162 return Result::OK;
163 };
164
165 uint16_t GetPixelColor(uint16_t idx)
166 {
167 if(idx >= num_pixels_)
168 return 0;
169 uint32_t c = 0;
170 const uint8_t *pixel = (uint8_t *)&pixels_[idx];
171 c = c | (pixel[r_offset_] << 16);
172 c = c | (pixel[g_offset_] << 8);
173 c = c | pixel[b_offset_];
174 return c;
175 }
176
183 void SetPixelColor(uint16_t idx, const Color &color)
184 {
185 SetPixelColor(idx, color.Red8(), color.Green8(), color.Blue8());
186 }
187
192 void SetPixelColor(uint16_t idx, uint32_t color)
193 {
194 uint8_t r = (color >> 16) & 0xFF;
195 uint8_t g = (color >> 8) & 0xFF;
196 uint8_t b = color & 0xFF;
197 SetPixelColor(idx, r, g, b);
198 }
199
208 Result SetPixelColor(uint16_t idx, uint8_t r, uint8_t g, uint8_t b)
209 {
210 if(idx >= num_pixels_)
211 {
213 }
214 uint8_t *pixel = (uint8_t *)(&pixels_[idx]);
215 pixel[r_offset_] = r;
216 pixel[b_offset_] = b;
217 pixel[g_offset_] = g;
218 return Result::OK;
219 };
220
225 void Fill(const Color &color)
226 {
227 for(uint16_t i = 0; i < num_pixels_; i++)
228 {
229 SetPixelColor(i, color);
230 }
231 }
232
237 void Fill(uint32_t color)
238 {
239 for(uint16_t i = 0; i < num_pixels_; i++)
240 {
241 SetPixelColor(i, color);
242 }
243 }
244
251 void Fill(uint8_t r, uint8_t g, uint8_t b)
252 {
253 for(uint16_t i = 0; i < num_pixels_; i++)
254 {
255 SetPixelColor(i, r, g, b);
256 }
257 }
258
263 void Clear()
264 {
265 for(uint16_t i = 0; i < num_pixels_; i++)
266 {
267 SetPixelColor(i, 0);
268 }
269 };
270
273 {
274 uint8_t sf[4] = {0x00, 0x00, 0x00, 0x00};
275 uint8_t ef[4] = {0xFF, 0xFF, 0xFF, 0xFF};
276 if(!transport_.Write(sf, 4))
277 {
279 }
280 for(uint16_t i = 0; i < num_pixels_; i++)
281 {
282 if(!transport_.Write((uint8_t *)&pixels_[i], 4))
283 {
285 }
286 }
287 if(!transport_.Write(ef, 4))
288 {
290 }
291 return Result::OK;
292 };
293
294 private:
295 static const size_t kMaxNumPixels = 64;
296 Transport transport_;
297 uint16_t num_pixels_;
298 uint32_t pixels_[kMaxNumPixels];
299 uint8_t r_offset_, g_offset_, b_offset_;
300};
301
303
304} // namespace daisy
305
306#endif
Definition color.h:24
uint8_t Green8() const
Definition color.h:67
uint8_t Blue8() const
Definition color.h:68
uint8_t Red8() const
Definition color.h:66
Device support for Adafruit DotStar LEDs (Opsco SK9822)
Definition dotstar.h:67
Result Show()
Writes current pixel buffer data to LEDs.
Definition dotstar.h:272
void SetPixelColor(uint16_t idx, uint32_t color)
Sets color of a single pixel.
Definition dotstar.h:192
~DotStar()
Definition dotstar.h:103
Result Init(Config &config)
Definition dotstar.h:105
DotStar()
Definition dotstar.h:102
uint16_t GetPixelColor(uint16_t idx)
Definition dotstar.h:165
Result SetPixelGlobalBrightness(uint16_t idx, uint16_t b)
Set global brightness for a single pixel.
Definition dotstar.h:154
Result SetPixelColor(uint16_t idx, uint8_t r, uint8_t g, uint8_t b)
Sets color of a single pixel.
Definition dotstar.h:208
void Fill(uint32_t color)
Fills all pixels with color.
Definition dotstar.h:237
void Clear()
Clears all current color data. Does not reset global brightnesses. Does not write pixel buffer data t...
Definition dotstar.h:263
void Fill(uint8_t r, uint8_t g, uint8_t b)
Fill all pixels with color.
Definition dotstar.h:251
void SetAllGlobalBrightness(uint16_t b)
Set global brightness for all pixels.
Definition dotstar.h:135
void SetPixelColor(uint16_t idx, const Color &color)
Sets color of a single pixel.
Definition dotstar.h:183
Result
Definition dotstar.h:70
void Fill(const Color &color)
Fills all pixels with color.
Definition dotstar.h:225
SPI Transport for DotStars.
Definition dotstar.h:14
bool Write(uint8_t *data, size_t size)
Definition dotstar.h:51
void Init(Config &config)
Definition dotstar.h:32
Definition spi.h:24
Result BlockingTransmit(uint8_t *buff, size_t size, uint32_t timeout=100)
Result Init(const Config &config)
Hardware defines and helpers for daisy field platform.
Definition index.h:2
@ PORTB
Definition daisy_core.h:178
@ PORTG
Definition daisy_core.h:183
Definition dotstar.h:77
uint16_t num_pixels
Definition dotstar.h:92
void Defaults()
Definition dotstar.h:94
Transport::Config transport_config
Definition dotstar.h:90
ColorOrder
Definition dotstar.h:79
@ GBR
Definition dotstar.h:84
@ RGB
Definition dotstar.h:81
@ BRG
Definition dotstar.h:85
@ BGR
Definition dotstar.h:86
@ RBG
Definition dotstar.h:82
@ GRB
Definition dotstar.h:83
ColorOrder color_order
Definition dotstar.h:91
Definition dotstar.h:17
Pin data_pin
Definition dotstar.h:21
SpiHandle::Config::BaudPrescaler baud_prescaler
Definition dotstar.h:19
Pin clk_pin
Definition dotstar.h:20
SpiHandle::Config::Peripheral periph
Definition dotstar.h:18
void Defaults()
Definition dotstar.h:23
representation of hardware port/pin combination
Definition daisy_core.h:193
Definition spi.h:27
ClockPolarity clock_polarity
Definition spi.h:104
BaudPrescaler
Definition spi.h:72
Peripheral periph
Definition spi.h:100
Pin nss
Definition spi.h:88
struct daisy::SpiHandle::Config::@18 pin_config
Mode mode
Definition spi.h:101
Peripheral
Definition spi.h:29
Pin mosi
Definition spi.h:87
Pin sclk
Definition spi.h:85
ClockPhase clock_phase
Definition spi.h:105
BaudPrescaler baud_prescaler
Definition spi.h:107
Pin miso
Definition spi.h:86
Direction direction
Definition spi.h:102
unsigned long datasize
Definition spi.h:103