2012-07-13 21:18:35 +02:00
|
|
|
/*
|
|
|
|
* vim: sw=2 ts=2 expandtab
|
|
|
|
*
|
2013-02-02 21:54:53 +01:00
|
|
|
* "THE PIZZA-WARE LICENSE" (derived from "THE BEER-WARE LICENCE"):
|
|
|
|
* Thomas Kolb <cfr34k@tkolb.de> wrote this file. As long as you retain this
|
|
|
|
* notice you can do whatever you want with this stuff. If we meet some day,
|
|
|
|
* and you think this stuff is worth it, you can buy me a pizza in return.
|
|
|
|
* - Thomas Kolb
|
2012-07-13 21:18:35 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2018-06-24 22:23:39 +02:00
|
|
|
#include "sk6812.h"
|
2012-07-13 21:18:35 +02:00
|
|
|
|
|
|
|
#define SET_COLOR 0
|
|
|
|
#define FADE_COLOR 1
|
|
|
|
#define ADD_COLOR 2
|
|
|
|
#define SET_FADESTEP 3
|
|
|
|
|
|
|
|
// creates the socket needed for steering the LED strip
|
2018-06-30 01:42:34 +02:00
|
|
|
int sk6812_init(struct sk6812_ctx *ctx, const char *host, unsigned short port) {
|
2012-07-13 21:18:35 +02:00
|
|
|
struct addrinfo hints;
|
|
|
|
struct addrinfo *result;
|
|
|
|
char portstr[6];
|
|
|
|
|
|
|
|
memset(&hints, 0, sizeof(struct addrinfo));
|
|
|
|
hints.ai_family = AF_UNSPEC;
|
|
|
|
hints.ai_socktype = SOCK_DGRAM;
|
|
|
|
hints.ai_flags = 0;
|
|
|
|
hints.ai_protocol = 0;
|
|
|
|
|
|
|
|
sprintf(portstr, "%u", port);
|
|
|
|
|
|
|
|
if(getaddrinfo(host, portstr, &hints, &result) != 0) {
|
|
|
|
perror("getaddrinfo() failed");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-06-30 01:42:34 +02:00
|
|
|
ctx->socket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
|
|
|
|
if (ctx->socket == -1) {
|
2012-07-13 21:18:35 +02:00
|
|
|
perror("socket() failed");
|
|
|
|
freeaddrinfo(result);
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
2018-06-30 01:42:34 +02:00
|
|
|
if (connect(ctx->socket, result->ai_addr, result->ai_addrlen) == -1) {
|
2012-07-13 21:18:35 +02:00
|
|
|
perror("connect() failed");
|
|
|
|
freeaddrinfo(result);
|
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
freeaddrinfo(result);
|
|
|
|
|
2018-06-30 01:42:34 +02:00
|
|
|
ctx->queueIndex = 0;
|
|
|
|
|
2012-07-13 21:18:35 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-06-30 01:42:34 +02:00
|
|
|
void sk6812_set_color(struct sk6812_ctx *ctx, uint16_t module, uint8_t r, uint8_t g, uint8_t b, uint8_t w) {
|
|
|
|
ctx->packetQueue[ctx->queueIndex].action = SET_COLOR;
|
|
|
|
ctx->packetQueue[ctx->queueIndex].module = htons(module);
|
|
|
|
ctx->packetQueue[ctx->queueIndex].data[0] = r;
|
|
|
|
ctx->packetQueue[ctx->queueIndex].data[1] = g;
|
|
|
|
ctx->packetQueue[ctx->queueIndex].data[2] = b;
|
|
|
|
ctx->packetQueue[ctx->queueIndex].data[3] = w;
|
|
|
|
ctx->queueIndex++;
|
2012-07-13 21:18:35 +02:00
|
|
|
}
|
|
|
|
|
2018-06-30 01:42:34 +02:00
|
|
|
void sk6812_fade_color(struct sk6812_ctx *ctx, uint16_t module, uint8_t r, uint8_t g, uint8_t b, uint8_t w) {
|
|
|
|
ctx->packetQueue[ctx->queueIndex].action = FADE_COLOR;
|
|
|
|
ctx->packetQueue[ctx->queueIndex].module = htons(module);
|
|
|
|
ctx->packetQueue[ctx->queueIndex].data[0] = r;
|
|
|
|
ctx->packetQueue[ctx->queueIndex].data[1] = g;
|
|
|
|
ctx->packetQueue[ctx->queueIndex].data[2] = b;
|
|
|
|
ctx->packetQueue[ctx->queueIndex].data[3] = w;
|
|
|
|
ctx->queueIndex++;
|
2012-07-13 21:18:35 +02:00
|
|
|
}
|
|
|
|
|
2018-06-30 01:42:34 +02:00
|
|
|
void sk6812_add_color(struct sk6812_ctx *ctx, uint16_t module, uint8_t r, uint8_t g, uint8_t b, uint8_t w) {
|
|
|
|
ctx->packetQueue[ctx->queueIndex].action = ADD_COLOR;
|
|
|
|
ctx->packetQueue[ctx->queueIndex].module = htons(module);
|
|
|
|
ctx->packetQueue[ctx->queueIndex].data[0] = r;
|
|
|
|
ctx->packetQueue[ctx->queueIndex].data[1] = g;
|
|
|
|
ctx->packetQueue[ctx->queueIndex].data[2] = b;
|
|
|
|
ctx->packetQueue[ctx->queueIndex].data[3] = w;
|
|
|
|
ctx->queueIndex++;
|
2012-07-13 21:18:35 +02:00
|
|
|
}
|
|
|
|
|
2018-06-30 01:42:34 +02:00
|
|
|
void sk6812_set_fadestep(struct sk6812_ctx *ctx, uint8_t fadestep) {
|
|
|
|
ctx->packetQueue[ctx->queueIndex].action = SET_FADESTEP;
|
|
|
|
ctx->packetQueue[ctx->queueIndex].data[0] = fadestep;
|
|
|
|
ctx->queueIndex++;
|
2012-07-13 21:18:35 +02:00
|
|
|
}
|
|
|
|
|
2018-06-30 01:42:34 +02:00
|
|
|
int sk6812_commit(struct sk6812_ctx *ctx) {
|
|
|
|
if(send(ctx->socket, ctx->packetQueue, ctx->queueIndex * sizeof(struct SK6812Packet), 0) == -1) {
|
2012-07-13 21:18:35 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-06-30 01:42:34 +02:00
|
|
|
ctx->queueIndex = 0;
|
2012-07-13 21:18:35 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-06-30 01:42:34 +02:00
|
|
|
void sk6812_shutdown(struct sk6812_ctx *ctx) {
|
|
|
|
close(ctx->socket);
|
2012-07-13 21:18:35 +02:00
|
|
|
}
|