From b4164e44a304f20ebbefb4c80daaac7f9bc436a2 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Thu, 18 Jan 2024 12:08:15 +0000 Subject: [PATCH] cargo: bump the minor group in /src/exercises/bare-metal/compass with 1 update (#1718) Bumps the minor group in /src/exercises/bare-metal/compass with 1 update: [lsm303agr](https://github.com/eldruin/lsm303agr-rs). Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- src/exercises/bare-metal/compass/Cargo.lock | 11 ++++- src/exercises/bare-metal/compass/Cargo.toml | 2 +- src/exercises/bare-metal/compass/src/main.rs | 51 +++++++++++++------- 3 files changed, 43 insertions(+), 21 deletions(-) diff --git a/src/exercises/bare-metal/compass/Cargo.lock b/src/exercises/bare-metal/compass/Cargo.lock index cb069a81..18f33bb4 100644 --- a/src/exercises/bare-metal/compass/Cargo.lock +++ b/src/exercises/bare-metal/compass/Cargo.lock @@ -29,6 +29,12 @@ version = "0.13.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "46afbd2983a5d5a7bd740ccb198caf5b82f45c40c09c0eed36052d91cb92e719" +[[package]] +name = "bitflags" +version = "2.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed570934406eb16438a4e976b1b4500774099c13b8cb96eec99f620f05090ddf" + [[package]] name = "bytemuck" version = "1.14.0" @@ -161,10 +167,11 @@ dependencies = [ [[package]] name = "lsm303agr" -version = "0.2.2" +version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "aab0b8c3a75dfdebe9f79c738458da139e8111207d176e9fb6b360117432ac4f" +checksum = "82d672443f0535384ea86fff40c50156c5fae3fe0446242edeb9ff993b63e0dc" dependencies = [ + "bitflags", "embedded-hal", "nb 1.1.0", ] diff --git a/src/exercises/bare-metal/compass/Cargo.toml b/src/exercises/bare-metal/compass/Cargo.toml index 072ad64a..c5447b80 100644 --- a/src/exercises/bare-metal/compass/Cargo.toml +++ b/src/exercises/bare-metal/compass/Cargo.toml @@ -9,6 +9,6 @@ publish = false [dependencies] cortex-m-rt = "0.7.3" embedded-hal = "0.2.6" -lsm303agr = "0.2.2" +lsm303agr = "0.3.0" microbit-v2 = "0.13.0" panic-halt = "0.2.0" diff --git a/src/exercises/bare-metal/compass/src/main.rs b/src/exercises/bare-metal/compass/src/main.rs index 05ef0322..3e9c8b25 100644 --- a/src/exercises/bare-metal/compass/src/main.rs +++ b/src/exercises/bare-metal/compass/src/main.rs @@ -23,12 +23,14 @@ use core::fmt::Write; use cortex_m_rt::entry; // ANCHOR_END: top use core::cmp::{max, min}; -use lsm303agr::{AccelOutputDataRate, Lsm303agr, MagOutputDataRate}; +use lsm303agr::{ + AccelMode, AccelOutputDataRate, Lsm303agr, MagMode, MagOutputDataRate, +}; use microbit::display::blocking::Display; use microbit::hal::prelude::*; use microbit::hal::twim::Twim; use microbit::hal::uarte::{Baudrate, Parity, Uarte}; -use microbit::hal::Timer; +use microbit::hal::{Delay, Timer}; use microbit::pac::twim0::frequency::FREQUENCY_A; use microbit::Board; @@ -48,14 +50,27 @@ fn main() -> ! { Baudrate::BAUD115200, ); + // Use the system timer as a delay provider. + let mut delay = Delay::new(board.SYST); + // Set up the I2C controller and Inertial Measurement Unit. // ANCHOR_END: main writeln!(serial, "Setting up IMU...").unwrap(); let i2c = Twim::new(board.TWIM0, board.i2c_internal.into(), FREQUENCY_A::K100); let mut imu = Lsm303agr::new_with_i2c(i2c); imu.init().unwrap(); - imu.set_mag_odr(MagOutputDataRate::Hz50).unwrap(); - imu.set_accel_odr(AccelOutputDataRate::Hz50).unwrap(); + imu.set_mag_mode_and_odr( + &mut delay, + MagMode::HighResolution, + MagOutputDataRate::Hz50, + ) + .unwrap(); + imu.set_accel_mode_and_odr( + &mut delay, + AccelMode::Normal, + AccelOutputDataRate::Hz50, + ) + .unwrap(); let mut imu = imu.into_mag_continuous().ok().unwrap(); // Set up display and timer. @@ -71,41 +86,41 @@ fn main() -> ! { loop { // Read compass data and log it to the serial port. // ANCHOR_END: loop - while !(imu.mag_status().unwrap().xyz_new_data - && imu.accel_status().unwrap().xyz_new_data) + while !(imu.mag_status().unwrap().xyz_new_data() + && imu.accel_status().unwrap().xyz_new_data()) {} - let compass_reading = imu.mag_data().unwrap(); - let accelerometer_reading = imu.accel_data().unwrap(); + let compass_reading = imu.magnetic_field().unwrap(); + let accelerometer_reading = imu.acceleration().unwrap(); writeln!( serial, "{},{},{}\t{},{},{}", - compass_reading.x, - compass_reading.y, - compass_reading.z, - accelerometer_reading.x, - accelerometer_reading.y, - accelerometer_reading.z, + compass_reading.x_nt(), + compass_reading.y_nt(), + compass_reading.z_nt(), + accelerometer_reading.x_mg(), + accelerometer_reading.y_mg(), + accelerometer_reading.z_mg(), ) .unwrap(); let mut image = [[0; 5]; 5]; let (x, y) = match mode { Mode::Compass => ( - scale(-compass_reading.x, -COMPASS_SCALE, COMPASS_SCALE, 0, 4) + scale(-compass_reading.x_nt(), -COMPASS_SCALE, COMPASS_SCALE, 0, 4) as usize, - scale(compass_reading.y, -COMPASS_SCALE, COMPASS_SCALE, 0, 4) + scale(compass_reading.y_nt(), -COMPASS_SCALE, COMPASS_SCALE, 0, 4) as usize, ), Mode::Accelerometer => ( scale( - accelerometer_reading.x, + accelerometer_reading.x_mg(), -ACCELEROMETER_SCALE, ACCELEROMETER_SCALE, 0, 4, ) as usize, scale( - -accelerometer_reading.y, + -accelerometer_reading.y_mg(), -ACCELEROMETER_SCALE, ACCELEROMETER_SCALE, 0,