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

Add option to display accelerometer rather than compass.

This commit is contained in:
Andrew Walbran 2023-03-09 15:12:10 +00:00
parent 8f2a851e95
commit f40261646c

View File

@ -26,6 +26,7 @@ use lsm303agr::{AccelOutputDataRate, Lsm303agr, MagOutputDataRate};
use microbit::{ use microbit::{
display::blocking::Display, display::blocking::Display,
hal::{ hal::{
prelude::*,
twim::Twim, twim::Twim,
uarte::{Baudrate, Parity, Uarte}, uarte::{Baudrate, Parity, Uarte},
Timer, Timer,
@ -35,6 +36,7 @@ use microbit::{
}; };
const COMPASS_SCALE: i32 = 30000; const COMPASS_SCALE: i32 = 30000;
const ACCELEROMETER_SCALE: i32 = 700;
// ANCHOR: main // ANCHOR: main
#[entry] #[entry]
@ -63,15 +65,19 @@ fn main() -> ! {
let mut timer = Timer::new(board.TIMER0); let mut timer = Timer::new(board.TIMER0);
let mut display = Display::new(board.display_pins); let mut display = Display::new(board.display_pins);
let mut mode = Mode::Compass;
let mut button_pressed = false;
// ANCHOR: loop // ANCHOR: loop
writeln!(serial, "Ready.").unwrap(); writeln!(serial, "Ready.").unwrap();
loop { loop {
// Read compass data and log it to the serial port. // Read compass data and log it to the serial port.
// ANCHOR_END: loop // ANCHOR_END: loop
while !imu.mag_status().unwrap().xyz_new_data {} while !(imu.mag_status().unwrap().xyz_new_data
&& imu.accel_status().unwrap().xyz_new_data)
{}
let compass_reading = imu.mag_data().unwrap(); let compass_reading = imu.mag_data().unwrap();
while !imu.accel_status().unwrap().xyz_new_data {}
let accelerometer_reading = imu.accel_data().unwrap(); let accelerometer_reading = imu.accel_data().unwrap();
writeln!( writeln!(
serial, serial,
@ -86,12 +92,56 @@ fn main() -> ! {
.unwrap(); .unwrap();
let mut image = [[0; 5]; 5]; let mut image = [[0; 5]; 5];
let x = scale(-compass_reading.x, -COMPASS_SCALE, COMPASS_SCALE, 0, 4) as usize; let (x, y) = match mode {
let y = scale(compass_reading.y, -COMPASS_SCALE, COMPASS_SCALE, 0, 4) as usize; Mode::Compass => (
let x = scale(accelerometer_reading.x, -700, 700, 0, 4) as usize; scale(-compass_reading.x, -COMPASS_SCALE, COMPASS_SCALE, 0, 4) as usize,
let y = scale(-accelerometer_reading.y, -700, 700, 0, 4) as usize; scale(compass_reading.y, -COMPASS_SCALE, COMPASS_SCALE, 0, 4) as usize,
),
Mode::Accelerometer => (
scale(
accelerometer_reading.x,
-ACCELEROMETER_SCALE,
ACCELEROMETER_SCALE,
0,
4,
) as usize,
scale(
-accelerometer_reading.y,
-ACCELEROMETER_SCALE,
ACCELEROMETER_SCALE,
0,
4,
) as usize,
),
};
image[y][x] = 255; image[y][x] = 255;
display.show(&mut timer, image, 100); display.show(&mut timer, image, 100);
// If button A is pressed, switch to the next mode and briefly blink all LEDs on.
if board.buttons.button_a.is_low().unwrap() {
if !button_pressed {
mode = mode.next();
display.show(&mut timer, [[255; 5]; 5], 200);
}
button_pressed = true;
} else {
button_pressed = false;
}
}
}
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
enum Mode {
Compass,
Accelerometer,
}
impl Mode {
fn next(self) -> Self {
match self {
Self::Compass => Self::Accelerometer,
Self::Accelerometer => Self::Compass,
}
} }
} }