Skip to content

Commit 4bea96b

Browse files
thedjnKhenrikbrixandersen
authored andcommitted
drivers: led_strip: Add length function
Adds a length function which returns the length of the LED strip Signed-off-by: Jamie McCrae <spam@helper3000.net>
1 parent 8ca3999 commit 4bea96b

File tree

11 files changed

+97
-4
lines changed

11 files changed

+97
-4
lines changed

drivers/led_strip/apa102.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
struct apa102_config {
1616
struct spi_dt_spec bus;
17+
size_t length;
1718
};
1819

1920
static int apa102_update(const struct device *dev, void *buf, size_t size)
@@ -80,6 +81,13 @@ static int apa102_update_channels(const struct device *dev, uint8_t *channels,
8081
return -EINVAL;
8182
}
8283

84+
static size_t apa102_length(const struct device *dev)
85+
{
86+
const struct apa102_config *config = dev->config;
87+
88+
return config->length;
89+
}
90+
8391
static int apa102_init(const struct device *dev)
8492
{
8593
const struct apa102_config *config = dev->config;
@@ -94,6 +102,7 @@ static int apa102_init(const struct device *dev)
94102
static const struct led_strip_driver_api apa102_api = {
95103
.update_rgb = apa102_update_rgb,
96104
.update_channels = apa102_update_channels,
105+
.length = apa102_length,
97106
};
98107

99108
#define APA102_DEVICE(idx) \
@@ -102,6 +111,7 @@ static const struct led_strip_driver_api apa102_api = {
102111
idx, \
103112
SPI_OP_MODE_MASTER | SPI_TRANSFER_MSB | SPI_WORD_SET(8), \
104113
0), \
114+
.length = DT_INST_PROP(idx, chain_length), \
105115
}; \
106116
\
107117
DEVICE_DT_INST_DEFINE(idx, \

drivers/led_strip/lpd880x.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ LOG_MODULE_REGISTER(lpd880x);
3636

3737
struct lpd880x_config {
3838
struct spi_dt_spec bus;
39+
size_t length;
3940
};
4041

4142
static int lpd880x_update(const struct device *dev, void *data, size_t size)
@@ -125,6 +126,13 @@ static int lpd880x_strip_update_channels(const struct device *dev,
125126
return lpd880x_update(dev, channels, num_channels);
126127
}
127128

129+
static size_t lpd880x_strip_length(const struct device *dev)
130+
{
131+
const struct lpd880x_config *config = dev->config;
132+
133+
return config->length;
134+
}
135+
128136
static int lpd880x_strip_init(const struct device *dev)
129137
{
130138
const struct lpd880x_config *config = dev->config;
@@ -138,12 +146,14 @@ static int lpd880x_strip_init(const struct device *dev)
138146
}
139147

140148
static const struct lpd880x_config lpd880x_config = {
141-
.bus = SPI_DT_SPEC_INST_GET(0, LPD880X_SPI_OPERATION, 0)
149+
.bus = SPI_DT_SPEC_INST_GET(0, LPD880X_SPI_OPERATION, 0),
150+
.length = DT_INST_PROP(0, chain_length),
142151
};
143152

144153
static const struct led_strip_driver_api lpd880x_strip_api = {
145154
.update_rgb = lpd880x_strip_update_rgb,
146155
.update_channels = lpd880x_strip_update_channels,
156+
.length = lpd880x_strip_length,
147157
};
148158

149159
DEVICE_DT_INST_DEFINE(0, lpd880x_strip_init, NULL,

drivers/led_strip/tlc5971.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,13 @@ static int tlc5971_update_channels(const struct device *dev, uint8_t *channels,
269269
return -ENOTSUP;
270270
}
271271

272+
static size_t tlc5971_length(const struct device *dev)
273+
{
274+
const struct tlc5971_config *cfg = dev->config;
275+
276+
return (size_t)cfg->num_pixels;
277+
}
278+
272279
int tlc5971_set_global_brightness(const struct device *dev, struct led_rgb pixel)
273280
{
274281
const struct tlc5971_config *cfg = dev->config;
@@ -334,6 +341,7 @@ static int tlc5971_init(const struct device *dev)
334341
static const struct led_strip_driver_api tlc5971_api = {
335342
.update_rgb = tlc5971_update_rgb,
336343
.update_channels = tlc5971_update_channels,
344+
.length = tlc5971_length,
337345
};
338346

339347
#define TLC5971_DATA_BUFFER_LENGTH(inst) \

drivers/led_strip/ws2812_gpio.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ struct ws2812_gpio_cfg {
2828
struct gpio_dt_spec gpio;
2929
uint8_t num_colors;
3030
const uint8_t *color_mapping;
31+
size_t length;
3132
};
3233

3334
/*
@@ -187,9 +188,17 @@ static int ws2812_gpio_update_channels(const struct device *dev,
187188
return -ENOTSUP;
188189
}
189190

191+
static size_t ws2812_gpio_length(const struct device *dev)
192+
{
193+
const struct ws2812_gpio_cfg *config = dev->config;
194+
195+
return config->length;
196+
}
197+
190198
static const struct led_strip_driver_api ws2812_gpio_api = {
191199
.update_rgb = ws2812_gpio_update_rgb,
192200
.update_channels = ws2812_gpio_update_channels,
201+
.length = ws2812_gpio_length,
193202
};
194203

195204
/*
@@ -245,6 +254,7 @@ static const uint8_t ws2812_gpio_##idx##_color_mapping[] = \
245254
.gpio = GPIO_DT_SPEC_INST_GET(idx, gpios), \
246255
.num_colors = WS2812_NUM_COLORS(idx), \
247256
.color_mapping = ws2812_gpio_##idx##_color_mapping, \
257+
.length = DT_INST_PROP(idx, chain_length), \
248258
}; \
249259
\
250260
DEVICE_DT_INST_DEFINE(idx, \

drivers/led_strip/ws2812_i2s.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ struct ws2812_i2s_cfg {
3838
size_t tx_buf_bytes;
3939
struct k_mem_slab *mem_slab;
4040
uint8_t num_colors;
41+
size_t length;
4142
const uint8_t *color_mapping;
4243
uint16_t reset_words;
4344
uint32_t lrck_period;
@@ -170,6 +171,13 @@ static int ws2812_strip_update_channels(const struct device *dev, uint8_t *chann
170171
return -ENOTSUP;
171172
}
172173

174+
static size_t ws2812_strip_length(const struct device *dev)
175+
{
176+
const struct ws2812_i2s_cfg *cfg = dev->config;
177+
178+
return cfg->length;
179+
}
180+
173181
static int ws2812_i2s_init(const struct device *dev)
174182
{
175183
const struct ws2812_i2s_cfg *cfg = dev->config;
@@ -218,6 +226,7 @@ static int ws2812_i2s_init(const struct device *dev)
218226
static const struct led_strip_driver_api ws2812_i2s_api = {
219227
.update_rgb = ws2812_strip_update_rgb,
220228
.update_channels = ws2812_strip_update_channels,
229+
.length = ws2812_strip_length,
221230
};
222231

223232
/* Integer division, but always rounds up: e.g. 10/3 = 4 */
@@ -250,6 +259,7 @@ static const struct led_strip_driver_api ws2812_i2s_api = {
250259
.tx_buf_bytes = WS2812_I2S_BUFSIZE(idx), \
251260
.mem_slab = &ws2812_i2s_##idx##_slab, \
252261
.num_colors = WS2812_NUM_COLORS(idx), \
262+
.length = DT_INST_PROP(idx, chain_length), \
253263
.color_mapping = ws2812_i2s_##idx##_color_mapping, \
254264
.lrck_period = WS2812_I2S_LRCK_PERIOD_US(idx), \
255265
.extra_wait_time_us = DT_INST_PROP(idx, extra_wait_time), \

drivers/led_strip/ws2812_rpi_pico_pio.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ struct ws2812_led_strip_config {
2424
const struct device *piodev;
2525
const uint8_t gpio_pin;
2626
uint8_t num_colors;
27+
size_t length;
2728
uint32_t frequency;
2829
const uint8_t *const color_mapping;
2930
uint16_t reset_delay;
@@ -115,9 +116,17 @@ static int ws2812_led_strip_update_channels(const struct device *dev, uint8_t *c
115116
return -ENOTSUP;
116117
}
117118

119+
static size_t ws2812_led_strip_length(const struct device *dev)
120+
{
121+
const struct ws2812_led_strip_config *config = dev->config;
122+
123+
return config->length;
124+
}
125+
118126
static const struct led_strip_driver_api ws2812_led_strip_api = {
119127
.update_rgb = ws2812_led_strip_update_rgb,
120128
.update_channels = ws2812_led_strip_update_channels,
129+
.length = ws2812_led_strip_length,
121130
};
122131

123132
/*
@@ -190,6 +199,7 @@ static int ws2812_rpi_pico_pio_init(const struct device *dev)
190199
.piodev = DEVICE_DT_GET(DT_PARENT(DT_PARENT(node))), \
191200
.gpio_pin = DT_GPIO_PIN_BY_IDX(node, gpios, 0), \
192201
.num_colors = DT_PROP_LEN(node, color_mapping), \
202+
.length = DT_PROP(node, chain_length), \
193203
.color_mapping = ws2812_led_strip_##node##_color_mapping, \
194204
.reset_delay = DT_PROP(node, reset_delay), \
195205
.frequency = DT_PROP(node, frequency), \

drivers/led_strip/ws2812_spi.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ struct ws2812_spi_cfg {
4444
uint8_t zero_frame;
4545
uint8_t num_colors;
4646
const uint8_t *color_mapping;
47+
size_t length;
4748
uint16_t reset_delay;
4849
};
4950

@@ -160,6 +161,13 @@ static int ws2812_strip_update_channels(const struct device *dev,
160161
return -ENOTSUP;
161162
}
162163

164+
static size_t ws2812_strip_length(const struct device *dev)
165+
{
166+
const struct ws2812_spi_cfg *cfg = dev_cfg(dev);
167+
168+
return cfg->length;
169+
}
170+
163171
static int ws2812_spi_init(const struct device *dev)
164172
{
165173
const struct ws2812_spi_cfg *cfg = dev_cfg(dev);
@@ -191,6 +199,7 @@ static int ws2812_spi_init(const struct device *dev)
191199
static const struct led_strip_driver_api ws2812_spi_api = {
192200
.update_rgb = ws2812_strip_update_rgb,
193201
.update_channels = ws2812_strip_update_channels,
202+
.length = ws2812_strip_length,
194203
};
195204

196205
#define WS2812_SPI_NUM_PIXELS(idx) \
@@ -231,6 +240,7 @@ static const struct led_strip_driver_api ws2812_spi_api = {
231240
.zero_frame = WS2812_SPI_ZERO_FRAME(idx), \
232241
.num_colors = WS2812_NUM_COLORS(idx), \
233242
.color_mapping = ws2812_spi_##idx##_color_mapping, \
243+
.length = DT_INST_PROP(idx, chain_length), \
234244
.reset_delay = WS2812_RESET_DELAY(idx), \
235245
}; \
236246
\

dts/bindings/led_strip/apa,apa102.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ description: APA102 SPI LED strip
22

33
compatible: "apa,apa102"
44

5-
include: spi-device.yaml
5+
include: [spi-device.yaml, led-strip.yaml]

dts/bindings/led_strip/greeled,lpd8803.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ description: GreeLed LPD8803 SPI LED strip
55

66
compatible: "greeled,lpd8803"
77

8-
include: spi-device.yaml
8+
include: [spi-device.yaml, led-strip.yaml]

dts/bindings/led_strip/greeled,lpd8806.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ description: GreeLed LPD8806 SPI LED strip
55

66
compatible: "greeled,lpd8806"
77

8-
include: spi-device.yaml
8+
include: [spi-device.yaml, led-strip.yaml]

0 commit comments

Comments
 (0)