Support multiple strips

This commit is contained in:
cfr34k 2018-08-04 23:48:45 +00:00
parent fc7173afff
commit 4734b8eaeb
2 changed files with 32 additions and 18 deletions

View File

@ -2,5 +2,5 @@
mkdir -p build
cd build
cmake ..
cmake -DCMAKE_BUILD_TYPE=Debug ..
make $@

View File

@ -17,14 +17,14 @@
#define PORT 2703
#define NUM_MODULES 60
#define GPIO_IDX 960
#define BASE_ADDR ((void*)0x40000000U)
#define NUM_STRIPS 4
#define INTERVAL 0.01
struct CmdLineArgs {
uint16_t port;
uint32_t num_modules;
uint32_t num_strips;
};
void wait_frame(double *nextFrame, double interval)
@ -39,7 +39,7 @@ int parse_args(int argc, char **argv, struct CmdLineArgs *args)
unsigned long tmp_ul;
unsigned long long tmp_ull;
while((opt = getopt(argc, argv, "p:a:g:n:")) != -1) {
while((opt = getopt(argc, argv, "p:s:n:")) != -1) {
switch(opt) {
case 'p': // port
tmp_ul = strtoul(optarg, NULL, 0);
@ -54,6 +54,9 @@ int parse_args(int argc, char **argv, struct CmdLineArgs *args)
case 'n': // number of modules
args->num_modules = strtoul(optarg, NULL, 0);
break;
case 's': // number of modules
args->num_strips = strtoul(optarg, NULL, 0);
break;
default:
return -1;
@ -65,8 +68,6 @@ int parse_args(int argc, char **argv, struct CmdLineArgs *args)
int main(int argc, char **argv)
{
struct sk6812_ctx ctx;
struct fader_ctx fader_ctx;
struct hat_spi_ctx hat_ctx;
struct CmdLineArgs args;
@ -79,39 +80,47 @@ int main(int argc, char **argv)
// default arguments
args.port = PORT;
args.num_modules = NUM_MODULES;
args.num_strips = NUM_STRIPS;
// parse command line arguments
if(parse_args(argc, argv, &args) == -1) {
LOG(LVL_FATAL, "Error while parsing command line arguments!\n\n"
"Usage: %s [-p port] [-a base_address] [-g gpio_index] [-n number_of_modules]\n\n", argv[0]);
"Usage: %s [-p port] [-s number_of_strips] [-n number_of_modules]\n\n", argv[0]);
return 1;
}
struct fader_ctx fader_ctx[args.num_strips];
struct sk6812_ctx ctx[args.num_strips];
// initialize SPI interface for hat
if(hat_spi_init(&hat_ctx) == -1) {
LOG(LVL_FATAL, "Could not initialize SPI interface.");
return 1;
}
if(hat_spi_config(&hat_ctx, NUM_MODULES*4) == -1) {
if(hat_spi_config(&hat_ctx, args.num_modules*4) == -1) {
LOG(LVL_FATAL, "Could not configure LED driver hat.");
return 1;
}
// initialize sk6812 library
if(sk6812_init(&ctx, &hat_ctx, 0, NUM_MODULES) == -1) {
LOG(LVL_FATAL, "Could not initialize SK6812 library.");
return 1;
for(int i = 0; i < args.num_strips; i++) {
if(sk6812_init(&(ctx[i]), &hat_ctx, i, args.num_modules) == -1) {
LOG(LVL_FATAL, "Could not initialize SK6812 library.");
return 1;
}
}
// initialise the LED fader
if(fader_init(&fader_ctx, NUM_MODULES, &ctx) == -1) {
LOG(LVL_FATAL, "Could not initialize the LED fader.");
return 1;
for(int i = 0; i < args.num_strips; i++) {
if(fader_init(&(fader_ctx[i]), args.num_modules, &(ctx[i])) == -1) {
LOG(LVL_FATAL, "Could not initialize the LED fader.");
return 1;
}
}
// initialise the UDP server
if(udpproto_init(PORT, &fader_ctx, 1) == -1) {
if(udpproto_init(PORT, fader_ctx, args.num_strips) == -1) {
LOG(LVL_FATAL, "Could not initialize the UDP server.");
return 1;
}
@ -120,14 +129,19 @@ int main(int argc, char **argv)
while(1) {
udpproto_process();
fader_update(&fader_ctx);
for(int i = 0; i < args.num_strips; i++) {
fader_update(&(fader_ctx[i]));
}
hat_spi_flush(&hat_ctx);
wait_frame(&nextFrame, INTERVAL);
}
// shut down all modules
udpproto_shutdown();
fader_shutdown(&fader_ctx);
sk6812_shutdown(&ctx);
for(int i = 0; i < args.num_strips; i++) {
fader_shutdown(&(fader_ctx[i]));
sk6812_shutdown(&(ctx[i]));
}
return 0;
}