WPILibC++ 2025.0.0-alpha-1-2-g5e745bc
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
11#include <hal/Types.h>
12#include <units/time.h>
13
14#include "util/Color.h"
15#include "util/Color8Bit.h"
16
17namespace frc {
18
19/**
20 * A class for driving addressable LEDs, such as WS2812Bs and NeoPixels.
21 *
22 * By default, the timing supports WS2812B LEDs, but is configurable using
23 * SetBitTiming()
24 *
25 * <p>Only 1 LED driver is currently supported by the roboRIO. However,
26 * multiple LED strips can be connected in series and controlled from the
27 * single driver.
28 */
30 public:
32 public:
33 LEDData() : LEDData(0, 0, 0) {}
34 LEDData(int _r, int _g, int _b) {
35 r = _r;
36 g = _g;
37 b = _b;
38 padding = 0;
39 }
40
41 /**
42 * A helper method to set all values of the LED.
43 *
44 * @param r the r value [0-255]
45 * @param g the g value [0-255]
46 * @param b the b value [0-255]
47 */
48 void SetRGB(int r, int g, int b) {
49 this->r = r;
50 this->g = g;
51 this->b = b;
52 }
53
54 /**
55 * A helper method to set all values of the LED.
56 *
57 * @param h the h value [0-180]
58 * @param s the s value [0-255]
59 * @param v the v value [0-255]
60 */
61 void SetHSV(int h, int s, int v);
62
63 /*
64 * Sets a specific LED in the buffer.
65 *
66 * @param color The color of the LED
67 */
68 void SetLED(const Color& color) {
69 this->r = color.red * 255;
70 this->g = color.green * 255;
71 this->b = color.blue * 255;
72 }
73
74 /*
75 * Sets a specific LED in the buffer.
76 *
77 * @param color The color of the LED
78 */
79 void SetLED(const Color8Bit& color) {
80 this->r = color.red;
81 this->g = color.green;
82 this->b = color.blue;
83 }
84 };
85
86 /**
87 * Constructs a new driver for a specific port.
88 *
89 * @param port the output port to use (Must be a PWM header)
90 */
91 explicit AddressableLED(int port);
92
94
95 /**
96 * Sets the length of the LED strip.
97 *
98 * <p>Calling this is an expensive call, so its best to call it once, then
99 * just update data.
100 *
101 * <p>The max length is 5460 LEDs.
102 *
103 * @param length the strip length
104 */
105 void SetLength(int length);
106
107 /**
108 * Sets the led output data.
109 *
110 * <p>If the output is enabled, this will start writing the next data cycle.
111 * It is safe to call, even while output is enabled.
112 *
113 * @param ledData the buffer to write
114 */
115 void SetData(std::span<const LEDData> ledData);
116
117 /**
118 * Sets the led output data.
119 *
120 * <p>If the output is enabled, this will start writing the next data cycle.
121 * It is safe to call, even while output is enabled.
122 *
123 * @param ledData the buffer to write
124 */
125 void SetData(std::initializer_list<LEDData> ledData);
126
127 /**
128 * Sets the bit timing.
129 *
130 * <p>By default, the driver is set up to drive WS2812Bs, so nothing needs to
131 * be set for those.
132 *
133 * @param highTime0 high time for 0 bit (default 400ns)
134 * @param lowTime0 low time for 0 bit (default 900ns)
135 * @param highTime1 high time for 1 bit (default 900ns)
136 * @param lowTime1 low time for 1 bit (default 600ns)
137 */
138 void SetBitTiming(units::nanosecond_t highTime0, units::nanosecond_t lowTime0,
139 units::nanosecond_t highTime1,
140 units::nanosecond_t lowTime1);
141
142 /**
143 * Sets the sync time.
144 *
145 * <p>The sync time is the time to hold output so LEDs enable. Default set for
146 * WS2812B.
147 *
148 * @param syncTime the sync time (default 280us)
149 */
150 void SetSyncTime(units::microsecond_t syncTime);
151
152 /**
153 * Starts the output.
154 *
155 * <p>The output writes continuously.
156 */
157 void Start();
158
159 /**
160 * Stops the output.
161 */
162 void Stop();
163
164 private:
167 int m_port;
168};
169} // namespace frc
Definition: AddressableLED.h:31
LEDData()
Definition: AddressableLED.h:33
void SetLED(const Color8Bit &color)
Definition: AddressableLED.h:79
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:48
LEDData(int _r, int _g, int _b)
Definition: AddressableLED.h:34
void SetLED(const Color &color)
Definition: AddressableLED.h:68
A class for driving addressable LEDs, such as WS2812Bs and NeoPixels.
Definition: AddressableLED.h:29
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(int port)
Constructs a new driver for a specific port.
void Stop()
Stops the output.
void SetLength(int length)
Sets the length of the LED strip.
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
Definition: AprilTagDetector_cv.h:11
static constexpr const unit_t< compound_unit< energy::joule, time::seconds > > h(6.626070040e-34)
Planck constant.
structure for holding one LED's color data.
Definition: AddressableLEDTypes.h:13
uint8_t r
red value
Definition: AddressableLEDTypes.h:16
uint8_t padding
Definition: AddressableLEDTypes.h:17
uint8_t g
green value
Definition: AddressableLEDTypes.h:15
uint8_t b
blue value
Definition: AddressableLEDTypes.h:14
color
Definition: color.h:16