forked from FastLED/FastLED
-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathchipsets.h
More file actions
1522 lines (1285 loc) · 55.5 KB
/
chipsets.h
File metadata and controls
1522 lines (1285 loc) · 55.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#pragma once
#ifndef __INC_CHIPSETS_H
#define __INC_CHIPSETS_H
#include "pixeltypes.h"
#include "fl/five_bit_hd_gamma.h"
#include "fl/force_inline.h"
#include "fl/bit_cast.h"
#include "pixel_iterator.h"
#include "crgb.h"
#include "eorder.h"
#include "fl/namespace.h"
#include "fl/math_macros.h"
// Include UCS7604 controller
#include "fl/chipsets/ucs7604.h" // optional.
// Conditional namespace handling for WASM builds
#ifdef FASTLED_FORCE_NAMESPACE
#define FASTLED_CLOCKLESS_CONTROLLER fl::ClocklessController
#else
#define FASTLED_CLOCKLESS_CONTROLLER ClocklessController
#endif
#ifndef FASTLED_CLOCKLESS_USES_NANOSECONDS
#if defined(FASTLED_TEENSY4)
#define FASTLED_CLOCKLESS_USES_NANOSECONDS 1
#elif defined(ESP32)
#include "third_party/espressif/led_strip/src/enabled.h"
// RMT 5.1 driver converts from nanoseconds to RMT ticks.
#if FASTLED_RMT5
#define FASTLED_CLOCKLESS_USES_NANOSECONDS 1
#else
#define FASTLED_CLOCKLESS_USES_NANOSECONDS 0
#endif
#else
#define FASTLED_CLOCKLESS_USES_NANOSECONDS 0
#endif // FASTLED_TEENSY4
#endif // FASTLED_CLOCKLESS_USES_NANOSECONDS
// Allow overclocking of the clockless family of leds. 1.2 would be
// 20% overclocking. In tests WS2812 can be overclocked at 20%, but
// various manufacturers may be different. This is a global value
// which is overridable by each supported chipset.
#ifdef FASTLED_LED_OVERCLOCK
#warning "FASTLED_LED_OVERCLOCK has been changed to FASTLED_OVERCLOCK. Please update your code."
#define FASTLED_OVERCLOCK FASTLED_LED_OVERCLOCK
#endif
#ifndef FASTLED_OVERCLOCK
#define FASTLED_OVERCLOCK 1.0
#else
#ifndef FASTLED_OVERCLOCK_SUPPRESS_WARNING
#warning "FASTLED_OVERCLOCK is now active, #define FASTLED_OVERCLOCK_SUPPRESS_WARNING to disable this warning"
#endif
#endif
// So many platforms have specialized WS2812 controllers. Why? Because they
// are the cheapest chipsets use. So we special case this.
#include "platforms/chipsets_specialized_ws2812.h"
/// @file chipsets.h
/// Contains the bulk of the definitions for the various LED chipsets supported.
/// @defgroup Chipsets LED Chipset Controllers
/// Implementations of ::CLEDController classes for various led chipsets.
///
/// @{
#if defined(ARDUINO) //&& defined(SoftwareSerial_h)
#if defined(SoftwareSerial_h) || defined(__SoftwareSerial_h)
#include <SoftwareSerial.h>
#define HAS_PIXIE
FASTLED_NAMESPACE_BEGIN
/// Adafruit Pixie controller class
/// @tparam DATA_PIN the pin to write data out on
/// @tparam RGB_ORDER the RGB ordering for the LED data
template<fl::u8 DATA_PIN, EOrder RGB_ORDER = RGB>
class PixieController : public CPixelLEDController<RGB_ORDER> {
SoftwareSerial Serial;
CMinWait<2000> mWait;
public:
PixieController() : Serial(-1, DATA_PIN) {}
protected:
/// Initialize the controller
virtual void init() {
Serial.begin(115200);
mWait.mark();
}
/// @copydoc CPixelLEDController::showPixels()
virtual void showPixels(PixelController<RGB_ORDER> & pixels) {
mWait.wait();
while(pixels.has(1)) {
fl::u8 r = pixels.loadAndScale0();
Serial.write(r);
fl::u8 g = pixels.loadAndScale1();
Serial.write(g);
fl::u8 b = pixels.loadAndScale2();
Serial.write(b);
pixels.advanceData();
pixels.stepDithering();
}
mWait.mark();
}
};
// template<SoftwareSerial & STREAM, EOrder RGB_ORDER = RGB>
// class PixieController : public PixieBaseController<STREAM, RGB_ORDER> {
// public:
// virtual void init() {
// STREAM.begin(115200);
// }
// };
FASTLED_NAMESPACE_END
#endif
#endif
/// @brief Emulation layer to support RGBW LEDs on RGB controllers
/// @details This template class allows you to use RGBW (4-channel) LED strips with
/// controllers that only support RGB (3-channel) output. It works by:
/// 1. Creating an internal buffer to store the converted RGBW data
/// 2. Converting RGB color values to RGBW using configurable color conversion modes
/// 3. Packing the RGBW data (4 bytes per pixel) into RGB format (3 bytes) for transmission
/// 4. Sending the packed data to the underlying RGB controller
///
/// @tparam CONTROLLER The base RGB controller type (e.g., WS2812)
/// @tparam RGB_ORDER The color channel ordering (e.g., GRB for WS2812)
///
/// Usage Example:
/// @code
/// // Define your base RGB controller (must use RGB ordering, no reordering allowed)
/// typedef WS2812<DATA_PIN, RGB> ControllerT;
///
/// // Create the RGBW emulator with your desired color ordering
/// static RGBWEmulatedController<ControllerT, GRB> rgbwController;
///
/// // Add to FastLED
/// FastLED.addLeds(&rgbwController, leds, NUM_LEDS);
/// @endcode
///
/// Color Conversion Modes (via Rgbw parameter):
/// - kRGBWExactColors: Preserves color accuracy, reduces max brightness
/// - kRGBWMaxBrightness: Maximizes brightness, may oversaturate colors
/// - kRGBWBoostedWhite: Boosts white channel for better whites
/// - kRGBWNullWhitePixel: Disables white channel (RGB mode only)
///
/// @note The base CONTROLLER must use RGB ordering (no internal reordering).
/// Color channel reordering is handled by this wrapper class via RGB_ORDER.
FASTLED_NAMESPACE_BEGIN
template <
typename CONTROLLER,
EOrder RGB_ORDER = GRB> // Default on WS2812>
class RGBWEmulatedController
: public CPixelLEDController<RGB_ORDER, CONTROLLER::LANES_VALUE,
CONTROLLER::MASK_VALUE> {
public:
// ControllerT is a helper class. It subclasses the device controller class
// and has three methods to call the three protected methods we use.
// This is janky, but redeclaring public methods protected in a derived class
// is janky, too.
// N.B., byte order must be RGB.
typedef CONTROLLER ControllerBaseT;
class ControllerT : public CONTROLLER {
friend class RGBWEmulatedController<CONTROLLER, RGB_ORDER>;
void *callBeginShowLeds(int size) { return ControllerBaseT::beginShowLeds(size); }
void callShow(CRGB *data, int nLeds, fl::u8 brightness) {
ControllerBaseT::show(data, nLeds, brightness);
}
void callEndShowLeds(void *data) { ControllerBaseT::endShowLeds(data); }
};
static const int LANES = CONTROLLER::LANES_VALUE;
static const uint32_t MASK = CONTROLLER::MASK_VALUE;
// The delegated controller must do no reordering.
static_assert(RGB == CONTROLLER::RGB_ORDER_VALUE, "The delegated controller MUST NOT do reordering");
/// @brief Constructor with optional RGBW configuration
/// @param rgbw Configuration for RGBW color conversion (defaults to kRGBWExactColors mode)
RGBWEmulatedController(const Rgbw& rgbw = RgbwDefault()) {
this->setRgbw(rgbw);
};
/// @brief Destructor - cleans up the internal RGBW buffer
~RGBWEmulatedController() { delete[] mRGBWPixels; }
virtual void *beginShowLeds(int size) override {
return mController.callBeginShowLeds(Rgbw::size_as_rgb(size));
}
virtual void endShowLeds(void *data) override {
return mController.callEndShowLeds(data);
}
/// @brief Main rendering function that converts RGB to RGBW and shows pixels
/// @details This function:
/// 1. Converts each RGB pixel to RGBW format based on the configured conversion mode
/// 2. Packs the RGBW data into a format the RGB controller can transmit
/// 3. Temporarily bypasses color correction/temperature on the base controller
/// 4. Sends the packed data to the physical LED strip
/// @param pixels The pixel controller containing RGB data to be converted
virtual void showPixels(PixelController<RGB_ORDER, LANES, MASK> &pixels) override {
// Ensure buffer is large enough
ensureBuffer(pixels.size());
Rgbw rgbw = this->getRgbw();
fl::u8 *data = fl::bit_cast_ptr<fl::u8>(mRGBWPixels);
while (pixels.has(1)) {
pixels.stepDithering();
pixels.loadAndScaleRGBW(rgbw, data, data + 1, data + 2, data + 3);
data += 4;
pixels.advanceData();
}
// Force the device controller to a state where it passes data through
// unmodified: color correction, color temperature, dither, and brightness
// (passed as an argument to show()). Temporarily enable the controller,
// show the LEDs, and disable it again.
//
// The device controller is in the global controller list, so if we
// don't keep it disabled, it will refresh again with unknown brightness,
// temperature, etc.
mController.setCorrection(CRGB(255, 255, 255));
mController.setTemperature(CRGB(255, 255, 255));
mController.setDither(DISABLE_DITHER);
mController.setEnabled(true);
mController.callShow(mRGBWPixels, Rgbw::size_as_rgb(pixels.size()), 255);
mController.setEnabled(false);
}
private:
/// @brief Initialize the controller and disable the base controller
/// @details The base controller is kept disabled to prevent it from
/// refreshing with its own settings. We only enable it temporarily during show().
void init() override {
mController.init();
mController.setEnabled(false);
}
/// @brief Ensures the internal RGBW buffer is large enough for the LED count
/// @param num_leds Number of RGB LEDs to convert to RGBW
/// @details Reallocates the buffer if needed, accounting for the 4:3 byte ratio
/// when packing RGBW data into RGB format
void ensureBuffer(int32_t num_leds) {
if (num_leds != mNumRGBLeds) {
mNumRGBLeds = num_leds;
// The delegate controller expects the raw pixel byte data in multiples of 3.
// In the case of src data not a multiple of 3, then we need to
// add pad bytes so that the delegate controller doesn't walk off the end
// of the array and invoke a buffer overflow panic.
uint32_t new_size = Rgbw::size_as_rgb(num_leds);
delete[] mRGBWPixels;
mRGBWPixels = new CRGB[new_size];
// showPixels may never clear the last two pixels.
for (uint32_t i = 0; i < new_size; i++) {
mRGBWPixels[i] = CRGB(0, 0, 0);
}
mController.setLeds(mRGBWPixels, new_size);
}
}
CRGB *mRGBWPixels = nullptr; ///< Internal buffer for packed RGBW data
int32_t mNumRGBLeds = 0; ///< Number of RGB LEDs in the original array
int32_t mNumRGBWLeds = 0; ///< Number of RGBW pixels the buffer can hold
ControllerT mController; ///< The underlying RGB controller instance
};
/// @defgroup ClockedChipsets Clocked Chipsets
/// Nominally SPI based, these chipsets have a data and a clock line.
/// @{
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// LPD8806 controller class - takes data/clock/select pin values (N.B. should take an SPI definition?)
//
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// LPD8806 controller class.
/// @tparam DATA_PIN the data pin for these LEDs
/// @tparam CLOCK_PIN the clock pin for these LEDs
/// @tparam RGB_ORDER the RGB ordering for these LEDs
/// @tparam SPI_SPEED the clock divider used for these LEDs. Set using the ::DATA_RATE_MHZ / ::DATA_RATE_KHZ macros. Defaults to ::DATA_RATE_MHZ(12)
template <fl::u8 DATA_PIN, fl::u8 CLOCK_PIN, EOrder RGB_ORDER = RGB, uint32_t SPI_SPEED = DATA_RATE_MHZ(12) >
class LPD8806Controller : public CPixelLEDController<RGB_ORDER> {
typedef SPIOutput<DATA_PIN, CLOCK_PIN, SPI_SPEED> SPI;
class LPD8806_ADJUST {
public:
// LPD8806 spec wants the high bit of every rgb data byte sent out to be set.
FASTLED_FORCE_INLINE static fl::u8 adjust(FASTLED_REGISTER fl::u8 data) { return ((data>>1) | 0x80) + ((data && (data<254)) & 0x01); }
FASTLED_FORCE_INLINE static void postBlock(int len, void* context = NULL) {
SPI* pSPI = static_cast<SPI*>(context);
pSPI->writeBytesValueRaw(0, ((len*3+63)>>6));
}
};
SPI mSPI;
public:
LPD8806Controller() {}
virtual void init() {
mSPI.init();
}
protected:
/// @copydoc CPixelLEDController::showPixels()
virtual void showPixels(PixelController<RGB_ORDER> & pixels) {
mSPI.template writePixels<0, LPD8806_ADJUST, RGB_ORDER>(pixels, &mSPI);
}
public:
/// Get the protocol-safe padding byte for LPD8806
/// Used for quad-SPI lane padding when strips have different lengths
/// @returns 0x00 (latch continuation byte)
static constexpr fl::u8 getPaddingByte() { return 0x00; }
/// Get a black LED frame for synchronized latching
/// Used for quad-SPI lane padding to ensure all strips latch simultaneously
/// @returns Black LED frame (invisible LED: GRB with MSB set)
static fl::span<const fl::u8> getPaddingLEDFrame() {
static const fl::u8 frame[] = {
0x80, // Green = 0 (with MSB=1)
0x80, // Red = 0 (with MSB=1)
0x80 // Blue = 0 (with MSB=1)
};
return fl::span<const fl::u8>(frame, 3);
}
/// Get the size of the padding LED frame in bytes
/// @returns 3 bytes per LED for LPD8806
static constexpr size_t getPaddingLEDFrameSize() {
return 3;
}
/// Calculate total byte count for LPD8806 protocol
/// Used for quad-SPI buffer pre-allocation
/// @param num_leds Number of LEDs in the strip
/// @returns Total bytes needed (RGB data + latch bytes)
static constexpr size_t calculateBytes(size_t num_leds) {
// LPD8806 protocol:
// - LED data: 3 bytes per LED (GRB with high bit set)
// - Latch: ((num_leds * 3 + 63) / 64) bytes of 0x00
return (num_leds * 3) + ((num_leds * 3 + 63) / 64);
}
};
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// WS2801 definition - takes data/clock/select pin values (N.B. should take an SPI definition?)
//
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// WS2801 controller class.
/// @tparam DATA_PIN the data pin for these LEDs
/// @tparam CLOCK_PIN the clock pin for these LEDs
/// @tparam RGB_ORDER the RGB ordering for these LEDs
/// @tparam SPI_SPEED the clock divider used for these LEDs. Set using the ::DATA_RATE_MHZ / ::DATA_RATE_KHZ macros. Defaults to ::DATA_RATE_MHZ(1)
template <fl::u8 DATA_PIN, fl::u8 CLOCK_PIN, EOrder RGB_ORDER = RGB, uint32_t SPI_SPEED = DATA_RATE_MHZ(1)>
class WS2801Controller : public CPixelLEDController<RGB_ORDER> {
typedef SPIOutput<DATA_PIN, CLOCK_PIN, SPI_SPEED> SPI;
SPI mSPI;
CMinWait<1000> mWaitDelay;
public:
WS2801Controller() {}
/// Initialize the controller
virtual void init() {
mSPI.init();
mWaitDelay.mark();
}
protected:
/// @copydoc CPixelLEDController::showPixels()
virtual void showPixels(PixelController<RGB_ORDER> & pixels) {
mWaitDelay.wait();
mSPI.template writePixels<0, DATA_NOP, RGB_ORDER>(pixels, NULL);
mWaitDelay.mark();
}
public:
/// Get the protocol-safe padding byte for WS2801
/// Used for quad-SPI lane padding when strips have different lengths
/// @returns 0x00 (no protocol state)
static constexpr fl::u8 getPaddingByte() { return 0x00; }
/// Get a black LED frame for synchronized latching
/// Used for quad-SPI lane padding to ensure all strips latch simultaneously
/// @returns Black LED frame (invisible LED: RGB all zero)
static fl::span<const fl::u8> getPaddingLEDFrame() {
static const fl::u8 frame[] = {
0x00, // Red = 0
0x00, // Green = 0
0x00 // Blue = 0
};
return fl::span<const fl::u8>(frame, 3);
}
/// Get the size of the padding LED frame in bytes
/// @returns 3 bytes per LED for WS2801
static constexpr size_t getPaddingLEDFrameSize() {
return 3;
}
/// Calculate total byte count for WS2801 protocol
/// Used for quad-SPI buffer pre-allocation
/// @param num_leds Number of LEDs in the strip
/// @returns Total bytes needed (RGB data only, no overhead)
static constexpr size_t calculateBytes(size_t num_leds) {
// WS2801 protocol:
// - LED data: 3 bytes per LED (RGB)
// - No frame overhead (latch is timing-based, not data-based)
return num_leds * 3;
}
};
/// WS2803 controller class.
/// @copydetails WS2801Controller
template <fl::u8 DATA_PIN, fl::u8 CLOCK_PIN, EOrder RGB_ORDER = RGB, uint32_t SPI_SPEED = DATA_RATE_MHZ(25)>
class WS2803Controller : public WS2801Controller<DATA_PIN, CLOCK_PIN, RGB_ORDER, SPI_SPEED> {};
/// LPD6803 controller class (LPD1101).
/// 16 bit (1 bit const "1", 5 bit red, 5 bit green, 5 bit blue).
/// In chip CMODE pin must be set to 1 (inside oscillator mode).
/// @tparam DATA_PIN the data pin for these LEDs
/// @tparam CLOCK_PIN the clock pin for these LEDs
/// @tparam RGB_ORDER the RGB ordering for these LEDs
/// @tparam SPI_SPEED the clock divider used for these LEDs. Set using the ::DATA_RATE_MHZ / ::DATA_RATE_KHZ macros. Defaults to ::DATA_RATE_MHZ(12)
/// @see Datasheet: https://cdn-shop.adafruit.com/datasheets/LPD6803.pdf
template <fl::u8 DATA_PIN, fl::u8 CLOCK_PIN, EOrder RGB_ORDER = RGB, uint32_t SPI_SPEED = DATA_RATE_MHZ(12)>
class LPD6803Controller : public CPixelLEDController<RGB_ORDER> {
typedef SPIOutput<DATA_PIN, CLOCK_PIN, SPI_SPEED> SPI;
SPI mSPI;
void startBoundary() { mSPI.writeByte(0); mSPI.writeByte(0); mSPI.writeByte(0); mSPI.writeByte(0); }
void endBoundary(int nLeds) { int nDWords = (nLeds/32); do { mSPI.writeByte(0xFF); mSPI.writeByte(0x00); mSPI.writeByte(0x00); mSPI.writeByte(0x00); } while(nDWords--); }
public:
LPD6803Controller() {}
virtual void init() {
mSPI.init();
}
protected:
/// @copydoc CPixelLEDController::showPixels()
virtual void showPixels(PixelController<RGB_ORDER> & pixels) {
mSPI.select();
startBoundary();
while(pixels.has(1)) {
FASTLED_REGISTER fl::u16 command;
command = 0x8000;
command |= (pixels.loadAndScale0() & 0xF8) << 7; // red is the high 5 bits
command |= (pixels.loadAndScale1() & 0xF8) << 2; // green is the middle 5 bits
mSPI.writeByte((command >> 8) & 0xFF);
command |= pixels.loadAndScale2() >> 3 ; // blue is the low 5 bits
mSPI.writeByte(command & 0xFF);
pixels.stepDithering();
pixels.advanceData();
}
endBoundary(pixels.size());
mSPI.waitFully();
mSPI.release();
}
};
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// APA102 definition - takes data/clock/select pin values (N.B. should take an SPI definition?)
//
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// APA102 controller class.
/// @tparam DATA_PIN the data pin for these LEDs
/// @tparam CLOCK_PIN the clock pin for these LEDs
/// @tparam RGB_ORDER the RGB ordering for these LEDs
/// @tparam SPI_SPEED the clock divider used for these LEDs. Set using the ::DATA_RATE_MHZ / ::DATA_RATE_KHZ macros. Defaults to ::DATA_RATE_MHZ(12)
template <
fl::u8 DATA_PIN, fl::u8 CLOCK_PIN,
EOrder RGB_ORDER = RGB,
// APA102 has a bug where long strip can't handle full speed due to clock degredation.
// This only affects long strips, but then again if you have a short strip does 6 mhz actually slow
// you down? Probably not. And you can always bump it up for speed. Therefore we are prioritizing
// "just works" over "fastest possible" here.
// https://www.pjrc.com/why-apa102-leds-have-trouble-at-24-mhz/
uint32_t SPI_SPEED = DATA_RATE_MHZ(6),
fl::FiveBitGammaCorrectionMode GAMMA_CORRECTION_MODE = fl::kFiveBitGammaCorrectionMode_Null,
uint32_t START_FRAME = 0x00000000,
uint32_t END_FRAME = 0xFF000000
>
class APA102Controller : public CPixelLEDController<RGB_ORDER> {
typedef SPIOutput<DATA_PIN, CLOCK_PIN, SPI_SPEED> SPI;
SPI mSPI;
void startBoundary() {
mSPI.writeWord(START_FRAME >> 16);
mSPI.writeWord(START_FRAME & 0xFFFF);
}
void endBoundary(int nLeds) {
int nDWords = (nLeds/32);
const fl::u8 b0 = fl::u8(END_FRAME >> 24 & 0x000000ff);
const fl::u8 b1 = fl::u8(END_FRAME >> 16 & 0x000000ff);
const fl::u8 b2 = fl::u8(END_FRAME >> 8 & 0x000000ff);
const fl::u8 b3 = fl::u8(END_FRAME >> 0 & 0x000000ff);
do {
mSPI.writeByte(b0);
mSPI.writeByte(b1);
mSPI.writeByte(b2);
mSPI.writeByte(b3);
} while(nDWords--);
}
FASTLED_FORCE_INLINE void writeLed(fl::u8 brightness, fl::u8 b0, fl::u8 b1, fl::u8 b2) {
#ifdef FASTLED_SPI_BYTE_ONLY
mSPI.writeByte(0xE0 | brightness);
mSPI.writeByte(b0);
mSPI.writeByte(b1);
mSPI.writeByte(b2);
#else
fl::u16 b = 0xE000 | (brightness << 8) | (fl::u16)b0;
mSPI.writeWord(b);
fl::u16 w = b1 << 8;
w |= b2;
mSPI.writeWord(w);
#endif
}
FASTLED_FORCE_INLINE void write2Bytes(fl::u8 b1, fl::u8 b2) {
#ifdef FASTLED_SPI_BYTE_ONLY
mSPI.writeByte(b1);
mSPI.writeByte(b2);
#else
mSPI.writeWord(fl::u16(b1) << 8 | b2);
#endif
}
public:
APA102Controller() {}
virtual void init() override {
mSPI.init();
}
protected:
/// @copydoc CPixelLEDController::showPixels()
virtual void showPixels(PixelController<RGB_ORDER> & pixels) override {
switch (GAMMA_CORRECTION_MODE) {
case fl::kFiveBitGammaCorrectionMode_Null: {
showPixelsDefault(pixels);
break;
}
case fl::kFiveBitGammaCorrectionMode_BitShift: {
showPixelsGammaBitShift(pixels);
break;
}
}
}
private:
static inline void getGlobalBrightnessAndScalingFactors(
PixelController<RGB_ORDER> & pixels,
fl::u8* out_s0, fl::u8* out_s1, fl::u8* out_s2, fl::u8* out_brightness) {
#if FASTLED_HD_COLOR_MIXING
fl::u8 brightness;
pixels.getHdScale(out_s0, out_s1, out_s2, &brightness);
struct Math {
static fl::u16 map(fl::u16 x, fl::u16 in_min, fl::u16 in_max, fl::u16 out_min, fl::u16 out_max) {
const fl::u16 run = in_max - in_min;
const fl::u16 rise = out_max - out_min;
const fl::u16 delta = x - in_min;
return (delta * rise) / run + out_min;
}
};
// *out_brightness = Math::map(brightness, 0, 255, 0, 31);
fl::u16 bri = Math::map(brightness, 0, 255, 0, 31);
if (bri == 0 && brightness != 0) {
// Fixes https://github.com/FastLED/FastLED/issues/1908
bri = 1;
}
*out_brightness = static_cast<fl::u8>(bri);
return;
#else
fl::u8 s0, s1, s2;
pixels.loadAndScaleRGB(&s0, &s1, &s2);
#if FASTLED_USE_GLOBAL_BRIGHTNESS == 1
// This function is pure magic.
const fl::u16 maxBrightness = 0x1F;
fl::u16 brightness = ((((fl::u16)FL_MAX(FL_MAX(s0, s1), s2) + 1) * maxBrightness - 1) >> 8) + 1;
s0 = (maxBrightness * s0 + (brightness >> 1)) / brightness;
s1 = (maxBrightness * s1 + (brightness >> 1)) / brightness;
s2 = (maxBrightness * s2 + (brightness >> 1)) / brightness;
#else
const fl::u8 brightness = 0x1F;
#endif // FASTLED_USE_GLOBAL_BRIGHTNESS
*out_s0 = s0;
*out_s1 = s1;
*out_s2 = s2;
*out_brightness = static_cast<fl::u8>(brightness);
#endif // FASTLED_HD_COLOR_MIXING
}
// Legacy showPixels implementation.
inline void showPixelsDefault(PixelController<RGB_ORDER> & pixels) {
mSPI.select();
fl::u8 s0, s1, s2, global_brightness;
getGlobalBrightnessAndScalingFactors(pixels, &s0, &s1, &s2, &global_brightness);
startBoundary();
while (pixels.has(1)) {
fl::u8 c0, c1, c2;
pixels.loadAndScaleRGB(&c0, &c1, &c2);
writeLed(global_brightness, c0, c1, c2);
pixels.stepDithering();
pixels.advanceData();
}
endBoundary(pixels.size());
mSPI.waitFully();
mSPI.release();
// Finalize transmission (no-op on non-ESP32, flushes Quad-SPI on ESP32)
mSPI.finalizeTransmission();
}
inline void showPixelsGammaBitShift(PixelController<RGB_ORDER> & pixels) {
mSPI.select();
startBoundary();
while (pixels.has(1)) {
// Load raw uncorrected r,g,b values.
fl::u8 brightness, c0, c1, c2; // c0-c2 is the RGB data re-ordered for pixel
pixels.loadAndScale_APA102_HD(&c0, &c1, &c2, &brightness);
writeLed(brightness, c0, c1, c2);
pixels.stepDithering();
pixels.advanceData();
}
endBoundary(pixels.size());
mSPI.waitFully();
mSPI.release();
// Finalize transmission (no-op on non-ESP32, flushes Quad-SPI on ESP32)
mSPI.finalizeTransmission();
}
public:
/// Get the protocol-safe padding byte for APA102
/// Used for quad-SPI lane padding when strips have different lengths
/// @returns 0xFF (end frame continuation byte)
/// @deprecated Use getPaddingLEDFrame() for synchronized latching
static constexpr fl::u8 getPaddingByte() { return 0xFF; }
/// Get padding LED frame for synchronized latching in quad-SPI
/// Returns a black LED frame to prepend to shorter strips, ensuring
/// all strips finish transmitting simultaneously for synchronized updates
/// @returns Black LED frame (4 bytes: brightness=0, RGB=0,0,0)
static fl::span<const fl::u8> getPaddingLEDFrame() {
// APA102 LED frame format: [111BBBBB][B][G][R]
// Black LED: 0xE0 (brightness=0), RGB=0,0,0
static const fl::u8 frame[] = {
0xE0, // Brightness byte (111 00000 = brightness 0)
0x00, // Blue = 0
0x00, // Green = 0
0x00 // Red = 0
};
return fl::span<const fl::u8>(frame, 4);
}
/// Get size of padding LED frame in bytes
/// @returns 4 (APA102 uses 4 bytes per LED)
static constexpr size_t getPaddingLEDFrameSize() {
return 4;
}
/// Calculate total byte count for APA102 protocol
/// Used for quad-SPI buffer pre-allocation
/// @param num_leds Number of LEDs in the strip
/// @returns Total bytes needed (start frame + LED data + end frame)
static constexpr size_t calculateBytes(size_t num_leds) {
// APA102 protocol:
// - Start frame: 4 bytes (0x00000000)
// - LED data: 4 bytes per LED (brightness + RGB)
// - End frame: (num_leds / 32) + 1 DWords = 4 * ((num_leds / 32) + 1) bytes
return 4 + (num_leds * 4) + (4 * ((num_leds / 32) + 1));
}
};
/// APA102 high definition controller class.
/// @tparam DATA_PIN the data pin for these LEDs
/// @tparam CLOCK_PIN the clock pin for these LEDs
/// @tparam RGB_ORDER the RGB ordering for these LEDs
/// @tparam SPI_SPEED the clock divider used for these LEDs. Set using the ::DATA_RATE_MHZ / ::DATA_RATE_KHZ macros. Defaults to ::DATA_RATE_MHZ(24)
template <
fl::u8 DATA_PIN,
fl::u8 CLOCK_PIN,
EOrder RGB_ORDER = RGB,
// APA102 has a bug where long strip can't handle full speed due to clock degredation.
// This only affects long strips, but then again if you have a short strip does 6 mhz actually slow
// you down? Probably not. And you can always bump it up for speed. Therefore we are prioritizing
// "just works" over "fastest possible" here.
// https://www.pjrc.com/why-apa102-leds-have-trouble-at-24-mhz/
uint32_t SPI_SPEED = DATA_RATE_MHZ(6)
>
class APA102ControllerHD : public APA102Controller<
DATA_PIN,
CLOCK_PIN,
RGB_ORDER,
SPI_SPEED,
fl::kFiveBitGammaCorrectionMode_BitShift,
uint32_t(0x00000000),
uint32_t(0x00000000)> {
public:
APA102ControllerHD() = default;
APA102ControllerHD(const APA102ControllerHD&) = delete;
};
/// SK9822 controller class. It's exactly the same as the APA102Controller protocol but with a different END_FRAME and default SPI_SPEED.
/// @tparam DATA_PIN the data pin for these LEDs
/// @tparam CLOCK_PIN the clock pin for these LEDs
/// @tparam RGB_ORDER the RGB ordering for these LEDs
/// @tparam SPI_SPEED the clock divider used for these LEDs. Set using the ::DATA_RATE_MHZ / ::DATA_RATE_KHZ macros. Defaults to ::DATA_RATE_MHZ(24)
template <
fl::u8 DATA_PIN,
fl::u8 CLOCK_PIN,
EOrder RGB_ORDER = RGB,
uint32_t SPI_SPEED = DATA_RATE_MHZ(12)
>
class SK9822Controller : public APA102Controller<
DATA_PIN,
CLOCK_PIN,
RGB_ORDER,
SPI_SPEED,
fl::kFiveBitGammaCorrectionMode_Null,
0x00000000,
0x00000000
> {
};
/// SK9822 controller class. It's exactly the same as the APA102Controller protocol but with a different END_FRAME and default SPI_SPEED.
/// @tparam DATA_PIN the data pin for these LEDs
/// @tparam CLOCK_PIN the clock pin for these LEDs
/// @tparam RGB_ORDER the RGB ordering for these LEDs
/// @tparam SPI_SPEED the clock divider used for these LEDs. Set using the ::DATA_RATE_MHZ / ::DATA_RATE_KHZ macros. Defaults to ::DATA_RATE_MHZ(24)
template <
fl::u8 DATA_PIN,
fl::u8 CLOCK_PIN,
EOrder RGB_ORDER = RGB,
uint32_t SPI_SPEED = DATA_RATE_MHZ(12)
>
class SK9822ControllerHD : public APA102Controller<
DATA_PIN,
CLOCK_PIN,
RGB_ORDER,
SPI_SPEED,
fl::kFiveBitGammaCorrectionMode_BitShift,
0x00000000,
0x00000000
> {
};
/// HD107 is just the APA102 with a default 40Mhz clock rate.
template <
fl::u8 DATA_PIN,
fl::u8 CLOCK_PIN,
EOrder RGB_ORDER = RGB,
uint32_t SPI_SPEED = DATA_RATE_MHZ(40)
>
class HD107Controller : public APA102Controller<
DATA_PIN,
CLOCK_PIN,
RGB_ORDER,
SPI_SPEED,
fl::kFiveBitGammaCorrectionMode_Null,
0x00000000,
0x00000000
> {};
/// HD107HD is just the APA102HD with a default 40Mhz clock rate.
template <
fl::u8 DATA_PIN,
fl::u8 CLOCK_PIN,
EOrder RGB_ORDER = RGB,
uint32_t SPI_SPEED = DATA_RATE_MHZ(40)
>
class HD107HDController : public APA102ControllerHD<
DATA_PIN,
CLOCK_PIN,
RGB_ORDER,
SPI_SPEED> {
};
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// P9813 definition - takes data/clock/select pin values (N.B. should take an SPI definition?)
//
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// P9813 controller class.
/// @tparam DATA_PIN the data pin for these LEDs
/// @tparam CLOCK_PIN the clock pin for these LEDs
/// @tparam RGB_ORDER the RGB ordering for these LEDs
/// @tparam SPI_SPEED the clock divider used for these LEDs. Set using the ::DATA_RATE_MHZ / ::DATA_RATE_KHZ macros. Defaults to ::DATA_RATE_MHZ(10)
template <fl::u8 DATA_PIN, fl::u8 CLOCK_PIN, EOrder RGB_ORDER = RGB, uint32_t SPI_SPEED = DATA_RATE_MHZ(10)>
class P9813Controller : public CPixelLEDController<RGB_ORDER> {
typedef SPIOutput<DATA_PIN, CLOCK_PIN, SPI_SPEED> SPI;
SPI mSPI;
void writeBoundary() { mSPI.writeWord(0); mSPI.writeWord(0); }
FASTLED_FORCE_INLINE void writeLed(fl::u8 r, fl::u8 g, fl::u8 b) {
FASTLED_REGISTER fl::u8 top = 0xC0 | ((~b & 0xC0) >> 2) | ((~g & 0xC0) >> 4) | ((~r & 0xC0) >> 6);
mSPI.writeByte(top); mSPI.writeByte(b); mSPI.writeByte(g); mSPI.writeByte(r);
}
public:
P9813Controller() {}
virtual void init() {
mSPI.init();
}
protected:
/// @copydoc CPixelLEDController::showPixels()
virtual void showPixels(PixelController<RGB_ORDER> & pixels) {
mSPI.select();
writeBoundary();
while(pixels.has(1)) {
writeLed(pixels.loadAndScale0(), pixels.loadAndScale1(), pixels.loadAndScale2());
pixels.advanceData();
pixels.stepDithering();
}
writeBoundary();
mSPI.waitFully();
mSPI.release();
}
public:
/// Get the protocol-safe padding byte for P9813
/// Used for quad-SPI lane padding when strips have different lengths
/// @returns 0x00 (boundary byte)
static constexpr fl::u8 getPaddingByte() { return 0x00; }
/// Get a black LED frame for synchronized latching
/// Used for quad-SPI lane padding to ensure all strips latch simultaneously
/// @returns Black LED frame (invisible LED: flag byte + BGR all zero)
static fl::span<const fl::u8> getPaddingLEDFrame() {
static const fl::u8 frame[] = {
0xFF, // Flag byte for RGB=0,0,0
0x00, // Blue = 0
0x00, // Green = 0
0x00 // Red = 0
};
return fl::span<const fl::u8>(frame, 4);
}
/// Get the size of the padding LED frame in bytes
/// @returns 4 bytes per LED for P9813
static constexpr size_t getPaddingLEDFrameSize() {
return 4;
}
/// Calculate total byte count for P9813 protocol
/// Used for quad-SPI buffer pre-allocation
/// @param num_leds Number of LEDs in the strip
/// @returns Total bytes needed (boundaries + LED data)
static constexpr size_t calculateBytes(size_t num_leds) {
// P9813 protocol:
// - Start boundary: 4 bytes (0x00000000)
// - LED data: 4 bytes per LED (flag byte + BGR)
// - End boundary: 4 bytes (0x00000000)
return 4 + (num_leds * 4) + 4;
}
};
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// SM16716 definition - takes data/clock/select pin values (N.B. should take an SPI definition?)
//
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// SM16716 controller class.
/// @tparam DATA_PIN the data pin for these LEDs
/// @tparam CLOCK_PIN the clock pin for these LEDs
/// @tparam RGB_ORDER the RGB ordering for these LEDs
/// @tparam SPI_SPEED the clock divider used for these LEDs. Set using the ::DATA_RATE_MHZ / ::DATA_RATE_KHZ macros. Defaults to ::DATA_RATE_MHZ(16)
template <fl::u8 DATA_PIN, fl::u8 CLOCK_PIN, EOrder RGB_ORDER = RGB, uint32_t SPI_SPEED = DATA_RATE_MHZ(16)>
class SM16716Controller : public CPixelLEDController<RGB_ORDER> {
typedef SPIOutput<DATA_PIN, CLOCK_PIN, SPI_SPEED> SPI;
SPI mSPI;
void writeHeader() {
// Write out 50 zeros to the spi line (6 blocks of 8 followed by two single bit writes)
mSPI.select();
mSPI.template writeBit<0>(0);
mSPI.writeByte(0);
mSPI.writeByte(0);
mSPI.writeByte(0);
mSPI.template writeBit<0>(0);
mSPI.writeByte(0);
mSPI.writeByte(0);
mSPI.writeByte(0);
mSPI.waitFully();
mSPI.release();
}
public:
SM16716Controller() {}
virtual void init() {
mSPI.init();
}
protected:
/// @copydoc CPixelLEDController::showPixels()
virtual void showPixels(PixelController<RGB_ORDER> & pixels) {
// Make sure the FLAG_START_BIT flag is set to ensure that an extra 1 bit is sent at the start
// of each triplet of bytes for rgb data
// writeHeader();
mSPI.template writePixels<FLAG_START_BIT, DATA_NOP, RGB_ORDER>(pixels, NULL);
writeHeader();
}
};
/// @} ClockedChipsets
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Clockless template instantiations - see clockless.h for how the timing values are used
//
#ifdef FASTLED_HAS_CLOCKLESS
/// @defgroup ClocklessChipsets Clockless Chipsets
/// These chipsets have only a single data line.
///
/// The clockless chipset controllers use the same base class
/// and the same protocol, but with varying timing periods.
///
/// These controllers have 3 control points in their cycle for each bit:
/// @code
/// At T=0 : the line is raised hi to start a bit
/// At T=T1 : the line is dropped low to transmit a zero bit
/// At T=T1+T2 : the line is dropped low to transmit a one bit
/// At T=T1+T2+T3 : the cycle is concluded (next bit can be sent)
/// @endcode
///
/// The units used for T1, T2, and T3 is nanoseconds.
///
/// For 8MHz/16MHz/24MHz frequencies, these values are also guaranteed
/// to be integral multiples of an 8MHz clock (125ns increments).
///
/// @note The base class, ClocklessController, is platform-specific.
/// @{
// Allow clock that clockless controller is based on to have different
// frequency than the CPU.
#if !defined(CLOCKLESS_FREQUENCY)
#define CLOCKLESS_FREQUENCY F_CPU
#endif
// We want to force all avr's to use the Trinket controller when running at 8Mhz, because even the 328's at 8Mhz
// need the more tightly defined timeframes.
#if defined(__LGT8F__) || (CLOCKLESS_FREQUENCY == 8000000 || CLOCKLESS_FREQUENCY == 16000000 || CLOCKLESS_FREQUENCY == 24000000) || defined(FASTLED_DOXYGEN) // || CLOCKLESS_FREQUENCY == 48000000 || CLOCKLESS_FREQUENCY == 96000000) // 125ns/clock
/// Frequency multiplier for each clockless data interval.
/// @see Notes in @ref ClocklessChipsets
#define FMUL (CLOCKLESS_FREQUENCY/8000000)
/// GE8822 controller class.
/// @copydetails WS2812Controller800Khz