use rppal::{
    i2c::I2c,
    spi::{self, Spi},
};
use std::{error::Error, thread, time::Duration};

const SPI_CLOCK_SPEED: u32 = 1_000_000; // 1 MHz

mod adt7410;
mod mcp3208;

fn main() -> Result<(), Box<dyn Error>> {
    // I2cバスを表す値を生成
    let mut i2c_bus = I2c::new()?;

    // SPIバスを表す値を生成
    let mut spi_bus = Spi::new(
        spi::Bus::Spi0,
        spi::SlaveSelect::Ss0,
        SPI_CLOCK_SPEED,
        spi::Mode::Mode0,
    )?;

    loop {
        match adt7410::celcius(&mut i2c_bus) {
            Ok(temp) => println!("気温: {temp} 度"),
            Err(e) => eprintln!("Error:ADT7410: {e}"),
        }

        match mcp3208::brightness(&mut spi_bus) {
            Ok(brightness) => println!("明るさ: {brightness}"),
            Err(e) => eprintln!("Error:MCP3208: {e:?}"),
        }

        // 1秒スリープ
        thread::sleep(Duration::from_secs(1));
    }
}
