1
0
mirror of https://github.com/google/comprehensive-rust.git synced 2025-02-13 16:32:23 +02:00

Fill in solution for compass exercise.

This commit is contained in:
Andrew Walbran 2023-02-15 03:57:53 +00:00
parent fb2a81186b
commit 78d1fc2836
5 changed files with 85 additions and 41 deletions

View File

@ -47,6 +47,17 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "compass"
version = "0.1.0"
dependencies = [
"cortex-m-rt",
"embedded-hal",
"lsm303agr",
"microbit-v2",
"panic-halt",
]
[[package]]
name = "cortex-m"
version = "0.7.7"
@ -147,6 +158,16 @@ dependencies = [
"crunchy",
]
[[package]]
name = "lsm303agr"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "aab0b8c3a75dfdebe9f79c738458da139e8111207d176e9fb6b360117432ac4f"
dependencies = [
"embedded-hal",
"nb 1.0.0",
]
[[package]]
name = "microbit-common"
version = "0.13.0"
@ -308,16 +329,6 @@ dependencies = [
"unicode-ident",
]
[[package]]
name = "temperature-logger"
version = "0.1.0"
dependencies = [
"cortex-m-rt",
"embedded-hal",
"microbit-v2",
"panic-halt",
]
[[package]]
name = "tiny-led-matrix"
version = "1.0.2"

View File

@ -1,12 +1,13 @@
[workspace]
[package]
name = "temperature-logger"
name = "compass"
version = "0.1.0"
edition = "2021"
[dependencies]
cortex-m-rt = "0.7.0"
embedded-hal = "0.2.6"
lsm303agr = "0.2.2"
microbit-v2 = "0.13.0"
panic-halt = "0.2.0"

View File

@ -0,0 +1,62 @@
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#![no_main]
#![no_std]
extern crate panic_halt as _;
use core::fmt::Write;
use cortex_m_rt::entry;
use lsm303agr::{Lsm303agr, MagOutputDataRate};
use microbit::{
hal::{
twim::Twim,
uarte::{Baudrate, Parity, Uarte},
},
pac::twim0::frequency::FREQUENCY_A,
Board,
};
#[entry]
fn main() -> ! {
let board = Board::take().unwrap();
let mut serial = Uarte::new(
board.UARTE0,
board.uart.into(),
Parity::EXCLUDED,
Baudrate::BAUD115200,
);
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();
let mut imu = imu.into_mag_continuous().ok().unwrap();
writeln!(serial, "Ready.").unwrap();
loop {
while !imu.mag_status().unwrap().xyz_new_data {}
let compass_reading = imu.mag_data().unwrap();
writeln!(
serial,
"{},{},{}",
compass_reading.x, compass_reading.y, compass_reading.z
)
.unwrap();
}
}

View File

@ -1,30 +0,0 @@
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#![no_main]
#![no_std]
extern crate panic_halt as _;
use cortex_m_rt::entry;
use microbit::{hal::twim::Twim, pac::twim0::frequency::FREQUENCY_A, Board};
#[entry]
fn main() -> ! {
let board = Board::take().unwrap();
let i2c = Twim::new(board.TWIM0, board.i2c_internal.into(), FREQUENCY_A::K100);
loop {}
}