Skip to content

Commit

Permalink
Add fan_tach command, fix issues with newer clap
Browse files Browse the repository at this point in the history
  • Loading branch information
jackpot51 committed Dec 8, 2023
1 parent cbad8e0 commit e5cd903
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 22 deletions.
2 changes: 2 additions & 0 deletions src/common/include/common/command.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ enum Command {
CMD_SECURITY_GET = 20,
// Set security state
CMD_SECURITY_SET = 21,
// Get fan tachometer
CMD_FAN_TACH = 22,
//TODO
};

Expand Down
15 changes: 15 additions & 0 deletions tool/src/ec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ enum Cmd {
SetNoInput = 19,
SecurityGet = 20,
SecuritySet = 21,
FanTach = 22,
}

const CMD_SPI_FLAG_READ: u8 = 1 << 0;
Expand Down Expand Up @@ -327,6 +328,20 @@ impl<A: Access> Ec<A> {
self.command(Cmd::SecuritySet, &mut data)
}

/// Read fan tachometer by fan index
pub unsafe fn fan_tach(&mut self, index: u8) -> Result<u16, Error> {
let mut data = [
index,
0,
0
];
self.command(Cmd::FanTach, &mut data)?;
Ok(
(data[1] as u16) |
((data[2] as u16) << 8)
)
}

pub fn into_dyn(self) -> Ec<Box<dyn Access>>
where A: 'static {
Ec {
Expand Down
69 changes: 47 additions & 22 deletions tool/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,13 @@ unsafe fn fan_set(ec: &mut Ec<Box<dyn Access>>, index: u8, duty: u8) -> Result<(
ec.fan_set(index, duty)
}

unsafe fn fan_tach(ec: &mut Ec<Box<dyn Access>>, index: u8) -> Result<(), Error> {
let tach = ec.fan_tach(index)?;
println!("{}", tach);

Ok(())
}

unsafe fn keymap_get(ec: &mut Ec<Box<dyn Access>>, layer: u8, output: u8, input: u8) -> Result<(), Error> {
let value = ec.keymap_get(layer, output, input)?;
println!("{:04X}", value);
Expand Down Expand Up @@ -321,6 +328,12 @@ fn main() {
.value_parser(clap::value_parser!(u8))
)
)
.subcommand(SubCommand::with_name("fan_tach")
.arg(Arg::with_name("index")
.value_parser(clap::value_parser!(u8))
.required(true)
)
)
.subcommand(SubCommand::with_name("flash")
.arg(Arg::with_name("path")
.required(true)
Expand Down Expand Up @@ -423,7 +436,9 @@ fn main() {
// System76 launch_2
(0x3384, 0x0006, 1) |
// System76 launch_heavy_1
(0x3384, 0x0007, 1) => {
(0x3384, 0x0007, 1) |
// System76 thelio_io_2
(0x3384, 0x000B, 1) => {
let device = info.open_device(&api)?;
let access = AccessHid::new(device, 10, 100)?;
return Ok(Ec::new(access)?.into_dyn());
Expand Down Expand Up @@ -454,17 +469,17 @@ fn main() {
},
},
Some(("fan", sub_m)) => {
let index = sub_m.value_of("index").unwrap().parse::<u8>().unwrap();
let duty_opt = sub_m.value_of("duty").map(|x| x.parse::<u8>().unwrap());
let index = sub_m.get_one::<u8>("index").unwrap();
let duty_opt = sub_m.get_one::<u8>("duty");
match duty_opt {
Some(duty) => match unsafe { fan_set(&mut ec, index, duty) } {
Some(duty) => match unsafe { fan_set(&mut ec, *index, *duty) } {
Ok(()) => (),
Err(err) => {
eprintln!("failed to set fan {} to {}: {:X?}", index, duty, err);
process::exit(1);
},
},
None => match unsafe { fan_get(&mut ec, index) } {
None => match unsafe { fan_get(&mut ec, *index) } {
Ok(()) => (),
Err(err) => {
eprintln!("failed to get fan {}: {:X?}", index, err);
Expand All @@ -473,6 +488,16 @@ fn main() {
},
}
},
Some(("fan_tach", sub_m)) => {
let index = sub_m.get_one::<u8>("index").unwrap();
match unsafe { fan_tach(&mut ec, *index) } {
Ok(()) => (),
Err(err) => {
eprintln!("failed to get fan {} tachometer: {:X?}", index, err);
process::exit(1);
},
}
},
Some(("flash", sub_m)) => {
let path = sub_m.value_of("path").unwrap();
match unsafe { flash(&mut ec, path, SpiTarget::Main) } {
Expand Down Expand Up @@ -501,12 +526,12 @@ fn main() {
},
},
Some(("keymap", sub_m)) => {
let layer = sub_m.value_of("layer").unwrap().parse::<u8>().unwrap();
let output = sub_m.value_of("output").unwrap().parse::<u8>().unwrap();
let input = sub_m.value_of("input").unwrap().parse::<u8>().unwrap();
let layer = sub_m.get_one::<u8>("layer").unwrap();
let output = sub_m.get_one::<u8>("output").unwrap();
let input = sub_m.get_one::<u8>("input").unwrap();
match sub_m.value_of("value") {
Some(value_str) => match u16::from_str_radix(value_str.trim_start_matches("0x"), 16) {
Ok(value) => match unsafe { keymap_set(&mut ec, layer, output, input, value) } {
Ok(value) => match unsafe { keymap_set(&mut ec, *layer, *output, *input, value) } {
Ok(()) => (),
Err(err) => {
eprintln!("failed to set keymap {}, {}, {} to {}: {:X?}", layer, output, input, value, err);
Expand All @@ -518,7 +543,7 @@ fn main() {
process::exit(1);
}
},
None => match unsafe { keymap_get(&mut ec, layer, output, input) } {
None => match unsafe { keymap_get(&mut ec, *layer, *output, *input) } {
Ok(()) => (),
Err(err) => {
eprintln!("failed to get keymap {}, {}, {}: {:X?}", layer, output, input, err);
Expand All @@ -528,19 +553,19 @@ fn main() {
}
},
Some(("led_color", sub_m)) => {
let index = sub_m.value_of("index").unwrap().parse::<u8>().unwrap();
let index = sub_m.get_one::<u8>("index").unwrap();
let value = sub_m.value_of("value");
if let Some(value) = value {
let (r, g, b) = parse_color(value).unwrap();
match unsafe { ec.led_set_color(index, r, g, b) } {
match unsafe { ec.led_set_color(*index, r, g, b) } {
Ok(()) => (),
Err(err) => {
eprintln!("failed to set color {}: {:X?}", value, err);
process::exit(1);
},
}
} else {
match unsafe { ec.led_get_color(index) } {
match unsafe { ec.led_get_color(*index) } {
Ok((r, g, b)) => println!("{:02x}{:02x}{:02x}", r, g, b),
Err(err) => {
eprintln!("failed to get color: {:X?}", err);
Expand All @@ -550,18 +575,18 @@ fn main() {
}
},
Some(("led_value", sub_m)) => {
let index = sub_m.value_of("index").unwrap().parse::<u8>().unwrap();
let value = sub_m.value_of("value").map(|x| x.parse::<u8>().unwrap());
let index = sub_m.get_one::<u8>("index").unwrap();
let value = sub_m.get_one::<u8>("value");
if let Some(value) = value {
match unsafe { ec.led_set_value(index, value) } {
match unsafe { ec.led_set_value(*index, *value) } {
Ok(()) => (),
Err(err) => {
eprintln!("failed to set value {}: {:X?}", value, err);
process::exit(1);
},
}
} else {
match unsafe { ec.led_get_value(index) } {
match unsafe { ec.led_get_value(*index) } {
Ok((value, max)) => {
println!("value: {}", value);
println!("max: {}", max);
Expand All @@ -574,19 +599,19 @@ fn main() {
}
},
Some(("led_mode", sub_m)) => {
let layer = sub_m.value_of("layer").unwrap().parse::<u8>().unwrap();
let mode = sub_m.value_of("mode").map(|x| x.parse::<u8>().unwrap());
let speed = sub_m.value_of("speed").map(|x| x.parse::<u8>().unwrap());
let layer = sub_m.get_one::<u8>("layer").unwrap();
let mode = sub_m.get_one::<u8>("mode");
let speed = sub_m.get_one::<u8>("speed");
if let (Some(mode), Some(speed)) = (mode, speed) {
match unsafe { ec.led_set_mode(layer, mode, speed) } {
match unsafe { ec.led_set_mode(*layer, *mode, *speed) } {
Ok(()) => (),
Err(err) => {
eprintln!("failed to set layer {} mode {} at speed {}: {:X?}", layer, mode, speed, err);
process::exit(1);
},
}
} else {
match unsafe { ec.led_get_mode(layer) } {
match unsafe { ec.led_get_mode(*layer) } {
Ok((mode, speed)) => {
println!("mode: {}", mode);
println!("speed: {}", speed);
Expand Down

0 comments on commit e5cd903

Please sign in to comment.