Solution 2

  1. #![deny(unsafe_code)]
  2. #![no_main]
  3. #![no_std]
  4. // You'll find this useful ;-)
  5. use core::f32::consts::PI;
  6. #[allow(unused_imports)]
  7. use aux15::{entry, iprint, iprintln, prelude::*, Direction, I16x3};
  8. use m::Float;
  9. #[entry]
  10. fn main() -> ! {
  11. let (mut leds, mut lsm303dlhc, mut delay, _itm) = aux15::init();
  12. loop {
  13. let I16x3 { x, y, .. } = lsm303dlhc.mag().unwrap();
  14. let theta = (y as f32).atan2(x as f32); // in radians
  15. let dir = if theta < -7. * PI / 8. {
  16. Direction::North
  17. } else if theta < -5. * PI / 8. {
  18. Direction::Northwest
  19. } else if theta < -3. * PI / 8. {
  20. Direction::West
  21. } else if theta < -PI / 8. {
  22. Direction::Southwest
  23. } else if theta < PI / 8. {
  24. Direction::South
  25. } else if theta < 3. * PI / 8. {
  26. Direction::Southeast
  27. } else if theta < 5. * PI / 8. {
  28. Direction::East
  29. } else if theta < 7. * PI / 8. {
  30. Direction::Northeast
  31. } else {
  32. Direction::North
  33. };
  34. leds.iter_mut().for_each(|led| led.off());
  35. leds[dir].on();
  36. delay.delay_ms(100_u8);
  37. }
  38. }