105 lines
2.2 KiB
C
105 lines
2.2 KiB
C
#include <math.h>
|
|
|
|
#include <time.h>
|
|
#include <string.h>
|
|
#include <errno.h>
|
|
#include <stdint.h>
|
|
|
|
#include <arpa/inet.h>
|
|
#include <netinet/in.h>
|
|
#include <sys/types.h>
|
|
#include <sys/socket.h>
|
|
#include <unistd.h>
|
|
|
|
#include "../include/ws2801.h"
|
|
#include "../include/logger.h"
|
|
|
|
#define PORT 2703
|
|
|
|
#define NUM_MODULES 20
|
|
|
|
#define SET_COLOUR 0
|
|
#define FADE_COLOUR 1
|
|
#define ADD_COLOUR 2
|
|
#define SET_FADESTEP 3
|
|
|
|
int main(void)
|
|
{
|
|
int sock;
|
|
socklen_t remote_addr_len;
|
|
struct sockaddr_in listen_addr, remote_addr;
|
|
uint8_t pkgbuf[256];
|
|
ssize_t rcvbytes, offset = 0;
|
|
|
|
uint8_t cmd, r, g, b, action, module;
|
|
|
|
// initialize logger
|
|
logger_init();
|
|
|
|
// initialize UDP server socket
|
|
sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
|
if(sock == -1) {
|
|
LOG(LVL_FATAL, "Could not initialize UDP socket: %s.", strerror(errno));
|
|
return 1;
|
|
}
|
|
|
|
memset(&listen_addr, 0, sizeof(listen_addr));
|
|
listen_addr.sin_family = AF_INET;
|
|
listen_addr.sin_port = htons(PORT);
|
|
listen_addr.sin_addr.s_addr = htonl(INADDR_ANY);
|
|
|
|
if(bind(sock, (struct sockaddr*)&listen_addr, sizeof(listen_addr)) == -1) {
|
|
LOG(LVL_FATAL, "Could not bind socket to port %i: %s.", PORT, strerror(errno));
|
|
return 1;
|
|
}
|
|
|
|
// initialize ws2801 library
|
|
if(ws2801_init(NUM_MODULES) == -1) {
|
|
LOG(LVL_FATAL, "Could not initialize WS2801 library.");
|
|
return 1;
|
|
}
|
|
|
|
LOG(LVL_INFO, "Initialisation complete.");
|
|
|
|
|
|
while(1) {
|
|
remote_addr_len = sizeof(remote_addr);
|
|
rcvbytes = recvfrom(sock, pkgbuf, 256, 0, (struct sockaddr*)&remote_addr, &remote_addr_len);
|
|
if(rcvbytes == -1) {
|
|
LOG(LVL_ERR, "recvfrom() failed: %s. Shutting down.", strerror(errno));
|
|
break;
|
|
}
|
|
|
|
pkgbuf[rcvbytes] = 0;
|
|
|
|
// commands from packet
|
|
offset = 0;
|
|
while(offset <= rcvbytes - 3) {
|
|
cmd = pkgbuf[offset + 0];
|
|
r = pkgbuf[offset + 1];
|
|
g = pkgbuf[offset + 2];
|
|
b = pkgbuf[offset + 3];
|
|
offset += 4;
|
|
|
|
action = (cmd & 0xC0) >> 6; // the upper 2 bits
|
|
module = cmd & 0x3F; // the lower 6 bits
|
|
|
|
if(action != SET_COLOUR) {
|
|
// action not implemented yet -> ignore
|
|
LOG(LVL_DEBUG, "Action %u not implemented yet.", action);
|
|
continue;
|
|
}
|
|
|
|
ws2801_set_colour(module, r, g, b);
|
|
}
|
|
|
|
ws2801_send_update();
|
|
}
|
|
|
|
close(sock);
|
|
|
|
ws2801_shutdown();
|
|
|
|
return 0;
|
|
}
|