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 "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"
|
||||||
|
@ -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 char doc1[] = "apply sync or separate command\vFIXME LATER ON PLEASE";
|
||||||
|
|
||||||
static struct argp_option options[] = {
|
static struct argp_option options[] = {
|
||||||
{"sync", 's', "COMMAND", 0, "Synchronized command" },
|
{"sync", 's', 0, 0, "Synchronized command" },
|
||||||
{"separate", 'e', "COMMAND", 0, "Separate command(s)" },
|
{"separate", 'e', 0, 0, "Separate command(s)" },
|
||||||
{"color", 'c', "RGB_COLOR", 0, "Define color (000000..ffffff)" },
|
{"color", 'c', "RGB_COLOR", 0, "Define color (000000..ffffff)" },
|
||||||
{"brightness", 'b', "VALUE", 0, "Define brightness (0..5)" },
|
{"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 }
|
{ 0 }
|
||||||
};
|
};
|
||||||
|
|
||||||
struct arguments{
|
struct arguments{
|
||||||
|
char *args[1];
|
||||||
int quiet;
|
int quiet;
|
||||||
char *sync;
|
int sync;
|
||||||
char *separate;
|
int separate;
|
||||||
char *color;
|
char *color;
|
||||||
char *brightness;
|
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. */
|
know is a pointer to our arguments structure. */
|
||||||
struct arguments *arguments = state->input;
|
struct arguments *arguments = state->input;
|
||||||
|
|
||||||
switch (key){
|
switch (key) {
|
||||||
case 'q':
|
case 'q':
|
||||||
arguments->quiet = 1;
|
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;
|
break;
|
||||||
case 's':
|
/*
|
||||||
arguments->sync = arg;
|
case ARGP_KEY_END:
|
||||||
|
if (state->arg_num < 1) // Not enough arguments
|
||||||
|
argp_usage (state);
|
||||||
break;
|
break;
|
||||||
case 'e':
|
*/
|
||||||
arguments->separate = arg;
|
default:
|
||||||
break;
|
return ARGP_ERR_UNKNOWN;
|
||||||
case 'c':
|
}
|
||||||
arguments->color = arg;
|
|
||||||
break;
|
|
||||||
case 'b':
|
|
||||||
arguments->brightness = arg;
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
return ARGP_ERR_UNKNOWN;
|
|
||||||
}
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static struct argp argp = { options, parse_opt, doc, doc1 };
|
static struct argp argp = { options, parse_opt, doc, doc1 };
|
||||||
|
|
||||||
int parse_color(char *color, unsigned int *red, unsigned int *green, unsigned int *blue ){
|
int parse_color(char *color, unsigned int *red, unsigned int *green, unsigned int *blue ){
|
||||||
printf("%s\n", color);
|
|
||||||
if (strlen(color) != 6)
|
if (strlen(color) != 6)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
|
@ -95,12 +108,14 @@ int sync_flow(char* directive){
|
||||||
return wave1();
|
return wave1();
|
||||||
else if (strcmp(directive, "wave2") == 0)
|
else if (strcmp(directive, "wave2") == 0)
|
||||||
return wave2();
|
return wave2();
|
||||||
|
else if (strcmp(directive, "color") == 0)
|
||||||
|
return staticColorSync(red, green, blue);
|
||||||
else if (strcmp(directive, "off") == 0)
|
else if (strcmp(directive, "off") == 0)
|
||||||
return turnOffBacklightSync();
|
return turnOffBacklightSync();
|
||||||
else if (strcmp(directive, "off") == 0)
|
|
||||||
return staticColorSync(red, green, blue);
|
|
||||||
else{
|
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
|
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;
|
struct arguments arguments;
|
||||||
/* Default values. */
|
/* Default values. */
|
||||||
arguments.quiet = 0;
|
arguments.quiet = 0;
|
||||||
arguments.sync = "-";
|
arguments.sync = 0;
|
||||||
arguments.separate = "-";
|
arguments.separate = 0;
|
||||||
arguments.color = "ff2fff";
|
arguments.color = "ff2fff";
|
||||||
arguments.brightness = "5";
|
arguments.brightness = "5";
|
||||||
|
|
||||||
|
@ -124,6 +139,11 @@ int main(int argc, char *argv[]) {
|
||||||
if (arguments.quiet)
|
if (arguments.quiet)
|
||||||
freopen("/dev/null", "a", stdout);
|
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){
|
if (parse_color(arguments.color, &red, &green, &blue) != 0){
|
||||||
printf("Color parse failure\n");
|
printf("Color parse failure\n");
|
||||||
return -1;
|
return -1;
|
||||||
|
@ -133,29 +153,33 @@ int main(int argc, char *argv[]) {
|
||||||
if (brightness > 5)
|
if (brightness > 5)
|
||||||
brightness = 0;
|
brightness = 0;
|
||||||
|
|
||||||
printf("%s\n", arguments.sync);
|
|
||||||
|
|
||||||
// - - -
|
// - - -
|
||||||
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);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
if (verbose_output)
|
||||||
|
printf("Device configuration complete\n");
|
||||||
|
|
||||||
if (init_sequence()){
|
if (init_sequence()){
|
||||||
printf("Initial sequence transfer failure\n");
|
printf("Initial sequence transfer failure\n");
|
||||||
libusb_close(dev_handle);
|
libusb_close(dev_handle);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
// - - - - - - - - - - - - - -
|
if (verbose_output)
|
||||||
if (strcmp(arguments.sync, "-") != 0){ // Sync flow
|
printf("Initialization sequence sent\n");
|
||||||
if (sync_flow(arguments.sync)){
|
|
||||||
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
||||||
|
|
||||||
|
if (arguments.sync == 1){ // Sync flow
|
||||||
|
if (sync_flow(arguments.args[0])){
|
||||||
printf("Command transfer failure\n");
|
printf("Command transfer failure\n");
|
||||||
libusb_close(dev_handle);
|
libusb_close(dev_handle);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (strcmp(arguments.separate, "-") != 0){ // Separate flow
|
else if (arguments.separate == 0){ // Separate flow
|
||||||
if(staticColorSync(red, green, blue)){ // TODO: FIX!
|
if(staticColorSync(red, green, blue)){ // TODO: FIX!
|
||||||
printf("Command transfer failure\n");
|
printf("Command transfer failure\n");
|
||||||
libusb_close(dev_handle);
|
libusb_close(dev_handle);
|
||||||
|
@ -189,7 +213,9 @@ int main(int argc, char *argv[]) {
|
||||||
libusb_close(dev_handle);
|
libusb_close(dev_handle);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
if (verbose_output)
|
||||||
|
printf("Termination sequence sent\n");
|
||||||
|
|
||||||
libusb_close(dev_handle);
|
libusb_close(dev_handle);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
|
@ -95,7 +95,7 @@ int staticColorSeparate(unsigned char brightness, unsigned char red, unsigned ch
|
||||||
if(64 != writeUsb(end_transaction))
|
if(64 != writeUsb(end_transaction))
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
printf("\rStatic Color (Separate) sequence sent\n");
|
printf("One color (Separate) applied\n");
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -141,7 +141,7 @@ int turnOffBacklight(){
|
||||||
if(64 != writeUsb(end_transaction))
|
if(64 != writeUsb(end_transaction))
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
printf("\rTurn off backlight (Separate) sequence sent\n");
|
printf("Turn off backlight (Separate) applied\n");
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -162,7 +162,7 @@ int turnOffBacklightSync(){
|
||||||
if(64 != writeUsb(end_transaction))
|
if(64 != writeUsb(end_transaction))
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
printf("\rTurn off backlight (Sync) sequence sent\n");
|
printf("Turn off backlight (Sync) applied\n");
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -186,7 +186,7 @@ int wave1(){
|
||||||
if(64 != writeUsb(end_alt_transaction))
|
if(64 != writeUsb(end_alt_transaction))
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
printf("\rWave 1 (Sync) sequence sent\n");
|
printf("Wave (Sync) applied\n");
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -210,7 +210,7 @@ int wave2(){
|
||||||
if(64 != writeUsb(end_alt_transaction))
|
if(64 != writeUsb(end_alt_transaction))
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
printf("\rWave 2 (Sync) sequence sent\n");
|
printf("Wave 2 (Sync) applied\n");
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -231,7 +231,7 @@ int staticColorSync(unsigned char red, unsigned char green, unsigned char blue){
|
||||||
if(64 != writeUsb(end_transaction))
|
if(64 != writeUsb(end_transaction))
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
printf("\rStatic Color (Sync) sequence sent\n");
|
printf("One color (Sync) applied\n");
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -307,7 +307,7 @@ int impulse(int intensity, unsigned char red, unsigned char green, unsigned char
|
||||||
if(64 != writeUsb(end_transaction))
|
if(64 != writeUsb(end_transaction))
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
printf("\rImpulse (Separate) sequence sent\n");
|
printf("Impulse (Separate) applied\n");
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -407,7 +407,7 @@ int flash(int brightness, int frequency, unsigned char red, unsigned char green,
|
||||||
if(64 != writeUsb(end_transaction))
|
if(64 != writeUsb(end_transaction))
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
printf("\rFlash (Separate) sequence sent\n");
|
printf("Flash (Separate) applied\n");
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -509,7 +509,7 @@ int doubleFlash(int brighness, int frequency, unsigned char red, unsigned char g
|
||||||
if(64 != writeUsb(end_transaction))
|
if(64 != writeUsb(end_transaction))
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
printf("\rDouble flash (Separate) sequence sent\n");
|
printf("Double flash (Separate) applied\n");
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -605,7 +605,7 @@ int cycle(int intensity, int brightness){
|
||||||
if(64 != writeUsb(end_transaction))
|
if(64 != writeUsb(end_transaction))
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
printf("\rImpulse (Separate) sequence sent\n");
|
printf("Impulse (Separate) applied\n");
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
|
@ -12,4 +12,5 @@ const uint16_t PID = 0x5711; // REAL
|
||||||
//const uint16_t PID = 0x2101;
|
//const uint16_t PID = 0x2101;
|
||||||
unsigned int TIMEOUT = 2000;
|
unsigned int TIMEOUT = 2000;
|
||||||
int counter;
|
int counter;
|
||||||
int limit;
|
int limit;
|
||||||
|
int verbose_output = 0;
|
|
@ -9,7 +9,9 @@ int findDevice(libusb_device *dev){
|
||||||
}
|
}
|
||||||
|
|
||||||
if (desc.idVendor == VID && desc.idProduct == PID){
|
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;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -76,8 +78,10 @@ int configure_device(){
|
||||||
#endif
|
#endif
|
||||||
// Check if device used by kernel drivers already
|
// Check if device used by kernel drivers already
|
||||||
ret = libusb_kernel_driver_active(dev_handle, 1);
|
ret = libusb_kernel_driver_active(dev_handle, 1);
|
||||||
|
#ifdef DEBUG
|
||||||
printf("Kernel driver is%s", ret == 0?" not active\n":"active and ");
|
printf("Kernel driver is%s", ret == 0?" not active\n":"active and ");
|
||||||
|
#endif
|
||||||
|
|
||||||
// Active? Let's try to get control
|
// Active? Let's try to get control
|
||||||
if(ret != LIBUSB_ERROR_NOT_SUPPORTED && ret < 0){
|
if(ret != LIBUSB_ERROR_NOT_SUPPORTED && ret < 0){
|
||||||
ret = libusb_detach_kernel_driver(dev_handle, 1);
|
ret = libusb_detach_kernel_driver(dev_handle, 1);
|
||||||
|
|
|
@ -68,8 +68,6 @@ int init_sequence(){
|
||||||
if(64 != writeUsb(message))
|
if(64 != writeUsb(message))
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
printf("\rInitialization sequence sent\n");
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -80,8 +78,6 @@ int terminate_sequence(){
|
||||||
unsigned char message[64] = { 0xcc, 0x47, 0x01, }; //cc4701
|
unsigned char message[64] = { 0xcc, 0x47, 0x01, }; //cc4701
|
||||||
if(64 != writeUsb(message))
|
if(64 != writeUsb(message))
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
printf("\rTermination sequence sent\n");
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
|
@ -2,7 +2,8 @@
|
||||||
|
|
||||||
int writeUsb(unsigned char *message){
|
int writeUsb(unsigned char *message){
|
||||||
#ifndef DEBUG
|
#ifndef DEBUG
|
||||||
printf("\r%02d / %02d (write)", ++counter, limit);
|
if (verbose_output)
|
||||||
|
printf("\r[%02d / %02d] write - ", ++counter, limit);
|
||||||
#endif
|
#endif
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
print_array(message, 64);
|
print_array(message, 64);
|
||||||
|
@ -17,7 +18,8 @@ int writeUsb(unsigned char *message){
|
||||||
|
|
||||||
int readUsb(){
|
int readUsb(){
|
||||||
#ifndef DEBUG
|
#ifndef DEBUG
|
||||||
printf("\r%02d / %02d (read)", ++counter, limit);
|
if (verbose_output)
|
||||||
|
printf("\r[%02d / %02d] read - ", ++counter, limit);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
unsigned char buffer[64] = {};
|
unsigned char buffer[64] = {};
|
||||||
|
|
Loading…
Reference in a new issue