#![no_std]
#![no_main]
 
use esp32c3_hal::{
    clock::ClockControl,
    gpio::IO,
    peripherals::Peripherals,
    prelude::*,
    Delay
};
 
use max7219::*;
use esp_backtrace as _;
use dht_sensor::*;
 
const DIGITS: [[u8;8]; 10] = [
    [0x7c, 0xc6, 0xce, 0xde, 0xf6, 0xe6, 0x7c, 0x00],  // 0030 (zero)
    [0x30, 0x70, 0x30, 0x30, 0x30, 0x30, 0xfc, 0x00],  // 0031 (one)
    [0x78, 0xcc, 0x0c, 0x38, 0x60, 0xc4, 0xfc, 0x00],  // 0032 (two)
    [0x78, 0xcc, 0x0c, 0x38, 0x0c, 0xcc, 0x78, 0x00],  // 0033 (three)
    [0x1c, 0x3c, 0x6c, 0xcc, 0xfe, 0x0c, 0x1e, 0x00],  // 0034 (four)
    [0xfc, 0xc0, 0xf8, 0x0c, 0x0c, 0xcc, 0x78, 0x00],  // 0035 (five)
    [0x38, 0x60, 0xc0, 0xf8, 0xcc, 0xcc, 0x78, 0x00],  // 0036 (six)
    [0xfc, 0xcc, 0x0c, 0x18, 0x30, 0x30, 0x30, 0x00],  // 0037 (seven)
    [0x78, 0xcc, 0xcc, 0x78, 0xcc, 0xcc, 0x78, 0x00],  // 0038 (eight)
    [0x78, 0xcc, 0xcc, 0x7c, 0x0c, 0x18, 0x70, 0x00]   // 0039 (nine)
]; 
 
fn flip_bits(mut b: u8) -> u8 {
    b = (b & 0xF0) >> 4 | (b & 0x0F) << 4;
    b = (b & 0xCC) >> 2 | (b & 0x33) << 2;
    b = (b & 0xAA) >> 1 | (b & 0x55) << 1;
    return b;
}
 
#[entry]
fn main() -> ! {
    let peripherals = Peripherals::take();
    let system = peripherals.SYSTEM.split();
    let clocks = ClockControl::boot_defaults(system.clock_control).freeze();
    let mut delay = Delay::new(&clocks);
    let io = IO::new(peripherals.GPIO, peripherals.IO_MUX);
 
    //Setup DHT sensor.
    let mut dht_pin = io.pins.gpio7.into_open_drain_output();
    let _ = dht_pin.set_high();
    delay.delay_ms(1000_u16);
 
    //Setup display.
    let data = io.pins.gpio3.into_push_pull_output();
    let sck = io.pins.gpio5.into_push_pull_output();
    let cs = io.pins.gpio2.into_push_pull_output();
    let mut display = MAX7219::from_pins(2, data, cs, sck).unwrap();
    display.power_on().unwrap();
 
    loop {
        let reading = dht22::Reading::read(&mut delay, &mut dht_pin).unwrap();
        let mut temp_c = reading.temperature as usize;
        let mut _temp_f = ((reading.temperature * 1.8) + 32.0) as usize;
 
        temp_c = if temp_c > 99 { 99 } else { temp_c };
        _temp_f = if _temp_f > 99 { 99 } else { _temp_f };
 
        let right_digit = temp_c % 10;
        let left_digit = temp_c / 10;
 
        let mut data: [u8; 8] = DIGITS[left_digit];
        for i in 0..8 {
            data[i] = flip_bits(DIGITS[left_digit][i]);
        }
        display.write_raw(0, &data).unwrap();
 
        let mut data: [u8; 8] = DIGITS[right_digit];
        for i in 0..8 {
            data[i] = flip_bits(DIGITS[right_digit][i]);
        }
        display.write_raw(1, &data).unwrap();
 
        delay.delay_ms(1000_u16);
    }
}