From 0ae3492b8251d52d0d4c0cd5e66c091f8cf25d88 Mon Sep 17 00:00:00 2001 From: Thomas Kolb Date: Sat, 30 Jun 2018 01:42:34 +0200 Subject: [PATCH] Triple LED Stripe test --- config.lua | 4 +-- flame.lua | 18 ++++++------ main.c | 33 ++++++++++++++------- sk6812.c | 86 +++++++++++++++++++++++++----------------------------- sk6812.h | 27 ++++++++++++----- 5 files changed, 93 insertions(+), 75 deletions(-) diff --git a/config.lua b/config.lua index 172ef95..4af0c4c 100644 --- a/config.lua +++ b/config.lua @@ -1,7 +1,7 @@ WS2801_HOST = "zybot" WS2801_PORT = 2703 -NUM_MODULES = 300 -CENTER_MODULE = 150 +NUM_MODULES = 54 +CENTER_MODULE = 54 GAMMA = 2.0 diff --git a/flame.lua b/flame.lua index 585b69c..e107520 100644 --- a/flame.lua +++ b/flame.lua @@ -3,12 +3,12 @@ OVERDRIVE = 1.70 EXPONENT = 1.5 W_EXPONENT = 2.2 -M = 1.0--1.7 -- mass +M = 2.3 -- mass D = 1 -- spring strength DAMPING = {} -- filled in init() -num_modules = 300 -center_module = 150 +num_modules = 54 +center_module = 54 num_masses = math.floor(num_modules/2) excitement_pos = 1 @@ -148,10 +148,10 @@ function periodic() b_tmp[i] = pos_b[i] w_tmp[i] = pos_w[i] - r_tmp[num_modules-i+1] = pos_r[i] - g_tmp[num_modules-i+1] = pos_g[i] - b_tmp[num_modules-i+1] = pos_b[i] - w_tmp[num_modules-i+1] = pos_w[i] + --r_tmp[num_modules-i+1] = pos_r[i] + --g_tmp[num_modules-i+1] = pos_g[i] + --b_tmp[num_modules-i+1] = pos_b[i] + --w_tmp[num_modules-i+1] = pos_w[i] --print(i, pos_r[i]) end @@ -172,7 +172,7 @@ function init(nmod, cmod) num_modules = nmod center_module = cmod - num_masses = math.floor(nmod/2) + num_masses = nmod --math.floor(nmod/2) excitement_pos = 1 for i = 1,nmod do @@ -198,7 +198,7 @@ function init(nmod, cmod) acc_b[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 -- don't use fading diff --git a/main.c b/main.c index 936fc8a..8f799a0 100644 --- a/main.c +++ b/main.c @@ -45,6 +45,8 @@ sample signal[BLOCK_LEN]; sem_t fftSemaphore; +struct sk6812_ctx sk6812[3]; + int running = 1; void* fft_thread(void *param) { @@ -220,13 +222,17 @@ int main(int argc, char **argv) { fadeStep = lua_tointeger(L, -1); useFading = fadeStep > 0; 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); } // initialize the WS2801 library 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 sem_init(&fftSemaphore, 0, 1); @@ -248,25 +254,25 @@ int main(int argc, char **argv) { lua_readdoublearray(L, green, num_modules); lua_readdoublearray(L, red, num_modules); - /*if((++frame & 1) == 0)*/ { + for(int s = 0; s < 3; s++) { if(useFading) { 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(green[i], gamma), 255 * gamma_correct(blue[i], gamma), 255 * gamma_correct(white[i], gamma)); } - sk6812_commit(); + sk6812_commit(&(sk6812[s])); } else { 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(green[i], gamma), 255 * gamma_correct(blue[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"); 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; } @@ -289,7 +300,9 @@ int main(int argc, char **argv) { sleep_until(nextFrame); } - sk6812_shutdown(); + for(int i = 0; i < 3; i++) { + sk6812_shutdown(&(sk6812[i])); + } // free arrays free(red); diff --git a/sk6812.c b/sk6812.c index cca8306..7780e88 100644 --- a/sk6812.c +++ b/sk6812.c @@ -23,18 +23,8 @@ #define ADD_COLOR 2 #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 -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 *result; char portstr[6]; @@ -52,14 +42,14 @@ int sk6812_init(const char *host, unsigned short port) { return 1; } - sk6812_socket = socket(result->ai_family, result->ai_socktype, result->ai_protocol); - if (sk6812_socket == -1) { + ctx->socket = socket(result->ai_family, result->ai_socktype, result->ai_protocol); + if (ctx->socket == -1) { perror("socket() failed"); freeaddrinfo(result); 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"); freeaddrinfo(result); return 3; @@ -67,54 +57,56 @@ int sk6812_init(const char *host, unsigned short port) { freeaddrinfo(result); + ctx->queueIndex = 0; + return 0; } -void sk6812_set_color(uint16_t module, uint8_t r, uint8_t g, uint8_t b, uint8_t w) { - packetQueue[queueIndex].action = SET_COLOR; - packetQueue[queueIndex].module = htons(module); - packetQueue[queueIndex].data[0] = r; - packetQueue[queueIndex].data[1] = g; - packetQueue[queueIndex].data[2] = b; - packetQueue[queueIndex].data[3] = w; - queueIndex++; +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++; } -void sk6812_fade_color(uint16_t module, uint8_t r, uint8_t g, uint8_t b, uint8_t w) { - packetQueue[queueIndex].action = FADE_COLOR; - packetQueue[queueIndex].module = htons(module); - packetQueue[queueIndex].data[0] = r; - packetQueue[queueIndex].data[1] = g; - packetQueue[queueIndex].data[2] = b; - packetQueue[queueIndex].data[3] = w; - queueIndex++; +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++; } -void sk6812_add_color(uint16_t module, uint8_t r, uint8_t g, uint8_t b, uint8_t w) { - packetQueue[queueIndex].action = ADD_COLOR; - packetQueue[queueIndex].module = htons(module); - packetQueue[queueIndex].data[0] = r; - packetQueue[queueIndex].data[1] = g; - packetQueue[queueIndex].data[2] = b; - packetQueue[queueIndex].data[3] = w; - queueIndex++; +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++; } -void sk6812_set_fadestep(uint8_t fadestep) { - packetQueue[queueIndex].action = SET_FADESTEP; - packetQueue[queueIndex].data[0] = fadestep; - queueIndex++; +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++; } -int sk6812_commit(void) { - if(send(sk6812_socket, packetQueue, queueIndex * sizeof(struct SK6812Packet), 0) == -1) { +int sk6812_commit(struct sk6812_ctx *ctx) { + if(send(ctx->socket, ctx->packetQueue, ctx->queueIndex * sizeof(struct SK6812Packet), 0) == -1) { return 1; } - queueIndex = 0; + ctx->queueIndex = 0; return 0; } -void sk6812_shutdown() { - close(sk6812_socket); +void sk6812_shutdown(struct sk6812_ctx *ctx) { + close(ctx->socket); } diff --git a/sk6812.h b/sk6812.h index ce9a793..0d46ba8 100644 --- a/sk6812.h +++ b/sk6812.h @@ -11,12 +11,25 @@ #ifndef SK6812_H #define SK6812_H -int sk6812_init(const char *host, unsigned short port); -void sk6812_set_color(uint16_t module, uint8_t r, uint8_t g, uint8_t b, uint8_t w); -void sk6812_fade_color(uint16_t module, uint8_t r, uint8_t g, uint8_t b, uint8_t w); -void sk6812_add_color(uint16_t module, uint8_t r, uint8_t g, uint8_t b, uint8_t w); -void sk6812_set_fadestep(uint8_t fadestep); -int sk6812_commit(void); -void sk6812_shutdown(void); +struct __attribute__((__packed__)) SK6812Packet { + uint8_t action; + uint16_t module; + uint8_t data[4]; +}; + +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