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::pair_xy_float 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 if (!controller_added_) {
102 controller_added_ = true;
103 CRGB* leds = led_buffer_.data();
104 size_t n_leds = led_buffer_.size();
105 FastLED.addLeds<APA102, PIN_DATA, PIN_CLOCK>(leds, n_leds).setScreenMap(mScreenMap);
106 }
107 FastLED.show();
108}
109
112 : draw_offset_(0), lazy_initialized_(false), frame_buffer_(n_pixels) {
113 mScreenMap = init_screenmap();
114 led_buffer_.reserve(mScreenMap.getLength());
115}
116
120
123 RawBeginDraw();
124
125 const Color3i* begin = GetIterator(0);
126 const Color3i* middle = GetIterator(draw_offset_);
127 const Color3i* end = GetIterator(length() - 1);
128
129 for (const Color3i* it = middle; it != end; ++it) {
130 RawDrawPixel(*it);
131 }
132 for (const Color3i* it = begin; it != middle; ++it) {
133 RawDrawPixel(*it);
134 }
136}
137
140 RawBeginDraw();
141
142 const Color3i* begin = GetIterator(0);
143 const Color3i* middle = GetIterator(draw_offset_);
144 const Color3i* end = GetIterator(length());
145 for (const Color3i* it = middle; it != end; ++it) {
146 for (int i = 0; i < repeat; ++i) {
147 RawDrawPixel(it->r_, it->g_, it->b_);
148 }
149 }
150 for (const Color3i* it = begin; it != middle; ++it) {
151 for (int i = 0; i < repeat; ++i) {
152 RawDrawPixel(it->r_, it->g_, it->b_);
153 }
154 }
156}
157
159void LedRopeTCL::DrawRepeat(const int* value_array, int array_length) {
160 RawBeginDraw();
161
162 // Make sure that the number of colors to repeat does not exceed the length
163 // of the rope.
164 const int len = min(array_length, frame_buffer_.length());
165
166 for (int i = 0; i < len; ++i) {
167 const Color3i* cur_color = GetIterator(i); // Current color.
168 const int repeat_count = value_array[i];
169 // Repeatedly send the same color down the led rope.
170 for (int k = 0; k < repeat_count; ++k) {
171 RawDrawPixel(cur_color->r_, cur_color->g_, cur_color->b_);
172 }
173 }
174 // Finish the drawing.
176}
177
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:101
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 pair_xy_float &p)
bool isPressed() const
Definition ui.h:98
LedColumns LedLayoutArray()
UIButton buttonAllWhite("All white")
pair_xy< float > pair_xy_float
Definition lut.h:24
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:54
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