1
0
mirror of https://github.com/google/comprehensive-rust.git synced 2025-06-26 18:51:00 +02:00

Template, hint and extension for compass exercise.

This commit is contained in:
Andrew Walbran
2023-02-15 23:06:17 +00:00
parent 062129546a
commit 354834c711
2 changed files with 22 additions and 1 deletions

View File

@ -2,6 +2,18 @@
We will read the temperature from an I2C compass, and log the readings to a serial port. 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 ```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}}
}
}
``` ```

View File

@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
// ANCHOR: top
#![no_main] #![no_main]
#![no_std] #![no_std]
@ -19,6 +20,7 @@ extern crate panic_halt as _;
use core::fmt::Write; use core::fmt::Write;
use cortex_m_rt::entry; use cortex_m_rt::entry;
// ANCHOR_END: top
use lsm303agr::{Lsm303agr, MagOutputDataRate}; use lsm303agr::{Lsm303agr, MagOutputDataRate};
use microbit::{ use microbit::{
hal::{ hal::{
@ -29,10 +31,12 @@ use microbit::{
Board, Board,
}; };
// ANCHOR: main
#[entry] #[entry]
fn main() -> ! { fn main() -> ! {
let board = Board::take().unwrap(); let board = Board::take().unwrap();
// Configure serial port.
let mut serial = Uarte::new( let mut serial = Uarte::new(
board.UARTE0, board.UARTE0,
board.uart.into(), board.uart.into(),
@ -40,6 +44,8 @@ fn main() -> ! {
Baudrate::BAUD115200, Baudrate::BAUD115200,
); );
// Set up the I2C controller and IMU.
// ANCHOR_END: main
writeln!(serial, "Setting up IMU...").unwrap(); writeln!(serial, "Setting up IMU...").unwrap();
let i2c = Twim::new(board.TWIM0, board.i2c_internal.into(), FREQUENCY_A::K100); let i2c = Twim::new(board.TWIM0, board.i2c_internal.into(), FREQUENCY_A::K100);
let mut imu = Lsm303agr::new_with_i2c(i2c); let mut imu = Lsm303agr::new_with_i2c(i2c);
@ -47,9 +53,12 @@ fn main() -> ! {
imu.set_mag_odr(MagOutputDataRate::Hz50).unwrap(); imu.set_mag_odr(MagOutputDataRate::Hz50).unwrap();
let mut imu = imu.into_mag_continuous().ok().unwrap(); let mut imu = imu.into_mag_continuous().ok().unwrap();
// ANCHOR: loop
writeln!(serial, "Ready.").unwrap(); writeln!(serial, "Ready.").unwrap();
loop { loop {
// Read compass data and log it to the serial port.
// ANCHOR_END: loop
while !imu.mag_status().unwrap().xyz_new_data {} while !imu.mag_status().unwrap().xyz_new_data {}
let compass_reading = imu.mag_data().unwrap(); let compass_reading = imu.mag_data().unwrap();
writeln!( writeln!(