diff --git a/Cargo.lock b/Cargo.lock index 3f4098b..467ef21 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,6 +1,12 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. -version = 3 +version = 4 + +[[package]] +name = "anyhow" +version = "1.0.104" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "330a5ed07fa54e4702c9d6c4174f74427fc0ef6e214bbd677ae50a5099946470" [[package]] name = "atty" @@ -15,9 +21,9 @@ dependencies = [ [[package]] name = "autocfg" -version = "1.1.0" +version = "1.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" +checksum = "f2032f911046de80f0a198e0901378627c33f59ea0ac00e363d481118bd70a53" [[package]] name = "bitflags" @@ -26,28 +32,43 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" [[package]] -name = "cc" -version = "1.0.73" +name = "bitflags" +version = "2.13.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2fff2a6927b3bb87f9595d67196a70493f627687a71d87a0d692242c33f58c11" +checksum = "3ded4057c258ba199e2d26386d3af3780957ecaee6c4ef4041c6b4b8b97c0b06" + +[[package]] +name = "block2" +version = "0.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cdeb9d870516001442e364c5220d3574d2da8dc765554b4a617230d33fa58ef5" +dependencies = [ + "objc2", +] [[package]] name = "cfg-if" -version = "1.0.0" +version = "1.0.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +checksum = "4e7648175b45a9a48536d676f68d918270699102aa8dab5496df06904c914600" + +[[package]] +name = "cfg_aliases" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f079e83a288787bcd14a6aea84cee5c87a67c5a3e660c30f557a3d24761b3527" [[package]] name = "clap" -version = "3.2.10" +version = "3.2.25" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "69c5a7f9997be616e47f0577ee38c91decb33392c5be4866494f34cdf329a9aa" +checksum = "4ea181bf566f71cb9a5d17a59e1871af638180a18fb0035c92ae62b705207123" dependencies = [ "atty", - "bitflags", + "bitflags 1.3.2", "clap_derive", "clap_lex", - "indexmap", + "indexmap 1.9.3", "once_cell", "strsim", "termcolor", @@ -56,15 +77,15 @@ dependencies = [ [[package]] name = "clap_derive" -version = "3.2.7" +version = "3.2.25" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "759bf187376e1afa7b85b959e6a664a3e7a95203415dba952ad19139e798f902" +checksum = "ae6371b8bdc8b7d3959e9cf7b22d4435ef3e79e138688421ec654acf8c81b008" dependencies = [ "heck", "proc-macro-error", "proc-macro2", "quote", - "syn", + "syn 1.0.109", ] [[package]] @@ -78,31 +99,50 @@ dependencies = [ [[package]] name = "ctrlc" -version = "3.2.2" +version = "3.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b37feaa84e6861e00a1f5e5aa8da3ee56d605c9992d33e082786754828e20865" +checksum = "e0b1fab2ae45819af2d0731d60f2afe17227ebb1a1538a236da84c93e9a60162" dependencies = [ + "dispatch2", "nix", - "winapi", + "windows-sys", ] [[package]] -name = "glob" -version = "0.2.11" +name = "dispatch2" +version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8be18de09a56b60ed0edf84bc9df007e30040691af7acd1c41874faac5895bfb" +checksum = "1e0e367e4e7da84520dedcac1901e4da967309406d1e51017ae1abfb97adbd38" +dependencies = [ + "bitflags 2.13.2", + "block2", + "libc", + "objc2", +] + +[[package]] +name = "equivalent" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "00d174d5400e5e8fd687ad1049e2f578285fa914201b1af7e8b112a4546bd826" [[package]] name = "hashbrown" -version = "0.12.2" +version = "0.12.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "607c8a29735385251a339424dd462993c0fed8fa09d378f259377df08c126022" +checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" + +[[package]] +name = "hashbrown" +version = "0.17.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed5909b6e89a2db4456e54cd5f673791d7eca6732202bbf2a9cc504fe2f9b84a" [[package]] name = "heck" -version = "0.4.0" +version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2540771e65fc8cb83cd6e8a237f70c319bd5c29f78ed1084ba5d50eeac86f7f9" +checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" [[package]] name = "hermit-abi" @@ -115,51 +155,80 @@ dependencies = [ [[package]] name = "indexmap" -version = "1.9.1" +version = "1.9.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "10a35a97730320ffe8e2d410b5d3b69279b98d2c14bdb8b70ea89ecf7888d41e" +checksum = "bd070e393353796e801d209ad339e89596eb4c8d430d18ede6a1cced8fafbd99" dependencies = [ "autocfg", - "hashbrown", + "hashbrown 0.12.3", +] + +[[package]] +name = "indexmap" +version = "2.14.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cc4e190f5d26ca7051642629da2c52fc03bde85a03197c99408dcd291734c855" +dependencies = [ + "equivalent", + "hashbrown 0.17.1", ] [[package]] name = "libc" -version = "0.2.126" +version = "0.2.191" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "349d5a591cd28b49e1d1037471617a32ddcda5731b99419008085f72d5a53836" +checksum = "65cd87c9a6d60536580b316ad985726104730a5151c99845f2490428ef91240a" [[package]] name = "motor" version = "0.1.0" dependencies = [ + "anyhow", "clap", "ctrlc", - "wiringpi", + "rppal", + "serde", + "toml", ] [[package]] name = "nix" -version = "0.24.1" +version = "0.31.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8f17df307904acd05aa8e32e97bb20f2a0df1728bbc2d771ae8f9a90463441e9" +checksum = "cf20d2fde8ff38632c426f1165ed7436270b44f199fc55284c38276f9db47c3d" dependencies = [ - "bitflags", + "bitflags 2.13.2", "cfg-if", + "cfg_aliases", "libc", ] [[package]] -name = "once_cell" -version = "1.13.0" +name = "objc2" +version = "0.6.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "18a6dbe30758c9f83eb00cbea4ac95966305f5a7772f3f42ebfc7fc7eddbd8e1" +checksum = "08849bbd4767dfae9457696856ae1c84fe4e0281bbe4a7abff2d0e06fb7981f8" +dependencies = [ + "objc2-encode", +] + +[[package]] +name = "objc2-encode" +version = "4.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ef25abbcd74fb2609453eb695bd2f860d389e457f67dc17cafc8b8cbc89d0c33" + +[[package]] +name = "once_cell" +version = "1.21.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50" [[package]] name = "os_str_bytes" -version = "6.1.0" +version = "6.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "21326818e99cfe6ce1e524c2a805c189a99b5ae555a35d19f9a284b427d86afa" +checksum = "e2355d85b9a3786f481747ced0e0ff2ba35213a1f9bd406ed906554d7af805a1" [[package]] name = "proc-macro-error" @@ -170,7 +239,7 @@ dependencies = [ "proc-macro-error-attr", "proc-macro2", "quote", - "syn", + "syn 1.0.109", "version_check", ] @@ -187,22 +256,70 @@ dependencies = [ [[package]] name = "proc-macro2" -version = "1.0.40" +version = "1.0.107" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dd96a1e8ed2596c337f8eae5f24924ec83f5ad5ab21ea8e455d3566c69fbcaf7" +checksum = "985e7ec9bb745e6ce6535b544d84d6cd6f7ad8bd711c398938ae983b91a766d9" dependencies = [ "unicode-ident", ] [[package]] name = "quote" -version = "1.0.20" +version = "1.0.47" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3bcdf212e9776fbcb2d23ab029360416bb1706b1aea2d1a5ba002727cbcab804" +checksum = "1fbf4db142a473a8d80c26bbf18454ed458bf8d26c8219c331daecfdbd079001" dependencies = [ "proc-macro2", ] +[[package]] +name = "rppal" +version = "0.22.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c1ce3b019009cff02cb6b0e96e7cc2e5c5b90187dc1a490f8ef1521d0596b026" +dependencies = [ + "libc", +] + +[[package]] +name = "serde" +version = "1.0.229" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4148590afebada386688f18773da617792bf2ef03ffc1e4cbd2b1d45b023e0ba" +dependencies = [ + "serde_core", + "serde_derive", +] + +[[package]] +name = "serde_core" +version = "1.0.229" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "67dca2c9c51e58a4791a4b1ed58308b39c64224d349a935ab5039aa360942a48" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.229" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7a5d71263a5a7d47b41f6b3f06ba276f10cc18b0931f1799f710578e2309348" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.9", +] + +[[package]] +name = "serde_spanned" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4e7523beb55eece201a2356bee0bbca0d1ab466c14c07703b2e0ee6d42cb0c2c" +dependencies = [ + "serde_core", +] + [[package]] name = "strsim" version = "0.10.0" @@ -211,9 +328,20 @@ checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" [[package]] name = "syn" -version = "1.0.98" +version = "1.0.109" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c50aef8a904de4c23c788f104b7dddc7d6f79c647c7c8ce4cc8f73eb0ca773dd" +checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "syn" +version = "3.0.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d78c8dee4c7bf0e14673097256fed6142ce9d3b85a408189d07482442145823b" dependencies = [ "proc-macro2", "quote", @@ -222,30 +350,69 @@ dependencies = [ [[package]] name = "termcolor" -version = "1.1.3" +version = "1.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bab24d30b911b2376f3a13cc2cd443142f0c81dda04c118693e35b3835757755" +checksum = "06794f8f6c5c898b3275aebefa6b8a1cb24cd2c6c79397ab15774837a0bc5755" dependencies = [ "winapi-util", ] [[package]] name = "textwrap" -version = "0.15.0" +version = "0.16.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b1141d4d61095b28419e22cb0bbf02755f5e54e0526f97f1e3d1d160e60885fb" +checksum = "6ecfad6c3abc80a577f2b91c1e412ee57e7a060d430b553c1b0c940974ebcd49" + +[[package]] +name = "toml" +version = "1.1.8+spec-1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "20489e00e4d8741d6be680764cc12e270655e375a20d1011e844a9c3379e678d" +dependencies = [ + "indexmap 2.14.2", + "serde_core", + "serde_spanned", + "toml_datetime", + "toml_parser", + "toml_writer", + "winnow", +] + +[[package]] +name = "toml_datetime" +version = "1.1.2+spec-1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b86d767906c6c42421dcba507eb9d203e779497710a47782a224bb871653053" +dependencies = [ + "serde_core", +] + +[[package]] +name = "toml_parser" +version = "1.1.5+spec-1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baa693a8032d7e1cada7d0041e96126df243179ff061456783ac7f12bda4744c" +dependencies = [ + "winnow", +] + +[[package]] +name = "toml_writer" +version = "1.1.3+spec-1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06bdbd8cfc056b8d2e2e85f29b56a3bdbecb527cef81eb39e3e7b98af4652770" [[package]] name = "unicode-ident" -version = "1.0.1" +version = "1.0.27" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5bd2fe26506023ed7b5e1e315add59d6f584c621d037f9368fea9cfb988f368c" +checksum = "a2c754d6c33795a1c324727428e5a7dedb5b06195f9890bdbcba760d3e246563" [[package]] name = "version_check" -version = "0.9.4" +version = "0.9.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" +checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" [[package]] name = "winapi" @@ -265,11 +432,11 @@ checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" [[package]] name = "winapi-util" -version = "0.1.5" +version = "0.1.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178" +checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22" dependencies = [ - "winapi", + "windows-sys", ] [[package]] @@ -279,12 +446,22 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" [[package]] -name = "wiringpi" -version = "0.2.4" +name = "windows-link" +version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e3e065de8885b0ae1e1928eb3bf905c7d0a0a28ceab823852e9ffe123a126f3d" +checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5" + +[[package]] +name = "windows-sys" +version = "0.61.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc" dependencies = [ - "cc", - "glob", - "libc", + "windows-link", ] + +[[package]] +name = "winnow" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23b97319f7b8343df12cc98938e5c3eb436064524c8d2b4e30a1d3a36eecdf81" diff --git a/Cargo.toml b/Cargo.toml index 4d8d477..24e1630 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,9 +2,12 @@ name = "motor" version = "0.1.0" description = "raspberry pi stepper motor controller" -edition = "2021" +edition = "2024" [dependencies] +anyhow = "1.0.104" clap = { version = "3.2.10", features = ["derive"] } ctrlc = "3.2.2" -wiringpi = "0.2.4" +rppal = "0.22.1" +serde = { version = "1.0.229", features = ["derive"] } +toml = { version = "1.1.8", features = ["serde"] } diff --git a/example.motor.toml b/example.motor.toml new file mode 100644 index 0000000..b826016 --- /dev/null +++ b/example.motor.toml @@ -0,0 +1,2 @@ +pin_mappings = [0, 2, 3, 4] +sleep_time = { secs = 0, nanos = 7_000_000 } diff --git a/run-on-pi.sh b/run-on-pi.sh index 6d73b54..035557e 100755 --- a/run-on-pi.sh +++ b/run-on-pi.sh @@ -4,8 +4,7 @@ NAME="$(pwd | rg "/[^/]*\$" -o | replace "/" "")" echo "Running $NAME" -set -x -set -r +set -xer cargo build -vvv --target arm-unknown-linux-gnueabihf $@ diff --git a/src/main.rs b/src/main.rs index 5bb1d46..bd0e4ff 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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, - - /// 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 { + 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(()) } diff --git a/src/motor.rs b/src/motor.rs index dcbc1c9..9cdea87 100644 --- a/src/motor.rs +++ b/src/motor.rs @@ -1,46 +1,58 @@ use crate::Direction; +use anyhow::bail; +use rppal::gpio::Level::{High, Low}; +use rppal::gpio::{Gpio, Level, OutputPin}; +use serde::{Deserialize, Serialize}; use std::thread::sleep; use std::time::Duration; -use wiringpi::pin::{self, OutputPin, Value::*}; -use wiringpi::WiringPi; pub struct Motor { pins: MotorPins, sleep_time: Duration, } +#[derive(Clone, Debug, Serialize, Deserialize)] pub struct MotorConfig { - pub pin_mappings: [u16; 4], + pub pin_mappings: MotorPinMappings, + + /// The time in milliseconds between each tick of the stepper motor pub sleep_time: Duration, } pub struct MotorPins { - pins: [OutputPin; 4], + pins: [OutputPin; 4], } -pub type MotorPinMappings = [u16; 4]; +pub type MotorPinMappings = [u8; 4]; impl MotorPins { - pub fn new(wiringpi: &WiringPi, pin_mappings: MotorPinMappings) -> Self { - MotorPins { - pins: pin_mappings.map(|id| wiringpi.output_pin(id)), + pub fn new(gpio: &Gpio, pin_mappings: MotorPinMappings) -> anyhow::Result { + let pins = pin_mappings.map(|id| gpio.get(id).map(|pin| pin.into_output_low())); + for pin in &pins { + if let Err(e) = pin { + bail!("Failed to configure pin: {e:?}"); + } } + + Ok(MotorPins { + pins: pins.map(|r| r.unwrap()), + }) } - pub fn set(&mut self, values: [pin::Value; 4]) { - for (pin, &value) in self.pins.iter_mut().zip(values.iter()) { - pin.digital_write(value); + pub fn set(&mut self, levels: [Level; 4]) { + for (pin, &value) in self.pins.iter_mut().zip(levels.iter()) { + pin.write(value); } } } impl Motor { - pub fn new(wiringpi: &WiringPi, config: &MotorConfig) -> Self { - let pins = MotorPins::new(wiringpi, config.pin_mappings); - Motor { + pub fn new(gpio: &Gpio, config: &MotorConfig) -> anyhow::Result { + let pins = MotorPins::new(gpio, config.pin_mappings)?; + Ok(Motor { sleep_time: config.sleep_time, pins, - } + }) } pub fn spin_step(&mut self, direction: Direction) {