diff --git a/src/argbColors.c b/src/argbColors.c index a681240..cb19271 100644 --- a/src/argbColors.c +++ b/src/argbColors.c @@ -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; }; @@ -49,32 +50,44 @@ static error_t parse_opt(int key, char *arg, struct argp_state *state){ know is a pointer to our arguments structure. */ struct arguments *arguments = state->input; - switch (key){ - case 'q': - arguments->quiet = 1; + switch (key) { + case 'q': + arguments->quiet = 1; + break; + case 's': + arguments->sync = 1; + break; + case 'e': + arguments->separate = 1; + break; + case 'c': + arguments->color = arg; + break; + 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 's': - arguments->sync = arg; +/* + case ARGP_KEY_END: + if (state->arg_num < 1) // Not enough arguments + argp_usage (state); break; - case 'e': - arguments->separate = arg; - break; - case 'c': - arguments->color = arg; - break; - case 'b': - arguments->brightness = arg; - break; - default: - return ARGP_ERR_UNKNOWN; - } +*/ + default: + return ARGP_ERR_UNKNOWN; + } return 0; } 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,7 +213,9 @@ int main(int argc, char *argv[]) { libusb_close(dev_handle); return -1; } - + if (verbose_output) + printf("Termination sequence sent\n"); + libusb_close(dev_handle); return 0; diff --git a/src/commands.c b/src/commands.c index 97cecce..37b2a93 100644 --- a/src/commands.c +++ b/src/commands.c @@ -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; } \ No newline at end of file diff --git a/src/configuration.h b/src/configuration.h index 0de17ea..e9709b8 100644 --- a/src/configuration.h +++ b/src/configuration.h @@ -12,4 +12,5 @@ const uint16_t PID = 0x5711; // REAL //const uint16_t PID = 0x2101; unsigned int TIMEOUT = 2000; int counter; -int limit; \ No newline at end of file +int limit; +int verbose_output = 0; \ No newline at end of file diff --git a/src/device_setup.c b/src/device_setup.c index 8eb61cd..23391b8 100644 --- a/src/device_setup.c +++ b/src/device_setup.c @@ -9,7 +9,9 @@ int findDevice(libusb_device *dev){ } if (desc.idVendor == VID && desc.idProduct == PID){ - printf("Device found: %04x:%04x\n", desc.idVendor, desc.idProduct); + if (verbose_output){ + printf("Device found: %04x:%04x\n", desc.idVendor, desc.idProduct); + } return 1; } @@ -76,8 +78,10 @@ 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){ ret = libusb_detach_kernel_driver(dev_handle, 1); diff --git a/src/init_terminate.c b/src/init_terminate.c index fcbb29a..80763c8 100644 --- a/src/init_terminate.c +++ b/src/init_terminate.c @@ -68,8 +68,6 @@ int init_sequence(){ if(64 != writeUsb(message)) return 1; - printf("\rInitialization sequence sent\n"); - return 0; } @@ -80,8 +78,6 @@ int terminate_sequence(){ unsigned char message[64] = { 0xcc, 0x47, 0x01, }; //cc4701 if(64 != writeUsb(message)) return 1; - - printf("\rTermination sequence sent\n"); return 0; } \ No newline at end of file diff --git a/src/iousb.c b/src/iousb.c index 39e26aa..15632db 100644 --- a/src/iousb.c +++ b/src/iousb.c @@ -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] = {};