#include #include #include #include #include #include #include #include "sk6812.h" #include "logger.h" #include "fader.h" #include "udpproto.h" #include "hat_spi.h" #include "utils.h" #define PORT 2703 #define NUM_MODULES 60 #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) { sleep_until(*nextFrame); *nextFrame += interval; } int parse_args(int argc, char **argv, struct CmdLineArgs *args) { int opt; unsigned long tmp_ul; unsigned long long tmp_ull; while((opt = getopt(argc, argv, "p:s:n:")) != -1) { switch(opt) { case 'p': // port tmp_ul = strtoul(optarg, NULL, 0); if(tmp_ul < 1 || tmp_ul > 65535) { LOG(LVL_ERR, "Port number is out of range [1..65535]: %ul", tmp_ul); return -1; } args->port = (uint16_t)tmp_ul; break; 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; } } return 0; } int main(int argc, char **argv) { struct hat_spi_ctx hat_ctx; struct CmdLineArgs args; double nextFrame = get_hires_time() + INTERVAL; // initialize logger logger_init(); // 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] [-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, args.num_modules*4) == -1) { LOG(LVL_FATAL, "Could not configure LED driver hat."); return 1; } // initialize sk6812 library 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 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, args.num_strips) == -1) { LOG(LVL_FATAL, "Could not initialize the UDP server."); return 1; } LOG(LVL_INFO, "Initialisation complete."); while(1) { udpproto_process(); 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(); for(int i = 0; i < args.num_strips; i++) { fader_shutdown(&(fader_ctx[i])); sk6812_shutdown(&(ctx[i])); } return 0; }