This commit is contained in:
hulthe committed 2024-11-04 23:13:07 +01:00
commit b5c341b413
7 files changed
+472

No files matched your search

+88
View File
@@ -0,0 +1,88 @@
mod motor;
use crate::motor::{Motor, MotorConfig, MotorPinMappings, MotorPins};
use clap::{Parser, Subcommand};
use std::sync::atomic::{AtomicBool, Ordering};
use std::time::Duration;
#[derive(Parser)]
struct Opt {
#[clap(subcommand)]
command: Command,
}
#[derive(Subcommand)]
enum Command {
/// Reset motor pins, making sure the motor is off
Reset,
/// Turn the motor
Turn {
/// The direction to turn
#[clap(subcommand)]
direction: Direction,
/// The number of full stepper motor steps to turn, if not specified the motor will turn
/// 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,
},
}
#[derive(Clone, Copy, Subcommand)]
pub enum Direction {
Left,
Right,
}
fn main() {
let opt = Opt::parse();
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];
match &opt.command {
Command::Reset => {
let _ = MotorPins::new(&pi, 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);
for i in 0.. {
if exit.load(Ordering::Acquire) {
break;
}
motor.spin_step(*direction);
if i % 100 == 0 {
println!("{i}");
}
if let Some(steps) = steps {
if i >= *steps {
break;
}
}
}
}
}
}