1
0
mirror of https://github.com/google/comprehensive-rust.git synced 2025-03-20 14:31:15 +02:00

Use clamp in bare-metal compass solution (#2537)

The custom function `cap` does the same as `Ord::clamp`, which was
introduced in Rust 1.50. Let's use the latter instead.

I've flashed the new program onto my microbit and can confirm it still
works as intended.
This commit is contained in:
Martin Huschenbett 2025-01-07 12:54:58 +01:00 committed by GitHub
parent 76e2cfe53e
commit 8233599741
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -22,7 +22,6 @@ extern crate panic_halt as _;
use core::fmt::Write;
use cortex_m_rt::entry;
// ANCHOR_END: top
use core::cmp::{max, min};
use embedded_hal::digital::InputPin;
use lsm303agr::{
AccelMode, AccelOutputDataRate, Lsm303agr, MagMode, MagOutputDataRate,
@ -163,9 +162,6 @@ impl Mode {
fn scale(value: i32, min_in: i32, max_in: i32, min_out: i32, max_out: i32) -> i32 {
let range_in = max_in - min_in;
let range_out = max_out - min_out;
cap(min_out + range_out * (value - min_in) / range_in, min_out, max_out)
}
fn cap(value: i32, min_value: i32, max_value: i32) -> i32 {
max(min_value, min(value, max_value))
let scaled = min_out + range_out * (value - min_in) / range_in;
scaled.clamp(min_out, max_out)
}