-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathled_with_artnet.c
128 lines (103 loc) · 2.64 KB
/
led_with_artnet.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
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <wiringPi.h>
#include <wiringPiSPI.h>
#define SS 8
#define DC 12
#define BUFFER_SIZE 12288
unsigned char pixel[BUFFER_SIZE];
unsigned char value;
int last_sequence[2] = {-1, -1};
bool flush = false;
void display()
{
digitalWrite(DC, 1);
sleep(0.00000001);
digitalWrite(DC, 0);
sleep(0.00000001);
digitalWrite(SS, 0);
wiringPiSPIDataRW(0, pixel, BUFFER_SIZE);
digitalWrite(SS, 1);
flush = false;
}
void update(char *data)
{
char id[8];
for (int i=0; i<8; i++) { id[i] = data[i]; }
if (strcmp(id,"ArtNet"))
{
int opcode = data[8] + (data[9] << 8);
int protocolVersion = (data[10] << 8) + data[11];
if ((opcode==0x5000) && (protocolVersion >= 14))
{
int sequence = data[12];
int universe = data[14] & 0x0F;
int net = data[15];
int data_length = (data[16] << 8) + data[17];
memcpy(&pixel[512*(universe+net*16)], &data[18], data_length);
if (last_sequence[net] != sequence)
{
if(net==1)
{
display();
}
}
last_sequence[net] = sequence;
}
}
}
int main(void)
{
// GPIO
wiringPiSetup();
wiringPiSetupGpio();
pinMode(SS, OUTPUT);
pinMode(DC, OUTPUT);
digitalWrite(SS, 1);
digitalWrite(DC, 0);
sleep(1);
// SPI
wiringPiSPISetup(0, 54000000);
// ArtNet
char buf[530];
struct sockaddr_in server_addr, client_addr;
socklen_t client_addr_size = sizeof(client_addr);
// Creating socket file descriptor
int sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
if (sock_fd<0)
{
perror("Failed to create socket");
return -1;
}
// IP and Port
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(6454);
server_addr.sin_addr.s_addr = INADDR_ANY;
// Bind the socket to reciever address
if (bind(sock_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0)
{
perror("Failed to bind");
return -1;
}
int nonblocking = 1;
ioctl(sock_fd, FIONBIO, &nonblocking);
memset(pixel, 0, sizeof(pixel));
while(1)
{
// Receive data
recvfrom(sock_fd, buf, sizeof(buf), 0, (struct sockaddr *)&client_addr, &client_addr_size);
update(buf);
if (flush)
{
display();
}
}
close(sock_fd);
return 0;
}