Add speed option to CLI

This commit is contained in:
Cyril Bosselut 2024-01-31 17:06:29 +00:00 committed by Adam Honse
parent 479dbe979d
commit 5f538260a2

70
cli.cpp
View file

@ -28,6 +28,7 @@ using namespace std::chrono_literals;
static std::string profile_save_filename = "";
const unsigned int brightness_percentage = 100;
const unsigned int speed_percentage = 100;
enum
{
@ -47,6 +48,7 @@ struct DeviceOptions
int zone = -1;
std::vector<std::tuple<unsigned char, unsigned char, unsigned char>> colors;
std::string mode;
unsigned int speed = 100;
unsigned int brightness = 100;
unsigned int size;
bool random_colors = false;
@ -397,7 +399,8 @@ void OptionHelp()
help_text += " If there are more LEDs than colors given, the last color will be applied to the remaining LEDs\n";
help_text += "-m, --mode [breathing | static | ...] Sets the mode to be applied, check --list-devices to see which modes are supported on your device\n";
help_text += "-b, --brightness [0-100] Sets the brightness as a percentage if the mode supports brightness\n";
help_text += "-s, --size [0-N] Sets the new size of the specified device zone.\n";
help_text += "-s, --speed [0-100] Sets the speed as a percentage if the mode supports speed\n";
help_text += "-sz, --size [0-N] Sets the new size of the specified device zone.\n";
help_text += " Must be specified after specifying a zone.\n";
help_text += " If the specified size is out of range, or the zone does not offer resizing capability, the size will not be changed\n";
help_text += "-V, --version Display version and software build information\n";
@ -718,6 +721,42 @@ bool OptionMode(std::vector<DeviceOptions>* current_devices, std::string argumen
return found;
}
bool OptionSpeed(std::vector<DeviceOptions>* current_devices, std::string argument, Options* options)
{
if(argument.size() == 0)
{
std::cout << "Error: --speed passed with no argument" << std::endl;
return false;
}
/*---------------------------------------------------------*\
| If a device is not selected i.e. size() == 0 |
| then add speed to allDeviceOptions |
\*---------------------------------------------------------*/
bool found = false;
DeviceOptions* currentDevOpts = &options->allDeviceOptions;
if(current_devices->size() == 0)
{
currentDevOpts->speed = std::min(std::max(std::stoi(argument), 0),(int)speed_percentage);
currentDevOpts->hasOption = true;
found = true;
}
else
{
for(size_t i = 0; i < current_devices->size(); i++)
{
DeviceOptions* currentDevOpts = &current_devices->at(i);
currentDevOpts->speed = std::min(std::max(std::stoi(argument), 0),(int)speed_percentage);
currentDevOpts->hasOption = true;
found = true;
}
}
return found;
}
bool OptionBrightness(std::vector<DeviceOptions>* current_devices, std::string argument, Options* options)
{
if(argument.size() == 0)
@ -956,9 +995,22 @@ int ProcessOptions(int argc, char* argv[], Options* options, std::vector<RGBCont
}
/*---------------------------------------------------------*\
| -s / --size |
| -s / --speed |
\*---------------------------------------------------------*/
else if(option == "--size" || option == "-s")
else if(option == "--speed" || option == "-s")
{
if(!OptionSpeed(&current_devices, argument, options))
{
return RET_FLAG_PRINT_HELP;
}
arg_index++;
}
/*---------------------------------------------------------*\
| -sz / --size |
\*---------------------------------------------------------*/
else if(option == "--size" || option == "-sz")
{
if(!OptionSize(&current_devices, argument, options, rgb_controllers))
{
@ -1102,6 +1154,15 @@ void ApplyOptions(DeviceOptions& options, std::vector<RGBController *>& rgb_cont
device->modes[mode].brightness = device->modes[mode].brightness_min + new_brightness;
}
if((device->modes[mode].flags & MODE_FLAG_HAS_SPEED))
{
unsigned int new_speed = device->modes[mode].speed_max - device->modes[mode].speed_min;
new_speed *= options.speed;
new_speed /= speed_percentage;
device->modes[mode].speed = device->modes[mode].speed_min + new_speed;
}
/*---------------------------------------------------------*\
| Determine which color mode this mode uses and update |
| colors accordingly |
@ -1160,7 +1221,8 @@ void ApplyOptions(DeviceOptions& options, std::vector<RGBController *>& rgb_cont
}
else
{
std::cout << "Wrong number of colors specified for mode" << std::endl;
std::cout << "Wrong number of colors specified for mode " + device->modes[mode].name << std::endl;
std::cout << "Please provide between " + std::to_string(device->modes[mode].colors_min) + " and " + std::to_string(device->modes[mode].colors_min) + " colors" << std::endl;
exit(0);
}
break;