WPILibC++ 2025.0.0-alpha-1-9-ga2beb75
AddressableLEDSim.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 <memory>
8
10
12
13namespace frc {
14
15class AddressableLED;
16
17namespace sim {
18
19/**
20 * Class to control a simulated addressable LED.
21 */
23 public:
24 /**
25 * Constructs for the first addressable LED.
26 */
28
29 /**
30 * Constructs from an AddressableLED object.
31 *
32 * @param addressableLED AddressableLED to simulate
33 */
34 explicit AddressableLEDSim(const AddressableLED& addressableLED);
35
36 /**
37 * Creates an AddressableLEDSim for a PWM channel.
38 *
39 * @param pwmChannel PWM channel
40 * @return Simulated object
41 * @throws std::out_of_range if no AddressableLED is configured for that
42 * channel
43 */
44 static AddressableLEDSim CreateForChannel(int pwmChannel);
45
46 /**
47 * Creates an AddressableLEDSim for a simulated index.
48 * The index is incremented for each simulated AddressableLED.
49 *
50 * @param index simulator index
51 * @return Simulated object
52 */
54
55 /**
56 * Register a callback on the Initialized property.
57 *
58 * @param callback the callback that will be called whenever the Initialized
59 * property is changed
60 * @param initialNotify if true, the callback will be run on the initial value
61 * @return the CallbackStore object storing this callback
62 */
63 [[nodiscard]]
64 std::unique_ptr<CallbackStore> RegisterInitializedCallback(
65 NotifyCallback callback, bool initialNotify);
66
67 /**
68 * Check if initialized.
69 *
70 * @return true if initialized
71 */
72 bool GetInitialized() const;
73
74 /**
75 * Change the Initialized value of the LED strip.
76 *
77 * @param initialized the new value
78 */
79 void SetInitialized(bool initialized);
80
81 /**
82 * Register a callback on the output port.
83 *
84 * @param callback the callback that will be called whenever the output port
85 * is changed
86 * @param initialNotify if true, the callback will be run on the initial value
87 * @return the CallbackStore object associated with this callback
88 */
89 [[nodiscard]]
90 std::unique_ptr<CallbackStore> RegisterOutputPortCallback(
91 NotifyCallback callback, bool initialNotify);
92
93 /**
94 * Get the output port.
95 *
96 * @return the output port
97 */
98 int GetOutputPort() const;
99
100 /**
101 * Change the output port.
102 *
103 * @param outputPort the new output port
104 */
105 void SetOutputPort(int outputPort);
106
107 /**
108 * Register a callback on the length.
109 *
110 * @param callback the callback that will be called whenever the length is
111 * changed
112 * @param initialNotify if true, the callback will be run on the initial value
113 * @return the CallbackStore object associated with this callback
114 */
115 [[nodiscard]]
116 std::unique_ptr<CallbackStore> RegisterLengthCallback(NotifyCallback callback,
117 bool initialNotify);
118
119 /**
120 * Get the length of the LED strip.
121 *
122 * @return the length
123 */
124 int GetLength() const;
125
126 /**
127 * Change the length of the LED strip.
128 *
129 * @param length the new value
130 */
131 void SetLength(int length);
132
133 /**
134 * Register a callback on whether the LEDs are running.
135 *
136 * @param callback the callback that will be called whenever the LED state is
137 * changed
138 * @param initialNotify if true, the callback will be run on the initial value
139 * @return the CallbackStore object associated with this callback
140 */
141 [[nodiscard]]
142 std::unique_ptr<CallbackStore> RegisterRunningCallback(
143 NotifyCallback callback, bool initialNotify);
144
145 /**
146 * Check if the LEDs are running.
147 *
148 * @return true if they are
149 */
150 int GetRunning() const;
151
152 /**
153 * Change whether the LEDs are active.
154 *
155 * @param running the new value
156 */
157 void SetRunning(bool running);
158
159 /**
160 * Register a callback on the LED data.
161 *
162 * @param callback the callback that will be called whenever the LED data is
163 * changed
164 * @param initialNotify if true, the callback will be run on the initial value
165 * @return the CallbackStore object associated with this callback
166 */
167 [[nodiscard]]
168 std::unique_ptr<CallbackStore> RegisterDataCallback(
169 ConstBufferCallback callback, bool initialNotify);
170
171 /**
172 * Get the LED data.
173 *
174 * @param data output parameter to fill with LED data
175 * @return the length of the LED data
176 */
177 int GetData(struct HAL_AddressableLEDData* data) const;
178
179 /**
180 * Change the LED data.
181 *
182 * @param data the new data
183 * @param length the length of the LED data
184 */
185 void SetData(struct HAL_AddressableLEDData* data, int length);
186
187 private:
188 explicit AddressableLEDSim(int index) : m_index{index} {}
189
190 int m_index;
191};
192} // namespace sim
193} // namespace frc
A class for driving addressable LEDs, such as WS2812Bs and NeoPixels.
Definition: AddressableLED.h:29
Class to control a simulated addressable LED.
Definition: AddressableLEDSim.h:22
void SetInitialized(bool initialized)
Change the Initialized value of the LED strip.
std::unique_ptr< CallbackStore > RegisterRunningCallback(NotifyCallback callback, bool initialNotify)
Register a callback on whether the LEDs are running.
AddressableLEDSim()
Constructs for the first addressable LED.
static AddressableLEDSim CreateForIndex(int index)
Creates an AddressableLEDSim for a simulated index.
int GetData(struct HAL_AddressableLEDData *data) const
Get the LED data.
AddressableLEDSim(const AddressableLED &addressableLED)
Constructs from an AddressableLED object.
void SetRunning(bool running)
Change whether the LEDs are active.
void SetLength(int length)
Change the length of the LED strip.
static AddressableLEDSim CreateForChannel(int pwmChannel)
Creates an AddressableLEDSim for a PWM channel.
bool GetInitialized() const
Check if initialized.
void SetOutputPort(int outputPort)
Change the output port.
int GetOutputPort() const
Get the output port.
int GetLength() const
Get the length of the LED strip.
void SetData(struct HAL_AddressableLEDData *data, int length)
Change the LED data.
std::unique_ptr< CallbackStore > RegisterLengthCallback(NotifyCallback callback, bool initialNotify)
Register a callback on the length.
int GetRunning() const
Check if the LEDs are running.
std::unique_ptr< CallbackStore > RegisterOutputPortCallback(NotifyCallback callback, bool initialNotify)
Register a callback on the output port.
std::unique_ptr< CallbackStore > RegisterDataCallback(ConstBufferCallback callback, bool initialNotify)
Register a callback on the LED data.
std::unique_ptr< CallbackStore > RegisterInitializedCallback(NotifyCallback callback, bool initialNotify)
Register a callback on the Initialized property.
std::function< void(std::string_view, const unsigned char *buffer, unsigned int count)> ConstBufferCallback
Definition: CallbackStore.h:16
std::function< void(std::string_view, const HAL_Value *)> NotifyCallback
Definition: CallbackStore.h:14
Definition: AprilTagDetector_cv.h:11
structure for holding one LED's color data.
Definition: AddressableLEDTypes.h:13