use crate::receiver::RadioReceiver;
use crate::transmitter::Transmitter;

pub mod receiver;
pub mod transmitter;

fn main()
{
    if std::env::args().len() == 2
    {
        println!("Transmitter");
        let tx = Transmitter::start_new();

        loop
        {
            let mut user_input = String::new();
            std::io::stdin().read_line(&mut user_input).unwrap();
            println!("Transmitting ...");
            tx.transmit(user_input.as_bytes().to_vec());
        }
    }
    else
    {
        println!("Receiver");
        let _tx = RadioReceiver::start_new();
    }
}

pub const SAMPLE_RATE: usize = 48_000;
pub const SAMPLE_PER_SYMBOL: usize = 48;
pub const DEVIATION: f64 = 500.;
pub const CARRIER: f64 = 1700.;

pub fn to_bits(n: u8) -> [bool; 8]
{
    [
        (n & 1) == 1,
        (n >> 1) & 1 == 1,
        (n >> 2) & 1 == 1,
        (n >> 3) & 1 == 1,
        (n >> 4) & 1 == 1,
        (n >> 5) & 1 == 1,
        (n >> 6) & 1 == 1,
        (n >> 7) & 1 == 1,
    ]
}

pub fn from_bits(n: [bool; 8]) -> u8
{
    (n[0] as u8)
        | ((n[1] as u8) << 1)
        | ((n[2] as u8) << 2)
        | ((n[3] as u8) << 3)
        | ((n[4] as u8) << 4)
        | ((n[5] as u8) << 5)
        | ((n[6] as u8) << 6)
        | ((n[7] as u8) << 7)
}

pub fn gaussian(sigma: f32, t: f32) -> f32
{
    let sq = (t - 0.5) / sigma;
    (-sq * sq).exp()
}
