FastLED 3.9.7
Loading...
Searching...
No Matches
LedRopeTCL.cpp
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"
9#include "../shared/framebuffer.h"
10#include "../shared/settings.h"
11#include "./LedRopeTCL.h"
12#include "../shared/led_layout_array.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
27Button buttonAllWhite("All white");
28
29ScreenMap init_screenmap() {
30 LedColumns cols = LedLayoutArray();
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
54void LedRopeTCL::PreDrawSetup() {
55 if (!lazy_initialized_) {
56 // This used to do something, now it does nothing.
57 lazy_initialized_ = true;
58 }
59}
60
62void LedRopeTCL::RawBeginDraw() {
63 PreDrawSetup();
64 led_buffer_.clear();
65}
66
68void LedRopeTCL::RawDrawPixel(const Color3i& c) {
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
94void LedRopeTCL::set_draw_offset(int val) {
95 draw_offset_ = constrain(val, 0, frame_buffer_.length());
96}
97
98
100void LedRopeTCL::RawCommitDraw() {
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
111LedRopeTCL::LedRopeTCL(int n_pixels)
112 : draw_offset_(0), lazy_initialized_(false), frame_buffer_(n_pixels) {
113 mScreenMap = init_screenmap();
114 led_buffer_.reserve(mScreenMap.getLength());
115}
116
118LedRopeTCL::~LedRopeTCL() {
119}
120
122void LedRopeTCL::Draw() {
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 }
135 RawCommitDraw();
136}
137
139void LedRopeTCL::DrawSequentialRepeat(int repeat) {
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 }
155 RawCommitDraw();
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.
175 RawCommitDraw();
176}
177
CFastLED FastLED
Global LED strip management instance.
Definition FastLED.cpp:45
central include file for FastLED, defines the CFastLED class/object
@ APA102
APA102 LED chipset.
Definition FastLED.h:101
void show(uint8_t scale)
Update all our controllers with the current led colors, using the passed in brightness.
Definition FastLED.cpp:94
static CLEDController & addLeds(CLEDController *pLed, struct CRGB *data, int nLedsOrOffset, int nLedsIfOffset=0)
Add a CLEDController instance to the world.
Definition FastLED.cpp:79
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
Definition color.h:8