forked from hzeller/rpi-rgb-led-matrix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fysh.cc
375 lines (326 loc) · 10.5 KB
/
fysh.cc
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/* -*- mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
*
* Fysh
*
*/
#include "led-matrix.h"
#include "graphics.h"
#include <assert.h>
#include <getopt.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <algorithm>
using std::max;
#define TERM_ERR "\033[1;31m"
#define TERM_NORM "\033[0m"
using namespace rgb_matrix;
volatile bool interrupt_received = false;
static void InterruptHandler(int signo) { interrupt_received = true; }
class DemoRunner {
protected:
DemoRunner(Canvas *canvas) : canvas_(canvas) {}
inline Canvas *canvas() { return canvas_; }
public:
virtual ~DemoRunner() {}
virtual void Run() = 0;
private:
Canvas *const canvas_;
};
/*
* The following are demo image generators. They all use the utility
* class DemoRunner to generate new frames.
*/
class SimpleSquare : public DemoRunner {
public:
SimpleSquare(Canvas *m) : DemoRunner(m) {}
void Run() override {
const int width = canvas()->width() - 1;
const int height = canvas()->height() - 1;
// Borders
DrawLine(canvas(), 0, 0, width, 0, Color(255, 0, 0));
DrawLine(canvas(), 0, height, width, height, Color(255, 255, 0));
DrawLine(canvas(), 0, 0, 0, height, Color(0, 0, 255));
DrawLine(canvas(), width, 0, width, height, Color(0, 255, 0));
// Diagonals.
DrawLine(canvas(), 0, 0, width, height, Color(255, 255, 255));
DrawLine(canvas(), 0, height, width, 0, Color(255, 0, 255));
}
};
class GrayScaleBlock : public DemoRunner {
public:
GrayScaleBlock(Canvas *m) : DemoRunner(m) {}
void Run() override {
const int sub_blocks = 16;
const int width = canvas()->width();
const int height = canvas()->height();
const int x_step = max(1, width / sub_blocks);
const int y_step = max(1, height / sub_blocks);
uint8_t count = 0;
while (!interrupt_received) {
for (int y = 0; y < height; ++y) {
for (int x = 0; x < width; ++x) {
int c = sub_blocks * (y / y_step) + x / x_step;
switch (count % 4) {
case 0:
canvas()->SetPixel(x, y, c, c, c);
break;
case 1:
canvas()->SetPixel(x, y, c, 0, 0);
break;
case 2:
canvas()->SetPixel(x, y, 0, c, 0);
break;
case 3:
canvas()->SetPixel(x, y, 0, 0, c);
break;
}
}
}
count++;
sleep(2);
}
}
};
class ImageScroller : public DemoRunner {
public:
// Scroll image with "scroll_jumps" pixels every "scroll_ms" milliseconds.
// If "scroll_ms" is negative, don't do any scrolling.
ImageScroller(RGBMatrix *m, int scroll_jumps, int scroll_ms = 30)
: DemoRunner(m), scroll_jumps_(scroll_jumps), scroll_ms_(scroll_ms),
horizontal_position_(0), matrix_(m) {
offscreen_ = matrix_->CreateFrameCanvas();
}
// _very_ simplified. Can only read binary P6 PPM. Expects newlines in headers
// Not really robust. Use at your own risk :)
// This allows reload of an image while things are running, e.g. you can
// live-update the content.
bool LoadPPM(const char *filename) {
FILE *f = fopen(filename, "r");
// check if file exists
if (f == NULL && access(filename, F_OK) == -1) {
fprintf(stderr, "File \"%s\" doesn't exist\n", filename);
return false;
}
if (f == NULL)
return false;
char header_buf[256];
const char *line = ReadLine(f, header_buf, sizeof(header_buf));
#define EXIT_WITH_MSG(m) \
{ \
fprintf(stderr, "%s: %s |%s", filename, m, line); \
fclose(f); \
return false; \
}
if (sscanf(line, "P6 ") == EOF)
EXIT_WITH_MSG("Can only handle P6 as PPM type.");
line = ReadLine(f, header_buf, sizeof(header_buf));
int new_width, new_height;
if (!line || sscanf(line, "%d %d ", &new_width, &new_height) != 2)
EXIT_WITH_MSG("Width/height expected");
int value;
line = ReadLine(f, header_buf, sizeof(header_buf));
if (!line || sscanf(line, "%d ", &value) != 1 || value != 255)
EXIT_WITH_MSG("Only 255 for maxval allowed.");
const size_t pixel_count = new_width * new_height;
Pixel *new_image = new Pixel[pixel_count];
assert(sizeof(Pixel) == 3); // we make that assumption.
if (fread(new_image, sizeof(Pixel), pixel_count, f) != pixel_count) {
line = "";
EXIT_WITH_MSG("Not enough pixels read.");
}
#undef EXIT_WITH_MSG
fclose(f);
fprintf(stderr, "Read image '%s' with %dx%d\n", filename, new_width,
new_height);
horizontal_position_ = 0;
MutexLock l(&mutex_new_image_);
new_image_.Delete(); // in case we reload faster than is picked up
new_image_.image = new_image;
new_image_.width = new_width;
new_image_.height = new_height;
return true;
}
void Run() override {
const int screen_height = offscreen_->height();
const int screen_width = offscreen_->width();
while (!interrupt_received) {
{
MutexLock l(&mutex_new_image_);
if (new_image_.IsValid()) {
current_image_.Delete();
current_image_ = new_image_;
new_image_.Reset();
}
}
if (!current_image_.IsValid()) {
usleep(100 * 1000);
continue;
}
for (int x = 0; x < screen_width; ++x) {
for (int y = 0; y < screen_height; ++y) {
const Pixel &p = current_image_.getPixel(
(horizontal_position_ + x) % current_image_.width, y);
offscreen_->SetPixel(x, y, p.red, p.green, p.blue);
}
}
offscreen_ = matrix_->SwapOnVSync(offscreen_);
horizontal_position_ += scroll_jumps_;
if (horizontal_position_ < 0)
horizontal_position_ = current_image_.width;
if (scroll_ms_ <= 0) {
// No scrolling. We don't need the image anymore.
current_image_.Delete();
} else {
usleep(scroll_ms_ * 1000);
}
}
}
private:
struct Pixel {
Pixel() : red(0), green(0), blue(0) {}
uint8_t red;
uint8_t green;
uint8_t blue;
};
struct Image {
Image() : width(-1), height(-1), image(NULL) {}
~Image() { Delete(); }
void Delete() {
delete[] image;
Reset();
}
void Reset() {
image = NULL;
width = -1;
height = -1;
}
inline bool IsValid() { return image && height > 0 && width > 0; }
const Pixel &getPixel(int x, int y) {
static Pixel black;
if (x < 0 || x >= width || y < 0 || y >= height)
return black;
return image[x + width * y];
}
int width;
int height;
Pixel *image;
};
// Read line, skip comments.
char *ReadLine(FILE *f, char *buffer, size_t len) {
char *result;
do {
result = fgets(buffer, len, f);
} while (result != NULL && result[0] == '#');
return result;
}
const int scroll_jumps_;
const int scroll_ms_;
// Current image is only manipulated in our thread.
Image current_image_;
// New image can be loaded from another thread, then taken over in main thread
Mutex mutex_new_image_;
Image new_image_;
int32_t horizontal_position_;
RGBMatrix *matrix_;
FrameCanvas *offscreen_;
};
static int usage(const char *progname) {
fprintf(stderr, "usage: %s <options> -D <demo-nr> [optional parameter]\n",
progname);
fprintf(stderr, "Options:\n");
fprintf(stderr, "\t-D <demo-nr> : Always needs to be set\n");
rgb_matrix::PrintMatrixFlags(stderr);
fprintf(stderr, "Demos, choosen with -D\n");
fprintf(stderr, "\t1 - forward scrolling an image (-m <scroll-ms>)\n"
"\t2 - backward scrolling an image (-m <scroll-ms>)\n"
"\t3 - test image: a square\n"
"\t5 - Grayscale Block\n");
fprintf(stderr,
"Example:\n\t%s -D 1 runtext.ppm\n"
"Scrolls the runtext until Ctrl-C is pressed\n",
progname);
return 1;
}
int main(int argc, char *argv[]) {
int demo = -1;
int scroll_ms = 30;
const char *demo_parameter = NULL;
RGBMatrix::Options matrix_options;
rgb_matrix::RuntimeOptions runtime_opt;
// These are the defaults when no command-line flags are given.
matrix_options.rows = 32;
matrix_options.chain_length = 1;
matrix_options.parallel = 1;
// First things first: extract the command line flags that contain
// relevant matrix options.
if (!ParseOptionsFromFlags(&argc, &argv, &matrix_options, &runtime_opt)) {
return usage(argv[0]);
}
int opt;
while ((opt = getopt(argc, argv, "dD:r:P:c:p:b:m:LR:")) != -1) {
switch (opt) {
case 'D':
demo = atoi(optarg);
break;
case 'm':
scroll_ms = atoi(optarg);
break;
default: /* '?' */
return usage(argv[0]);
}
}
if (optind < argc) {
demo_parameter = argv[optind];
}
if (demo < 0) {
fprintf(stderr, TERM_ERR "Expected required option -D <demo>\n" TERM_NORM);
return usage(argv[0]);
}
RGBMatrix *matrix = RGBMatrix::CreateFromOptions(matrix_options, runtime_opt);
if (matrix == NULL)
return 1;
printf("Size: %dx%d. Hardware gpio mapping: %s\n", matrix->width(),
matrix->height(), matrix_options.hardware_mapping);
Canvas *canvas = matrix;
// The DemoRunner objects are filling
// the matrix continuously.
DemoRunner *demo_runner = NULL;
switch (demo) {
case 1:
case 2:
if (demo_parameter) {
ImageScroller *scroller =
new ImageScroller(matrix, demo == 1 ? 1 : -1, scroll_ms);
if (!scroller->LoadPPM(demo_parameter))
return 1;
demo_runner = scroller;
} else {
fprintf(stderr, "Demo %d Requires PPM image as parameter\n", demo);
return 1;
}
break;
case 3:
demo_runner = new SimpleSquare(canvas);
break;
case 5:
demo_runner = new GrayScaleBlock(canvas);
break;
}
if (demo_runner == NULL)
return usage(argv[0]);
// Set up an interrupt handler to be able to stop animations while they go
// on. Each demo tests for while (!interrupt_received) {},
// so they exit as soon as they get a signal.
signal(SIGTERM, InterruptHandler);
signal(SIGINT, InterruptHandler);
printf("Press <CTRL-C> to exit and reset LEDs\n");
// Now, run our particular demo; it will exit when it sees interrupt_received.
demo_runner->Run();
delete demo_runner;
delete canvas;
printf("Received CTRL-C. Exiting.\n");
return 0;
}