update all the things

This commit is contained in:
hulthe committed 2026-10-25 22:47:23 +01:00
1 parent b5c341b413
commit baa755e3b0
6 files changed
+304 -110

No files matched your search

+29 -28
View File
@@ -1,12 +1,18 @@
mod motor;
use crate::motor::{Motor, MotorConfig, MotorPinMappings, MotorPins};
use crate::motor::{Motor, MotorConfig, MotorPins};
use anyhow::{Context, anyhow};
use clap::{Parser, Subcommand};
use rppal::gpio::Gpio;
use std::fs;
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicBool, Ordering};
use std::time::Duration;
#[derive(Parser)]
struct Opt {
#[clap(short, long, default_value = "/etc/motor.toml")]
config: PathBuf,
#[clap(subcommand)]
command: Command,
}
@@ -26,10 +32,6 @@ enum Command {
/// forever
#[clap(long, short)]
steps: Option<usize>,
/// The time in milliseconds between each tick of the stepper motor
#[clap(long, short, default_value = "7")]
motor_sleep_time: u64,
},
}
@@ -39,34 +41,31 @@ pub enum Direction {
Right,
}
fn main() {
let opt = Opt::parse();
let exit = Box::leak(Box::new(AtomicBool::new(false)));
fn load_config(path: &Path) -> anyhow::Result<MotorConfig> {
fs::read_to_string(path)
.context("Failed to open/read file")
.and_then(|file| toml::from_str(&file).context("Failed to deserialize config"))
.context(anyhow!("Error opening reading file at {path:?}"))
}
fn main() -> anyhow::Result<()> {
let opt = Opt::parse();
let config = load_config(&opt.config)?;
let exit = Box::leak(Box::new(AtomicBool::new(false)));
ctrlc::set_handler(|| {
exit.store(true, Ordering::Release);
})
.expect("Error setting Ctrl-C handler");
let pi = wiringpi::setup();
const PIN_MAPPINGS: MotorPinMappings = [0, 2, 3, 4];
let gpio = Gpio::new().context("Failed to access GPIOs")?;
match &opt.command {
Command::Reset => {
let _ = MotorPins::new(&pi, PIN_MAPPINGS);
let _ = MotorPins::new(&gpio, config.pin_mappings);
}
Command::Turn {
direction,
steps,
motor_sleep_time,
} => {
let motor_config = MotorConfig {
pin_mappings: PIN_MAPPINGS,
sleep_time: Duration::from_millis(*motor_sleep_time),
};
let mut motor = Motor::new(&pi, &motor_config);
Command::Turn { direction, steps } => {
let mut motor = Motor::new(&gpio, &config)?;
for i in 0.. {
if exit.load(Ordering::Acquire) {
break;
@@ -77,12 +76,14 @@ fn main() {
println!("{i}");
}
if let Some(steps) = steps {
if i >= *steps {
break;
}
if let Some(steps) = steps
&& i >= *steps
{
break;
}
}
}
}
Ok(())
}