Receive commands via UDP

This commit is contained in:
Thomas Kolb 2013-04-12 19:26:02 +02:00
parent 9b9f6ee3d6
commit b3b9d68ad7

View file

@ -1,53 +1,103 @@
#include <math.h> #include <math.h>
#include <unistd.h>
#include <time.h> #include <time.h>
#include <string.h> #include <string.h>
#include <errno.h> #include <errno.h>
#include <stdio.h>
#include <stdint.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/ws2801.h"
#include "../include/logger.h" #include "../include/logger.h"
#define PI 3.141592654f #define PORT 2703
void set_all(uint8_t r, uint8_t g, uint8_t b) #define NUM_MODULES 20
{
for(uint8_t i = 0; i < 20; i++) {
ws2801_set_colour(i, r, g, b);
}
}
float sin_off(float phase) #define SET_COLOUR 0
{ #define FADE_COLOUR 1
return 255 * (0.5f + 0.5f * sin(phase)); #define ADD_COLOUR 2
} #define SET_FADESTEP 3
int main(void) int main(void)
{ {
float phase = 0; 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(); logger_init();
if(ws2801_init(20) == -1) { // 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."); LOG(LVL_FATAL, "Could not initialize WS2801 library.");
return 1; return 1;
} }
LOG(LVL_INFO, "Initialisation complete.");
while(1) { while(1) {
//phase += PI/1000; 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;
}
for(uint8_t i = 0; i < 20; i++) { pkgbuf[rcvbytes] = 0;
ws2801_set_colour(i, sin_off(i * PI / 10 + phase), sin_off(i * PI / 10 + 1.241*phase), sin_off(i * PI / 10 + 1.537*phase));
// 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(); ws2801_send_update();
static const struct timespec sleepval = {0, 40000000};
nanosleep(&sleepval, NULL);
} }
close(sock);
ws2801_shutdown(); ws2801_shutdown();
return 0; return 0;