Skip to content

Commit

Permalink
Examples: Replace unwrap with expect
Browse files Browse the repository at this point in the history
...and some more useful error messages.
  • Loading branch information
dbrgn committed May 10, 2024
1 parent da131c5 commit 38719d6
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
16 changes: 11 additions & 5 deletions examples/linux-shtc1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,25 @@ fn main() {
println!();

println!("Soft reset...");
sht.reset(&mut delay).unwrap();
sht.reset(&mut delay).expect("Soft reset failed");
println!();

println!(
"Device identifier: 0x{:02x}",
sht.device_identifier().unwrap()
sht.device_identifier()
.expect("Failed to get device identifier")
);
println!(
"Raw ID register: 0b{:016b}",
sht.raw_id_register().unwrap()
sht.raw_id_register()
.expect("Failed to get raw ID register")
);

println!("\nNormal mode measurements:");
for _ in 0..3 {
let measurement = sht.measure(PowerMode::NormalMode, &mut delay).unwrap();
let measurement = sht
.measure(PowerMode::NormalMode, &mut delay)
.expect("Normal mode measurement failed");
println!(
" {:.2} °C | {:.2} %RH",
measurement.temperature.as_degrees_celsius(),
Expand All @@ -36,7 +40,9 @@ fn main() {

println!("\nLow power mode measurements:");
for _ in 0..3 {
let measurement = sht.measure(PowerMode::LowPower, &mut delay).unwrap();
let measurement = sht
.measure(PowerMode::LowPower, &mut delay)
.expect("Low power mode measurement failed");
println!(
" {:.2} °C | {:.2} %RH",
measurement.temperature.as_degrees_celsius(),
Expand Down
22 changes: 14 additions & 8 deletions examples/linux-shtc3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,24 @@ fn main() {
println!("Starting SHTC3 tests.");
println!("Waking up sensor.");
println!();
sht.wakeup(&mut delay).unwrap();
sht.wakeup(&mut delay).expect("Wakeup failed");

println!(
"Device identifier: 0x{:02x}",
sht.device_identifier().unwrap()
sht.device_identifier()
.expect("Failed to get device identifier")
);
println!(
"Raw ID register: 0b{:016b}",
sht.raw_id_register().unwrap()
sht.raw_id_register()
.expect("Failed to get raw ID register")
);

println!("\nNormal mode measurements:");
for _ in 0..3 {
let measurement = sht.measure(PowerMode::NormalMode, &mut delay).unwrap();
let measurement = sht
.measure(PowerMode::NormalMode, &mut delay)
.expect("Normal mode measurement failed");
println!(
" {:.2} °C | {:.2} %RH",
measurement.temperature.as_degrees_celsius(),
Expand All @@ -34,7 +38,9 @@ fn main() {

println!("\nLow power mode measurements:");
for _ in 0..3 {
let measurement = sht.measure(PowerMode::LowPower, &mut delay).unwrap();
let measurement = sht
.measure(PowerMode::LowPower, &mut delay)
.expect("Low power mode measurement failed");
println!(
" {:.2} °C | {:.2} %RH",
measurement.temperature.as_degrees_celsius(),
Expand All @@ -49,21 +55,21 @@ fn main() {
.unwrap();
println!("Success: {:.2} °C", temperature.as_degrees_celsius());
println!("-> Sleep");
sht.sleep().unwrap();
sht.sleep().expect("Sleep command failed");
print!("-> Measure: ");
let error = sht
.measure_temperature(PowerMode::NormalMode, &mut delay)
.unwrap_err();
println!("Error: {:?}", error);
println!("-> Wakeup");
sht.wakeup(&mut delay).unwrap();
sht.wakeup(&mut delay).expect("Wakeup command failed");
print!("-> Measure: ");
let temperature = sht
.measure_temperature(PowerMode::NormalMode, &mut delay)
.unwrap();
println!("Success: {:.2} °C", temperature.as_degrees_celsius());
println!("-> Soft reset");
sht.reset(&mut delay).unwrap();
sht.reset(&mut delay).expect("Reset command failed");
print!("-> Measure: ");
let temperature = sht
.measure_temperature(PowerMode::NormalMode, &mut delay)
Expand Down

0 comments on commit 38719d6

Please sign in to comment.