FastLED 3.10.10
Loading...
Searching...
No Matches
LedRopeTCL.cpp
Go to the documentation of this file.
1// Copyleft (c) 2012, Zach Vorhies
2// Public domain, no rights reserved.
3// This object holds a frame buffer and effects can be applied. This is a higher level
4// object than the TCL class which this object uses for drawing.
5
6//#include "./tcl.h"
7#include <Arduino.h>
8#include "FastLED.h"
9#include "fl/log/log.h"
10#include "fl/ui/ui.h"
11#include "fl/math/math.h"
12
13#include "../shared/color.h"
15#include "../shared/settings.h"
16#include "./LedRopeTCL.h"
18
19
20
21#define CHIPSET WS2812
22// The board declares its default clocked-chipset pins; a literal 1/2 is not
23// routable everywhere (the Adafruit Feather nRF52840 Sense marks D1 invalid).
24#ifndef PIN_DATA
25#define PIN_DATA FL_PIN_SPI_DATA_1
26#endif
27#ifndef PIN_CLOCK
28#define PIN_CLOCK FL_PIN_SPI_CLOCK_1
29#endif
30
31namespace {
32
34
37 const int length = cols.length;
38 int sum = 0;
39 int y_max = 0;
40 for (int i = 0; i < length; ++i) {
41 sum += cols.array[i];
42 int stagger = i % 2 ? 4 : 0;
43 int col_top = (cols.array[i] - 1) * 8 + stagger;
44 if (col_top > y_max) {
45 y_max = col_top;
46 }
47 }
48 fl::ScreenMap screen_map(sum, 0.8f);
49 int curr_idx = 0;
50 for (int i = 0; i < length; ++i) {
51 int n = cols.array[i];
52 int stagger = i % 2 ? 4 : 0;
53 for (int k = 0; k < n; ++k) {
54 // Wiring is serpentine by column: even columns wind one way, odd
55 // columns the other, so a single rope can snake up and down.
56 int j = i % 2 ? (n - 1 - k) : k;
57 // Mirror y (horizontal flip + 180 deg rotation) so the rendered
58 // keyboard matches the physical Luminescent Grand orientation.
59 fl::vec2f xy(i*4, y_max - (j*8 + stagger));
60 screen_map.set(curr_idx++, xy);
61 }
62 }
63
64 return screen_map;
65}
66
67}
68
69
72 if (!mLazyInitialized) {
73 // This used to do something, now it does nothing.
74 mLazyInitialized = true;
75 }
76}
77
81 mLedBuffer.clear();
82}
83
86 RawDrawPixel(c.r_, c.g_, c.b_);
87}
88
90void LedRopeTCL::RawDrawPixel(byte r, byte g, byte b) {
91 if (mLedBuffer.size() >= mScreenMap.getLength()) {
92 return;
93 }
94 if (buttonAllWhite.isPressed()) {
95 r = 0xff;
96 g = 0xff;
97 b = 0xff;
98 }
99 fl::CRGB c(r, g, b);
100 mLedBuffer.push_back(fl::CRGB(r, g, b));
101}
102
104void LedRopeTCL::RawDrawPixels(const Color3i& c, int n) {
105 for (int i = 0; i < n; ++i) {
106 RawDrawPixel(c);
107 }
108}
109
112 mDrawOffset = constrain(val, 0, mFrameBuffer.length());
113}
114
115
118 FL_WARN("\n\n############## COMMIT DRAW ################\n\n");
119 if (!mControllerAdded) {
120 mControllerAdded = true;
121 fl::CRGB* leds = mLedBuffer.data();
122 size_t n_leds = mLedBuffer.size();
123 FastLED.addLeds<APA102, PIN_DATA, PIN_CLOCK>(leds, n_leds).setScreenMap(mScreenMap);
124 }
125 FL_WARN("FastLED.show");
126 FastLED.show();
127}
128
131 : mDrawOffset(0), mLazyInitialized(false), mFrameBuffer(n_pixels) {
132 mScreenMap = init_screenmap();
133 mLedBuffer.reserve(mScreenMap.getLength());
134}
135
139
142 RawBeginDraw();
143
144 const Color3i* begin = GetIterator(0);
145 const Color3i* middle = GetIterator(mDrawOffset);
146 const Color3i* end = GetIterator(length() - 1);
147
148 for (const Color3i* it = middle; it != end; ++it) {
149 RawDrawPixel(*it);
150 }
151 for (const Color3i* it = begin; it != middle; ++it) {
152 RawDrawPixel(*it);
153 }
155}
156
159 RawBeginDraw();
160
161 const Color3i* begin = GetIterator(0);
162 const Color3i* middle = GetIterator(mDrawOffset);
163 const Color3i* end = GetIterator(length());
164 for (const Color3i* it = middle; it != end; ++it) {
165 for (int i = 0; i < repeat; ++i) {
166 RawDrawPixel(it->r_, it->g_, it->b_);
167 }
168 }
169 for (const Color3i* it = begin; it != middle; ++it) {
170 for (int i = 0; i < repeat; ++i) {
171 RawDrawPixel(it->r_, it->g_, it->b_);
172 }
173 }
175}
176
178void LedRopeTCL::DrawRepeat(const int* value_array, int array_length) {
179 RawBeginDraw();
180
181 // Make sure that the number of colors to repeat does not exceed the length
182 // of the rope.
183 const int len = fl::min(array_length, mFrameBuffer.length());
184
185 for (int i = 0; i < len; ++i) {
186 const Color3i* cur_color = GetIterator(i); // Current color.
187 const int repeat_count = value_array[i];
188 // Repeatedly send the same color down the led rope.
189 for (int k = 0; k < repeat_count; ++k) {
190 RawDrawPixel(cur_color->r_, cur_color->g_, cur_color->b_);
191 }
192 }
193 // Finish the drawing.
195}
196
CRGB leds[1]
#define PIN_DATA
FL_DISABLE_WARNING_PUSH FL_DISABLE_WARNING_GLOBAL_CONSTRUCTORS CFastLED FastLED
Global LED strip management instance.
@ APA102
APA102 LED chipset.
Definition FastLED.h:301
unsigned int xy(unsigned int x, unsigned int y)
fl::ScreenMap mScreenMap
Definition LedRopeTCL.h:77
FrameBuffer mFrameBuffer
Definition LedRopeTCL.h:74
void RawCommitDraw()
bool mLazyInitialized
Definition LedRopeTCL.h:73
void RawDrawPixel(const Color3i &c)
void set_draw_offset(int val)
void PreDrawSetup()
void RawDrawPixels(const Color3i &c, int n)
virtual ~LedRopeTCL()
int mDrawOffset
Definition LedRopeTCL.h:72
fl::vector< fl::CRGB > mLedBuffer
Definition LedRopeTCL.h:76
Color3i * GetIterator(int i)
Definition LedRopeTCL.h:58
int length() const
Definition LedRopeTCL.h:62
void DrawSequentialRepeat(int repeat)
bool mControllerAdded
Definition LedRopeTCL.h:75
LedRopeTCL(int n_pixels)
void RawBeginDraw()
void DrawRepeat(const int *value_array, int array_length)
void set(u16 index, const vec2f &p) FL_NO_EXCEPT
bool isPressed() const FL_NO_EXCEPT
Definition button.h:45
#define PIN_CLOCK
Definition curr.h:51
fl::UISlider length("Length", 1.0f, 0.0f, 1.0f, 0.01f)
LedColumns LedLayoutArray()
#define FL_WARN(...)
Definition log.h:437
Centralized logging categories for FastLED hardware interfaces and subsystems.
fl::UIButton buttonAllWhite("All white")
FL_DISABLE_WARNING_PUSH U constexpr common_type_t< T, U > min(T a, U b) FL_NO_EXCEPT
Definition math.h:71
vec2< float > vec2f
Definition geometry.h:333
uint8_t b_
Definition color.h:56
uint8_t g_
Definition color.h:56
uint8_t r_
Definition color.h:56
Definition color.h:6
const int * array
Representation of an 8-bit RGB pixel (Red, Green, Blue)
Definition crgb.h:38
Aggregator header for the fl/ui/ family of per-element UI types.