WPILibC++ 2025.3.2
Loading...
Searching...
No Matches
AddressableLED.h
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#pragma once
6
7#include <initializer_list>
8#include <span>
9
10#include <hal/AddressableLED.h>
12#include <hal/PWM.h>
13#include <hal/Types.h>
14#include <units/time.h>
15
16#include "util/Color.h"
17#include "util/Color8Bit.h"
18
19namespace frc {
20
21/**
22 * A class for driving addressable LEDs, such as WS2812B, WS2815, and NeoPixels.
23 *
24 * By default, the timing supports WS2812B and WS2815 LEDs, but is configurable
25 * using SetBitTiming()
26 *
27 * Some LEDs use a different color order than the default GRB. The color order
28 * is configurable using SetColorOrder().
29 *
30 * <p>Only 1 LED driver is currently supported by the roboRIO. However,
31 * multiple LED strips can be connected in series and controlled from the
32 * single driver.
33 */
35 public:
36 /**
37 * Order that color data is sent over the wire.
38 */
40 kRGB = HAL_ALED_RGB, ///< RGB order
41 kRBG = HAL_ALED_RBG, ///< RBG order
42 kBGR = HAL_ALED_BGR, ///< BGR order
43 kBRG = HAL_ALED_BRG, ///< BRG order
44 kGBR = HAL_ALED_GBR, ///< GBR order
45 kGRB = HAL_ALED_GRB ///< GRB order. This is the default order.
46 };
47
49 public:
50 LEDData() : LEDData(0, 0, 0) {}
51 LEDData(int _r, int _g, int _b) {
52 r = _r;
53 g = _g;
54 b = _b;
55 padding = 0;
56 }
57
58 /**
59 * A helper method to set all values of the LED.
60 *
61 * @param r the r value [0-255]
62 * @param g the g value [0-255]
63 * @param b the b value [0-255]
64 */
65 void SetRGB(int r, int g, int b) {
66 this->r = r;
67 this->g = g;
68 this->b = b;
69 }
70
71 /**
72 * A helper method to set all values of the LED.
73 *
74 * @param h the h value [0-180]
75 * @param s the s value [0-255]
76 * @param v the v value [0-255]
77 */
78 void SetHSV(int h, int s, int v);
79
80 /*
81 * Sets a specific LED in the buffer.
82 *
83 * @param color The color of the LED
84 */
85 void SetLED(const Color& color) {
86 this->r = color.red * 255;
87 this->g = color.green * 255;
88 this->b = color.blue * 255;
89 }
90
91 /*
92 * Sets a specific LED in the buffer.
93 *
94 * @param color The color of the LED
95 */
96 void SetLED(const Color8Bit& color) {
97 this->r = color.red;
98 this->g = color.green;
99 this->b = color.blue;
100 }
101 };
102
103 /**
104 * Constructs a new driver for a specific port.
105 *
106 * @param port the output port to use (Must be a PWM header)
107 */
108 explicit AddressableLED(int port);
109
112
113 /**
114 * Sets the color order for this AddressableLED. The default order is GRB.
115 *
116 * This will take effect on the next call to SetData().
117 *
118 * @param order the color order
119 */
121
122 /**
123 * Sets the length of the LED strip.
124 *
125 * <p>Calling this is an expensive call, so its best to call it once, then
126 * just update data.
127 *
128 * <p>The max length is 5460 LEDs.
129 *
130 * @param length the strip length
131 */
132 void SetLength(int length);
133
134 /**
135 * Sets the led output data.
136 *
137 * <p>If the output is enabled, this will start writing the next data cycle.
138 * It is safe to call, even while output is enabled.
139 *
140 * @param ledData the buffer to write
141 */
142 void SetData(std::span<const LEDData> ledData);
143
144 /**
145 * Sets the led output data.
146 *
147 * <p>If the output is enabled, this will start writing the next data cycle.
148 * It is safe to call, even while output is enabled.
149 *
150 * @param ledData the buffer to write
151 */
152 void SetData(std::initializer_list<LEDData> ledData);
153
154 /**
155 * Sets the bit timing.
156 *
157 * <p>By default, the driver is set up to drive WS2812B and WS2815, so nothing
158 * needs to be set for those.
159 *
160 * @param highTime0 high time for 0 bit (default 400ns)
161 * @param lowTime0 low time for 0 bit (default 900ns)
162 * @param highTime1 high time for 1 bit (default 900ns)
163 * @param lowTime1 low time for 1 bit (default 600ns)
164 */
165 void SetBitTiming(units::nanosecond_t highTime0, units::nanosecond_t lowTime0,
166 units::nanosecond_t highTime1,
167 units::nanosecond_t lowTime1);
168
169 /**
170 * Sets the sync time.
171 *
172 * <p>The sync time is the time to hold output so LEDs enable. Default set for
173 * WS2812B and WS2815.
174 *
175 * @param syncTime the sync time (default 280us)
176 */
177 void SetSyncTime(units::microsecond_t syncTime);
178
179 /**
180 * Starts the output.
181 *
182 * <p>The output writes continuously.
183 */
184 void Start();
185
186 /**
187 * Stops the output.
188 */
189 void Stop();
190
191 private:
194 int m_port;
195};
196
198 return static_cast<int32_t>(order);
199}
200
201} // namespace frc
@ HAL_ALED_BGR
Definition AddressableLEDTypes.h:27
@ HAL_ALED_RBG
Definition AddressableLEDTypes.h:26
@ HAL_ALED_BRG
Definition AddressableLEDTypes.h:28
@ HAL_ALED_GRB
Definition AddressableLEDTypes.h:30
@ HAL_ALED_GBR
Definition AddressableLEDTypes.h:29
@ HAL_ALED_RGB
Definition AddressableLEDTypes.h:25
Definition AddressableLED.h:48
LEDData()
Definition AddressableLED.h:50
void SetLED(const Color8Bit &color)
Definition AddressableLED.h:96
void SetHSV(int h, int s, int v)
A helper method to set all values of the LED.
void SetRGB(int r, int g, int b)
A helper method to set all values of the LED.
Definition AddressableLED.h:65
LEDData(int _r, int _g, int _b)
Definition AddressableLED.h:51
void SetLED(const Color &color)
Definition AddressableLED.h:85
A class for driving addressable LEDs, such as WS2812B, WS2815, and NeoPixels.
Definition AddressableLED.h:34
void Start()
Starts the output.
void SetBitTiming(units::nanosecond_t highTime0, units::nanosecond_t lowTime0, units::nanosecond_t highTime1, units::nanosecond_t lowTime1)
Sets the bit timing.
void SetData(std::initializer_list< LEDData > ledData)
Sets the led output data.
void SetData(std::span< const LEDData > ledData)
Sets the led output data.
AddressableLED & operator=(AddressableLED &&)=default
AddressableLED(int port)
Constructs a new driver for a specific port.
void SetColorOrder(ColorOrder order)
Sets the color order for this AddressableLED.
ColorOrder
Order that color data is sent over the wire.
Definition AddressableLED.h:39
@ kGBR
GBR order.
Definition AddressableLED.h:44
@ kRGB
RGB order.
Definition AddressableLED.h:40
@ kBRG
BRG order.
Definition AddressableLED.h:43
@ kGRB
GRB order. This is the default order.
Definition AddressableLED.h:45
@ kRBG
RBG order.
Definition AddressableLED.h:41
@ kBGR
BGR order.
Definition AddressableLED.h:42
void Stop()
Stops the output.
void SetLength(int length)
Sets the length of the LED strip.
AddressableLED(AddressableLED &&)=default
void SetSyncTime(units::microsecond_t syncTime)
Sets the sync time.
Represents colors that can be used with Addressable LEDs.
Definition Color8Bit.h:23
int red
Red component (0-255).
Definition Color8Bit.h:119
int blue
Blue component (0-255).
Definition Color8Bit.h:125
int green
Green component (0-255).
Definition Color8Bit.h:122
Represents colors that can be used with Addressable LEDs.
Definition Color.h:24
double green
Green component (0-1).
Definition Color.h:871
double red
Red component (0-1).
Definition Color.h:868
double blue
Blue component (0-1).
Definition Color.h:874
A move-only C++ wrapper around a HAL handle.
Definition Types.h:96
Definition CAN.h:11
constexpr auto format_as(AddressableLED::ColorOrder order)
Definition AddressableLED.h:197
structure for holding one LED's color data.
Definition AddressableLEDTypes.h:14
uint8_t r
red value
Definition AddressableLEDTypes.h:17
uint8_t padding
Definition AddressableLEDTypes.h:18
uint8_t g
green value
Definition AddressableLEDTypes.h:16
uint8_t b
blue value
Definition AddressableLEDTypes.h:15
color
Definition color.h:16