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-02-02 01:16:14 +03:00
|
|
|
#include "argbColors.h"
|
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
|
|
|
int parse_color(char *color, unsigned int *red, unsigned int *green, unsigned int *blue ){
|
|
|
|
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(®);
|
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();
|
2025-02-01 14:11:41 +03:00
|
|
|
else if (strcmp(directive, "color") == 0)
|
|
|
|
return staticColorSync(red, green, blue);
|
2025-01-30 02:09:42 +03:00
|
|
|
else if (strcmp(directive, "off") == 0)
|
|
|
|
return turnOffBacklightSync();
|
|
|
|
else{
|
2025-02-01 14:11:41 +03:00
|
|
|
printf("Command not recognized\n"
|
2025-02-02 01:16:14 +03:00
|
|
|
"Possible values are: color wave wave2 off\n");
|
2025-02-01 14:11:41 +03:00
|
|
|
|
2025-01-30 02:09:42 +03:00
|
|
|
return staticColorSync(red, green, blue); // TODO: refactor; leave information block and nothing instead
|
|
|
|
}
|
2025-01-28 23:24:25 +03:00
|
|
|
}
|
|
|
|
|
2025-02-02 01:16:14 +03:00
|
|
|
int separate_flow(){
|
|
|
|
|
|
|
|
}
|
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;
|
2025-02-01 14:11:41 +03:00
|
|
|
arguments.sync = 0;
|
|
|
|
arguments.separate = 0;
|
2025-01-28 23:24:25 +03:00
|
|
|
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-02-02 01:16:14 +03:00
|
|
|
if (arguments.sync && arguments.separate){
|
|
|
|
printf("Only one option must be defined: '-s' or '-e'\n");
|
2025-02-01 14:11:41 +03:00
|
|
|
return -1;
|
|
|
|
}
|
2025-02-02 01:16:14 +03:00
|
|
|
/*
|
|
|
|
if (arguments.separate && ){
|
|
|
|
printf("In separate flow each zone has to be defined: '-z1=color -z2=impulse ... -z6=color'\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
*/
|
2025-01-28 23:24:25 +03:00
|
|
|
if (parse_color(arguments.color, &red, &green, &blue) != 0){
|
|
|
|
printf("Color parse failure\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2025-02-02 01:16:14 +03:00
|
|
|
|
|
|
|
int brightness = atoi(arguments.brightness);
|
|
|
|
if (brightness > 5)
|
|
|
|
brightness = 0;
|
2025-01-29 23:57:32 +03:00
|
|
|
|
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
|
|
|
}
|
2025-02-01 14:11:41 +03:00
|
|
|
if (verbose_output)
|
|
|
|
printf("Device configuration complete\n");
|
2025-01-26 00:54:18 +03:00
|
|
|
|
|
|
|
if (init_sequence()){
|
|
|
|
printf("Initial sequence transfer failure\n");
|
|
|
|
libusb_close(dev_handle);
|
|
|
|
return -1;
|
|
|
|
}
|
2025-02-01 14:11:41 +03:00
|
|
|
if (verbose_output)
|
|
|
|
printf("Initialization sequence sent\n");
|
|
|
|
|
|
|
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
|
|
|
|
|
|
|
if (arguments.sync == 1){ // Sync flow
|
|
|
|
if (sync_flow(arguments.args[0])){
|
2025-01-29 23:57:32 +03:00
|
|
|
printf("Command transfer failure\n");
|
|
|
|
libusb_close(dev_handle);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2025-02-01 14:11:41 +03:00
|
|
|
else if (arguments.separate == 0){ // Separate flow
|
2025-01-30 02:09:42 +03:00
|
|
|
if(staticColorSync(red, green, blue)){ // TODO: FIX!
|
2025-01-29 23:57:32 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
//*/
|
2025-01-29 23:57:32 +03:00
|
|
|
}
|
|
|
|
else{
|
2025-01-30 02:09:42 +03:00
|
|
|
if(staticColorSync(red, green, blue)){ // Executed neither for sync, nor for separate => set single color
|
2025-01-29 23:57:32 +03:00
|
|
|
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;
|
|
|
|
}
|
2025-02-01 14:11:41 +03:00
|
|
|
if (verbose_output)
|
|
|
|
printf("Termination sequence sent\n");
|
|
|
|
|
2025-01-26 00:54:18 +03:00
|
|
|
libusb_close(dev_handle);
|
|
|
|
|
|
|
|
return 0;
|
2025-01-24 01:25:30 +03:00
|
|
|
}
|