Skip to content

Commit

Permalink
Merge pull request #3 from timrogers/min-max-kelvin
Browse files Browse the repository at this point in the history
[BREAKING CHANGE] Expose minimum and maximum temperature, rather than a list of allowed values
  • Loading branch information
timrogers authored Jan 15, 2024
2 parents cd020bd + eec74e8 commit b1e90ee
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 30 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ The following commands are available for controlling your devices:
- `litra on`: Turn your Logitech Litra device on
- `litra off`: Turn your Logitech Litra device off
- `litra toggle`: Toggles your Logitech Litra device on or off
- `litra brightness`: Sets the brightness of your Logitech Litra device, using either `--value` (measured in lumens) or `--percentage` (as a percentage of the device's maximum brightness)
- `litra temperature`: Sets the temperature of your Logitech Litra device, using a `--value` measured in kelvin (K)
- `litra brightness`: Sets the brightness of your Logitech Litra device, using either `--value` (measured in lumens) or `--percentage` (as a percentage of the device's maximum brightness). The brightness can be set to any value between the minimum and maximum for the device returned by the `devices` command.
- `litra temperature`: Sets the temperature of your Logitech Litra device, using a `--value` measured in kelvin (K). The temperature be set to any multiple of 100 between the minimum and maximum for the device returned by the `devices` command.

All of the these commands support a `--serial-number`/`-s` argument to specify the serial number of the device you want to target. If you only have one Litra device, you can omit this argument. If you have multiple devices, we recommend specifying it. If it isn't specified, the "first" device will be picked, but this isn't guaranteed to be stable between command runs.

Expand Down
46 changes: 18 additions & 28 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ enum Commands {
#[clap(
long,
short,
help = "The brightness to set, measured in lumens",
help = "The brightness to set, measured in lumens. This can be set to any value between the minimum and maximum for the device returned by the `devices` command.",
group = "brightness"
)]
value: Option<u16>,
Expand All @@ -56,7 +56,7 @@ enum Commands {
#[clap(
long,
short,
help = "The temperature to set, measured in Kelvin. You can check the allowed values for your device with the `devices` command."
help = "The temperature to set, measured in Kelvin. This can be set to any multiple of 100 between the minimum and maximum for the device returned by the `devices` command."
)]
value: u16,
},
Expand Down Expand Up @@ -98,7 +98,8 @@ struct Device {
device_handle: HidDevice,
minimum_brightness_in_lumen: u16,
maximum_brightness_in_lumen: u16,
allowed_temperatures_in_kelvin: Vec<u16>,
minimum_temperature_in_kelvin: u16,
maximum_temperature_in_kelvin: u16,
}

const VENDOR_ID: u16 = 0x046d;
Expand Down Expand Up @@ -136,9 +137,8 @@ fn multiples_within_range(multiples_of: u16, start_range: u16, end_range: u16) -
.collect()
}

fn get_allowed_temperatures_in_kelvin(_device_type: &DeviceType) -> Vec<u16> {
return multiples_within_range(100, 2700, 6500);
}
const MINIMUM_TEMPERATURE_IN_KELVIN: u16 = 2700;
const MAXIMUM_TEMPERATURE_IN_KELVIN: u16 = 6500;

fn get_connected_devices(api: HidApi, serial_number: Option<String>) -> Vec<Device> {
let hid_devices = api.device_list();
Expand Down Expand Up @@ -170,7 +170,6 @@ fn get_connected_devices(api: HidApi, serial_number: Option<String>) -> Vec<Devi
let temperature_in_kelvin = get_temperature_in_kelvin(&device_handle, &device_type);
let minimum_brightness_in_lumen = get_minimum_brightness_in_lumen(&device_type);
let maximum_brightness_in_lumen = get_maximum_brightness_in_lumen(&device_type);
let allowed_temperatures_in_kelvin = get_allowed_temperatures_in_kelvin(&device_type);

Device {
serial_number: device.serial_number().unwrap_or("").to_string(),
Expand All @@ -181,7 +180,8 @@ fn get_connected_devices(api: HidApi, serial_number: Option<String>) -> Vec<Devi
device_handle: device_handle,
minimum_brightness_in_lumen: minimum_brightness_in_lumen,
maximum_brightness_in_lumen: maximum_brightness_in_lumen,
allowed_temperatures_in_kelvin: allowed_temperatures_in_kelvin,
minimum_temperature_in_kelvin: MINIMUM_TEMPERATURE_IN_KELVIN,
maximum_temperature_in_kelvin: MAXIMUM_TEMPERATURE_IN_KELVIN,
}
})
.collect();
Expand Down Expand Up @@ -538,17 +538,9 @@ fn main() {
println!(" - Brightness: {} lm", device.brightness_in_lumen,);
println!(" - Minimum: {} lm", device.minimum_brightness_in_lumen);
println!(" - Maximum: {} lm", device.maximum_brightness_in_lumen);

let comma_separated_values = device
.allowed_temperatures_in_kelvin
.iter()
.map(|x| x.to_string())
.map(|x| format!("{} K", x))
.collect::<Vec<String>>()
.join(", ");

println!(" - Temperature: {} K", device.temperature_in_kelvin);
println!(" - Allowed values: {}", comma_separated_values);
println!(" - Minimum: {} K", device.minimum_temperature_in_kelvin);
println!(" - Maximum: {} K", device.maximum_temperature_in_kelvin);
}

if litra_devices.len() < 1 {
Expand Down Expand Up @@ -659,18 +651,16 @@ fn main() {

let device = &devices[0];

if !device.allowed_temperatures_in_kelvin.contains(&value) {
let comma_separated_values = device
.allowed_temperatures_in_kelvin
.iter()
.map(|x| x.to_string())
.map(|x| format!("{} K", x))
.collect::<Vec<String>>()
.join(", ");
let allowed_temperatures_in_kelvin = multiples_within_range(
100,
device.minimum_temperature_in_kelvin,
device.maximum_temperature_in_kelvin,
);

if !allowed_temperatures_in_kelvin.contains(&value) {
println!(
"Temperature must be set to one of the following allowed values in kelvin (K): {}",
comma_separated_values
"Temperature must be set to a multiple of 100 between {} K and {} K",
device.minimum_temperature_in_kelvin, device.maximum_temperature_in_kelvin
);
std::process::exit(exitcode::DATAERR);
}
Expand Down

0 comments on commit b1e90ee

Please sign in to comment.