1
0
mirror of https://github.com/google/comprehensive-rust.git synced 2025-04-15 13:26:37 +02:00

80 lines
2.2 KiB
Markdown
Raw Normal View History

2023-02-15 04:56:31 +00:00
# Compass
2023-03-24 17:45:09 +00:00
We will read the direction from an I2C compass, and log the readings to a serial port. If you have
time, try displaying it on the LEDs somehow too, or use the buttons somehow.
Hints:
- Check the documentation for the [`lsm303agr`](https://docs.rs/lsm303agr/latest/lsm303agr/) and
[`microbit-v2`](https://docs.rs/microbit-v2/latest/microbit/) crates, as well as the
[micro:bit hardware](https://tech.microbit.org/hardware/).
- The LSM303AGR Inertial Measurement Unit is connected to the internal I2C bus.
- TWI is another name for I2C, so the I2C master peripheral is called TWIM.
- The LSM303AGR driver needs something implementing the `embedded_hal::blocking::i2c::WriteRead`
trait. The
[`microbit::hal::Twim`](https://docs.rs/microbit-v2/latest/microbit/hal/struct.Twim.html) struct
implements this.
- You have a [`microbit::Board`](https://docs.rs/microbit-v2/latest/microbit/struct.Board.html)
struct with fields for the various pins and peripherals.
2023-03-24 17:45:09 +00:00
- You can also look at the
[nRF52833 datasheet](https://infocenter.nordicsemi.com/pdf/nRF52833_PS_v1.5.pdf) if you want, but
it shouldn't be necessary for this exercise.
Download the [exercise template](../../comprehensive-rust-exercises.zip) and look in the `compass`
directory for the following files.
`src/main.rs`:
2023-03-24 17:40:26 +00:00
<!-- File src/main.rs -->
2023-02-15 23:17:00 +00:00
```rust,compile_fail
{{#include compass/src/main.rs:top}}
use microbit::{hal::uarte::{Baudrate, Parity, Uarte}, Board};
{{#include compass/src/main.rs:main}}
// TODO
{{#include compass/src/main.rs:loop}}
// TODO
}
}
2023-02-15 04:56:31 +00:00
```
`Cargo.toml` (you shouldn't need to change this):
2023-03-24 17:40:26 +00:00
<!-- File Cargo.toml -->
```toml
{{#include compass/Cargo.toml}}
```
`Embed.toml` (you shouldn't need to change this):
2023-03-24 17:40:26 +00:00
<!-- File Embed.toml -->
```toml
{{#include compass/Embed.toml}}
```
`.cargo/config.toml` (you shouldn't need to change this):
2023-03-24 17:40:26 +00:00
<!-- File .cargo/config.toml -->
```toml
{{#include compass/.cargo/config.toml}}
```
See the serial output on Linux with:
```sh
picocom --baud 115200 --imap lfcrlf /dev/ttyACM0
```
Or on Mac OS something like (the device name may be slightly different):
```sh
picocom --baud 115200 --imap lfcrlf /dev/tty.usbmodem14502
```
Use Ctrl+A Ctrl+Q to quit picocom.