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.
|
2023-03-24 17:50:03 +00:00
|
|
|
- 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.
|
2023-02-15 23:06:17 +00:00
|
|
|
|
2023-04-05 16:15:42 +01:00
|
|
|
Download the [exercise template](../../comprehensive-rust-exercises.zip) and look in the `compass`
|
|
|
|
directory for the following files.
|
2023-03-30 15:35:06 +01:00
|
|
|
|
2023-03-17 17:17:28 +00:00
|
|
|
`src/main.rs`:
|
2023-03-24 17:40:26 +00:00
|
|
|
|
2023-04-03 14:14:13 +01:00
|
|
|
<!-- File src/main.rs -->
|
|
|
|
|
2023-02-15 23:17:00 +00:00
|
|
|
```rust,compile_fail
|
2023-02-15 23:06:17 +00:00
|
|
|
{{#include compass/src/main.rs:top}}
|
|
|
|
use microbit::{hal::uarte::{Baudrate, Parity, Uarte}, Board};
|
|
|
|
|
|
|
|
{{#include compass/src/main.rs:main}}
|
2023-03-17 17:17:28 +00:00
|
|
|
// TODO
|
2023-02-15 23:06:17 +00:00
|
|
|
|
|
|
|
{{#include compass/src/main.rs:loop}}
|
2023-03-17 17:17:28 +00:00
|
|
|
// TODO
|
2023-02-15 23:06:17 +00:00
|
|
|
}
|
|
|
|
}
|
2023-02-15 04:56:31 +00:00
|
|
|
```
|
2023-03-09 12:52:01 +00:00
|
|
|
|
2023-03-17 17:17:28 +00:00
|
|
|
`Cargo.toml` (you shouldn't need to change this):
|
2023-03-24 17:40:26 +00:00
|
|
|
|
2023-04-03 14:14:13 +01:00
|
|
|
<!-- File Cargo.toml -->
|
|
|
|
|
2023-03-17 17:17:28 +00:00
|
|
|
```toml
|
|
|
|
{{#include compass/Cargo.toml}}
|
|
|
|
```
|
|
|
|
|
|
|
|
`Embed.toml` (you shouldn't need to change this):
|
2023-03-24 17:40:26 +00:00
|
|
|
|
2023-04-03 14:14:13 +01:00
|
|
|
<!-- File Embed.toml -->
|
|
|
|
|
2023-03-17 17:17:28 +00:00
|
|
|
```toml
|
|
|
|
{{#include compass/Embed.toml}}
|
|
|
|
```
|
|
|
|
|
|
|
|
`.cargo/config.toml` (you shouldn't need to change this):
|
2023-03-24 17:40:26 +00:00
|
|
|
|
2023-04-03 14:14:13 +01:00
|
|
|
<!-- File .cargo/config.toml -->
|
|
|
|
|
2023-03-17 17:17:28 +00:00
|
|
|
```toml
|
|
|
|
{{#include compass/.cargo/config.toml}}
|
|
|
|
```
|
|
|
|
|
2023-03-24 17:41:29 +00:00
|
|
|
See the serial output on Linux with:
|
2023-03-09 12:52:01 +00:00
|
|
|
|
|
|
|
```sh
|
|
|
|
picocom --baud 115200 --imap lfcrlf /dev/ttyACM0
|
|
|
|
```
|
|
|
|
|
2023-03-24 17:41:29 +00:00
|
|
|
Or on Mac OS something like (the device name may be slightly different):
|
|
|
|
|
|
|
|
```sh
|
|
|
|
picocom --baud 115200 --imap lfcrlf /dev/tty.usbmodem14502
|
|
|
|
```
|
|
|
|
|
2023-03-09 12:52:01 +00:00
|
|
|
Use Ctrl+A Ctrl+Q to quit picocom.
|