FastLED 3.9.15
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#define PIN_DATA 1
23#define PIN_CLOCK 2
24
25namespace {
26
28
31 const int length = cols.length;
32 int sum = 0;
33 int y_max = 0;
34 for (int i = 0; i < length; ++i) {
35 sum += cols.array[i];
36 int stagger = i % 2 ? 4 : 0;
37 int col_top = (cols.array[i] - 1) * 8 + stagger;
38 if (col_top > y_max) {
39 y_max = col_top;
40 }
41 }
42 fl::ScreenMap screen_map(sum, 0.8f);
43 int curr_idx = 0;
44 for (int i = 0; i < length; ++i) {
45 int n = cols.array[i];
46 int stagger = i % 2 ? 4 : 0;
47 for (int k = 0; k < n; ++k) {
48 // Wiring is serpentine by column: even columns wind one way, odd
49 // columns the other, so a single rope can snake up and down.
50 int j = i % 2 ? (n - 1 - k) : k;
51 // Mirror y (horizontal flip + 180 deg rotation) so the rendered
52 // keyboard matches the physical Luminescent Grand orientation.
53 fl::vec2f xy(i*4, y_max - (j*8 + stagger));
54 screen_map.set(curr_idx++, xy);
55 }
56 }
57
58 return screen_map;
59}
60
61}
62
63
66 if (!mLazyInitialized) {
67 // This used to do something, now it does nothing.
68 mLazyInitialized = true;
69 }
70}
71
75 mLedBuffer.clear();
76}
77
80 RawDrawPixel(c.r_, c.g_, c.b_);
81}
82
84void LedRopeTCL::RawDrawPixel(byte r, byte g, byte b) {
85 if (mLedBuffer.size() >= mScreenMap.getLength()) {
86 return;
87 }
88 if (buttonAllWhite.isPressed()) {
89 r = 0xff;
90 g = 0xff;
91 b = 0xff;
92 }
93 fl::CRGB c(r, g, b);
94 mLedBuffer.push_back(fl::CRGB(r, g, b));
95}
96
98void LedRopeTCL::RawDrawPixels(const Color3i& c, int n) {
99 for (int i = 0; i < n; ++i) {
100 RawDrawPixel(c);
101 }
102}
103
106 mDrawOffset = constrain(val, 0, mFrameBuffer.length());
107}
108
109
112 FL_WARN("\n\n############## COMMIT DRAW ################\n\n");
113 if (!mControllerAdded) {
114 mControllerAdded = true;
115 fl::CRGB* leds = mLedBuffer.data();
116 size_t n_leds = mLedBuffer.size();
117 FastLED.addLeds<APA102, PIN_DATA, PIN_CLOCK>(leds, n_leds).setScreenMap(mScreenMap);
118 }
119 FL_WARN("FastLED.show");
120 FastLED.show();
121}
122
125 : mDrawOffset(0), mLazyInitialized(false), mFrameBuffer(n_pixels) {
126 mScreenMap = init_screenmap();
127 mLedBuffer.reserve(mScreenMap.getLength());
128}
129
133
136 RawBeginDraw();
137
138 const Color3i* begin = GetIterator(0);
139 const Color3i* middle = GetIterator(mDrawOffset);
140 const Color3i* end = GetIterator(length() - 1);
141
142 for (const Color3i* it = middle; it != end; ++it) {
143 RawDrawPixel(*it);
144 }
145 for (const Color3i* it = begin; it != middle; ++it) {
146 RawDrawPixel(*it);
147 }
149}
150
153 RawBeginDraw();
154
155 const Color3i* begin = GetIterator(0);
156 const Color3i* middle = GetIterator(mDrawOffset);
157 const Color3i* end = GetIterator(length());
158 for (const Color3i* it = middle; it != end; ++it) {
159 for (int i = 0; i < repeat; ++i) {
160 RawDrawPixel(it->r_, it->g_, it->b_);
161 }
162 }
163 for (const Color3i* it = begin; it != middle; ++it) {
164 for (int i = 0; i < repeat; ++i) {
165 RawDrawPixel(it->r_, it->g_, it->b_);
166 }
167 }
169}
170
172void LedRopeTCL::DrawRepeat(const int* value_array, int array_length) {
173 RawBeginDraw();
174
175 // Make sure that the number of colors to repeat does not exceed the length
176 // of the rope.
177 const int len = fl::min(array_length, mFrameBuffer.length());
178
179 for (int i = 0; i < len; ++i) {
180 const Color3i* cur_color = GetIterator(i); // Current color.
181 const int repeat_count = value_array[i];
182 // Repeatedly send the same color down the led rope.
183 for (int k = 0; k < repeat_count; ++k) {
184 RawDrawPixel(cur_color->r_, cur_color->g_, cur_color->b_);
185 }
186 }
187 // Finish the drawing.
189}
190
#define PIN_DATA
fl::CRGB leds[NUM_LEDS]
FL_DISABLE_WARNING_PUSH FL_DISABLE_WARNING_GLOBAL_CONSTRUCTORS CFastLED FastLED
Global LED strip management instance.
@ APA102
APA102 LED chipset.
Definition FastLED.h:262
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_NOEXCEPT
bool isPressed() const FL_NOEXCEPT
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(X)
Definition log.h:276
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_NOEXCEPT
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.