diff --git a/src/argbColors.c b/src/argbColors.c index 92aa595..106861f 100644 --- a/src/argbColors.c +++ b/src/argbColors.c @@ -7,20 +7,136 @@ #include #include +#include +#include +#include + #include "libusb-1.0/libusb.h" + #include "configuration.h" #include "device_setup.c" #include "init_terminate.c" #include "commands.c" +#include "iousb.c" +const char *argp_program_version = "argbColor 0.1"; +const char *argp_program_bug_address = "https://github.com/developersu/argbColors/issues/"; +static char doc[] = "TODO \ +TODO \ +there should be a well structed description one day"; + +static char args_doc[] = "ARG1"; +static struct argp_option options[] = { + {"sync", 's', "COMMAND", 0, "Bypass synchronized command" }, + {"separate", 'e', "COMMAND", 0, "Bypass separate command(s)" }, + {"color", 'c', "RGB_COLOR", 0, "Define color" }, + {"brightness", 'b', "VALUE", 0, "Define brightness" }, + {"quiet", 'q', 0, 0, "Don't produce any output" }, + { 0 } +}; + +struct arguments{ + char *args[1]; + int quiet; + char *sync; + char *separate; + char *color; + char *brightness; +}; + +static error_t parse_opt(int key, char *arg, struct argp_state *state){ + /* Get the input argument from argp_parse, which we + know is a pointer to our arguments structure. */ + struct arguments *arguments = state->input; + + switch (key){ + case 'q': + arguments->quiet = 1; + break; + case 's': + arguments->sync = arg; + break; + case 'e': + arguments->separate = arg; + break; + case 'c': + arguments->color = arg; + break; + case 'b': + arguments->brightness = arg; + break; + + case ARGP_KEY_ARG: + if (state->arg_num >= 1) /* Too many arguments. */ + argp_usage(state); + + arguments->args[state->arg_num] = arg; + + break; + + case ARGP_KEY_END: + if (state->arg_num < 1) /* Not enough arguments. */ + argp_usage(state); + break; + + default: + return ARGP_ERR_UNKNOWN; + } + return 0; +} + +static struct argp argp = { options, parse_opt, args_doc, doc }; + +int parse_color(char *color, unsigned int *red, unsigned int *green, unsigned int *blue ){ + printf("%s\n", color); + if (strlen(color) != 6) + return -1; + + regex_t reg; + regcomp(®, "^[0-9a-fA-F]{6}$", REG_EXTENDED); + if (regexec(®, color, 0, NULL, 0) != 0) + return -1; + regfree(®); + + if (sscanf(color, "%2x%2x%2x", red, green, blue) == 3) + return 0; + + return -1; +} + +int parse_brightness(){ + // TODO +} int main(int argc, char *argv[]) { - -// if (argc < 2){ -// printf("Missing arguments ('argbColors --help' for help)\n"); -// return -1; -// } - + + struct arguments arguments; + /* Default values. */ + arguments.quiet = 0; + arguments.sync = "-"; + arguments.separate = "-"; + arguments.color = "ff2fff"; + arguments.brightness = "5"; + + /* Parse our arguments; every option seen by parse_opt will + be reflected in arguments. */ + argp_parse(&argp, argc, argv, 0, 0, &arguments); + + printf("ARG1 = %s\nSYNC = %s\nSEPARATE = %s\nCOLOR = %s\n" + "QUIET = %s\n", + arguments.args[0], arguments.args[1], + arguments.sync, + arguments.separate, + arguments.color, + arguments.quiet ? "yes" : "no"); + + unsigned int red, green, blue; + + if (parse_color(arguments.color, &red, &green, &blue) != 0){ + printf("Color parse failure\n"); + return -1; + } + // - - - int ret = configure_device(); if (ret != 0){ printf("%s - %d\n", libusb_error_name(ret), ret); @@ -45,8 +161,7 @@ int main(int argc, char *argv[]) { return -1; } //*/ - // 0xff, 0x2f, 0xff <3 - if(staticColorSeparate(0xff, 0xff, 0x2b, 0x00)){ + if(staticColorSync(red, green, blue)){ printf("Command transfer failure\n"); libusb_close(dev_handle); return -1; diff --git a/src/commands.c b/src/commands.c index 313ed90..97cecce 100644 --- a/src/commands.c +++ b/src/commands.c @@ -1,5 +1,3 @@ -#include "iousb.c" - unsigned char end_transaction[64] = { 0xcc, 0x28, 0xff, 0x07, }; //cc28ff07 unsigned char end_alt_transaction[64] = { 0xcc, 0x28, 0x04, }; //cc2804