Skip to content

Commit e5cd903

Browse files
committed
Add fan_tach command, fix issues with newer clap
1 parent cbad8e0 commit e5cd903

File tree

3 files changed

+64
-22
lines changed

3 files changed

+64
-22
lines changed

src/common/include/common/command.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ enum Command {
5050
CMD_SECURITY_GET = 20,
5151
// Set security state
5252
CMD_SECURITY_SET = 21,
53+
// Get fan tachometer
54+
CMD_FAN_TACH = 22,
5355
//TODO
5456
};
5557

tool/src/ec.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ enum Cmd {
3939
SetNoInput = 19,
4040
SecurityGet = 20,
4141
SecuritySet = 21,
42+
FanTach = 22,
4243
}
4344

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

331+
/// Read fan tachometer by fan index
332+
pub unsafe fn fan_tach(&mut self, index: u8) -> Result<u16, Error> {
333+
let mut data = [
334+
index,
335+
0,
336+
0
337+
];
338+
self.command(Cmd::FanTach, &mut data)?;
339+
Ok(
340+
(data[1] as u16) |
341+
((data[2] as u16) << 8)
342+
)
343+
}
344+
330345
pub fn into_dyn(self) -> Ec<Box<dyn Access>>
331346
where A: 'static {
332347
Ec {

tool/src/main.rs

Lines changed: 47 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,13 @@ unsafe fn fan_set(ec: &mut Ec<Box<dyn Access>>, index: u8, duty: u8) -> Result<(
269269
ec.fan_set(index, duty)
270270
}
271271

272+
unsafe fn fan_tach(ec: &mut Ec<Box<dyn Access>>, index: u8) -> Result<(), Error> {
273+
let tach = ec.fan_tach(index)?;
274+
println!("{}", tach);
275+
276+
Ok(())
277+
}
278+
272279
unsafe fn keymap_get(ec: &mut Ec<Box<dyn Access>>, layer: u8, output: u8, input: u8) -> Result<(), Error> {
273280
let value = ec.keymap_get(layer, output, input)?;
274281
println!("{:04X}", value);
@@ -321,6 +328,12 @@ fn main() {
321328
.value_parser(clap::value_parser!(u8))
322329
)
323330
)
331+
.subcommand(SubCommand::with_name("fan_tach")
332+
.arg(Arg::with_name("index")
333+
.value_parser(clap::value_parser!(u8))
334+
.required(true)
335+
)
336+
)
324337
.subcommand(SubCommand::with_name("flash")
325338
.arg(Arg::with_name("path")
326339
.required(true)
@@ -423,7 +436,9 @@ fn main() {
423436
// System76 launch_2
424437
(0x3384, 0x0006, 1) |
425438
// System76 launch_heavy_1
426-
(0x3384, 0x0007, 1) => {
439+
(0x3384, 0x0007, 1) |
440+
// System76 thelio_io_2
441+
(0x3384, 0x000B, 1) => {
427442
let device = info.open_device(&api)?;
428443
let access = AccessHid::new(device, 10, 100)?;
429444
return Ok(Ec::new(access)?.into_dyn());
@@ -454,17 +469,17 @@ fn main() {
454469
},
455470
},
456471
Some(("fan", sub_m)) => {
457-
let index = sub_m.value_of("index").unwrap().parse::<u8>().unwrap();
458-
let duty_opt = sub_m.value_of("duty").map(|x| x.parse::<u8>().unwrap());
472+
let index = sub_m.get_one::<u8>("index").unwrap();
473+
let duty_opt = sub_m.get_one::<u8>("duty");
459474
match duty_opt {
460-
Some(duty) => match unsafe { fan_set(&mut ec, index, duty) } {
475+
Some(duty) => match unsafe { fan_set(&mut ec, *index, *duty) } {
461476
Ok(()) => (),
462477
Err(err) => {
463478
eprintln!("failed to set fan {} to {}: {:X?}", index, duty, err);
464479
process::exit(1);
465480
},
466481
},
467-
None => match unsafe { fan_get(&mut ec, index) } {
482+
None => match unsafe { fan_get(&mut ec, *index) } {
468483
Ok(()) => (),
469484
Err(err) => {
470485
eprintln!("failed to get fan {}: {:X?}", index, err);
@@ -473,6 +488,16 @@ fn main() {
473488
},
474489
}
475490
},
491+
Some(("fan_tach", sub_m)) => {
492+
let index = sub_m.get_one::<u8>("index").unwrap();
493+
match unsafe { fan_tach(&mut ec, *index) } {
494+
Ok(()) => (),
495+
Err(err) => {
496+
eprintln!("failed to get fan {} tachometer: {:X?}", index, err);
497+
process::exit(1);
498+
},
499+
}
500+
},
476501
Some(("flash", sub_m)) => {
477502
let path = sub_m.value_of("path").unwrap();
478503
match unsafe { flash(&mut ec, path, SpiTarget::Main) } {
@@ -501,12 +526,12 @@ fn main() {
501526
},
502527
},
503528
Some(("keymap", sub_m)) => {
504-
let layer = sub_m.value_of("layer").unwrap().parse::<u8>().unwrap();
505-
let output = sub_m.value_of("output").unwrap().parse::<u8>().unwrap();
506-
let input = sub_m.value_of("input").unwrap().parse::<u8>().unwrap();
529+
let layer = sub_m.get_one::<u8>("layer").unwrap();
530+
let output = sub_m.get_one::<u8>("output").unwrap();
531+
let input = sub_m.get_one::<u8>("input").unwrap();
507532
match sub_m.value_of("value") {
508533
Some(value_str) => match u16::from_str_radix(value_str.trim_start_matches("0x"), 16) {
509-
Ok(value) => match unsafe { keymap_set(&mut ec, layer, output, input, value) } {
534+
Ok(value) => match unsafe { keymap_set(&mut ec, *layer, *output, *input, value) } {
510535
Ok(()) => (),
511536
Err(err) => {
512537
eprintln!("failed to set keymap {}, {}, {} to {}: {:X?}", layer, output, input, value, err);
@@ -518,7 +543,7 @@ fn main() {
518543
process::exit(1);
519544
}
520545
},
521-
None => match unsafe { keymap_get(&mut ec, layer, output, input) } {
546+
None => match unsafe { keymap_get(&mut ec, *layer, *output, *input) } {
522547
Ok(()) => (),
523548
Err(err) => {
524549
eprintln!("failed to get keymap {}, {}, {}: {:X?}", layer, output, input, err);
@@ -528,19 +553,19 @@ fn main() {
528553
}
529554
},
530555
Some(("led_color", sub_m)) => {
531-
let index = sub_m.value_of("index").unwrap().parse::<u8>().unwrap();
556+
let index = sub_m.get_one::<u8>("index").unwrap();
532557
let value = sub_m.value_of("value");
533558
if let Some(value) = value {
534559
let (r, g, b) = parse_color(value).unwrap();
535-
match unsafe { ec.led_set_color(index, r, g, b) } {
560+
match unsafe { ec.led_set_color(*index, r, g, b) } {
536561
Ok(()) => (),
537562
Err(err) => {
538563
eprintln!("failed to set color {}: {:X?}", value, err);
539564
process::exit(1);
540565
},
541566
}
542567
} else {
543-
match unsafe { ec.led_get_color(index) } {
568+
match unsafe { ec.led_get_color(*index) } {
544569
Ok((r, g, b)) => println!("{:02x}{:02x}{:02x}", r, g, b),
545570
Err(err) => {
546571
eprintln!("failed to get color: {:X?}", err);
@@ -550,18 +575,18 @@ fn main() {
550575
}
551576
},
552577
Some(("led_value", sub_m)) => {
553-
let index = sub_m.value_of("index").unwrap().parse::<u8>().unwrap();
554-
let value = sub_m.value_of("value").map(|x| x.parse::<u8>().unwrap());
578+
let index = sub_m.get_one::<u8>("index").unwrap();
579+
let value = sub_m.get_one::<u8>("value");
555580
if let Some(value) = value {
556-
match unsafe { ec.led_set_value(index, value) } {
581+
match unsafe { ec.led_set_value(*index, *value) } {
557582
Ok(()) => (),
558583
Err(err) => {
559584
eprintln!("failed to set value {}: {:X?}", value, err);
560585
process::exit(1);
561586
},
562587
}
563588
} else {
564-
match unsafe { ec.led_get_value(index) } {
589+
match unsafe { ec.led_get_value(*index) } {
565590
Ok((value, max)) => {
566591
println!("value: {}", value);
567592
println!("max: {}", max);
@@ -574,19 +599,19 @@ fn main() {
574599
}
575600
},
576601
Some(("led_mode", sub_m)) => {
577-
let layer = sub_m.value_of("layer").unwrap().parse::<u8>().unwrap();
578-
let mode = sub_m.value_of("mode").map(|x| x.parse::<u8>().unwrap());
579-
let speed = sub_m.value_of("speed").map(|x| x.parse::<u8>().unwrap());
602+
let layer = sub_m.get_one::<u8>("layer").unwrap();
603+
let mode = sub_m.get_one::<u8>("mode");
604+
let speed = sub_m.get_one::<u8>("speed");
580605
if let (Some(mode), Some(speed)) = (mode, speed) {
581-
match unsafe { ec.led_set_mode(layer, mode, speed) } {
606+
match unsafe { ec.led_set_mode(*layer, *mode, *speed) } {
582607
Ok(()) => (),
583608
Err(err) => {
584609
eprintln!("failed to set layer {} mode {} at speed {}: {:X?}", layer, mode, speed, err);
585610
process::exit(1);
586611
},
587612
}
588613
} else {
589-
match unsafe { ec.led_get_mode(layer) } {
614+
match unsafe { ec.led_get_mode(*layer) } {
590615
Ok((mode, speed)) => {
591616
println!("mode: {}", mode);
592617
println!("speed: {}", speed);

0 commit comments

Comments
 (0)