From 354834c711cee411731e19a948e17b0a46fe140f Mon Sep 17 00:00:00 2001 From: Andrew Walbran Date: Wed, 15 Feb 2023 23:06:17 +0000 Subject: [PATCH] Template, hint and extension for compass exercise. --- src/exercises/bare-metal/compass.md | 14 +++++++++++++- src/exercises/bare-metal/compass/src/main.rs | 9 +++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/exercises/bare-metal/compass.md b/src/exercises/bare-metal/compass.md index dfa3cedf..2b08e882 100644 --- a/src/exercises/bare-metal/compass.md +++ b/src/exercises/bare-metal/compass.md @@ -2,6 +2,18 @@ We will read the temperature from an I2C compass, and log the readings to a serial port. +Hint: check the documentation for the [`lsm303agr`](https://docs.rs/lsm303agr/latest/lsm303agr/) and +[`microbit-v2`](https://docs.rs/microbit-v2/latest/microbit/) crates. + +If you have time, try displaying it on the LEDs somehow too, or use the buttons somehow. + ```rust,should_panic -{{#include compass/src/main.rs}} +{{#include compass/src/main.rs:top}} +use microbit::{hal::uarte::{Baudrate, Parity, Uarte}, Board}; + +{{#include compass/src/main.rs:main}} + +{{#include compass/src/main.rs:loop}} + } +} ``` diff --git a/src/exercises/bare-metal/compass/src/main.rs b/src/exercises/bare-metal/compass/src/main.rs index 6df0a840..3e7ea25d 100644 --- a/src/exercises/bare-metal/compass/src/main.rs +++ b/src/exercises/bare-metal/compass/src/main.rs @@ -12,6 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. +// ANCHOR: top #![no_main] #![no_std] @@ -19,6 +20,7 @@ extern crate panic_halt as _; use core::fmt::Write; use cortex_m_rt::entry; +// ANCHOR_END: top use lsm303agr::{Lsm303agr, MagOutputDataRate}; use microbit::{ hal::{ @@ -29,10 +31,12 @@ use microbit::{ Board, }; +// ANCHOR: main #[entry] fn main() -> ! { let board = Board::take().unwrap(); + // Configure serial port. let mut serial = Uarte::new( board.UARTE0, board.uart.into(), @@ -40,6 +44,8 @@ fn main() -> ! { Baudrate::BAUD115200, ); + // Set up the I2C controller and IMU. + // 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); @@ -47,9 +53,12 @@ fn main() -> ! { imu.set_mag_odr(MagOutputDataRate::Hz50).unwrap(); let mut imu = imu.into_mag_continuous().ok().unwrap(); + // ANCHOR: loop writeln!(serial, "Ready.").unwrap(); loop { + // Read compass data and log it to the serial port. + // ANCHOR_END: loop while !imu.mag_status().unwrap().xyz_new_data {} let compass_reading = imu.mag_data().unwrap(); writeln!(