-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9f7c1ad
commit 3edb6df
Showing
9 changed files
with
143 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
[build] | ||
target = "thumbv7em-none-eabihf" | ||
|
||
[target.'cfg(all(target_arch = "arm", target_os = "none"))'] | ||
[target.thumbv7em-none-eabihf] | ||
runner = "probe-rs run --chip nRF52833_xxAA" | ||
rustflags = [ | ||
"-C", "linker=rust-lld", | ||
"-C", "link-arg=-Tlink.x", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#![deny(unsafe_code)] | ||
#![no_main] | ||
#![no_std] | ||
|
||
use cortex_m_rt::entry; | ||
use rtt_target::{rtt_init_print, rprintln}; | ||
use panic_rtt_target as _; | ||
|
||
use microbit::{ | ||
hal::{Timer, twim}, | ||
pac::twim0::frequency::FREQUENCY_A, | ||
}; | ||
|
||
use lsm303agr::{AccelMode, AccelOutputDataRate, Lsm303agr}; | ||
|
||
#[entry] | ||
fn main() -> ! { | ||
rtt_init_print!(); | ||
let board = microbit::Board::take().unwrap(); | ||
|
||
let i2c = { twim::Twim::new(board.TWIM0, board.i2c_internal.into(), FREQUENCY_A::K100) }; | ||
let mut timer0 = Timer::new(board.TIMER0); | ||
|
||
let mut sensor = Lsm303agr::new_with_i2c(i2c); | ||
sensor.init().unwrap(); | ||
sensor.set_accel_mode_and_odr( | ||
&mut timer0, | ||
AccelMode::HighResolution, | ||
AccelOutputDataRate::Hz10, | ||
).unwrap(); | ||
loop { | ||
if sensor.accel_status().unwrap().xyz_new_data() { | ||
let (x, y, z) = sensor.acceleration().unwrap().xyz_mg(); | ||
rprintln!("Acceleration: x {} y {} z {}", x, y, z); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,91 +1,8 @@ | ||
# My solution | ||
|
||
``` rust | ||
#![deny(unsafe_code)] | ||
#![no_main] | ||
#![no_std] | ||
|
||
use cortex_m_rt::entry; | ||
use rtt_target::{rtt_init_print, rprintln}; | ||
use panic_rtt_target as _; | ||
|
||
use microbit::{ | ||
hal::twim, | ||
pac::twim0::frequency::FREQUENCY_A, | ||
}; | ||
|
||
use lsm303agr::{ | ||
AccelScale, AccelOutputDataRate, Lsm303agr, | ||
}; | ||
|
||
use microbit::hal::timer::Timer; | ||
use microbit::hal::prelude::*; | ||
use nb::Error; | ||
|
||
#[entry] | ||
fn main() -> ! { | ||
const THRESHOLD: f32 = 0.5; | ||
|
||
rtt_init_print!(); | ||
let board = microbit::Board::take().unwrap(); | ||
Here's my solution (`src/main.rs`). Note that you can get quite high G values by rapping the edge of | ||
your MB2 on a table. Note also that this can break the accelerometer, so probably don't? | ||
|
||
let i2c = { twim::Twim::new(board.TWIM0, board.i2c_internal.into(), FREQUENCY_A::K100) }; | ||
|
||
let mut countdown = Timer::new(board.TIMER0); | ||
let mut delay = Timer::new(board.TIMER1); | ||
let mut sensor = Lsm303agr::new_with_i2c(i2c); | ||
sensor.init().unwrap(); | ||
sensor.set_accel_odr(AccelOutputDataRate::Hz50).unwrap(); | ||
// Allow the sensor to measure up to 16 G since human punches | ||
// can actually be quite fast | ||
sensor.set_accel_scale(AccelScale::G16).unwrap(); | ||
|
||
let mut max_g = 0.; | ||
let mut measuring = false; | ||
|
||
loop { | ||
while !sensor.accel_status().unwrap().xyz_new_data {} | ||
// x acceleration in g | ||
let g_x = sensor.accel_data().unwrap().x as f32 / 1000.0; | ||
|
||
if measuring { | ||
// Check the status of our contdown | ||
match countdown.wait() { | ||
// countdown isn't done yet | ||
Err(Error::WouldBlock) => { | ||
if g_x > max_g { | ||
max_g = g_x; | ||
} | ||
}, | ||
// Countdown is done | ||
Ok(_) => { | ||
// Report max value | ||
rprintln!("Max acceleration: {}g", max_g); | ||
|
||
// Reset | ||
max_g = 0.; | ||
measuring = false; | ||
}, | ||
// Since the nrf52 and nrf51 HAL have Void as an error type | ||
// this path cannot occur, as Void is an empty type | ||
Err(Error::Other(_)) => { | ||
unreachable!() | ||
} | ||
} | ||
} else { | ||
// If acceleration goes above a threshold, we start measuring | ||
if g_x > THRESHOLD { | ||
rprintln!("START!"); | ||
|
||
measuring = true; | ||
max_g = g_x; | ||
// The documentation notes that the timer works at a frequency | ||
// of 1 Mhz, so in order to wait for 1 second we have to | ||
// set it to 1_000_000 ticks. | ||
countdown.start(1_000_000_u32); | ||
} | ||
} | ||
delay.delay_ms(20_u8); | ||
} | ||
} | ||
``` rust | ||
{{#include src/main.rs}} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.