WPILibC++ 2024.1.1-beta-4
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.
26 */
28 public:
30 public:
31 LEDData() : LEDData(0, 0, 0) {}
32 LEDData(int _r, int _g, int _b) {
33 r = _r;
34 g = _g;
35 b = _b;
36 padding = 0;
37 }
38
39 /**
40 * A helper method to set all values of the LED.
41 *
42 * @param r the r value [0-255]
43 * @param g the g value [0-255]
44 * @param b the b value [0-255]
45 */
46 void SetRGB(int r, int g, int b) {
47 this->r = r;
48 this->g = g;
49 this->b = b;
50 }
51
52 /**
53 * A helper method to set all values of the LED.
54 *
55 * @param h the h value [0-180]
56 * @param s the s value [0-255]
57 * @param v the v value [0-255]
58 */
59 void SetHSV(int h, int s, int v);
60
61 /*
62 * Sets a specific LED in the buffer.
63 *
64 * @param color The color of the LED
65 */
66 void SetLED(const Color& color) {
67 this->r = color.red * 255;
68 this->g = color.green * 255;
69 this->b = color.blue * 255;
70 }
71
72 /*
73 * Sets a specific LED in the buffer.
74 *
75 * @param color The color of the LED
76 */
77 void SetLED(const Color8Bit& color) {
78 this->r = color.red;
79 this->g = color.green;
80 this->b = color.blue;
81 }
82 };
83
84 /**
85 * Constructs a new driver for a specific port.
86 *
87 * @param port the output port to use (Must be a PWM header)
88 */
89 explicit AddressableLED(int port);
90
92
93 /**
94 * Sets the length of the LED strip.
95 *
96 * <p>Calling this is an expensive call, so its best to call it once, then
97 * just update data.
98 *
99 * <p>The max length is 5460 LEDs.
100 *
101 * @param length the strip length
102 */
103 void SetLength(int length);
104
105 /**
106 * Sets the led output data.
107 *
108 * <p>If the output is enabled, this will start writing the next data cycle.
109 * It is safe to call, even while output is enabled.
110 *
111 * @param ledData the buffer to write
112 */
113 void SetData(std::span<const LEDData> ledData);
114
115 /**
116 * Sets the led output data.
117 *
118 * <p>If the output is enabled, this will start writing the next data cycle.
119 * It is safe to call, even while output is enabled.
120 *
121 * @param ledData the buffer to write
122 */
123 void SetData(std::initializer_list<LEDData> ledData);
124
125 /**
126 * Sets the bit timing.
127 *
128 * <p>By default, the driver is set up to drive WS2812Bs, so nothing needs to
129 * be set for those.
130 *
131 * @param highTime0 high time for 0 bit (default 400ns)
132 * @param lowTime0 low time for 0 bit (default 900ns)
133 * @param highTime1 high time for 1 bit (default 900ns)
134 * @param lowTime1 low time for 1 bit (default 600ns)
135 */
136 void SetBitTiming(units::nanosecond_t highTime0, units::nanosecond_t lowTime0,
137 units::nanosecond_t highTime1,
138 units::nanosecond_t lowTime1);
139
140 /**
141 * Sets the sync time.
142 *
143 * <p>The sync time is the time to hold output so LEDs enable. Default set for
144 * WS2812B.
145 *
146 * @param syncTime the sync time (default 280us)
147 */
148 void SetSyncTime(units::microsecond_t syncTime);
149
150 /**
151 * Starts the output.
152 *
153 * <p>The output writes continuously.
154 */
155 void Start();
156
157 /**
158 * Stops the output.
159 */
160 void Stop();
161
162 private:
165 int m_port;
166};
167} // namespace frc
Definition: AddressableLED.h:29
LEDData()
Definition: AddressableLED.h:31
void SetLED(const Color8Bit &color)
Definition: AddressableLED.h:77
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:46
LEDData(int _r, int _g, int _b)
Definition: AddressableLED.h:32
void SetLED(const Color &color)
Definition: AddressableLED.h:66
A class for driving addressable LEDs, such as WS2812Bs and NeoPixels.
Definition: AddressableLED.h:27
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
Definition: Color8Bit.h:118
int blue
Definition: Color8Bit.h:120
int green
Definition: Color8Bit.h:119
Represents colors that can be used with Addressable LEDs.
Definition: Color.h:23
double green
Definition: Color.h:867
double red
Definition: Color.h:866
double blue
Definition: Color.h:868
Definition: AprilTagPoseEstimator.h:15
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