WPILibC++ 2027.0.0-alpha-4
Loading...
Searching...
No Matches
AddressableLED.hpp
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
12#include "wpi/hal/Types.hpp"
13#include "wpi/util/Color.hpp"
15
16namespace wpi {
17
18/**
19 * A class for driving addressable LEDs, such as WS2812B, WS2815, and NeoPixels.
20 *
21 * Some LEDs use a different color order than the default GRB. The color order
22 * is configurable using SetColorOrder().
23 *
24 * Up to 1024 LEDs may be controlled in total across all AddressableLED
25 * instances. A single global buffer is used for all instances. The start
26 * position used for LED data for the output is set via SetStart() and the
27 * length of the strip is set via SetLength(). Both of these default to zero, so
28 * multiple instances will access the same pixel data unless SetStart() is
29 * called to adjust the starting point.
30 */
32 public:
33 /**
34 * Order that color data is sent over the wire.
35 */
37 kRGB = HAL_ALED_RGB, ///< RGB order
38 kRBG = HAL_ALED_RBG, ///< RBG order
39 kBGR = HAL_ALED_BGR, ///< BGR order
40 kBRG = HAL_ALED_BRG, ///< BRG order
41 kGBR = HAL_ALED_GBR, ///< GBR order
42 kGRB = HAL_ALED_GRB ///< GRB order. This is the default order.
43 };
44
46 public:
47 LEDData() : LEDData(0, 0, 0) {}
48 LEDData(int _r, int _g, int _b) {
49 r = _r;
50 g = _g;
51 b = _b;
52 }
53
54 /**
55 * A helper method to set all values of the LED.
56 *
57 * @param r the r value [0-255]
58 * @param g the g value [0-255]
59 * @param b the b value [0-255]
60 */
61 void SetRGB(int r, int g, int b) {
62 this->r = r;
63 this->g = g;
64 this->b = b;
65 }
66
67 /**
68 * A helper method to set all values of the LED.
69 *
70 * @param h the h value [0-180]
71 * @param s the s value [0-255]
72 * @param v the v value [0-255]
73 */
74 void SetHSV(int h, int s, int v);
75
76 /*
77 * Sets a specific LED in the buffer.
78 *
79 * @param color The color of the LED
80 */
82 this->r = color.red * 255;
83 this->g = color.green * 255;
84 this->b = color.blue * 255;
85 }
86
87 /*
88 * Sets a specific LED in the buffer.
89 *
90 * @param color The color of the LED
91 */
93 this->r = color.red;
94 this->g = color.green;
95 this->b = color.blue;
96 }
97 };
98
99 /**
100 * Constructs a new driver for a specific channel.
101 *
102 * @param channel the output channel to use
103 */
104 explicit AddressableLED(int channel);
105
108
109 /**
110 * Gets the channel for this addressable LED.
111 *
112 * @return channel
113 */
114 int GetChannel() const { return m_channel; }
115
116 /**
117 * Sets the color order for this AddressableLED. The default order is GRB.
118 *
119 * This will take effect on the next call to SetData().
120 *
121 * @param order the color order
122 */
124
125 /**
126 * Sets the display start of the LED strip in the global buffer.
127 *
128 * @param start the strip start, in LEDs
129 */
130 void SetStart(int start);
131
132 /**
133 * Gets the display start of the LED strip in the global buffer.
134 *
135 * @return the strip start, in LEDs
136 */
137 int GetStart() const { return m_start; }
138
139 /**
140 * Sets the length of the LED strip.
141 *
142 * @param length the strip length, in LEDs
143 */
144 void SetLength(int length);
145
146 /**
147 * Sets the LED output data. This will write to the global buffer starting at
148 * the location set by SetStart() and up to the length set by SetLength().
149 *
150 * @param ledData the buffer to write
151 */
152 void SetData(std::span<const LEDData> ledData);
153
154 /**
155 * Sets the LED output data. This will write to the global buffer starting at
156 * the location set by SetStart() and up to the length set by SetLength().
157 *
158 * @param ledData the buffer to write
159 */
160 void SetData(std::initializer_list<LEDData> ledData);
161
162 /**
163 * Sets the LED output data at an arbitrary location in the global buffer.
164 *
165 * @param start the start location, in LEDs
166 * @param colorOrder the color order
167 * @param ledData the buffer to write
168 */
169 static void SetGlobalData(int start, ColorOrder colorOrder,
170 std::span<const LEDData> ledData);
171
172 private:
174 int m_channel;
175 int m_start{0};
176 int m_length{0};
177 ColorOrder m_colorOrder{kGRB};
178};
179
181 return static_cast<int32_t>(order);
182}
183
184} // namespace wpi
@ 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
void SetLED(const wpi::util::Color8Bit &color)
Definition AddressableLED.hpp:92
LEDData(int _r, int _g, int _b)
Definition AddressableLED.hpp:48
void SetRGB(int r, int g, int b)
A helper method to set all values of the LED.
Definition AddressableLED.hpp:61
LEDData()
Definition AddressableLED.hpp:47
void SetHSV(int h, int s, int v)
A helper method to set all values of the LED.
void SetLED(const wpi::util::Color &color)
Definition AddressableLED.hpp:81
void SetColorOrder(ColorOrder order)
Sets the color order for this AddressableLED.
AddressableLED(int channel)
Constructs a new driver for a specific channel.
void SetData(std::span< const LEDData > ledData)
Sets the LED output data.
void SetLength(int length)
Sets the length of the LED strip.
void SetData(std::initializer_list< LEDData > ledData)
Sets the LED output data.
int GetStart() const
Gets the display start of the LED strip in the global buffer.
Definition AddressableLED.hpp:137
ColorOrder
Order that color data is sent over the wire.
Definition AddressableLED.hpp:36
@ kGBR
GBR order.
Definition AddressableLED.hpp:41
@ kBRG
BRG order.
Definition AddressableLED.hpp:40
@ kBGR
BGR order.
Definition AddressableLED.hpp:39
@ kRBG
RBG order.
Definition AddressableLED.hpp:38
@ kGRB
GRB order. This is the default order.
Definition AddressableLED.hpp:42
@ kRGB
RGB order.
Definition AddressableLED.hpp:37
static void SetGlobalData(int start, ColorOrder colorOrder, std::span< const LEDData > ledData)
Sets the LED output data at an arbitrary location in the global buffer.
void SetStart(int start)
Sets the display start of the LED strip in the global buffer.
AddressableLED(AddressableLED &&)=default
int GetChannel() const
Gets the channel for this addressable LED.
Definition AddressableLED.hpp:114
AddressableLED & operator=(AddressableLED &&)=default
A move-only C++ wrapper around a HAL handle.
Definition Types.hpp:16
Represents colors that can be used with Addressable LEDs.
Definition Color8Bit.hpp:23
Represents colors that can be used with Addressable LEDs.
Definition Color.hpp:42
color
Definition color.h:16
Definition CvSource.hpp:15
constexpr auto format_as(AddressableLED::ColorOrder order)
Definition AddressableLED.hpp:180
structure for holding one LED's color data.
Definition AddressableLEDTypes.h:15
uint8_t r
red value
Definition AddressableLEDTypes.h:16
uint8_t g
green value
Definition AddressableLEDTypes.h:17
uint8_t b
blue value
Definition AddressableLEDTypes.h:18