add 'verbose' option & move out unnecessary stuff
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed
This commit is contained in:
parent
84e029c794
commit
748b087cbf
6 changed files with 84 additions and 55 deletions
|
@ -13,7 +13,6 @@
|
|||
|
||||
#include "libusb-1.0/libusb.h"
|
||||
|
||||
|
||||
#include "configuration.h"
|
||||
#include "device_setup.c"
|
||||
#include "init_terminate.c"
|
||||
|
@ -28,18 +27,20 @@ static char doc[] = "-s COMMAND\n-e SOMETHING-SOMETHING";
|
|||
static char doc1[] = "apply sync or separate command\vFIXME LATER ON PLEASE";
|
||||
|
||||
static struct argp_option options[] = {
|
||||
{"sync", 's', "COMMAND", 0, "Synchronized command" },
|
||||
{"separate", 'e', "COMMAND", 0, "Separate command(s)" },
|
||||
{"sync", 's', 0, 0, "Synchronized command" },
|
||||
{"separate", 'e', 0, 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" },
|
||||
{"quiet", 'q', 0, 0, "Mute output" },
|
||||
{"verbose", 'v', 0, 0, "Verbose output" },
|
||||
{ 0 }
|
||||
};
|
||||
|
||||
struct arguments{
|
||||
char *args[1];
|
||||
int quiet;
|
||||
char *sync;
|
||||
char *separate;
|
||||
int sync;
|
||||
int separate;
|
||||
char *color;
|
||||
char *brightness;
|
||||
};
|
||||
|
@ -54,10 +55,10 @@ static error_t parse_opt(int key, char *arg, struct argp_state *state){
|
|||
arguments->quiet = 1;
|
||||
break;
|
||||
case 's':
|
||||
arguments->sync = arg;
|
||||
arguments->sync = 1;
|
||||
break;
|
||||
case 'e':
|
||||
arguments->separate = arg;
|
||||
arguments->separate = 1;
|
||||
break;
|
||||
case 'c':
|
||||
arguments->color = arg;
|
||||
|
@ -65,6 +66,19 @@ static error_t parse_opt(int key, char *arg, struct argp_state *state){
|
|||
case 'b':
|
||||
arguments->brightness = arg;
|
||||
break;
|
||||
case 'v':
|
||||
verbose_output = 1;
|
||||
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;
|
||||
}
|
||||
|
@ -74,7 +88,6 @@ static error_t parse_opt(int key, char *arg, struct argp_state *state){
|
|||
static struct argp argp = { options, parse_opt, doc, doc1 };
|
||||
|
||||
int parse_color(char *color, unsigned int *red, unsigned int *green, unsigned int *blue ){
|
||||
printf("%s\n", color);
|
||||
if (strlen(color) != 6)
|
||||
return -1;
|
||||
|
||||
|
@ -95,12 +108,14 @@ int sync_flow(char* directive){
|
|||
return wave1();
|
||||
else if (strcmp(directive, "wave2") == 0)
|
||||
return wave2();
|
||||
else if (strcmp(directive, "color") == 0)
|
||||
return staticColorSync(red, green, blue);
|
||||
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");
|
||||
printf("Command not recognized\n"
|
||||
"Possible values are: wave wave2 color off\n");
|
||||
|
||||
return staticColorSync(red, green, blue); // TODO: refactor; leave information block and nothing instead
|
||||
}
|
||||
}
|
||||
|
@ -112,8 +127,8 @@ int main(int argc, char *argv[]) {
|
|||
struct arguments arguments;
|
||||
/* Default values. */
|
||||
arguments.quiet = 0;
|
||||
arguments.sync = "-";
|
||||
arguments.separate = "-";
|
||||
arguments.sync = 0;
|
||||
arguments.separate = 0;
|
||||
arguments.color = "ff2fff";
|
||||
arguments.brightness = "5";
|
||||
|
||||
|
@ -124,6 +139,11 @@ int main(int argc, char *argv[]) {
|
|||
if (arguments.quiet)
|
||||
freopen("/dev/null", "a", stdout);
|
||||
|
||||
if (arguments.sync == arguments.separate == 1){
|
||||
printf("Only one option must be defined: '-s' or '-e'\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (parse_color(arguments.color, &red, &green, &blue) != 0){
|
||||
printf("Color parse failure\n");
|
||||
return -1;
|
||||
|
@ -133,29 +153,33 @@ int main(int argc, char *argv[]) {
|
|||
if (brightness > 5)
|
||||
brightness = 0;
|
||||
|
||||
printf("%s\n", arguments.sync);
|
||||
|
||||
// - - -
|
||||
int ret = configure_device();
|
||||
if (ret != 0){
|
||||
printf("%s - %d\n", libusb_error_name(ret), ret);
|
||||
return -1;
|
||||
}
|
||||
if (verbose_output)
|
||||
printf("Device configuration complete\n");
|
||||
|
||||
if (init_sequence()){
|
||||
printf("Initial sequence transfer failure\n");
|
||||
libusb_close(dev_handle);
|
||||
return -1;
|
||||
}
|
||||
// - - - - - - - - - - - - - -
|
||||
if (strcmp(arguments.sync, "-") != 0){ // Sync flow
|
||||
if (sync_flow(arguments.sync)){
|
||||
if (verbose_output)
|
||||
printf("Initialization sequence sent\n");
|
||||
|
||||
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
||||
|
||||
if (arguments.sync == 1){ // Sync flow
|
||||
if (sync_flow(arguments.args[0])){
|
||||
printf("Command transfer failure\n");
|
||||
libusb_close(dev_handle);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
else if (strcmp(arguments.separate, "-") != 0){ // Separate flow
|
||||
else if (arguments.separate == 0){ // Separate flow
|
||||
if(staticColorSync(red, green, blue)){ // TODO: FIX!
|
||||
printf("Command transfer failure\n");
|
||||
libusb_close(dev_handle);
|
||||
|
@ -189,6 +213,8 @@ int main(int argc, char *argv[]) {
|
|||
libusb_close(dev_handle);
|
||||
return -1;
|
||||
}
|
||||
if (verbose_output)
|
||||
printf("Termination sequence sent\n");
|
||||
|
||||
libusb_close(dev_handle);
|
||||
|
||||
|
|
|
@ -95,7 +95,7 @@ int staticColorSeparate(unsigned char brightness, unsigned char red, unsigned ch
|
|||
if(64 != writeUsb(end_transaction))
|
||||
return 1;
|
||||
|
||||
printf("\rStatic Color (Separate) sequence sent\n");
|
||||
printf("One color (Separate) applied\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -141,7 +141,7 @@ int turnOffBacklight(){
|
|||
if(64 != writeUsb(end_transaction))
|
||||
return 1;
|
||||
|
||||
printf("\rTurn off backlight (Separate) sequence sent\n");
|
||||
printf("Turn off backlight (Separate) applied\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -162,7 +162,7 @@ int turnOffBacklightSync(){
|
|||
if(64 != writeUsb(end_transaction))
|
||||
return 1;
|
||||
|
||||
printf("\rTurn off backlight (Sync) sequence sent\n");
|
||||
printf("Turn off backlight (Sync) applied\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -186,7 +186,7 @@ int wave1(){
|
|||
if(64 != writeUsb(end_alt_transaction))
|
||||
return 1;
|
||||
|
||||
printf("\rWave 1 (Sync) sequence sent\n");
|
||||
printf("Wave (Sync) applied\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -210,7 +210,7 @@ int wave2(){
|
|||
if(64 != writeUsb(end_alt_transaction))
|
||||
return 1;
|
||||
|
||||
printf("\rWave 2 (Sync) sequence sent\n");
|
||||
printf("Wave 2 (Sync) applied\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -231,7 +231,7 @@ int staticColorSync(unsigned char red, unsigned char green, unsigned char blue){
|
|||
if(64 != writeUsb(end_transaction))
|
||||
return 1;
|
||||
|
||||
printf("\rStatic Color (Sync) sequence sent\n");
|
||||
printf("One color (Sync) applied\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -307,7 +307,7 @@ int impulse(int intensity, unsigned char red, unsigned char green, unsigned char
|
|||
if(64 != writeUsb(end_transaction))
|
||||
return 1;
|
||||
|
||||
printf("\rImpulse (Separate) sequence sent\n");
|
||||
printf("Impulse (Separate) applied\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -407,7 +407,7 @@ int flash(int brightness, int frequency, unsigned char red, unsigned char green,
|
|||
if(64 != writeUsb(end_transaction))
|
||||
return 1;
|
||||
|
||||
printf("\rFlash (Separate) sequence sent\n");
|
||||
printf("Flash (Separate) applied\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -509,7 +509,7 @@ int doubleFlash(int brighness, int frequency, unsigned char red, unsigned char g
|
|||
if(64 != writeUsb(end_transaction))
|
||||
return 1;
|
||||
|
||||
printf("\rDouble flash (Separate) sequence sent\n");
|
||||
printf("Double flash (Separate) applied\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -605,7 +605,7 @@ int cycle(int intensity, int brightness){
|
|||
if(64 != writeUsb(end_transaction))
|
||||
return 1;
|
||||
|
||||
printf("\rImpulse (Separate) sequence sent\n");
|
||||
printf("Impulse (Separate) applied\n");
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -13,3 +13,4 @@ const uint16_t PID = 0x5711; // REAL
|
|||
unsigned int TIMEOUT = 2000;
|
||||
int counter;
|
||||
int limit;
|
||||
int verbose_output = 0;
|
|
@ -9,7 +9,9 @@ int findDevice(libusb_device *dev){
|
|||
}
|
||||
|
||||
if (desc.idVendor == VID && desc.idProduct == PID){
|
||||
if (verbose_output){
|
||||
printf("Device found: %04x:%04x\n", desc.idVendor, desc.idProduct);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
@ -76,7 +78,9 @@ int configure_device(){
|
|||
#endif
|
||||
// Check if device used by kernel drivers already
|
||||
ret = libusb_kernel_driver_active(dev_handle, 1);
|
||||
#ifdef DEBUG
|
||||
printf("Kernel driver is%s", ret == 0?" not active\n":"active and ");
|
||||
#endif
|
||||
|
||||
// Active? Let's try to get control
|
||||
if(ret != LIBUSB_ERROR_NOT_SUPPORTED && ret < 0){
|
||||
|
|
|
@ -68,8 +68,6 @@ int init_sequence(){
|
|||
if(64 != writeUsb(message))
|
||||
return 1;
|
||||
|
||||
printf("\rInitialization sequence sent\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -81,7 +79,5 @@ int terminate_sequence(){
|
|||
if(64 != writeUsb(message))
|
||||
return 1;
|
||||
|
||||
printf("\rTermination sequence sent\n");
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -2,7 +2,8 @@
|
|||
|
||||
int writeUsb(unsigned char *message){
|
||||
#ifndef DEBUG
|
||||
printf("\r%02d / %02d (write)", ++counter, limit);
|
||||
if (verbose_output)
|
||||
printf("\r[%02d / %02d] write - ", ++counter, limit);
|
||||
#endif
|
||||
#ifdef DEBUG
|
||||
print_array(message, 64);
|
||||
|
@ -17,7 +18,8 @@ int writeUsb(unsigned char *message){
|
|||
|
||||
int readUsb(){
|
||||
#ifndef DEBUG
|
||||
printf("\r%02d / %02d (read)", ++counter, limit);
|
||||
if (verbose_output)
|
||||
printf("\r[%02d / %02d] read - ", ++counter, limit);
|
||||
#endif
|
||||
|
||||
unsigned char buffer[64] = {};
|
||||
|
|
Loading…
Reference in a new issue