FastLED 3.9.7
Loading...
Searching...
No Matches
ObjectFLED.h
1/* ObjectFLED - Teensy 4.x DMA to all pins for independent control of large and
2 multiple LED display objects
3
4 Copyright (c) 2024 Kurt Funderburg
5
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
15
16 OctoWS2811 library code was well-studied and substantial portions of it used
17 to implement high-speed, non-blocking DMA for LED signal output in this library.
18 See ObjectFLED.cpp for a summary of changes made to the original OctoWS2811.
19
20 OctoWS2811 - High Performance WS2811 LED Display Library
21 Copyright (c) 2013 Paul Stoffregen, PJRC.COM, LLC
22 http://www.pjrc.com/teensy/td_libs_OctoWS2811.html
23
24 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
30 THE SOFTWARE.
31*/
32
33#ifndef __IMXRT1062__
34// Do nothing for other platforms.
35#endif
36#if TEENSYDUINO < 121
37#error "Teensyduino version 1.21 or later is required to compile this library."
38#endif
39#ifndef ObjectFLED_h
40#define ObjectFLED_h
41#include <WProgram.h>
42#include "DMAChannel.h"
43
44//Experimentally found DSE=3, SPEED=0 gave best LED overclocking
45//boot defaults DSE=6, SPEED=2.
46#define OUTPUT_PAD_DSE 3 //Legal values 0-7
47#define OUTPUT_PAD_SPEED 0 //Legal values 0-3
48
49// Ordinary RGB data is converted to GPIO bitmasks on-the-fly using
50// a transmit buffer sized for 2 DMA transfers. The larger this setting,
51// the more interrupt latency OctoWS2811 can tolerate, but the transmit
52// buffer grows in size. For good performance, the buffer should be kept
53// smaller than the half the Cortex-M7 data cache.
54//bitdata[B_P_D * 64] buffer holds data (10KB) for 80 LED bytes: 4DW * 8b = 32DW/LEDB = 96DW/LED
55//framebuffer_index = B_P_D * 2 = pointer to next block for transfer (80 LEDB / bitdata buffer)
56#define BYTES_PER_DMA 20 //= number of pairs of LEDB (40=80B) bitmasks in bitdata.
57
58#define CORDER_RGB 0 //* WS2811, YF923
59#define CORDER_RBG 1
60#define CORDER_GRB 2 //* WS2811B, Most LED strips are wired this way
61#define CORDER_GBR 3
62#define CORDER_BRG 4 //* Adafruit Product ID: 5984 As of November 5, 2024 - this strand has different 'internal' color ordering. It's now BRG not RGB,
63#define CORDER_BGR 5 // Adafruit Dotstar LEDs SK9822 uses this CO but they use inverted start/stop bits
64#define CORDER_RGBW 6 //* Popular
65#define CORDER_RBGW 7
66#define CORDER_GRBW 8
67#define CORDER_GBRW 9
68#define CORDER_BRGW 10
69#define CORDER_BGRW 11 // Adafruit Dotstar LEDs SK9822 uses this CO but they use inverted start/stop bits
70#define CORDER_WRGB 12
71#define CORDER_WRBG 13
72#define CORDER_WGRB 14
73#define CORDER_WGBR 15
74#define CORDER_WBRG 16
75#define CORDER_WBGR 17
76#define CORDER_RWGB 18
77#define CORDER_RWBG 19
78#define CORDER_GWRB 20
79#define CORDER_GWBR 21
80#define CORDER_BWRG 22
81#define CORDER_BWGR 23
82#define CORDER_RGWB 24
83#define CORDER_RBWG 25
84#define CORDER_GRWB 26
85#define CORDER_GBWR 27
86#define CORDER_BRWG 28
87#define CORDER_BGWR 29
88
89
90//Usage: ObjectFLED myCube ( Num_LEDs, *drawBuffer, LED_type, numPins, *pinList, serpentineNumber )
92public:
93 // Usage: ObjectFLED myCube ( Num_LEDs, *drawBuffer, LED_type, numPins, *pinList, serpentineNumber )
94 // Example:
95 // byte pinList[NUM_CHANNELS] = {1, 8, 14, 17, 24, 29, 20, 0, 15, 16, 18, 19, 21, 22, 23, 25};
96 // byte pinListBlank[7] = {1, 8, 14, 17, 24, 29, 20};
97 // CRGB testCube[NUM_PLANES][NUM_ROWS][PIX_PER_ROW];
98 // CRGB blankLeds[7][8][8];
99 // ObjectFLED leds(PIX_PER_ROW * NUM_ROWS * NUM_PLANES, testCube, CORDER_RGB, NUM_CHANNELS, pinList);
100 // void setup() {
101 // leds.begin(1.6, 72); //1.6 ocervlock factor, 72uS LED latch delay
102 // leds.setBrightness(64);
103 // }
104 // void loop() {
105 // leds.show();
106 // delay(100);
107 // }
108 ObjectFLED(uint16_t numLEDs, void* drawBuf, uint8_t color_order, uint8_t numPins, const uint8_t* pinList, \
109 uint8_t serpentine = 0);
110
111 ~ObjectFLED() { delete frameBuffer; }
112
113 //begin() - Use defalut LED timing: 1.0 OC Factor, 1250 nS CLK (=800 KHz), 417 nS T0H, 834 nS T1H, 70 uS LED Latch Delay.
114 void begin(void);
115
116 //begin(LED_Overclock_Factor) - divides default 1250 nS LED CLK (=800 KHz), 417 nS T0H, 834 nS T1H.
117 void begin(float);
118
119 //begin(LED_Overclock_Factor, LED_Latch_Delay_uS) - divides default 1250 nS LED CLK (=800 KHz),
120 // 417 nS T0H, 834 nS T1H; and sets the LED Latch Delay.
121 void begin(float, uint16_t);
122
123 //begin(LED_Overclock_Factor, LED_CLK_nS, LED_T0H_nS, LED_T1H_nS, LED_Latch_Delay_uS) -
124 //specifies full LED timing. Values given for CLK, T0H, T1H are divided by OC Factor.
125 void begin(float, uint16_t, uint16_t, uint16_t, uint16_t);
126
127 void show(void);
128
129 int busy(void);
130
131 //Brightness values 0-255
132 void setBrightness(uint8_t);
133
134 //Color Balance is 3-byte number in RGB order. Each byte is a brightness value for that color.
135 void setBalance(uint32_t);
136
137 uint8_t getBrightness() { return brightness; }
138
139 uint32_t getBalance() { return colorBalance; }
140
141private:
142 static void isr(void);
143
144 void genFrameBuffer(uint32_t);
145
146 static uint8_t* frameBuffer; //isr()
147 static uint32_t numbytes; //isr()
148 static uint8_t numpins; //isr()
149 static uint8_t pin_bitnum[NUM_DIGITAL_PINS]; //isr()
150 static uint8_t pin_offset[NUM_DIGITAL_PINS]; //isr()
151 DMAMEM static uint32_t bitdata[BYTES_PER_DMA * 64] __attribute__((used, aligned(32))); //isr()
152 DMAMEM static uint32_t bitmask[4] __attribute__((used, aligned(32)));
153 static DMAChannel dma1, dma2, dma3;
154 static DMASetting dma2next;
155
156 uint32_t update_begin_micros = 0;
157 uint8_t brightness = 255;
158 uint32_t colorBalance = 0xFFFFFF;
159 uint32_t rLevel = 65025;
160 uint32_t gLevel = 65025;
161 uint32_t bLevel = 65025;
162 void* drawBuffer;
163 uint16_t stripLen;
164 uint8_t params;
165 uint8_t pinlist[NUM_DIGITAL_PINS];
166 uint16_t comp1load[3];
167 uint8_t serpNumber;
168 float OC_FACTOR = 1.0; //used to reduce period of LED output
169 uint16_t TH_TL = 1250; //nS- period of LED output
170 uint16_t T0H = TH_TL / 3; //nS- duration of T0H
171 uint16_t T1H = TH_TL * 2 / 3; //nS- duration of T1H
172 uint16_t LATCH_DELAY = 75; //uS time to hold output low for LED latch.
173
174 //for show context switch
175 uint32_t bitmaskLocal[4];
176 uint8_t numpinsLocal;
177 uint8_t* frameBufferLocal;
178 uint32_t numbytesLocal;
179 uint8_t pin_bitnumLocal[NUM_DIGITAL_PINS];
180 uint8_t pin_offsetLocal[NUM_DIGITAL_PINS];
181}; // class ObjectFLED
182
183
184//fadeToColorBy(RGB_array, LED_count, Color, FadeAmount)
185//Fades an RGB array towards the background color by amount.
186void fadeToColorBy(void*, uint16_t, uint32_t, uint8_t);
187
188
189//drawSquare(RGB_Array, LED_Rows, LED_Cols, Y_Corner, X_Corner, square_Size)
190//Draws square in a 2D RGB array with lower left corner at (Y_Corner, X_Corner).
191//Safe to specify -Y, -X corner, safe to draw a box which partially fits on LED plane.
192void drawSquare(void*, uint16_t, uint16_t, int, int, uint32_t, uint32_t);
193
194#endif