forked from Yona-Appletree/LEDscape
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathartnet-rx.c
122 lines (103 loc) · 2.54 KB
/
artnet-rx.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
/** \file
* UDP image packet receiver.
*
* Based on the HackRockCity LED Display code:
* https://github.com/agwn/pyramidTransmitter/blob/master/LEDDisplay.pde
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <inttypes.h>
#include <errno.h>
#include <string.h>
#include "ledscape.h"
#include "pru.h"
int
main(
int argc,
char ** argv
)
{
int port = 9999;
int num_pixels = 256;
int num_strips = LEDSCAPE_NUM_STRIPS;
extern char *optarg;
int opt;
while ((opt = getopt(argc, argv, "p:c:d:")) != -1)
{
switch (opt)
{
case 'p':
port = atoi(optarg);
break;
case 'c':
num_pixels = atoi(optarg);
break;
case 'd': {
int width=0, height=0;
if (sscanf(optarg,"%dx%d", &width, &height) == 2) {
num_pixels = width * height;
} else {
printf("Invalid argument for -d; expected NxN; actual: %s", optarg);
exit(EXIT_FAILURE);
}
}
break;
default:
fprintf(stderr, "Usage: %s [-p <port>] [-c <led_count> | -d <width>x<height>]\n", argv[0]);
exit(EXIT_FAILURE);
}
}
const int sock = socket(AF_INET, SOCK_DGRAM, 0);
struct sockaddr_in addr = {
.sin_family = AF_INET,
.sin_addr.s_addr = INADDR_ANY,
.sin_port = htons(port),
};
if (sock < 0)
die("socket failed: %s\n", strerror(errno));
if (bind(sock, (const struct sockaddr*) &addr, sizeof(addr)) < 0)
die("bind port %d failed: %s\n", port, strerror(errno));
ledscape_t * const leds = ledscape_init(num_pixels);
fprintf(stderr, "Started LEDscape UDP receiver on port %d for %d pixels\n", port, num_pixels);
unsigned frame_num = 0;
uint8_t buf[num_pixels * num_strips * 4];
time_t last_time = time(NULL);
int fps_counter=0;
while (1)
{
const ssize_t rc = recv(sock, buf, sizeof(buf), 0);
if (rc < 0) {
printf("recv failed: %s\n", strerror(errno));
continue;
}
ledscape_frame_t * const frame
= ledscape_frame(leds, frame_num);
for(unsigned x=0 ; x < num_pixels ; x++)
{
for(unsigned strip = 0 ; strip < num_strips ; strip++)
{
const uint8_t r = buf[strip*num_pixels*3 + x*3 + 0];
const uint8_t g = buf[strip*num_pixels*3 + x*3 + 1];
const uint8_t b = buf[strip*num_pixels*3 + x*3 + 2];
ledscape_set_color(frame, strip, x, r, g, b);
}
}
ledscape_wait(leds);
ledscape_draw(leds, frame_num);
time_t now = time(NULL);
if (now != last_time)
{
printf("%d fps\n", fps_counter);
last_time = now;
fps_counter = 0;
}
fps_counter++;
frame_num = (frame_num+1) % 2;
}
ledscape_close(leds);
return 0;
}