argbColors/src/argbColors.c

197 lines
5.1 KiB
C
Raw Normal View History

2025-01-24 01:25:30 +03:00
/**********************
Dmitry Isaenko
License: GNU GPL v.3
redrise.ru, github.com/developersu
2025, Russia
***********************/
#include <stdio.h>
#include <stdlib.h>
2025-01-28 23:24:25 +03:00
#include <string.h>
#include <argp.h>
#include <regex.h>
2025-01-24 01:25:30 +03:00
#include "libusb-1.0/libusb.h"
2025-01-28 23:24:25 +03:00
2025-01-27 01:55:17 +03:00
#include "configuration.h"
#include "device_setup.c"
2025-01-24 01:25:30 +03:00
#include "init_terminate.c"
#include "commands.c"
2025-01-28 23:24:25 +03:00
#include "iousb.c"
2025-01-30 02:09:42 +03:00
unsigned int red, green, blue;
2025-01-28 23:24:25 +03:00
const char *argp_program_version = "argbColor 0.1";
const char *argp_program_bug_address = "https://github.com/developersu/argbColors/issues/";
2025-01-30 02:09:42 +03:00
static char doc[] = "-s COMMAND\n-e SOMETHING-SOMETHING";
static char doc1[] = "apply sync or separate command\vFIXME LATER ON PLEASE";
2025-01-24 01:25:30 +03:00
2025-01-28 23:24:25 +03:00
static struct argp_option options[] = {
{"sync", 's', "COMMAND", 0, "Synchronized command" },
{"separate", 'e', "COMMAND", 0, "Separate command(s)" },
{"color", 'c', "RGB_COLOR", 0, "Define color (000000..ffffff)" },
{"brightness", 'b', "VALUE", 0, "Define brightness (0..5)" },
{"quiet", 'q', 0, 0, "Don't produce any output" },
2025-01-28 23:24:25 +03:00
{ 0 }
};
struct arguments{
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;
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}
2025-01-30 02:09:42 +03:00
static struct argp argp = { options, parse_opt, doc, doc1 };
2025-01-28 23:24:25 +03:00
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);
2025-01-26 19:08:12 +03:00
2025-01-28 23:24:25 +03:00
if (sscanf(color, "%2x%2x%2x", red, green, blue) == 3)
return 0;
return -1;
}
2025-01-30 02:09:42 +03:00
int sync_flow(char* directive){
if (strcmp(directive, "wave") == 0)
return wave1();
else if (strcmp(directive, "wave2") == 0)
return wave2();
else if (strcmp(directive, "off") == 0)
return turnOffBacklightSync();
else if (strcmp(directive, "off") == 0)
return staticColorSync(red, green, blue);
else{
printf("Command not recognized\n");
return staticColorSync(red, green, blue); // TODO: refactor; leave information block and nothing instead
}
2025-01-28 23:24:25 +03:00
}
2025-01-30 02:09:42 +03:00
2025-01-28 23:24:25 +03:00
int main(int argc, char *argv[]) {
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);
2025-01-30 02:09:42 +03:00
if (arguments.quiet)
freopen("/dev/null", "a", stdout);
2025-01-28 23:24:25 +03:00
if (parse_color(arguments.color, &red, &green, &blue) != 0){
printf("Color parse failure\n");
return -1;
}
int brightness = atoi(arguments.brightness);
if (brightness > 5)
brightness = 0;
printf("%s\n", arguments.sync);
2025-01-28 23:24:25 +03:00
// - - -
2025-01-27 01:55:17 +03:00
int ret = configure_device();
if (ret != 0){
printf("%s - %d\n", libusb_error_name(ret), ret);
return -1;
2025-01-26 00:54:18 +03:00
}
if (init_sequence()){
printf("Initial sequence transfer failure\n");
libusb_close(dev_handle);
return -1;
}
// - - - - - - - - - - - - - -
2025-01-30 02:09:42 +03:00
if (strcmp(arguments.sync, "-") != 0){ // Sync flow
if (sync_flow(arguments.sync)){
printf("Command transfer failure\n");
libusb_close(dev_handle);
return -1;
}
}
2025-01-30 02:09:42 +03:00
else if (strcmp(arguments.separate, "-") != 0){ // Separate flow
if(staticColorSync(red, green, blue)){ // TODO: FIX!
printf("Command transfer failure\n");
libusb_close(dev_handle);
return -1;
}
2025-01-30 02:09:42 +03:00
/*
if (cycle(0, 4)){
printf("Command transfer failure\n");
libusb_close(dev_handle);
return -1;
}
if (impulse(0, 0xff, 0x2f, 0xff)){
printf("Command transfer failure\n");
libusb_close(dev_handle);
return -1;
}
//*/
}
else{
2025-01-30 02:09:42 +03:00
if(staticColorSync(red, green, blue)){ // Executed neither for sync, nor for separate => set single color
printf("Command transfer failure\n");
libusb_close(dev_handle);
return -1;
}
}
2025-01-26 00:54:18 +03:00
if (terminate_sequence()){
printf("Termination sequence transfer failure\n");
libusb_close(dev_handle);
return -1;
}
libusb_close(dev_handle);
return 0;
2025-01-24 01:25:30 +03:00
}