#![allow(unused)]

use process_image as pi;

mod display;
mod fieldbus;
mod logic;
mod util;

fn main() {
    env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("info"))
        .format_timestamp_micros()
        .init();

    let mut display = display::Display::new();
    display.update_line(0, &format!("{:^20}", "torque tester"));

    let mut fieldbus = fieldbus::Fieldbus::new();
    let mut logic = logic::Logic::new();

    fieldbus.enter_state(fieldbus::OperatingState::Operate);

    loop {
        fieldbus.with_process_images(|pii, piq| {
            use process_image::{tag, tag_mut};

            *tag_mut!(piq, X, 0, 0) = logic.outputs().cylinder_left; // -MBB1
            *tag_mut!(piq, X, 0, 1) = logic.outputs().cylinder_right; // -MBB2
                                                                      //
            *tag_mut!(piq, X, 0, 2) = logic.outputs().signal_operate; // -PZA1:WHITE
            *tag_mut!(piq, X, 0, 3) = logic.outputs().signal_ready; // -PZA1:BLUE
            *tag_mut!(piq, X, 0, 4) = logic.outputs().signal_fault; // -PZA1:ORANGE
            *tag_mut!(piq, X, 0, 5) = logic.outputs().signal_alarm; // -PZA1:BUZZER (VIOLET)

            logic.inputs_mut().left_limit = tag!(pii, X, 0, 0); // -BGB1
            logic.inputs_mut().right_limit = tag!(pii, X, 0, 1); // -BGB2
            logic.inputs_mut().pressure_switch = tag!(pii, X, 0, 2); // -BPB1

            logic.inputs_mut().switch_run_continuous = tag!(pii, X, 0, 4);
            logic.inputs_mut().switch_run_once = tag!(pii, X, 0, 5);

            logic.inputs_mut().rio_online = tag!(pii, X, 64, 0);
        });

        logic.run(profirust::time::Instant::now());

        display.update_all(&logic.outputs().display);

        std::thread::sleep(std::time::Duration::from_millis(20));
    }
}
