My solution

  1. #![deny(unsafe_code)]
  2. #![no_main]
  3. #![no_std]
  4. #[allow(unused_imports)]
  5. use aux16::{entry, iprint, iprintln, prelude::*, I16x3, Sensitivity};
  6. use m::Float;
  7. #[entry]
  8. fn main() -> ! {
  9. const SENSITIVITY: f32 = 12. / (1 << 14) as f32;
  10. const THRESHOLD: f32 = 0.5;
  11. let (mut lsm303dlhc, mut delay, mono_timer, mut itm) = aux16::init();
  12. lsm303dlhc.set_accel_sensitivity(Sensitivity::G12).unwrap();
  13. let measurement_time = mono_timer.frequency().0; // 1 second in ticks
  14. let mut instant = None;
  15. let mut max_g = 0.;
  16. loop {
  17. let g_x = f32::from(lsm303dlhc.accel().unwrap().x).abs() * SENSITIVITY;
  18. match instant {
  19. None => {
  20. // If acceleration goes above a threshold, we start measuring
  21. if g_x > THRESHOLD {
  22. iprintln!(&mut itm.stim[0], "START!");
  23. max_g = g_x;
  24. instant = Some(mono_timer.now());
  25. }
  26. }
  27. // Still measuring
  28. Some(ref instant) if instant.elapsed() < measurement_time => {
  29. if g_x > max_g {
  30. max_g = g_x;
  31. }
  32. }
  33. _ => {
  34. // Report max value
  35. iprintln!(&mut itm.stim[0], "Max acceleration: {}g", max_g);
  36. // Measurement done
  37. instant = None;
  38. // Reset
  39. max_g = 0.;
  40. }
  41. }
  42. delay.delay_ms(50_u8);
  43. }
  44. }