Skip to content

Commit

Permalink
extract configuration to toml file
Browse files Browse the repository at this point in the history
  • Loading branch information
Florian Guggi committed Jul 6, 2023
1 parent 0afd25c commit a6267d3
Show file tree
Hide file tree
Showing 5 changed files with 165 additions and 16 deletions.
131 changes: 131 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ simplelog = "^0.12.0"
subprocess = "0.2.8"
crc = "3.0.0"
rppal = "^0.13.1"
toml = "0.7.6"
serde = { version = "1.0.166", features = ["derive"] }

[features]
mock = []
rpi = []

[dev-dependencies]
file-per-thread-logger = "*"
file-per-thread-logger = "*"
6 changes: 6 additions & 0 deletions config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
uart = "/dev/serial0"
baudrate = 921600
heartbeat_pin = 34
update_pin = 35
heartbeat_freq = 10 # Hz
log_path = "./log"
4 changes: 2 additions & 2 deletions src/communication/uart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ impl UARTHandle {
/// ## Returns:
/// A `UARTHandle`(r) that uses Raspberry Pi's UART Peripheral
///
pub fn new(baud: u32) -> UARTHandle {
pub fn new(path: &str, baud: u32) -> UARTHandle {
let mut uart_handler: UARTHandle =
UARTHandle { uart_PI: Uart::new(baud, Parity::None, DATA_BITS, STOP_BITS).unwrap() };
UARTHandle { uart_PI: Uart::with_path(path, baud, Parity::None, DATA_BITS, STOP_BITS).unwrap() };

let _ = uart_handler.uart_PI.set_write_mode(true);

Expand Down
36 changes: 23 additions & 13 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,50 +11,60 @@ mod command;
mod communication;
mod persist;

const HEARTBEAT_PIN: u8 = 34;
const HEARTBEAT_FREQ: u64 = 10; //Hz
const UPDATE_PIN: u8 = 35;
#[derive(serde::Deserialize)]
struct Configuration {
uart: String,
baudrate: u32,
heartbeat_pin: u8,
update_pin: u8,
heartbeat_freq: u64,
log_path: String,
}

fn main() -> ! {
let config: Configuration =
toml::from_str(&std::fs::read_to_string("./config.toml").unwrap()).unwrap();

// write all logging into a file
let _ = sl::WriteLogger::init(
sl::LevelFilter::Info,
sl::Config::default(),
std::fs::File::create("log").unwrap(),
std::fs::File::create(&config.log_path).unwrap(),
);

log::info!("Scheduler started");

// construct a wrapper for UART communication
let mut com = communication::UARTHandle::new(921600);
let mut com = communication::UARTHandle::new(&config.uart, config.baudrate);

// construct a wrapper for resources that are shared between different commands
let ec = command::ExecutionContext::new(
"./data/status_queue".into(),
"./data/result_queue".into(),
UPDATE_PIN,
config.update_pin,
)
.unwrap();
let mut exec = Arc::new(Mutex::new(ec));

// start a thread that will update the heartbeat pin
thread::spawn(heartbeat_loop);
thread::spawn(move || heartbeat_loop(config.heartbeat_pin, config.heartbeat_freq));

// main loop
loop {
command::handle_command(&mut com, &mut exec);
}
}

fn heartbeat_loop() -> ! {
const TOGGLE_TIME_MS: time::Duration =
time::Duration::from_millis((1000 / HEARTBEAT_FREQ / 2) as u64);
fn heartbeat_loop(heartbeat_pin: u8, freq: u64) -> ! {
let toogle_time = time::Duration::from_millis((1000 / freq / 2) as u64);

let gpio = Gpio::new().unwrap();
let mut pin = gpio.get(HEARTBEAT_PIN).unwrap().into_output();
let mut pin = gpio.get(heartbeat_pin).unwrap().into_output();

loop {
pin.set_high();
thread::sleep(TOGGLE_TIME_MS);
thread::sleep(toogle_time);
pin.set_low();
thread::sleep(TOGGLE_TIME_MS);
thread::sleep(toogle_time);
}
}

0 comments on commit a6267d3

Please sign in to comment.