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 "../shared/color.h"
10#include "../shared/settings.h"
11#include "./LedRopeTCL.h"
13
14#include "FastLED.h"
15#include "fl/dbg.h"
16#include "fl/ui.h"
17
18using namespace fl;
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 for (int i = 0; i < length; ++i) {
34 sum += cols.array[i];
35 }
36 ScreenMap screen_map(sum, 0.8f);
37 int curr_idx = 0;
38 for (int i = 0; i < length; ++i) {
39 int n = cols.array[i];
40 int stagger = i % 2 ? 4 : 0;
41 for (int j = 0; j < n; ++j) {
42 fl::vec2f xy(i*4, j*8 + stagger);
43 screen_map.set(curr_idx++, xy);
44 }
45 }
46
47 return screen_map;
48}
49
50}
51
52
55 if (!lazy_initialized_) {
56 // This used to do something, now it does nothing.
57 lazy_initialized_ = true;
58 }
59}
60
64 led_buffer_.clear();
65}
66
69 RawDrawPixel(c.r_, c.g_, c.b_);
70}
71
73void LedRopeTCL::RawDrawPixel(byte r, byte g, byte b) {
74 if (led_buffer_.size() >= mScreenMap.getLength()) {
75 return;
76 }
77 if (buttonAllWhite.isPressed()) {
78 r = 0xff;
79 g = 0xff;
80 b = 0xff;
81 }
82 CRGB c(r, g, b);
83 led_buffer_.push_back(CRGB(r, g, b));
84}
85
87void LedRopeTCL::RawDrawPixels(const Color3i& c, int n) {
88 for (int i = 0; i < n; ++i) {
89 RawDrawPixel(c);
90 }
91}
92
95 draw_offset_ = constrain(val, 0, frame_buffer_.length());
96}
97
98
101 FASTLED_WARN("\n\n############## COMMIT DRAW ################\n\n");
102 if (!controller_added_) {
103 controller_added_ = true;
104 CRGB* leds = led_buffer_.data();
105 size_t n_leds = led_buffer_.size();
106 FastLED.addLeds<APA102, PIN_DATA, PIN_CLOCK>(leds, n_leds).setScreenMap(mScreenMap);
107 }
108 FASTLED_WARN("FastLED.show");
109 FastLED.show();
110}
111
114 : draw_offset_(0), lazy_initialized_(false), frame_buffer_(n_pixels) {
115 mScreenMap = init_screenmap();
116 led_buffer_.reserve(mScreenMap.getLength());
117}
118
122
125 RawBeginDraw();
126
127 const Color3i* begin = GetIterator(0);
128 const Color3i* middle = GetIterator(draw_offset_);
129 const Color3i* end = GetIterator(length() - 1);
130
131 for (const Color3i* it = middle; it != end; ++it) {
132 RawDrawPixel(*it);
133 }
134 for (const Color3i* it = begin; it != middle; ++it) {
135 RawDrawPixel(*it);
136 }
138}
139
142 RawBeginDraw();
143
144 const Color3i* begin = GetIterator(0);
145 const Color3i* middle = GetIterator(draw_offset_);
146 const Color3i* end = GetIterator(length());
147 for (const Color3i* it = middle; it != end; ++it) {
148 for (int i = 0; i < repeat; ++i) {
149 RawDrawPixel(it->r_, it->g_, it->b_);
150 }
151 }
152 for (const Color3i* it = begin; it != middle; ++it) {
153 for (int i = 0; i < repeat; ++i) {
154 RawDrawPixel(it->r_, it->g_, it->b_);
155 }
156 }
158}
159
161void LedRopeTCL::DrawRepeat(const int* value_array, int array_length) {
162 RawBeginDraw();
163
164 // Make sure that the number of colors to repeat does not exceed the length
165 // of the rope.
166 const int len = MIN(array_length, frame_buffer_.length());
167
168 for (int i = 0; i < len; ++i) {
169 const Color3i* cur_color = GetIterator(i); // Current color.
170 const int repeat_count = value_array[i];
171 // Repeatedly send the same color down the led rope.
172 for (int k = 0; k < repeat_count; ++k) {
173 RawDrawPixel(cur_color->r_, cur_color->g_, cur_color->b_);
174 }
175 }
176 // Finish the drawing.
178}
179
CRGB leds[NUM_LEDS]
Definition Apa102.ino:11
CFastLED FastLED
Global LED strip management instance.
Definition FastLED.cpp:58
@ APA102
APA102 LED chipset.
Definition FastLED.h:118
central include file for FastLED, defines the CFastLED class/object
#define PIN_CLOCK
#define PIN_DATA
unsigned int xy(unsigned int x, unsigned int y)
fl::ScreenMap mScreenMap
Definition LedRopeTCL.h:78
fl::HeapVector< CRGB > led_buffer_
Definition LedRopeTCL.h:77
void RawCommitDraw()
bool lazy_initialized_
Definition LedRopeTCL.h:74
void RawDrawPixel(const Color3i &c)
void set_draw_offset(int val)
void PreDrawSetup()
void RawDrawPixels(const Color3i &c, int n)
virtual ~LedRopeTCL()
FrameBuffer frame_buffer_
Definition LedRopeTCL.h:75
Color3i * GetIterator(int i)
Definition LedRopeTCL.h:59
int length() const
Definition LedRopeTCL.h:63
bool controller_added_
Definition LedRopeTCL.h:76
void DrawSequentialRepeat(int repeat)
int draw_offset_
Definition LedRopeTCL.h:73
LedRopeTCL(int n_pixels)
void RawBeginDraw()
void DrawRepeat(const int *value_array, int array_length)
void set(uint16_t index, const vec2f &p)
bool isPressed() const
Definition ui.h:119
UISlider length("Length", 1.0f, 0.0f, 1.0f, 0.01f)
LedColumns LedLayoutArray()
#define MIN(a, b)
Definition math_macros.h:15
UIButton buttonAllWhite("All white")
vec2< float > vec2f
Definition geometry.h:301
Implements a simple red square effect for 2D LED grids.
Definition crgb.h:16
Representation of an RGB pixel (Red, Green, Blue)
Definition crgb.h:55
uint8_t b_
Definition color.h:52
uint8_t g_
Definition color.h:52
uint8_t r_
Definition color.h:52
Definition color.h:8
const int * array
#define FASTLED_WARN
Definition warn.h:7