Initial Commit
This commit is contained in:
14
flight/Cargo.toml
Normal file
14
flight/Cargo.toml
Normal file
@@ -0,0 +1,14 @@
|
||||
[package]
|
||||
name = "nautilus_flight"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
anyhow = "1.0.97"
|
||||
fern = "0.7.1"
|
||||
log = "0.4.27"
|
||||
rppal = { version = "0.22.1", optional = true }
|
||||
chrono = "0.4.40"
|
||||
|
||||
[features]
|
||||
raspi = ["dep:rppal"]
|
||||
14
flight/src/hal/emulated/mod.rs
Normal file
14
flight/src/hal/emulated/mod.rs
Normal file
@@ -0,0 +1,14 @@
|
||||
use crate::hal::Hal;
|
||||
use log::info;
|
||||
|
||||
pub struct EmulatedHal {}
|
||||
|
||||
impl EmulatedHal {
|
||||
pub fn new() -> Self {
|
||||
info!("Running in Emulated Hardware Mode");
|
||||
|
||||
Self {}
|
||||
}
|
||||
}
|
||||
|
||||
impl Hal for EmulatedHal {}
|
||||
19
flight/src/hal/mod.rs
Normal file
19
flight/src/hal/mod.rs
Normal file
@@ -0,0 +1,19 @@
|
||||
use anyhow::Result;
|
||||
|
||||
pub trait Hal {}
|
||||
|
||||
#[cfg(feature = "raspi")]
|
||||
mod raspi;
|
||||
|
||||
#[cfg(feature = "raspi")]
|
||||
pub fn initialize() -> Result<impl Hal> {
|
||||
raspi::RaspiHal::new()
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "raspi"))]
|
||||
mod emulated;
|
||||
|
||||
#[cfg(not(feature = "raspi"))]
|
||||
pub fn initialize() -> Result<impl Hal> {
|
||||
Ok(emulated::EmulatedHal::new())
|
||||
}
|
||||
23
flight/src/hal/raspi/mod.rs
Normal file
23
flight/src/hal/raspi/mod.rs
Normal file
@@ -0,0 +1,23 @@
|
||||
use crate::hal::Hal;
|
||||
use anyhow::Result;
|
||||
use log::info;
|
||||
use rppal::gpio::Gpio;
|
||||
|
||||
pub struct RaspiHal {
|
||||
gpio: Gpio,
|
||||
}
|
||||
|
||||
impl RaspiHal {
|
||||
pub fn new() -> Result<Self> {
|
||||
let device = rppal::system::DeviceInfo::new()?;
|
||||
info!(
|
||||
"Running on Raspberry Pi Emulated Hardware Model {} with {}",
|
||||
device.model(),
|
||||
device.soc()
|
||||
);
|
||||
|
||||
Self { gpio: Gpio::new()? }
|
||||
}
|
||||
}
|
||||
|
||||
impl Hal for RaspiHal {}
|
||||
21
flight/src/lib.rs
Normal file
21
flight/src/lib.rs
Normal file
@@ -0,0 +1,21 @@
|
||||
use crate::hal::initialize;
|
||||
use crate::logger::setup_logger;
|
||||
use anyhow::Result;
|
||||
use log::info;
|
||||
|
||||
mod hal;
|
||||
mod logger;
|
||||
|
||||
pub fn run() -> Result<()> {
|
||||
setup_logger()?;
|
||||
info!(
|
||||
"Project Nautilus Flight Software {}",
|
||||
env!("CARGO_PKG_VERSION")
|
||||
);
|
||||
|
||||
let hal = initialize()?;
|
||||
|
||||
drop(hal);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
39
flight/src/logger.rs
Normal file
39
flight/src/logger.rs
Normal file
@@ -0,0 +1,39 @@
|
||||
use anyhow::Result;
|
||||
use std::env;
|
||||
use std::fs::create_dir_all;
|
||||
use std::str::FromStr;
|
||||
|
||||
pub fn setup_logger() -> Result<()> {
|
||||
let log_file = env::var("LOG_FILE").or_else(|_| {
|
||||
create_dir_all("logs/")?;
|
||||
|
||||
anyhow::Ok(format!(
|
||||
"logs/{}_{}.log",
|
||||
env!("CARGO_PKG_NAME"),
|
||||
chrono::Local::now().format("%Y%m%d_%H%M%S")
|
||||
))
|
||||
})?;
|
||||
let log_level = match env::var("LOG_LEVEL") {
|
||||
Ok(log_level) => log::LevelFilter::from_str(&log_level).unwrap_or(log::LevelFilter::Info),
|
||||
Err(_) => log::LevelFilter::Info,
|
||||
};
|
||||
|
||||
fern::Dispatch::new()
|
||||
.format(|out, message, record| {
|
||||
out.finish(format_args!(
|
||||
"[{}][{}][{}] {}",
|
||||
chrono::Local::now().format("%Y-%m-%d %H:%M:%S"),
|
||||
record.target(),
|
||||
record.level(),
|
||||
message
|
||||
))
|
||||
})
|
||||
.chain(
|
||||
fern::Dispatch::new()
|
||||
.level(log_level)
|
||||
.chain(std::io::stdout()),
|
||||
)
|
||||
.chain(fern::log_file(log_file)?)
|
||||
.apply()?;
|
||||
Ok(())
|
||||
}
|
||||
11
flight/src/main.rs
Normal file
11
flight/src/main.rs
Normal file
@@ -0,0 +1,11 @@
|
||||
use log::error;
|
||||
use nautilus_flight::run;
|
||||
|
||||
fn main() {
|
||||
match run() {
|
||||
Ok(_) => {}
|
||||
Err(err) => {
|
||||
error!("An unhandled error occurred: {}", err);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user