sk6812d/src/main.c

148 lines
3.1 KiB
C
Raw Normal View History

2018-06-13 21:51:30 +02:00
#include <math.h>
#include <time.h>
#include <string.h>
#include <errno.h>
#include <stdint.h>
2018-06-23 19:42:19 +02:00
#include <stdlib.h>
#include <unistd.h>
2018-06-13 21:51:30 +02:00
#include "sk6812.h"
2018-06-13 21:51:30 +02:00
#include "logger.h"
#include "fader.h"
#include "udpproto.h"
2018-07-31 23:09:41 +02:00
#include "hat_spi.h"
#include "utils.h"
2018-06-13 21:51:30 +02:00
#define PORT 2703
2018-06-23 19:42:19 +02:00
#define NUM_MODULES 60
2018-08-05 01:48:45 +02:00
#define NUM_STRIPS 4
2018-06-13 21:51:30 +02:00
#define INTERVAL 0.01
2018-06-23 19:42:19 +02:00
struct CmdLineArgs {
uint16_t port;
uint32_t num_modules;
2018-08-05 01:48:45 +02:00
uint32_t num_strips;
2018-06-23 19:42:19 +02:00
};
void wait_frame(double *nextFrame, double interval)
{
sleep_until(*nextFrame);
*nextFrame += interval;
}
2018-06-23 19:42:19 +02:00
int parse_args(int argc, char **argv, struct CmdLineArgs *args)
{
int opt;
unsigned long tmp_ul;
unsigned long long tmp_ull;
2018-08-05 01:48:45 +02:00
while((opt = getopt(argc, argv, "p:s:n:")) != -1) {
2018-06-23 19:42:19 +02:00
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;
2018-08-05 01:48:45 +02:00
case 's': // number of modules
args->num_strips = strtoul(optarg, NULL, 0);
break;
2018-06-23 19:42:19 +02:00
default:
return -1;
}
}
return 0;
}
int main(int argc, char **argv)
2018-06-13 21:51:30 +02:00
{
2018-07-31 23:09:41 +02:00
struct hat_spi_ctx hat_ctx;
2018-06-23 19:42:19 +02:00
struct CmdLineArgs args;
double nextFrame = get_hires_time() + INTERVAL;
2018-06-23 19:42:19 +02:00
// initialize logger
2018-06-13 21:51:30 +02:00
logger_init();
2018-06-23 19:42:19 +02:00
// default arguments
args.port = PORT;
args.num_modules = NUM_MODULES;
2018-08-05 01:48:45 +02:00
args.num_strips = NUM_STRIPS;
2018-06-23 19:42:19 +02:00
// parse command line arguments
if(parse_args(argc, argv, &args) == -1) {
LOG(LVL_FATAL, "Error while parsing command line arguments!\n\n"
2018-08-05 01:48:45 +02:00
"Usage: %s [-p port] [-s number_of_strips] [-n number_of_modules]\n\n", argv[0]);
2018-06-23 19:42:19 +02:00
return 1;
}
2018-08-05 01:48:45 +02:00
struct fader_ctx fader_ctx[args.num_strips];
struct sk6812_ctx ctx[args.num_strips];
2018-07-31 23:09:41 +02:00
// initialize SPI interface for hat
if(hat_spi_init(&hat_ctx) == -1) {
LOG(LVL_FATAL, "Could not initialize SPI interface.");
return 1;
}
2018-08-05 01:48:45 +02:00
if(hat_spi_config(&hat_ctx, args.num_modules*4) == -1) {
2018-07-31 23:09:41 +02:00
LOG(LVL_FATAL, "Could not configure LED driver hat.");
return 1;
}
// initialize sk6812 library
2018-08-05 01:48:45 +02:00
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;
}
2018-06-13 21:51:30 +02:00
}
// initialise the LED fader
2018-08-05 01:48:45 +02:00
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;
}
2018-06-13 21:51:30 +02:00
}
// initialise the UDP server
2018-08-05 01:48:45 +02:00
if(udpproto_init(PORT, fader_ctx, args.num_strips) == -1) {
LOG(LVL_FATAL, "Could not initialize the UDP server.");
return 1;
}
2018-06-13 21:51:30 +02:00
LOG(LVL_INFO, "Initialisation complete.");
while(1) {
udpproto_process();
2018-08-05 01:48:45 +02:00
for(int i = 0; i < args.num_strips; i++) {
fader_update(&(fader_ctx[i]));
}
hat_spi_flush(&hat_ctx);
wait_frame(&nextFrame, INTERVAL);
2018-06-13 21:51:30 +02:00
}
// shut down all modules
udpproto_shutdown();
2018-08-05 01:48:45 +02:00
for(int i = 0; i < args.num_strips; i++) {
fader_shutdown(&(fader_ctx[i]));
sk6812_shutdown(&(ctx[i]));
}
2018-06-13 21:51:30 +02:00
return 0;
}