-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
419 lines (355 loc) · 9.86 KB
/
main.c
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/*
* newtest.c
*
* Copyright (c) 2014 Jeremy Garff <jer @ jers.net>
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted
* provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
* 3. Neither the name of the owner nor the names of its contributors may be used to endorse
* or promote products derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
static char VERSION[] = "XX.YY.ZZ";
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <signal.h>
#include <stdarg.h>
#include <getopt.h>
#include "clk.h"
#include "gpio.h"
#include "dma.h"
#include "pwm.h"
#include "version.h"
#include "ws2811.h"
#define ARRAY_SIZE(stuff) (sizeof(stuff) / sizeof(stuff[0]))
// defaults for cmdline options
#define TARGET_FREQ WS2811_TARGET_FREQ
#define GPIO_PIN 18
#define DMA 10
//#define STRIP_TYPE WS2811_STRIP_RGB // WS2812/SK6812RGB integrated chip+leds
#define STRIP_TYPE WS2811_STRIP_GBR // WS2812/SK6812RGB integrated chip+leds
//#define STRIP_TYPE SK6812_STRIP_RGBW // SK6812RGBW (NOT SK6812RGB)
#define WIDTH 8
#define HEIGHT 8
#define LED_COUNT (WIDTH * HEIGHT)
int width = WIDTH;
int height = HEIGHT;
int led_count = LED_COUNT;
int clear_on_exit = 0;
ws2811_t ledstring =
{
.freq = TARGET_FREQ,
.dmanum = DMA,
.channel =
{
[0] =
{
.gpionum = GPIO_PIN,
.count = LED_COUNT,
.invert = 0,
.brightness = 255,
.strip_type = STRIP_TYPE,
},
[1] =
{
.gpionum = 0,
.count = 0,
.invert = 0,
.brightness = 0,
},
},
};
ws2811_led_t *matrix;
static uint8_t running = 1;
void matrix_render(void)
{
int x, y;
for (x = 0; x < width; x++)
{
for (y = 0; y < height; y++)
{
ledstring.channel[0].leds[(y * width) + x] = matrix[y * width + x];
}
}
}
void matrix_raise(void)
{
int x, y;
for (y = 0; y < (height - 1); y++)
{
for (x = 0; x < width; x++)
{
// This is for the 8x8 Pimoroni Unicorn-HAT where the LEDS in subsequent
// rows are arranged in opposite directions
matrix[y * width + x] = matrix[(y + 1)*width + width - x - 1];
}
}
}
void matrix_clear(void)
{
int x, y;
for (y = 0; y < (height ); y++)
{
for (x = 0; x < width; x++)
{
matrix[y * width + x] = 0;
}
}
}
int dotspos[] = { 0, 1, 2, 3, 4, 5, 6, 7 };
ws2811_led_t dotcolors[] =
{
0x00200000, // red
0x00201000, // orange
0x00202000, // yellow
0x00002000, // green
0x00002020, // lightblue
0x00000020, // blue
0x00100010, // purple
0x00200010, // pink
};
ws2811_led_t dotcolors_rgbw[] =
{
0x00200000, // red
0x10200000, // red + W
0x00002000, // green
0x10002000, // green + W
0x00000020, // blue
0x10000020, // blue + W
0x00101010, // white
0x10101010, // white + W
};
void matrix_bottom(void)
{
int i;
for (i = 0; i < (int)(ARRAY_SIZE(dotspos)); i++)
{
dotspos[i]++;
if (dotspos[i] > (width - 1))
{
dotspos[i] = 0;
}
if (ledstring.channel[0].strip_type == SK6812_STRIP_RGBW) {
matrix[dotspos[i] + (height - 1) * width] = dotcolors_rgbw[i];
} else {
matrix[dotspos[i] + (height - 1) * width] = dotcolors[i];
}
}
}
static void ctrl_c_handler(int signum)
{
(void)(signum);
running = 0;
}
static void setup_handlers(void)
{
struct sigaction sa =
{
.sa_handler = ctrl_c_handler,
};
sigaction(SIGINT, &sa, NULL);
sigaction(SIGTERM, &sa, NULL);
}
void parseargs(int argc, char **argv, ws2811_t *ws2811)
{
int index;
int c;
static struct option longopts[] =
{
{"help", no_argument, 0, 'h'},
{"dma", required_argument, 0, 'd'},
{"gpio", required_argument, 0, 'g'},
{"invert", no_argument, 0, 'i'},
{"clear", no_argument, 0, 'c'},
{"strip", required_argument, 0, 's'},
{"height", required_argument, 0, 'y'},
{"width", required_argument, 0, 'x'},
{"version", no_argument, 0, 'v'},
{0, 0, 0, 0}
};
while (1)
{
index = 0;
c = getopt_long(argc, argv, "cd:g:his:vx:y:", longopts, &index);
if (c == -1)
break;
switch (c)
{
case 0:
/* handle flag options (array's 3rd field non-0) */
break;
case 'h':
fprintf(stderr, "%s version %s\n", argv[0], VERSION);
fprintf(stderr, "Usage: %s \n"
"-h (--help) - this information\n"
"-s (--strip) - strip type - rgb, grb, gbr, rgbw\n"
"-x (--width) - matrix width (default 8)\n"
"-y (--height) - matrix height (default 8)\n"
"-d (--dma) - dma channel to use (default 10)\n"
"-g (--gpio) - GPIO to use\n"
" If omitted, default is 18 (PWM0)\n"
"-i (--invert) - invert pin output (pulse LOW)\n"
"-c (--clear) - clear matrix on exit.\n"
"-v (--version) - version information\n"
, argv[0]);
exit(-1);
case 'D':
break;
case 'g':
if (optarg) {
int gpio = atoi(optarg);
/*
PWM0, which can be set to use GPIOs 12, 18, 40, and 52.
Only 12 (pin 32) and 18 (pin 12) are available on the B+/2B/3B
PWM1 which can be set to use GPIOs 13, 19, 41, 45 and 53.
Only 13 is available on the B+/2B/PiZero/3B, on pin 33
PCM_DOUT, which can be set to use GPIOs 21 and 31.
Only 21 is available on the B+/2B/PiZero/3B, on pin 40.
SPI0-MOSI is available on GPIOs 10 and 38.
Only GPIO 10 is available on all models.
The library checks if the specified gpio is available
on the specific model (from model B rev 1 till 3B)
*/
ws2811->channel[0].gpionum = gpio;
}
break;
case 'i':
ws2811->channel[0].invert=1;
break;
case 'c':
clear_on_exit=1;
break;
case 'd':
if (optarg) {
int dma = atoi(optarg);
if (dma < 14) {
ws2811->dmanum = dma;
} else {
printf ("invalid dma %d\n", dma);
exit (-1);
}
}
break;
case 'y':
if (optarg) {
height = atoi(optarg);
if (height > 0) {
ws2811->channel[0].count = height * width;
} else {
printf ("invalid height %d\n", height);
exit (-1);
}
}
break;
case 'x':
if (optarg) {
width = atoi(optarg);
if (width > 0) {
ws2811->channel[0].count = height * width;
} else {
printf ("invalid width %d\n", width);
exit (-1);
}
}
break;
case 's':
if (optarg) {
if (!strncasecmp("rgb", optarg, 4)) {
ws2811->channel[0].strip_type = WS2811_STRIP_RGB;
}
else if (!strncasecmp("rbg", optarg, 4)) {
ws2811->channel[0].strip_type = WS2811_STRIP_RBG;
}
else if (!strncasecmp("grb", optarg, 4)) {
ws2811->channel[0].strip_type = WS2811_STRIP_GRB;
}
else if (!strncasecmp("gbr", optarg, 4)) {
ws2811->channel[0].strip_type = WS2811_STRIP_GBR;
}
else if (!strncasecmp("brg", optarg, 4)) {
ws2811->channel[0].strip_type = WS2811_STRIP_BRG;
}
else if (!strncasecmp("bgr", optarg, 4)) {
ws2811->channel[0].strip_type = WS2811_STRIP_BGR;
}
else if (!strncasecmp("rgbw", optarg, 4)) {
ws2811->channel[0].strip_type = SK6812_STRIP_RGBW;
}
else if (!strncasecmp("grbw", optarg, 4)) {
ws2811->channel[0].strip_type = SK6812_STRIP_GRBW;
}
else {
printf ("invalid strip %s\n", optarg);
exit (-1);
}
}
break;
case 'v':
fprintf(stderr, "%s version %s\n", argv[0], VERSION);
exit(-1);
case '?':
/* getopt_long already reported error? */
exit(-1);
default:
exit(-1);
}
}
}
int main(int argc, char *argv[])
{
ws2811_return_t ret;
sprintf(VERSION, "%d.%d.%d", VERSION_MAJOR, VERSION_MINOR, VERSION_MICRO);
parseargs(argc, argv, &ledstring);
matrix = malloc(sizeof(ws2811_led_t) * width * height);
setup_handlers();
if ((ret = ws2811_init(&ledstring)) != WS2811_SUCCESS)
{
fprintf(stderr, "ws2811_init failed: %s\n", ws2811_get_return_t_str(ret));
return ret;
}
while (running)
{
matrix_raise();
matrix_bottom();
matrix_render();
if ((ret = ws2811_render(&ledstring)) != WS2811_SUCCESS)
{
fprintf(stderr, "ws2811_render failed: %s\n", ws2811_get_return_t_str(ret));
break;
}
// 15 frames /sec
usleep(1000000 / 15);
}
if (clear_on_exit) {
matrix_clear();
matrix_render();
ws2811_render(&ledstring);
}
ws2811_fini(&ledstring);
printf ("\n");
return ret;
}