embedded_stepper/lib.rs
1#![no_std]
2
3//! # embedded_stepper
4//!
5//! A tiny **`no_std`** stepper motor helper built on top of
6//! [`embedded-hal`](https://docs.rs/embedded-hal/latest/embedded_hal/).
7//!
8//! This library is a Rust port of the Arduino stepper library. More information can be found
9//! in the official Arduino [doc](https://docs.arduino.cc/libraries/stepper/) and the
10//! [header](https://github.com/arduino-libraries/Stepper/blob/master/src/Stepper.h) and
11//! [source](https://github.com/arduino-libraries/Stepper/blob/master/src/Stepper.cpp).
12//!
13//! ## Design
14//! - You supply a _coil driver_ that implements [StepperMotor](`driver::StepperMotor`) (or use one of the built-in drivers in [`motors`])
15//! - The high-level [Stepper](`stepper::Stepper`) performs stepping exposing the same functions as Arduino stepper.
16//! - Concrete coil drivers for common wirings live under [`motors`].
17//!
18//! ## Quick start
19//!
20//! The following example works with the esp32-c3 development board:
21//!
22//! ```no_run
23//! //Setup clock and peripherals
24//! let config = esp_hal::Config::default().with_cpu_clock(CpuClock::default());
25//! let peripherals = esp_hal::init(config);
26//!
27//! //Get the 4 GPIO pins, change these values based on your wiring
28//! let p1 = peripherals.GPIO0;
29//! let p2 = peripherals.GPIO1;
30//! let p3 = peripherals.GPIO2;
31//! let p4 = peripherals.GPIO3;
32//!
33//! //Set up the 4 pins as output, low, no pull and Push/Pull (default output config)
34//! let op1 = Output::new(p1, Level::Low, OutputConfig::default());
35//! let op2 = Output::new(p2, Level::Low, OutputConfig::default());
36//! let op3 = Output::new(p3, Level::Low, OutputConfig::default());
37//! let op4 = Output::new(p4, Level::Low, OutputConfig::default());
38//!
39//! //Adjust these values to fit your stepper motor. See the Arduino stepper library for more info
40//! let speed = 625;
41//! let steps_per_rev = 48;
42//!
43//! //Instantiate the motor with the 4 output pins
44//! let mut motor = create_stepper_4pin(op1, op2, op3, op4, Delay::new(), steps_per_rev);
45//!
46//! motor.set_speed(speed);
47//!
48//! //Turn motor
49//! let _ = motor.step(steps_per_rev as i32 * 10);
50//!
51//! //Reverse motor
52//! let _ = motor.step(steps_per_rev as i32 * -10);
53//!
54//! //Deenergise coils after use
55//! let _ = motor.deenergise();
56//! ```
57pub mod driver;
58pub mod stepper;
59pub mod motors;
60
61mod builders;
62
63/// High-level controller alias for a **2-pin** motor + `DelayNs`.
64pub type Stepper2<P1, P2, D> = stepper::Stepper<motors::StepperMotor2<P1, P2>, D>;
65
66/// High-level controller alias for a **4-pin** motor + `DelayNs`.
67pub type Stepper4<P1, P2, P3, P4, D> = stepper::Stepper<motors::StepperMotor4<P1, P2, P3, P4>, D>;
68
69/// High-level controller alias for a **5-pin** motor + `DelayNs`.
70pub type Stepper5<P1, P2, P3, P4, P5, D> = stepper::Stepper<motors::StepperMotor5<P1, P2, P3, P4, P5>, D>;
71
72/// Factory: build a [`Stepper2`] from two pins and a delay.
73pub use builders::create_stepper_2pin;
74
75/// Factory: build a [`Stepper4`] from four pins and a delay.
76pub use builders::create_stepper_4pin;
77
78/// Factory: build a [`Stepper5`] from five pins and a delay.
79pub use builders::create_stepper_5pin;