Add initial argp support
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed

This commit is contained in:
Dmitry Isaenko 2025-01-28 23:24:25 +03:00
parent dc2012b374
commit e2febb5686
2 changed files with 123 additions and 10 deletions

View file

@ -7,20 +7,136 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include <argp.h>
#include <regex.h>
#include "libusb-1.0/libusb.h" #include "libusb-1.0/libusb.h"
#include "configuration.h" #include "configuration.h"
#include "device_setup.c" #include "device_setup.c"
#include "init_terminate.c" #include "init_terminate.c"
#include "commands.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(&reg, "^[0-9a-fA-F]{6}$", REG_EXTENDED);
if (regexec(&reg, color, 0, NULL, 0) != 0)
return -1;
regfree(&reg);
if (sscanf(color, "%2x%2x%2x", red, green, blue) == 3)
return 0;
return -1;
}
int parse_brightness(){
// TODO
}
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
// if (argc < 2){ struct arguments arguments;
// printf("Missing arguments ('argbColors --help' for help)\n"); /* Default values. */
// return -1; 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(); int ret = configure_device();
if (ret != 0){ if (ret != 0){
printf("%s - %d\n", libusb_error_name(ret), ret); printf("%s - %d\n", libusb_error_name(ret), ret);
@ -45,8 +161,7 @@ int main(int argc, char *argv[]) {
return -1; return -1;
} }
//*/ //*/
// 0xff, 0x2f, 0xff <3 if(staticColorSync(red, green, blue)){
if(staticColorSeparate(0xff, 0xff, 0x2b, 0x00)){
printf("Command transfer failure\n"); printf("Command transfer failure\n");
libusb_close(dev_handle); libusb_close(dev_handle);
return -1; return -1;

View file

@ -1,5 +1,3 @@
#include "iousb.c"
unsigned char end_transaction[64] = { 0xcc, 0x28, 0xff, 0x07, }; //cc28ff07 unsigned char end_transaction[64] = { 0xcc, 0x28, 0xff, 0x07, }; //cc28ff07
unsigned char end_alt_transaction[64] = { 0xcc, 0x28, 0x04, }; //cc2804 unsigned char end_alt_transaction[64] = { 0xcc, 0x28, 0x04, }; //cc2804