FastLED 3.9.15
Loading...
Searching...
No Matches
ucs7604.h
Go to the documentation of this file.
1#pragma once
2
3#ifndef __INC_UCS7604_H
4#define __INC_UCS7604_H
5
6#include "pixel_controller.h"
11#include "fl/stl/bit_cast.h"
12#include "fl/stl/vector.h"
13#include "fl/math/ease.h"
14#include "fl/stl/iterator.h"
15#include "fl/stl/noexcept.h"
16
47
48#ifndef FL_UCS7604_BRIGHTNESS
49#define FL_UCS7604_BRIGHTNESS 0x0F
50#endif
51
52namespace fl {
53
55namespace ucs7604 {
58
63 void set_brightness(CurrentControl current);
64
72 inline void set_brightness(u8 r, u8 g, u8 b, u8 w) {
73 set_brightness(CurrentControl(r, g, b, w));
74 }
75
79
80} // namespace ucs7604
81
83template <
84 int DATA_PIN,
85 EOrder RGB_ORDER, // Color order for input pixels (converted to RGB internally)
86 fl::UCS7604Mode MODE,
87 typename CHIPSET_TIMING,
88 template<int, typename, EOrder> class CLOCKLESS_CONTROLLER
89>
90class UCS7604ControllerT : public CPixelLEDController<RGB_ORDER>
91{
92private:
93 using DelegateControllerBase = CLOCKLESS_CONTROLLER<DATA_PIN, CHIPSET_TIMING, RGB>;
94
95 // Helper class to access protected methods of the delegate controller
97 friend class UCS7604ControllerT<DATA_PIN, RGB_ORDER, MODE, CHIPSET_TIMING, CLOCKLESS_CONTROLLER>;
99 DelegateControllerBase::showPixels(pixels);
100 }
101 };
102
103 DelegateController mDelegate; // Clockless controller for wire transmission (always RGB)
104
105 // Reusable byte buffer (uses PSRAM on ESP32, regular heap elsewhere)
106 // Cleared each frame but memory is reused (no reallocation after first use)
108
109public:
111
112 virtual void init() override {
113 mDelegate.init();
114 // UCS7604 is always RGBW — override any user setting.
116 }
117
118 // Access delegate controller (for testing)
119 const DelegateController& getDelegate() const {
120 return mDelegate;
121 }
122
123 DelegateController& getDelegate() {
124 return mDelegate;
125 }
126
127protected:
128
130 return mByteBuffer;
131 }
132
133 virtual void showPixels(PixelController<RGB_ORDER> &pixels) override {
134 if (pixels.size() == 0) {
135 return;
136 }
137
138 // Get current control values (recalculated each frame)
140
141 // Reorder RGB current values to match the color order (RGB_ORDER template parameter)
142 // The current control values are semantic (current.r controls RED LEDs, etc.)
143 // but need to be sent in wire order matching the pixel data reordering
144 u8 rgb_currents[3] = {current.r, current.g, current.b};
145
146 // Extract channel positions from RGB_ORDER (octal encoding: 0=R, 1=G, 2=B)
147 // Octal digits are read right-to-left in bit positions but left-to-right semantically
148 // RGB (012 octal): wire position 0=R(0), 1=G(1), 2=B(2)
149 // GRB (102 octal): wire position 0=G(1), 1=R(0), 2=B(2)
150 u8 pos0 = (static_cast<int>(RGB_ORDER) >> 6) & 0x3; // Wire position 0 (leftmost octal digit)
151 u8 pos1 = (static_cast<int>(RGB_ORDER) >> 3) & 0x3; // Wire position 1 (middle octal digit)
152 u8 pos2 = (static_cast<int>(RGB_ORDER) >> 0) & 0x3; // Wire position 2 (rightmost octal digit)
153
154 // Reorder: wire R gets current for channel at pos0, etc.
155 u8 r_current = rgb_currents[pos0]; // Wire R (position 0)
156 u8 g_current = rgb_currents[pos1]; // Wire G (position 1)
157 u8 b_current = rgb_currents[pos2]; // Wire B (position 2)
158 u8 w_current = current.w; // W always in position 3
159
160 // Create current control struct with reordered values
161 fl::UCS7604CurrentControl wire_current(r_current, g_current, b_current, w_current);
162
163 // Get gamma LUT: per-controller override or default 2.8
164 float gamma = this->mSettings.mGamma.value_or(2.8f);
166
167 // UCS7604 is always RGBW — override any user setting.
169 fl::PixelIterator pixel_iter = pixels.as_iterator(rgbw);
170
171 // Clear buffer and use encoder to fill it
172 mByteBuffer.clear();
174 MODE, wire_current, pixel_iter.get_rgbw().active(), gamma8.get());
175
176 // Reinterpret byte buffer as CRGB pixels (encoder ensures divisible by 3)
177 size_t num_pixels = mByteBuffer.size() / 3;
178 CRGB* fake_pixels = fl::bit_cast<CRGB*>(mByteBuffer.data());
179
180 // Construct PixelController and send to delegate controller
181 PixelController<RGB> pixel_data(fake_pixels, num_pixels, ColorAdjustment::noAdjustment(), DISABLE_DITHER);
182 mDelegate.callShowPixels(pixel_data);
183 }
184};
185
186// now typedef the controllers
187template <int DATA_PIN, EOrder RGB_ORDER, template<int, typename, EOrder> class CLOCKLESS_CONTROLLER>
189
190template <int DATA_PIN, EOrder RGB_ORDER, template<int, typename, EOrder> class CLOCKLESS_CONTROLLER>
192
193template <int DATA_PIN, EOrder RGB_ORDER, template<int, typename, EOrder> class CLOCKLESS_CONTROLLER>
195
196} // namespace fl
197
198#endif // __INC_UCS7604_H
#define DATA_PIN
Definition ClientReal.h:82
#define RGB_ORDER
Rgbw rgbw
CPixelLEDController(RegistrationMode mode)
Protected constructor with registration mode.
CLEDController & setRgbw(const Rgbw &arg=RgbwDefault::value()) FL_NOEXCEPT
ChannelOptions mSettings
Optional channel settings (correction, temperature, dither, rgbw, affinity)
static fl::shared_ptr< const Gamma8 > getOrCreate(float gamma) FL_NOEXCEPT
Definition ease.cpp.hpp:459
Rgbw get_rgbw() const FL_NOEXCEPT
void callShowPixels(PixelController< RGB > &pixels)
Definition ucs7604.h:98
CLOCKLESS_CONTROLLER< DATA_PIN, CHIPSET_TIMING, RGB > DelegateControllerBase
Definition ucs7604.h:93
virtual void showPixels(PixelController< RGB_ORDER > &pixels) override
Definition ucs7604.h:133
UCS7604ControllerT() FL_NOEXCEPT
Definition ucs7604.h:110
fl::span< const u8 > bytes() const
Definition ucs7604.h:129
virtual void init() override
Initialize the LED controller.
Definition ucs7604.h:112
const DelegateController & getDelegate() const
Definition ucs7604.h:119
DelegateController & getDelegate()
Definition ucs7604.h:123
UCS7604 controller extending CPixelLEDController.
Definition ucs7604.h:91
T * get() const FL_NOEXCEPT
Definition shared_ptr.h:334
defines the templated version of the CLEDController class
#define DISABLE_DITHER
Disable dithering.
Definition dither_mode.h:10
UCS7604 LED chipset encoder implementation.
fl::EOrder EOrder
Definition eorder.h:12
Non-templated low level pixel data writing class.
Centralized LED chipset timing definitions with nanosecond precision.
CurrentControl brightness()
Get current global UCS7604 brightness value.
void set_brightness(CurrentControl current)
Set global UCS7604 brightness via current control (EXPERIMENTAL)
UCS7604CurrentControl CurrentControl
Type alias for current control (defined in ucs7604_encoder.h)
Definition ucs7604.h:57
unsigned char u8
Definition stdint.h:131
back_insert_iterator< Container > back_inserter(Container &c) FL_NOEXCEPT
Helper function to create a back_insert_iterator.
Definition iterator.h:139
UCS7604ControllerT< DATA_PIN, RGB_ORDER, fl::UCS7604Mode::UCS7604_MODE_16BIT_800KHZ, fl::TIMING_UCS7604_800KHZ, CLOCKLESS_CONTROLLER > UCS7604Controller16bitT
Definition ucs7604.h:191
UCS7604Mode
UCS7604 protocol configuration modes.
Definition ucs7604.h:28
constexpr u32 gamma(float g) FL_NOEXCEPT
Definition gamma_lut.h:36
UCS7604ControllerT< DATA_PIN, RGB_ORDER, fl::UCS7604Mode::UCS7604_MODE_8BIT_800KHZ, fl::TIMING_UCS7604_800KHZ, CLOCKLESS_CONTROLLER > UCS7604Controller8bitT
Definition ucs7604.h:188
To bit_cast(const From &from) FL_NOEXCEPT
Definition bit_cast.h:48
UCS7604ControllerT< DATA_PIN, RGB_ORDER, fl::UCS7604Mode::UCS7604_MODE_16BIT_1600KHZ, fl::TIMING_UCS7604_1600KHZ, CLOCKLESS_CONTROLLER > UCS7604Controller16bit1600T
Definition ucs7604.h:194
void encodeUCS7604(PixelIterator &pixel_iter, size_t num_leds, OutputIterator out, UCS7604Mode mode, const UCS7604CurrentControl &current, bool is_rgbw, const Gamma8 *gamma=nullptr)
Encode complete UCS7604 frame (preamble + padding + pixel data)
Definition ucs7604.h:209
Base definition for an LED controller.
Definition crgb.hpp:179
Low level pixel data writing class.
#define FL_NOEXCEPT
static ColorAdjustment noAdjustment()
the per-channel scale values premixed with brightness.
FASTLED_FORCE_INLINE int size() const
Get the length of the LED strip.
FASTLED_FORCE_INLINE fl::PixelIterator as_iterator(const Rgbw &rgbw)
Pixel controller class.
Representation of an 8-bit RGB pixel (Red, Green, Blue)
Definition crgb.h:38
FASTLED_FORCE_INLINE bool active() const FL_NOEXCEPT
Definition rgbw.h:182
static Rgbw value() FL_NOEXCEPT
Definition rgbw.h:214
u8 g
Green channel current (0x0-0xF)
Definition ucs7604.h:37
u8 b
Blue channel current (0x0-0xF)
Definition ucs7604.h:38
u8 w
White channel current (0x0-0xF)
Definition ucs7604.h:39
u8 r
Red channel current (0x0-0xF)
Definition ucs7604.h:36
UCS7604 current control structure with 4-bit fields for each channel.
Definition ucs7604.h:35