forked from kukulski/BeagleBoneUDPLED
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TCLZoned.hxx
338 lines (238 loc) · 7 KB
/
TCLZoned.hxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/*
* File: TCLFast.hxx
* Author: kukulski
*
* Created on November 9, 2012, 2:58 PM
*/
#ifndef TCLFAST_HXX
#define TCLFAST_HXX
#include "tclled.h"
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <math.h>
#include <algorithm>
#include <list>
#include <iostream>
using namespace std;
class Buffer {
public:
Buffer(int wd, int ht):
wd(wd),
ht(ht),
zero(0){
size = wd*ht;
buf = new uint32_t [size];
for(int i = 0 ; i < size; i++)
buf[i] = 0;
}
~Buffer() {
delete buf;
}
uint32_t *pixelAt(int x, int y) {
return buf + offset(x,y);
}
int offset(int x, int y) {
return x + (y * wd);
}
uint32_t *getZero() { return &zero;}
uint32_t *getBuffer() { return pixelAt(0,0);}
size_t getBufferSize() { return wd*ht*sizeof(uint32_t);}
void writePixel(int x, int y, unsigned char r, unsigned char g, unsigned char b) {
uint32_t *out = pixelAt(x,y);
buf[offset(x,y)] = (r << 8) | (g << 16) | (b << 24);
}
private:
int size;
int wd, ht;
uint32_t *buf;
uint32_t zero;
};
typedef struct {
int x, y;
} XY;
class Zone {
public:
// Zone(const int x, const int y, const int wd, const int ht, const int offset, const int phase):xOrigin(x),yOrigin(y),wd(wd),ht(ht),offset(offset),phase(phase) {
// }
Zone(XY &origin, XY &size, const int offset, const int flipX, const int flipY):
origin(origin),
size(size),
offset(offset),
flipX(flipX),
flipY(flipY) {
}
int endIndex() {
return size.x*size.y + offset;
}
virtual void dump() {
fprintf(stderr,"zone: @%d,%d, %dx%d offset:%d, phase%d,%d\n",origin.x, origin.y, size.x, size.y, offset, flipX, flipY);
}
virtual unsigned int *pixelForIndex(int ii) {
int i = ii- offset;
int suby = i/size.x;
int linePhase = (suby + flipX) & 1;
int subx = i%size.x;
subx = linePhase ? (size.x - 1 - subx) : subx;
suby = flipY ? (size.y -1 - suby) : suby;
return rawBuffer->pixelAt(origin.x + subx, origin.y+suby);
}
protected:
XY origin, size;
int flipX, flipY;
public:
int offset;
Buffer *rawBuffer;
};
class VZone: public Zone {
public:
VZone(XY &origin, XY &size, const int offset, const int flipX, const int flipY):
Zone(origin,size,offset,flipX, flipY) {
}
virtual unsigned int *pixelForIndex(int ii) {
int i = ii- offset;
int subx = i/size.y;
int linePhase = (subx + flipX) & 1;
int suby = i%size.y;
suby = linePhase ? (size.y - 1 - suby) : suby;
subx = flipY ? (size.x -1 - subx) : subx;
return rawBuffer->pixelAt(origin.x + subx, origin.y+suby);
}
virtual void dump() {
fprintf(stderr,"vertical zone: @%d,%d, %dx%d offset:%d, phase%d,%d\n",origin.x, origin.y, size.x, size.y, offset, flipX, flipY);
}
};
class BufferMap {
public:
BufferMap(int count, Buffer *rawBuffer, tcl_color *pixels):
count(count),
rawBuffer(rawBuffer),
pixels(pixels) {
pixelMap = new uint32_t*[count];
for(int i=0; i < count; i++) {
pixelMap[i] = rawBuffer->getBuffer();
}
}
void add(Zone *zone) {
// zone->dump();
zone->rawBuffer = rawBuffer;
int endIdx = std::min(count,zone->endIndex());
for(int i = zone->offset; i < endIdx; i++)
pixelMap[i] = zone->pixelForIndex(i);
}
void writeOut(tcl_color *pixels) {
for(int i = 0; i < count; i++) {
uint32_t *from = pixelMap[i];
int offset = from - rawBuffer->getBuffer();
::write_bgra_gamma(pixels+i,*from);
}
}
void mapOutBadPixel(uint32_t *px) {
int idx = findIndex(px);
if(idx < 0) return;
if(idx >= count) return;
mapOut(idx);
}
private:
void mapOut(int idx) {
for(int i = idx; i < count-1; i++) {
pixelMap[i] = pixelMap[i+1];
}
}
int findIndex(uint32_t *px) {
for(int i = 0; i < count; i++) {
uint32_t *from = pixelMap[i];
if(from == px) return i;
}
return -1;
}
int count;
uint32_t **pixelMap;
Buffer *rawBuffer;
tcl_color *pixels;
};
class TCLZoned {
public:
TCLZoned(int wd, int ht, int pixelCount):
width(wd),
height(ht),
spi(-1)
{
set_gamma(3.0, 3.0, 3.0);
initTCL(pixelCount);
rawBuffer = new Buffer(wd,ht);
pixelMap = new BufferMap(pixelCount, rawBuffer, tcl.pixels);
}
~TCLZoned() {
delete rawBuffer;
}
void mapOutBadPixel(XY px) {
pixelMap->mapOutBadPixel(rawBuffer->pixelAt(px.x,px.y));
}
Buffer *getBuffer() { return rawBuffer;}
void addZones(std::list<Zone*> zones) {
std::list<Zone*>::iterator first = zones.begin(), last = zones.end();
for ( ; first!=last; ++first )
add(*first);
}
void addOmissions(std::list<XY> omissions) {
std::list<XY>::iterator first = omissions.begin(), last = omissions.end();
for ( ; first!=last; ++first ) {
cout << "omitting " << first->x << first->y << endl;
mapOutBadPixel(*first);
}
}
void add(Zone *zone) {
pixelMap->add(zone);
}
void testPattern() {
for(int i = 0; i < width*height; i++) {
int row = i/width;
int col = i%width;
int quad = i /(width*height/4);
rawBuffer->writePixel(col,row, row*255/height,col*255/width,0);
}
}
void send() {
if(spi < 0)
openSPI();
if(spi < 0)
return;
pixelMap->writeOut(tcl.pixels);
::send_buffer(spi,&tcl);
}
private:
tcl_buffer tcl;
BufferMap *pixelMap;
Buffer *rawBuffer;
int width, height;
int spi;
int leds;
void openSPI() {
/* Open SPI device */
spi = open("/dev/spidev2.0",O_WRONLY);
if(spi<0) {
/* Open failed */
fprintf(stderr, "Error: SPI device open failed.\n");
return;
}
/* Initialize SPI bus for TCL pixels */
if(spi_init(spi)<0) {
/* Initialization failed */
fprintf(stderr, "Unable to initialize SPI bus.\n");
close(spi);
spi = -1;
return;
}
}
void initTCL(int leds) {
/* Allocate memory for the pixel buffer and initialize it */
if(tcl_init(&tcl,leds)<0) {
/* Memory allocation failed */
fprintf(stderr, "Insufficient memory for pixel buffer.\n");
exit(1);
}
}
}; // class TCLFast
/// next: make a new class that uses pixelDither
#endif /* TCLFAST_HXX */