Solution 1

  1. #![deny(unsafe_code)]
  2. #![no_main]
  3. #![no_std]
  4. #[allow(unused_imports)]
  5. use aux15::{entry, iprint, iprintln, prelude::*, Direction, I16x3};
  6. #[entry]
  7. fn main() -> ! {
  8. let (mut leds, mut lsm303dlhc, mut delay, _itm) = aux15::init();
  9. loop {
  10. let I16x3 { x, y, .. } = lsm303dlhc.mag().unwrap();
  11. // Look at the signs of the X and Y components to determine in which
  12. // quadrant the magnetic field is
  13. let dir = match (x > 0, y > 0) {
  14. // Quadrant I
  15. (true, true) => Direction::Southeast,
  16. // Quadrant II
  17. (false, true) => Direction::Northeast,
  18. // Quadrant III
  19. (false, false) => Direction::Northwest,
  20. // Quadrant IV
  21. (true, false) => Direction::Southwest,
  22. };
  23. leds.iter_mut().for_each(|led| led.off());
  24. leds[dir].on();
  25. delay.delay_ms(1_000_u16);
  26. }
  27. }