-
Notifications
You must be signed in to change notification settings - Fork 0
/
udp.cc
107 lines (89 loc) · 2.92 KB
/
udp.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
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
Compile in rpi-rgb-led-matrix directory.
make # compile library
g++ -Wall -O3 -g -Iinclude simple-udp.cc -o simple-udp -Llib -lrgbmatrix -lrt -lm -lpthread
*/
#include "led-matrix.h"
#include <arpa/inet.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
const int kListenPort = 9999;
using rgb_matrix::RGBMatrix;
volatile bool interrupt_received = false;
static void InterruptHandler(int signo) {
interrupt_received = true;
}
static rgb_matrix::FrameCanvas *FillFramebuffer(rgb_matrix::RGBMatrix *matrix, rgb_matrix::FrameCanvas *canvas,
const char *buffer) {
const int width = matrix->width();
const int height = matrix->height();
for (int y = 0; y < height; ++y) {
for (int x = 0; x < width; ++x) {
int r = *buffer++;
int g = *buffer++;
int b = *buffer++;
canvas->SetPixel(x, y, r, g, b);
}
}
return matrix->SwapOnVSync(canvas);
}
int main(int argc, char **argv) {
int port = kListenPort;
// Set some defaults
RGBMatrix::Options my_defaults;
my_defaults.chain_length = 4;
my_defaults.rows = 32;
my_defaults.show_refresh_rate = true;
rgb_matrix::RuntimeOptions runtime_defaults;
runtime_defaults.drop_privileges = 1;
RGBMatrix *matrix = rgb_matrix::CreateMatrixFromFlags(&argc, &argv,
&my_defaults,
&runtime_defaults);
if (matrix == NULL) {
PrintMatrixFlags(stderr, my_defaults, runtime_defaults);
return 1;
}
rgb_matrix::FrameCanvas *swap_buffer = matrix->CreateFrameCanvas();
const int framebuffer_size = matrix->width() * matrix->height() * 3;
char *const packet_buffer = new char[framebuffer_size];
struct sigaction sa;
sa.sa_handler = InterruptHandler;
sa.sa_flags = SA_RESETHAND | SA_NODEFER;
sigaction(SIGTERM, &sa, NULL);
sigaction(SIGINT, &sa, NULL);
int s;
if ((s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
perror("creating UDP socket");
exit(1);
}
struct sockaddr_in addr;
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = inet_addr("127.0.0.1");
addr.sin_port = htons(port);
if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
perror("bind");
exit(1);
}
matrix->Clear();
while (!interrupt_received) {
const ssize_t buffer_bytes = recvfrom(s, packet_buffer, framebuffer_size,
0, NULL, 0);
if (interrupt_received)
break;
if (buffer_bytes < 1)
continue;
if (buffer_bytes == framebuffer_size) {
swap_buffer = FillFramebuffer(matrix, swap_buffer, packet_buffer);
} else {
continue;
}
}
delete matrix; // Make sure to delete it in the end.
}