Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #[derive(Debug, Copy, Clone)]
- pub enum RpiV1P1 { // RPi Version 1, plug P1
- P1_03, // Pin P1-03
- P1_05, // Pin P1-05
- P1_07, // Pin P1-07
- P1_08, // Pin P1-08, defaults to alt function 0 UART0_TXD
- P1_10, // Pin P1-10, defaults to alt function 0 UART0_RXD
- P1_11, // Pin P1-11
- P1_12, // Pin P1-12, can be PWM channel 0 in ALT FUN 5
- P1_13, // Pin P1-13
- P1_15, // Pin P1-15
- P1_16, // Pin P1-16
- P1_18, // Pin P1-18
- P1_19, // Pin P1-19, MOSI when SPI0 in use
- P1_21, // Pin P1-21, MISO when SPI0 in use
- P1_22, // Pin P1-22
- P1_23, // Pin P1-23, CLK when SPI0 in use
- P1_24, // Pin P1-24, CE0 when SPI0 in use
- P1_26, // Pin P1-26, CE1 when SPI0 in use
- }
- #[derive(Debug, Copy, Clone)]
- pub enum RpiV2P1 { // RPi Version 2, plug P1
- P1_03, // Pin P1-03
- P1_05, // Pin P1-05
- P1_07, // Pin P1-07
- P1_08, // Pin P1-08, defaults to alt function 0 UART0_TXD
- P1_10, // Pin P1-10, defaults to alt function 0 UART0_RXD
- P1_11, // Pin P1-11
- P1_12, // Pin P1-12, can be PWM channel 0 in ALT FUN 5
- P1_13, // Pin P1-13
- P1_15, // Pin P1-15
- P1_16, // Pin P1-16
- P1_18, // Pin P1-18
- P1_19, // Pin P1-19, MOSI when SPI0 in use
- P1_21, // Pin P1-21, MISO when SPI0 in use
- P1_22, // Pin P1-22
- P1_23, // Pin P1-23, CLK when SPI0 in use
- P1_24, // Pin P1-24, CE0 when SPI0 in use
- P1_26, // Pin P1-26, CE1 when SPI0 in use
- P1_29, // Pin P1-29
- P1_31, // Pin P1-31
- P1_32, // Pin P1-32
- P1_33, // Pin P1-33
- P1_35, // Pin P1-35, can be PWM channel 1 in ALT FUN 5
- P1_36, // Pin P1-36
- P1_37, // Pin P1-37
- P1_38, // Pin P1-38
- P1_40, // Pin P1-40
- // RPi Version 2, new plug P5
- P5_03, // Pin P5-03
- P5_04, // Pin P5-04
- P5_05, // Pin P5-05
- P5_06, // Pin P5-06
- }
- #[derive(Debug, Copy, Clone)]
- pub enum RpiBPlusJ8 { // RPi Version 2, plug P1
- J8_03, // Pin J8-03
- J8_05, // Pin J8-05
- J8_07, // Pin J8-07
- J8_08, // Pin J8-08, defaults to alt function 0 UART0_TXD
- J8_10, // Pin J8-10, defaults to alt function 0 UART0_RXD
- J8_11, // Pin J8-11
- J8_12, // Pin J8-12, can be PWM channel 0 in ALT FUN 5
- J8_13, // Pin J8-13
- J8_15, // Pin J8-15
- J8_16, // Pin J8-16
- J8_18, // Pin J8-18
- J8_19, // Pin J8-19, MOSI when SPI0 in use
- J8_21, // Pin J8-21, MISO when SPI0 in use
- J8_22, // Pin J8-22
- J8_23, // Pin J8-23, CLK when SPI0 in use
- J8_24, // Pin J8-24, CE0 when SPI0 in use
- J8_26, // Pin J8-26, CE1 when SPI0 in use
- J8_29, // Pin J8-29
- J8_31, // Pin J8-31
- J8_32, // Pin J8-32
- J8_33, // Pin J8-33
- J8_35, // Pin J8-35, can be PWM channel 1 in ALT FUN 5
- J8_36, // Pin J8-36
- J8_37, // Pin J8-37
- J8_38, // Pin J8-38
- J8_40, // Pin J8-40
- }
- use std::marker::PhantomData;
- pub trait RpiVersion where Self::Pin:Copy+'static {
- type Pin;
- const PINS: &'static[Self::Pin];
- }
- pub struct RpiV1 {}
- pub struct RpiV2 {}
- pub struct RpiBPlus {}
- use std::sync::{Arc, Mutex};
- fn check_mode(mode: u8) -> Result<(), String> {
- match mode {
- 0...7 => Ok(()),
- x => Err(format!("Invalid mode value (must be in range 0..7): {}", x))
- }
- }
- fn check_on(on: u8) -> Result<(), String> {
- match on {
- 0...1 => Ok(()),
- x => Err(format!("Invalid on value (must be 1 (high) or 0 (low)): {}", x))
- }
- }
- lazy_static! {
- static ref INITIALIZED: Arc<Mutex<u32>> = Arc::new(Mutex::new(0));
- }
- pub struct Bcm2835 {}
- impl<V> Gpio<V> for Bcm2835 where V:RpiVersion, V::Pin:private::Supported, u8: std::convert::From<<V as RpiVersion>::Pin> {
- fn set_debug(debug: bool) -> Result<(), String> {
- match *INITIALIZED.lock().unwrap() {
- 0 => unsafe {
- raw::bcm2835_set_debug(debug as u8);
- Ok(())
- },
- _ => Err("Debug mode can only be set before BCM2835 library initialization".to_string())
- }
- }
- fn init() -> Result<Self, String> {
- let mut initialized = INITIALIZED.lock().unwrap();
- match *initialized {
- 0 => unsafe {
- match raw::bcm2835_init() {
- 1 => {
- *initialized += 1;
- Ok(Bcm2835{})
- },
- x => Err(format!("Initializing BCM2835 library failed: {}", x))
- }
- },
- _ => {
- *initialized += 1;
- Ok(Bcm2835{})
- }
- }
- }
- fn fsel(&self, pin: V::Pin, mode: u8) -> Result<(), String> {
- check_mode(mode)?;
- unsafe {
- raw::bcm2835_gpio_fsel(u8::from(pin), mode);
- Ok(())
- }
- }
- fn write(&self, pin: V::Pin, on: u8) -> Result<(), String> {
- check_on(on)?;
- unsafe {
- raw::bcm2835_gpio_write(u8::from(pin), on);
- Ok(())
- }
- }
- fn delay(&self, millis: u32) {
- unsafe {
- raw::bcm2835_delay(millis);
- }
- }
- fn delay_microseconds(&self, micros: u64) {
- unsafe {
- raw::bcm2835_delayMicroseconds(micros);
- }
- }
- }
- impl Drop for Bcm2835 {
- fn drop(&mut self) {
- let mut initialized = INITIALIZED.lock().unwrap();
- *initialized -= 1;
- match *initialized {
- 0 => unsafe {
- match raw::bcm2835_close() {
- 1 => (),
- x => eprintln!("Closing BCM2835 library failed: {}", x),
- }
- },
- _ => ()
- }
- }
- }
- mod raw {
- #[cfg(not(all(target_arch = "arm", target_vendor = "unknown", target_os = "linux", target_env = "gnu")))]
- pub unsafe fn bcm2835_init() -> i32 {
- debug!("bcm2835_init() -> {}", 1);
- 1
- }
- #[cfg(not(all(target_arch = "arm", target_vendor = "unknown", target_os = "linux", target_env = "gnu")))]
- pub unsafe fn bcm2835_close() -> i32 {
- debug!("bcm2835_close() -> {}", 1);
- 1
- }
- #[cfg(not(all(target_arch = "arm", target_vendor = "unknown", target_os = "linux", target_env = "gnu")))]
- pub unsafe fn bcm2835_set_debug(debug: u8) {
- debug!("bcm2835_set_debug(debug: {})", debug);
- }
- #[cfg(not(all(target_arch = "arm", target_vendor = "unknown", target_os = "linux", target_env = "gnu")))]
- pub unsafe fn bcm2835_gpio_fsel(pin: u8, mode: u8) {
- debug!("bcm2835_gpio_fsel(pin: {}, mode: {})", pin, mode);
- }
- #[cfg(not(all(target_arch = "arm", target_vendor = "unknown", target_os = "linux", target_env = "gnu")))]
- pub unsafe fn bcm2835_gpio_write(pin: u8, on: u8) {
- debug!("bcm2835_gpio_write(pin: {}, on: {})", pin, on);
- }
- #[cfg(not(all(target_arch = "arm", target_vendor = "unknown", target_os = "linux", target_env = "gnu")))]
- pub unsafe fn bcm2835_delay(millis: u32) {
- debug!("bcm2835_delay(millis: {})", millis);
- }
- #[cfg(not(all(target_arch = "arm", target_vendor = "unknown", target_os = "linux", target_env = "gnu")))]
- #[allow(non_snake_case)]
- pub unsafe fn bcm2835_delayMicroseconds(micros: u64) {
- debug!("bcm2835_delayMicroseconds(micros: {})", micros);
- }
- }
- mod private {
- macro_rules! enum_values {
- (pub enum $EnumName:ident { $($Variant:ident = $Dicriminant:expr),* $(,)? }) =>
- {
- #[derive(Debug, Copy, Clone)]
- pub enum $EnumName {
- $($Variant = $Dicriminant),*,
- }
- impl $EnumName {
- pub const VALUES: &'static [Self] = &[$($EnumName::$Variant),*];
- }
- }
- }
- enum_values!(
- pub enum RpiV1GpioP1 { // RPi Version 1
- P1_03 = 0, // Version 1, Pin P1-03
- P1_05 = 1, // Version 1, Pin P1-05
- P1_07 = 4, // Version 1, Pin P1-07
- P1_08 = 14, // Version 1, Pin P1-08, defaults to alt function 0 UART0_TXD
- P1_10 = 15, // Version 1, Pin P1-10, defaults to alt function 0 UART0_RXD
- P1_11 = 17, // Version 1, Pin P1-11
- P1_12 = 18, // Version 1, Pin P1-12, can be PWM channel 0 in ALT FUN 5
- P1_13 = 21, // Version 1, Pin P1-13
- P1_15 = 22, // Version 1, Pin P1-15
- P1_16 = 23, // Version 1, Pin P1-16
- P1_18 = 24, // Version 1, Pin P1-18
- P1_19 = 10, // Version 1, Pin P1-19, MOSI when SPI0 in use
- P1_21 = 9, // Version 1, Pin P1-21, MISO when SPI0 in use
- P1_22 = 25, // Version 1, Pin P1-22
- P1_23 = 11, // Version 1, Pin P1-23, CLK when SPI0 in use
- P1_24 = 8, // Version 1, Pin P1-24, CE0 when SPI0 in use
- P1_26 = 7 // Version 1, Pin P1-26, CE1 when SPI0 in use
- }
- );
- enum_values!(
- pub enum RpiV2GpioP1 { // RPi Version 2, new plug P5
- P1_03 = 2, // Version 2, Pin P1-03
- P1_05 = 3, // Version 2, Pin P1-05
- P1_07 = 4, // Version 2, Pin P1-07
- P1_08 = 14, // Version 2, Pin P1-08, defaults to alt function 0 UART0_TXD
- P1_10 = 15, // Version 2, Pin P1-10, defaults to alt function 0 UART0_RXD
- P1_11 = 17, // Version 2, Pin P1-11
- P1_12 = 18, // Version 2, Pin P1-12, can be PWM channel 0 in ALT FUN 5
- P1_13 = 27, // Version 2, Pin P1-13
- P1_15 = 22, // Version 2, Pin P1-15
- P1_16 = 23, // Version 2, Pin P1-16
- P1_18 = 24, // Version 2, Pin P1-18
- P1_19 = 10, // Version 2, Pin P1-19, MOSI when SPI0 in use
- P1_21 = 9, // Version 2, Pin P1-21, MISO when SPI0 in use
- P1_22 = 25, // Version 2, Pin P1-22
- P1_23 = 11, // Version 2, Pin P1-23, CLK when SPI0 in use
- P1_24 = 8, // Version 2, Pin P1-24, CE0 when SPI0 in use
- P1_26 = 7, // Version 2, Pin P1-26, CE1 when SPI0 in use
- P1_29 = 5, // Version 2, Pin P1-29
- P1_31 = 6, // Version 2, Pin P1-31
- P1_32 = 12, // Version 2, Pin P1-32
- P1_33 = 13, // Version 2, Pin P1-33
- P1_35 = 19, // Version 2, Pin P1-35, can be PWM channel 1 in ALT FUN 5
- P1_36 = 16, // Version 2, Pin P1-36
- P1_37 = 26, // Version 2, Pin P1-37
- P1_38 = 20, // Version 2, Pin P1-38
- P1_40 = 21, // Version 2, Pin P1-40
- // RPi Version 2, new plug P5
- P5_03 = 28, // Version 2, Pin P5-03
- P5_04 = 29, // Version 2, Pin P5-04
- P5_05 = 30, // Version 2, Pin P5-05
- P5_06 = 31 // Version 2, Pin P5-06
- }
- );
- enum_values!(
- pub enum RpiBPlusGpioJ8 { // RPi B+ J8 header, also RPi 2 40 pin GPIO header
- J8_03 = 2, // B+, Pin J8-03
- J8_05 = 3, // B+, Pin J8-05
- J8_07 = 4, // B+, Pin J8-07
- J8_08 = 14, // B+, Pin J8-08, defaults to alt function 0 UART0_TXD
- J8_10 = 15, // B+, Pin J8-10, defaults to alt function 0 UART0_RXD
- J8_11 = 17, // B+, Pin J8-11
- J8_12 = 18, // B+, Pin J8-12, can be PWM channel 0 in ALT FUN 5
- J8_13 = 27, // B+, Pin J8-13
- J8_15 = 22, // B+, Pin J8-15
- J8_16 = 23, // B+, Pin J8-16
- J8_18 = 24, // B+, Pin J8-18
- J8_19 = 10, // B+, Pin J8-19, MOSI when SPI0 in use
- J8_21 = 9, // B+, Pin J8-21, MISO when SPI0 in use
- J8_22 = 25, // B+, Pin J8-22
- J8_23 = 11, // B+, Pin J8-23, CLK when SPI0 in use
- J8_24 = 8, // B+, Pin J8-24, CE0 when SPI0 in use
- J8_26 = 7, // B+, Pin J8-26, CE1 when SPI0 in use
- J8_29 = 5, // B+, Pin J8-29,
- J8_31 = 6, // B+, Pin J8-31,
- J8_32 = 12, // B+, Pin J8-32,
- J8_33 = 13, // B+, Pin J8-33,
- J8_35 = 19, // B+, Pin J8-35, can be PWM channel 1 in ALT FUN 5
- J8_36 = 16, // B+, Pin J8-36,
- J8_37 = 26, // B+, Pin J8-37,
- J8_38 = 20, // B+, Pin J8-38,
- J8_40 = 21 // B+, Pin J8-40,
- }
- );
- pub trait Supported {}
- impl Supported for RpiV1GpioP1 {}
- impl Supported for RpiV2GpioP1 {}
- impl Supported for RpiBPlusGpioJ8 {}
- use crate::{RpiVersion, RpiV1, RpiV2, RpiBPlus};
- impl RpiVersion for RpiV1 {
- type Pin = RpiV1GpioP1;
- const PINS: &'static[Self::Pin] = RpiV1GpioP1::VALUES;
- }
- impl From<RpiV1GpioP1> for u8 {
- fn from(pin: RpiV1GpioP1) -> Self {
- pin as u8
- }
- }
- impl RpiVersion for RpiV2 {
- type Pin = RpiV2GpioP1;
- const PINS: &'static[Self::Pin] = RpiV2GpioP1::VALUES;
- }
- impl From<RpiV2GpioP1> for u8 {
- fn from(pin: RpiV2GpioP1) -> Self {
- pin as u8
- }
- }
- impl RpiVersion for RpiBPlus {
- type Pin = RpiBPlusGpioJ8;
- const PINS: &'static[Self::Pin] = RpiBPlusGpioJ8::VALUES;
- }
- impl From<RpiBPlusGpioJ8> for u8 {
- fn from(pin: RpiBPlusGpioJ8) -> Self {
- pin as u8
- }
- }
- }
- pub trait Gpio<V> where V:RpiVersion {
- fn set_debug(debug: bool) -> Result<(), String>;
- fn init() -> Result<Self, String> where Self:Sized;
- fn fsel(&self, pin: V::Pin, mode: u8) -> Result<(), String>;
- fn write(&self, pin: V::Pin, on: u8) -> Result<(), String>;
- fn delay(&self, millis: u32);
- fn delay_microseconds(&self, micros: u64);
- }
- pub trait Driver<'d, 'g, V, G:'g> where V:RpiVersion, G:Gpio<V> {
- type Motor:Motor;
- fn init(gpio: &'g G) -> Result<Self, String> where Self:Sized;
- fn nb_motors(&'d self) -> u8;
- fn get_motor(&'d self, motor: u8) -> Result<Self::Motor, String>;
- }
- pub trait Motor {
- fn init(&self) -> Result<(), String>;
- fn set_micro_step(&self) -> Result<(), String>;
- fn turn_step(&self) -> Result<(), String>;
- fn stop(&self) -> Result<(), String>;
- }
- pub struct Drv8825<'g, V, G:'g> where V:RpiVersion, G: Gpio<V> {
- gpio: &'g G,
- version: PhantomData<V>,
- }
- macro_rules! resolve_gpio_pins {
- ($($ConstRpiPinName:ident => $ConstGpioPinName:ident);* $(;)?) =>
- {
- $(const $ConstGpioPinName:V::Pin = V::PINS[Self::$ConstRpiPinName as usize]);*;
- }
- }
- #[allow(dead_code)]
- impl<'g, V, G:'g> Drv8825<'g, V, G> where V:RpiVersion, G: Gpio<V> {
- const NB_MOTORS:u8 = 2;
- // RPI pins used for motor 1
- const M1_ENABLE_RPI_PIN:RpiV2P1 = RpiV2P1::P1_32;
- const M1_DIR_RPI_PIN:RpiV2P1 = RpiV2P1::P1_33;
- const M1_STEP_RPI_PIN:RpiV2P1 = RpiV2P1::P1_35;
- const M1_M0_RPI_PIN:RpiV2P1 = RpiV2P1::P1_36;
- const M1_M1_RPI_PIN:RpiV2P1 = RpiV2P1::P1_11;
- const M1_M2_RPI_PIN:RpiV2P1 = RpiV2P1::P1_38;
- // RPI pins used for motor 2
- const M2_ENABLE_RPI_PIN:RpiV2P1 = RpiV2P1::P1_07;
- const M2_DIR_RPI_PIN:RpiV2P1 = RpiV2P1::P1_18;
- const M2_STEP_RPI_PIN:RpiV2P1 = RpiV2P1::P1_12;
- const M2_M0_RPI_PIN:RpiV2P1 = RpiV2P1::P1_40;
- const M2_M1_RPI_PIN:RpiV2P1 = RpiV2P1::P1_15;
- const M2_M2_RPI_PIN:RpiV2P1 = RpiV2P1::P1_13;
- resolve_gpio_pins!(
- // GPIO pins used for motor 1
- M1_ENABLE_RPI_PIN => M1_ENABLE_GPIO_PIN;
- M1_DIR_RPI_PIN => M1_DIR_GPIO_PIN;
- M1_STEP_RPI_PIN => M1_STEP_GPIO_PIN;
- M1_M0_RPI_PIN => M1_M0_GPIO_PIN;
- M1_M1_RPI_PIN => M1_M1_GPIO_PIN;
- M1_M2_RPI_PIN => M1_M2_GPIO_PIN;
- // GPIO pins used for motor 2
- M2_ENABLE_RPI_PIN => M2_ENABLE_GPIO_PIN;
- M2_DIR_RPI_PIN => M2_DIR_GPIO_PIN;
- M2_STEP_RPI_PIN => M2_STEP_GPIO_PIN;
- M2_M0_RPI_PIN => M2_M0_GPIO_PIN;
- M2_M1_RPI_PIN => M2_M1_GPIO_PIN;
- M2_M2_RPI_PIN => M2_M2_GPIO_PIN;
- );
- }
- macro_rules! debug_pins {
- ($($ConstRpiPinName:expr, $ConstGpioPinName:expr),* $(,)?) =>
- {
- $(error!("{} = {:?} -> {} = {:?}",
- String::from(stringify!($ConstRpiPinName)), $ConstRpiPinName,
- String::from(stringify!($ConstGpioPinName)), u8::from($ConstGpioPinName)));*;
- }
- }
- impl<'d, 'g:'d, V:'static, G:'g> Driver<'d, 'g, V, G> for Drv8825<'g, V, G> where V:RpiVersion, u8: std::convert::From<<V as RpiVersion>::Pin>, G: Gpio<V> {
- fn init(gpio: &'g G) -> Result<Drv8825<'g, V, G>, String> {
- debug_pins!(
- // Pins used for motor 1
- Self::M1_ENABLE_RPI_PIN, Self::M1_ENABLE_GPIO_PIN,
- Self::M1_DIR_RPI_PIN, Self::M1_DIR_GPIO_PIN,
- Self::M1_STEP_RPI_PIN, Self::M1_STEP_GPIO_PIN,
- Self::M1_M0_RPI_PIN, Self::M1_M0_GPIO_PIN,
- Self::M1_M1_RPI_PIN, Self::M1_M1_GPIO_PIN,
- Self::M1_M2_RPI_PIN, Self::M1_M2_GPIO_PIN,
- // Pins used for motor 2
- Self::M2_ENABLE_RPI_PIN, Self::M2_ENABLE_GPIO_PIN,
- Self::M2_DIR_RPI_PIN, Self::M2_DIR_GPIO_PIN,
- Self::M2_STEP_RPI_PIN, Self::M2_STEP_GPIO_PIN,
- Self::M2_M0_RPI_PIN, Self::M2_M0_GPIO_PIN,
- Self::M2_M1_RPI_PIN, Self::M2_M1_GPIO_PIN,
- Self::M2_M2_RPI_PIN, Self::M2_M2_GPIO_PIN,
- );
- Ok(Drv8825{gpio: gpio, version: PhantomData})
- }
- fn nb_motors(&'d self) -> u8 {
- Self::NB_MOTORS
- }
- type Motor = Drv8825Motor<'d, 'g, V, G>;
- fn get_motor(&'d self, motor: u8) -> Result<Self::Motor, String> {
- let motor = match motor {
- 0 => Ok(Drv8825Motor{driver: self, enable_pin: Self::M1_ENABLE_GPIO_PIN, dir_pin: Self::M1_DIR_GPIO_PIN, step_pin: Self::M1_STEP_GPIO_PIN, m0_pin: Self::M1_M0_GPIO_PIN, m1_pin: Self::M1_M1_GPIO_PIN, m2_pin: Self::M1_M2_GPIO_PIN}),
- 1 => Ok(Drv8825Motor{driver: self, enable_pin: Self::M2_ENABLE_GPIO_PIN, dir_pin: Self::M2_DIR_GPIO_PIN, step_pin: Self::M2_STEP_GPIO_PIN, m0_pin: Self::M2_M0_GPIO_PIN, m1_pin: Self::M2_M1_GPIO_PIN, m2_pin: Self::M2_M2_GPIO_PIN}),
- x => Err(format!("Invalid mode value (must be in range 0..1): {}", x))
- }?;
- motor.init()?;
- Ok(motor)
- }
- }
- pub struct Drv8825Motor<'d, 'g, V:'static, G:'g> where V:RpiVersion, G: Gpio<V> {
- driver: &'d Drv8825<'g, V, G>,
- enable_pin: V::Pin,
- dir_pin: V::Pin,
- step_pin: V::Pin,
- m0_pin: V::Pin,
- m1_pin: V::Pin,
- m2_pin: V::Pin,
- }
- impl<'d, 'g, V, G:'g> Motor for Drv8825Motor<'d, 'g, V, G> where V:RpiVersion, G:Gpio<V> {
- fn init(&self) -> Result<(), String> {
- error!("initializing motor");
- self.driver.gpio.fsel(self.enable_pin, 1)?;
- self.driver.gpio.fsel(self.dir_pin, 1)?;
- self.driver.gpio.fsel(self.step_pin, 1)?;
- self.driver.gpio.fsel(self.m0_pin, 1)?;
- self.driver.gpio.fsel(self.m1_pin, 1)?;
- self.driver.gpio.fsel(self.m2_pin, 1)
- }
- fn set_micro_step(&self) -> Result<(), String> {
- error!("set_micro_step");
- self.driver.gpio.write(self.m0_pin, 0)?;
- self.driver.gpio.write(self.m1_pin, 0)?;
- self.driver.gpio.write(self.m2_pin, 0)
- }
- fn turn_step(&self) -> Result<(), String> {
- error!("turn_step");
- self.driver.gpio.write(self.enable_pin, 0)?;
- self.driver.gpio.write(self.dir_pin, 0)?;
- self.driver.gpio.write(self.step_pin, 1)?;
- self.driver.gpio.delay(5);
- self.driver.gpio.write(self.step_pin, 0)?;
- self.driver.gpio.delay(5);
- Ok(())
- }
- fn stop(&self) -> Result<(), String> {
- error!("stopping motor");
- self.driver.gpio.write(self.enable_pin, 1)
- }
- }
- impl<'d, 'g, V, G:'g> Drop for Drv8825Motor<'d, 'g, V, G> where V:RpiVersion, G:Gpio<V> {
- fn drop(&mut self) {
- match Motor::stop(self) {
- Ok(_) => (),
- Err(msg) => eprintln!("Droping Fail motor failed: {}", msg)
- }
- }
- }
- #[macro_use] extern crate lazy_static;
- #[macro_use] extern crate log;
- extern crate env_logger;
- fn main() -> Result<(), String> {
- env_logger::init();
- let gpio = initialize_gpio::<RpiV2, Bcm2835>()?;
- let driver = initialize_driver::<RpiV2, Bcm2835, Drv8825<RpiV2, Bcm2835>>(&gpio)?;
- driver.nb_motors();
- let motor1 = driver.get_motor(0)?;
- motor1.set_micro_step()?;
- motor1.turn_step()?;
- Ok(())
- }
- fn initialize_gpio<V, G>() -> Result<G, String> where V:RpiVersion, G:Gpio<V> {
- G::set_debug(false)?;
- let gpio: G = Gpio::init()?;
- Ok(gpio)
- }
- fn initialize_driver<'d, 'g, V, G:'g, D>(gpio: &'g G) -> Result<D, String> where V:RpiVersion, D:Driver<'d, 'g, V, G>, G:Gpio<V> {
- let driver: D = Driver::init(gpio)?;
- Ok(driver)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement