Triple LED Stripe test

This commit is contained in:
Thomas Kolb 2018-06-30 01:42:34 +02:00
parent 0a76acd85b
commit 0ae3492b82
5 changed files with 93 additions and 75 deletions

View file

@ -1,7 +1,7 @@
WS2801_HOST = "zybot" WS2801_HOST = "zybot"
WS2801_PORT = 2703 WS2801_PORT = 2703
NUM_MODULES = 300 NUM_MODULES = 54
CENTER_MODULE = 150 CENTER_MODULE = 54
GAMMA = 2.0 GAMMA = 2.0

View file

@ -3,12 +3,12 @@ OVERDRIVE = 1.70
EXPONENT = 1.5 EXPONENT = 1.5
W_EXPONENT = 2.2 W_EXPONENT = 2.2
M = 1.0--1.7 -- mass M = 2.3 -- mass
D = 1 -- spring strength D = 1 -- spring strength
DAMPING = {} -- filled in init() DAMPING = {} -- filled in init()
num_modules = 300 num_modules = 54
center_module = 150 center_module = 54
num_masses = math.floor(num_modules/2) num_masses = math.floor(num_modules/2)
excitement_pos = 1 excitement_pos = 1
@ -148,10 +148,10 @@ function periodic()
b_tmp[i] = pos_b[i] b_tmp[i] = pos_b[i]
w_tmp[i] = pos_w[i] w_tmp[i] = pos_w[i]
r_tmp[num_modules-i+1] = pos_r[i] --r_tmp[num_modules-i+1] = pos_r[i]
g_tmp[num_modules-i+1] = pos_g[i] --g_tmp[num_modules-i+1] = pos_g[i]
b_tmp[num_modules-i+1] = pos_b[i] --b_tmp[num_modules-i+1] = pos_b[i]
w_tmp[num_modules-i+1] = pos_w[i] --w_tmp[num_modules-i+1] = pos_w[i]
--print(i, pos_r[i]) --print(i, pos_r[i])
end end
@ -172,7 +172,7 @@ function init(nmod, cmod)
num_modules = nmod num_modules = nmod
center_module = cmod center_module = cmod
num_masses = math.floor(nmod/2) num_masses = nmod --math.floor(nmod/2)
excitement_pos = 1 excitement_pos = 1
for i = 1,nmod do for i = 1,nmod do
@ -198,7 +198,7 @@ function init(nmod, cmod)
acc_b[i] = 0 acc_b[i] = 0
acc_w[i] = 0 acc_w[i] = 0
DAMPING[i] = 1 - 0.06 * math.pow(math.abs((i - excitement_pos) / num_masses), 2) DAMPING[i] = 1 - 0.10 * math.pow(math.abs((i - excitement_pos) / num_masses), 2)
end end
-- don't use fading -- don't use fading

33
main.c
View file

@ -45,6 +45,8 @@ sample signal[BLOCK_LEN];
sem_t fftSemaphore; sem_t fftSemaphore;
struct sk6812_ctx sk6812[3];
int running = 1; int running = 1;
void* fft_thread(void *param) { void* fft_thread(void *param) {
@ -220,13 +222,17 @@ int main(int argc, char **argv) {
fadeStep = lua_tointeger(L, -1); fadeStep = lua_tointeger(L, -1);
useFading = fadeStep > 0; useFading = fadeStep > 0;
if(useFading) { if(useFading) {
sk6812_set_fadestep(fadeStep); for(int i = 0; i < 3; i++) {
sk6812_set_fadestep(&(sk6812[i]), fadeStep);
}
printf("Fading enabled with fadestep %i.\n", fadeStep); printf("Fading enabled with fadestep %i.\n", fadeStep);
} }
// initialize the WS2801 library // initialize the WS2801 library
printf("Connecting to %s:%i\n", host, port); printf("Connecting to %s:%i\n", host, port);
sk6812_init(host, port); for(int i = 0; i < 3; i++) {
sk6812_init(&(sk6812[i]), host, port + i);
}
// create semaphores // create semaphores
sem_init(&fftSemaphore, 0, 1); sem_init(&fftSemaphore, 0, 1);
@ -248,25 +254,25 @@ int main(int argc, char **argv) {
lua_readdoublearray(L, green, num_modules); lua_readdoublearray(L, green, num_modules);
lua_readdoublearray(L, red, num_modules); lua_readdoublearray(L, red, num_modules);
/*if((++frame & 1) == 0)*/ { for(int s = 0; s < 3; s++) {
if(useFading) { if(useFading) {
for(i = 0; i < num_modules; i++) { for(i = 0; i < num_modules; i++) {
sk6812_fade_color(i, sk6812_fade_color(&(sk6812[s]), i,
255 * gamma_correct(red[i], gamma), 255 * gamma_correct(red[i], gamma),
255 * gamma_correct(green[i], gamma), 255 * gamma_correct(green[i], gamma),
255 * gamma_correct(blue[i], gamma), 255 * gamma_correct(blue[i], gamma),
255 * gamma_correct(white[i], gamma)); 255 * gamma_correct(white[i], gamma));
} }
sk6812_commit(); sk6812_commit(&(sk6812[s]));
} else { } else {
for(i = 0; i < num_modules; i++) { for(i = 0; i < num_modules; i++) {
sk6812_set_color(i, sk6812_set_color(&(sk6812[s]), i,
255 * gamma_correct(red[i], gamma), 255 * gamma_correct(red[i], gamma),
255 * gamma_correct(green[i], gamma), 255 * gamma_correct(green[i], gamma),
255 * gamma_correct(blue[i], gamma), 255 * gamma_correct(blue[i], gamma),
255 * gamma_correct(white[i], gamma)); 255 * gamma_correct(white[i], gamma));
} }
sk6812_commit(); sk6812_commit(&(sk6812[s]));
} }
} }
@ -274,9 +280,14 @@ int main(int argc, char **argv) {
printf("Idle for 1 second -> stopping updates.\n"); printf("Idle for 1 second -> stopping updates.\n");
for(i = 0; i < num_modules; i++) { for(i = 0; i < num_modules; i++) {
sk6812_fade_color(i, 0, 0, 0, 20); for(int s = 0; s < 3; s++) {
sk6812_fade_color(&(sk6812[s]), i, 0, 0, 0, 20);
}
}
for(int s = 0; s < 3; s++) {
sk6812_commit(&(sk6812[s]));
} }
sk6812_commit();
active = 0; active = 0;
} }
@ -289,7 +300,9 @@ int main(int argc, char **argv) {
sleep_until(nextFrame); sleep_until(nextFrame);
} }
sk6812_shutdown(); for(int i = 0; i < 3; i++) {
sk6812_shutdown(&(sk6812[i]));
}
// free arrays // free arrays
free(red); free(red);

View file

@ -23,18 +23,8 @@
#define ADD_COLOR 2 #define ADD_COLOR 2
#define SET_FADESTEP 3 #define SET_FADESTEP 3
struct __attribute__((__packed__)) SK6812Packet {
uint8_t action;
uint16_t module;
uint8_t data[4];
};
int sk6812_socket = -1;
struct __attribute__((__packed__)) SK6812Packet packetQueue[1024];
int queueIndex = 0;
// creates the socket needed for steering the LED strip // creates the socket needed for steering the LED strip
int sk6812_init(const char *host, unsigned short port) { int sk6812_init(struct sk6812_ctx *ctx, const char *host, unsigned short port) {
struct addrinfo hints; struct addrinfo hints;
struct addrinfo *result; struct addrinfo *result;
char portstr[6]; char portstr[6];
@ -52,14 +42,14 @@ int sk6812_init(const char *host, unsigned short port) {
return 1; return 1;
} }
sk6812_socket = socket(result->ai_family, result->ai_socktype, result->ai_protocol); ctx->socket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
if (sk6812_socket == -1) { if (ctx->socket == -1) {
perror("socket() failed"); perror("socket() failed");
freeaddrinfo(result); freeaddrinfo(result);
return 2; return 2;
} }
if (connect(sk6812_socket, result->ai_addr, result->ai_addrlen) == -1) { if (connect(ctx->socket, result->ai_addr, result->ai_addrlen) == -1) {
perror("connect() failed"); perror("connect() failed");
freeaddrinfo(result); freeaddrinfo(result);
return 3; return 3;
@ -67,54 +57,56 @@ int sk6812_init(const char *host, unsigned short port) {
freeaddrinfo(result); freeaddrinfo(result);
ctx->queueIndex = 0;
return 0; return 0;
} }
void sk6812_set_color(uint16_t module, uint8_t r, uint8_t g, uint8_t b, uint8_t w) { void sk6812_set_color(struct sk6812_ctx *ctx, uint16_t module, uint8_t r, uint8_t g, uint8_t b, uint8_t w) {
packetQueue[queueIndex].action = SET_COLOR; ctx->packetQueue[ctx->queueIndex].action = SET_COLOR;
packetQueue[queueIndex].module = htons(module); ctx->packetQueue[ctx->queueIndex].module = htons(module);
packetQueue[queueIndex].data[0] = r; ctx->packetQueue[ctx->queueIndex].data[0] = r;
packetQueue[queueIndex].data[1] = g; ctx->packetQueue[ctx->queueIndex].data[1] = g;
packetQueue[queueIndex].data[2] = b; ctx->packetQueue[ctx->queueIndex].data[2] = b;
packetQueue[queueIndex].data[3] = w; ctx->packetQueue[ctx->queueIndex].data[3] = w;
queueIndex++; ctx->queueIndex++;
} }
void sk6812_fade_color(uint16_t module, uint8_t r, uint8_t g, uint8_t b, uint8_t w) { void sk6812_fade_color(struct sk6812_ctx *ctx, uint16_t module, uint8_t r, uint8_t g, uint8_t b, uint8_t w) {
packetQueue[queueIndex].action = FADE_COLOR; ctx->packetQueue[ctx->queueIndex].action = FADE_COLOR;
packetQueue[queueIndex].module = htons(module); ctx->packetQueue[ctx->queueIndex].module = htons(module);
packetQueue[queueIndex].data[0] = r; ctx->packetQueue[ctx->queueIndex].data[0] = r;
packetQueue[queueIndex].data[1] = g; ctx->packetQueue[ctx->queueIndex].data[1] = g;
packetQueue[queueIndex].data[2] = b; ctx->packetQueue[ctx->queueIndex].data[2] = b;
packetQueue[queueIndex].data[3] = w; ctx->packetQueue[ctx->queueIndex].data[3] = w;
queueIndex++; ctx->queueIndex++;
} }
void sk6812_add_color(uint16_t module, uint8_t r, uint8_t g, uint8_t b, uint8_t w) { void sk6812_add_color(struct sk6812_ctx *ctx, uint16_t module, uint8_t r, uint8_t g, uint8_t b, uint8_t w) {
packetQueue[queueIndex].action = ADD_COLOR; ctx->packetQueue[ctx->queueIndex].action = ADD_COLOR;
packetQueue[queueIndex].module = htons(module); ctx->packetQueue[ctx->queueIndex].module = htons(module);
packetQueue[queueIndex].data[0] = r; ctx->packetQueue[ctx->queueIndex].data[0] = r;
packetQueue[queueIndex].data[1] = g; ctx->packetQueue[ctx->queueIndex].data[1] = g;
packetQueue[queueIndex].data[2] = b; ctx->packetQueue[ctx->queueIndex].data[2] = b;
packetQueue[queueIndex].data[3] = w; ctx->packetQueue[ctx->queueIndex].data[3] = w;
queueIndex++; ctx->queueIndex++;
} }
void sk6812_set_fadestep(uint8_t fadestep) { void sk6812_set_fadestep(struct sk6812_ctx *ctx, uint8_t fadestep) {
packetQueue[queueIndex].action = SET_FADESTEP; ctx->packetQueue[ctx->queueIndex].action = SET_FADESTEP;
packetQueue[queueIndex].data[0] = fadestep; ctx->packetQueue[ctx->queueIndex].data[0] = fadestep;
queueIndex++; ctx->queueIndex++;
} }
int sk6812_commit(void) { int sk6812_commit(struct sk6812_ctx *ctx) {
if(send(sk6812_socket, packetQueue, queueIndex * sizeof(struct SK6812Packet), 0) == -1) { if(send(ctx->socket, ctx->packetQueue, ctx->queueIndex * sizeof(struct SK6812Packet), 0) == -1) {
return 1; return 1;
} }
queueIndex = 0; ctx->queueIndex = 0;
return 0; return 0;
} }
void sk6812_shutdown() { void sk6812_shutdown(struct sk6812_ctx *ctx) {
close(sk6812_socket); close(ctx->socket);
} }

View file

@ -11,12 +11,25 @@
#ifndef SK6812_H #ifndef SK6812_H
#define SK6812_H #define SK6812_H
int sk6812_init(const char *host, unsigned short port); struct __attribute__((__packed__)) SK6812Packet {
void sk6812_set_color(uint16_t module, uint8_t r, uint8_t g, uint8_t b, uint8_t w); uint8_t action;
void sk6812_fade_color(uint16_t module, uint8_t r, uint8_t g, uint8_t b, uint8_t w); uint16_t module;
void sk6812_add_color(uint16_t module, uint8_t r, uint8_t g, uint8_t b, uint8_t w); uint8_t data[4];
void sk6812_set_fadestep(uint8_t fadestep); };
int sk6812_commit(void);
void sk6812_shutdown(void); struct sk6812_ctx {
int socket;
struct __attribute__((__packed__)) SK6812Packet packetQueue[1024];
int queueIndex;
};
int sk6812_init(struct sk6812_ctx *ctx, const char *host, unsigned short port);
void sk6812_set_color(struct sk6812_ctx *ctx, uint16_t module, uint8_t r, uint8_t g, uint8_t b, uint8_t w);
void sk6812_fade_color(struct sk6812_ctx *ctx, uint16_t module, uint8_t r, uint8_t g, uint8_t b, uint8_t w);
void sk6812_add_color(struct sk6812_ctx *ctx, uint16_t module, uint8_t r, uint8_t g, uint8_t b, uint8_t w);
void sk6812_set_fadestep(struct sk6812_ctx *ctx, uint8_t fadestep);
int sk6812_commit(struct sk6812_ctx *ctx);
void sk6812_shutdown(struct sk6812_ctx *ctx);
#endif // SK6812_H #endif // SK6812_H