134 lines
2.7 KiB
C
134 lines
2.7 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"
|
|
#include "hat_spi.h"
|
|
#include "utils.h"
|
|
|
|
#define PORT 2703
|
|
|
|
#define NUM_MODULES 60
|
|
#define GPIO_IDX 960
|
|
#define BASE_ADDR ((void*)0x40000000U)
|
|
|
|
#define INTERVAL 0.01
|
|
|
|
struct CmdLineArgs {
|
|
uint16_t port;
|
|
uint32_t num_modules;
|
|
};
|
|
|
|
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: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 '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 fader_ctx fader_ctx;
|
|
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;
|
|
|
|
// 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;
|
|
}
|
|
|
|
// 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) {
|
|
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;
|
|
}
|
|
|
|
// initialise the LED fader
|
|
if(fader_init(&fader_ctx, NUM_MODULES, &ctx) == -1) {
|
|
LOG(LVL_FATAL, "Could not initialize the LED fader.");
|
|
return 1;
|
|
}
|
|
|
|
// initialise the UDP server
|
|
if(udpproto_init(PORT, &fader_ctx, 1) == -1) {
|
|
LOG(LVL_FATAL, "Could not initialize the UDP server.");
|
|
return 1;
|
|
}
|
|
|
|
LOG(LVL_INFO, "Initialisation complete.");
|
|
|
|
while(1) {
|
|
udpproto_process();
|
|
fader_update(&fader_ctx);
|
|
wait_frame(&nextFrame, INTERVAL);
|
|
}
|
|
|
|
// shut down all modules
|
|
udpproto_shutdown();
|
|
fader_shutdown(&fader_ctx);
|
|
sk6812_shutdown(&ctx);
|
|
|
|
return 0;
|
|
}
|