sk6812d/src/main.c

122 lines
2.4 KiB
C

#include <math.h>
#include <time.h>
#include <string.h>
#include <errno.h>
#include <stdint.h>
#include <stdlib.h>
#include <unistd.h>
#include "sk6812.h"
#include "logger.h"
#include "fader.h"
#include "udpproto.h"
#define PORT 2703
#define NUM_MODULES 60
#define GPIO_IDX 960
#define BASE_ADDR ((void*)0x40000000U)
struct CmdLineArgs {
uint16_t port;
void *base_addr;
uint32_t gpio_idx;
uint32_t num_modules;
};
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:a:g: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 'a': // base address
tmp_ull = strtoull(optarg, NULL, 0);
args->base_addr = (void*)tmp_ull;
break;
case 'g': // gpio
args->gpio_idx = strtoul(optarg, NULL, 0);
break;
case 'n': // number of modules
args->num_modules = strtoul(optarg, NULL, 0);
break;
default:
return -1;
}
}
return 0;
}
int main(int argc, char **argv)
{
struct sk6812_ctx ctx;
struct CmdLineArgs args;
// initialize logger
logger_init();
// default arguments
args.port = PORT;
args.base_addr = BASE_ADDR;
args.gpio_idx = GPIO_IDX;
args.num_modules = NUM_MODULES;
// 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]);
return 1;
}
// initialise the UDP server
if(udpproto_init(PORT) == -1) {
LOG(LVL_FATAL, "Could not initialize the UDP server.");
return 1;
}
// initialize ws2801 library
if(sk6812_init(&ctx, GPIO_IDX, BASE_ADDR, NUM_MODULES) == -1) {
LOG(LVL_FATAL, "Could not initialize SK6812 library.");
return 1;
}
// initialise the LED fader
if(fader_init(NUM_MODULES, &ctx) == -1) {
LOG(LVL_FATAL, "Could not initialize the LED fader.");
return 1;
}
LOG(LVL_INFO, "Initialisation complete.");
while(1) {
udpproto_process();
fader_update();
fader_wait_frame();
}
// shut down all modules
fader_shutdown();
sk6812_shutdown(&ctx);
udpproto_shutdown();
return 0;
}