forked from osresearch/LEDscape
-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix-udp-rx.c
162 lines (137 loc) · 3.26 KB
/
matrix-udp-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
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
/** \file
* UDP image packet receiver.
*
* Based on the HackRockCity LED Display code:
* https://github.com/agwn/pyramidTransmitter/blob/master/LEDDisplay.pde
*
* Designed to render into the LED matrix.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <sys/time.h>
#include <fcntl.h>
#include <termios.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <netinet/in.h>
#include <inttypes.h>
#include <errno.h>
#include <string.h>
#include <pthread.h>
#include "util.h"
#include "bitslice.h"
#include "ledscape.h"
static int port = 9999;
static unsigned width = 16;
static unsigned height = 256;
static int
udp_socket(
const int port
)
{
const int sock = socket(AF_INET, SOCK_DGRAM, 0);
struct sockaddr_in addr = {
.sin_family = AF_INET,
.sin_port = htons(port),
.sin_addr.s_addr = INADDR_ANY,
};
if (sock < 0)
return -1;
if (bind(sock, (const struct sockaddr*) &addr, sizeof(addr)) < 0)
return -1;
return sock;
}
int
main(
int argc,
char ** argv
)
{
const int port = 9999;
const int sock = udp_socket(port);
if (sock < 0)
die("socket port %d failed: %s\n", port, strerror(errno));
const unsigned width = 256;
const unsigned height = 16;
const size_t image_size = width * height * 3;
// largest possible UDP packet
uint8_t buf[65536];
if (sizeof(buf) < image_size + 1)
die("%u x %u too large for UDP\n", width, height);
fprintf(stderr, "%u x %u, UDP port %u\n", width, height, port);
ledscape_t * const leds = ledscape_init(width);
const unsigned report_interval = 10;
unsigned last_report = 0;
unsigned long delta_sum = 0;
unsigned frames = 0;
while (1)
{
const ssize_t rlen = recv(sock, buf, sizeof(buf), 0);
if (rlen < 0)
die("recv failed: %s\n", strerror(errno));
warn_once("received %zu bytes\n", rlen);
if (buf[0] == 2)
{
// image type
printf("image type: %.*s\n",
(int) rlen - 1,
&buf[1]
);
continue;
}
if (buf[0] != 1)
{
// What is it?
warn_once("Unknown image type '%c' (%02x)\n",
buf[0],
buf[0]
);
continue;
}
if ((size_t) rlen != image_size + 1)
{
warn_once("WARNING: Received packet %zu bytes, expected %zu\n",
rlen,
image_size + 1
);
}
struct timeval start_tv, stop_tv, delta_tv;
gettimeofday(&start_tv, NULL);
const unsigned frame_num = 0;
ledscape_frame_t * const frame
= ledscape_frame(leds, frame_num);
uint32_t * const fb = (void*) frame;
for (unsigned x = 0 ; x < width ; x++)
{
for (unsigned y = 0 ; y < height ; y++)
{
uint8_t * const out = (void*) &fb[x + y*width];
//const uint8_t * const in = &buf[1 + (x*64 + y+24)*3];
const uint8_t * const in = &buf[1 + 3*(x + y*width)];
out[0] = in[0];
out[1] = in[1];
out[2] = in[2];
}
}
ledscape_draw(leds, frame_num);
gettimeofday(&stop_tv, NULL);
timersub(&stop_tv, &start_tv, &delta_tv);
frames++;
delta_sum += delta_tv.tv_usec;
if (stop_tv.tv_sec - last_report < report_interval)
continue;
last_report = stop_tv.tv_sec;
const unsigned delta_avg = delta_sum / frames;
printf("%6u usec avg, max %.2f fps, actual %.2f fps (over %u frames)\n",
delta_avg,
report_interval * 1.0e6 / delta_avg,
frames * 1.0 / report_interval,
frames
);
frames = delta_sum = 0;
}
return 0;
}