Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

解答 1

#![deny(unsafe_code)]
#![no_main]
#![no_std]

#[allow(unused_imports)]
use aux15::{entry, iprint, iprintln, prelude::*, switch_hal::OutputSwitch, Direction, I16x3};

#[entry]
fn main() -> ! {
    let (leds, mut lsm303dlhc, mut delay, _itm) = aux15::init();
    let mut leds = leds.into_array();

    loop {
        let I16x3 { x, y, .. } = lsm303dlhc.mag().unwrap();

        // X 成分と Y 成分の符号を見て、磁場がどの
        // 象限にあるかを判定する
        let dir = match (x > 0, y > 0) {
            // 第 I 象限
            (true, true) => Direction::Southeast,
            // 第 II 象限
            (false, true) => Direction::Northeast,
            // 第 III 象限
            (false, false) => Direction::Northwest,
            // 第 IV 象限
            (true, false) => Direction::Southwest,
        };

        leds.iter_mut().for_each(|led| led.off().unwrap());
        leds[dir as usize].on().unwrap();

        delay.delay_ms(1_000_u16);
    }
}