output channelization
This commit is contained in:
@@ -1,2 +1,52 @@
|
|||||||
pub const MCP23017_A_LED: u8 = 7;
|
#![allow(dead_code)]
|
||||||
pub const MCP23017_B_LED: u8 = 7;
|
|
||||||
|
use crate::hardware::mcp23017::{Mcp23017, Mcp23017OutputPin};
|
||||||
|
use anyhow::Result;
|
||||||
|
|
||||||
|
pub const RCS5: PinoutChannel = PinoutChannel::ExtA(0);
|
||||||
|
pub const RCS6: PinoutChannel = PinoutChannel::ExtA(1);
|
||||||
|
pub const RCS7: PinoutChannel = PinoutChannel::ExtA(2);
|
||||||
|
pub const RCS8: PinoutChannel = PinoutChannel::ExtA(3);
|
||||||
|
pub const RCS9: PinoutChannel = PinoutChannel::ExtA(4);
|
||||||
|
pub const MOTOR0_A: PinoutChannel = PinoutChannel::ExtA(5);
|
||||||
|
pub const MOTOR0_B: PinoutChannel = PinoutChannel::ExtA(6);
|
||||||
|
pub const LED_A: PinoutChannel = PinoutChannel::ExtA(7);
|
||||||
|
pub const RCS0: PinoutChannel = PinoutChannel::ExtA(8);
|
||||||
|
pub const RCS1: PinoutChannel = PinoutChannel::ExtA(9);
|
||||||
|
pub const DRIVE0_BRAKE: PinoutChannel = PinoutChannel::ExtA(10);
|
||||||
|
pub const DRIVE0_DIR: PinoutChannel = PinoutChannel::ExtA(11);
|
||||||
|
pub const DRIVE0_DRIVEOFF: PinoutChannel = PinoutChannel::ExtA(12);
|
||||||
|
pub const RCS2: PinoutChannel = PinoutChannel::ExtA(13);
|
||||||
|
pub const RCS3: PinoutChannel = PinoutChannel::ExtA(14);
|
||||||
|
pub const RCS4: PinoutChannel = PinoutChannel::ExtA(15);
|
||||||
|
pub const MOTOR1_A: PinoutChannel = PinoutChannel::ExtB(8);
|
||||||
|
pub const MOTOR1_B: PinoutChannel = PinoutChannel::ExtB(9);
|
||||||
|
pub const MOTOR2_A: PinoutChannel = PinoutChannel::ExtB(10);
|
||||||
|
pub const MOTOR2_B: PinoutChannel = PinoutChannel::ExtB(11);
|
||||||
|
pub const MOTOR3_A: PinoutChannel = PinoutChannel::ExtB(12);
|
||||||
|
pub const MOTOR3_B: PinoutChannel = PinoutChannel::ExtB(13);
|
||||||
|
pub const OUT1: PinoutChannel = PinoutChannel::ExtB(14);
|
||||||
|
pub const OUT2: PinoutChannel = PinoutChannel::ExtB(15);
|
||||||
|
pub const OUT3: PinoutChannel = PinoutChannel::ExtB(0);
|
||||||
|
pub const OUT4: PinoutChannel = PinoutChannel::ExtB(1);
|
||||||
|
pub const OUT5: PinoutChannel = PinoutChannel::ExtB(2);
|
||||||
|
pub const OUT6: PinoutChannel = PinoutChannel::ExtB(3);
|
||||||
|
pub const OUT7: PinoutChannel = PinoutChannel::ExtB(4);
|
||||||
|
pub const OUT8: PinoutChannel = PinoutChannel::ExtB(5);
|
||||||
|
pub const OUT9: PinoutChannel = PinoutChannel::ExtB(6);
|
||||||
|
pub const LED_B: PinoutChannel = PinoutChannel::ExtB(7);
|
||||||
|
|
||||||
|
#[derive(Copy, Clone, Debug)]
|
||||||
|
pub enum PinoutChannel {
|
||||||
|
ExtA(u8),
|
||||||
|
ExtB(u8),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PinoutChannel {
|
||||||
|
pub fn new_output_pin<'a, MCP: Mcp23017>(self, ext_a: &'a MCP, ext_b: &'a MCP) -> Result<impl Mcp23017OutputPin + 'a> {
|
||||||
|
match self {
|
||||||
|
PinoutChannel::ExtA(pin) => ext_a.new_output_pin(pin),
|
||||||
|
PinoutChannel::ExtB(pin) => ext_b.new_output_pin(pin),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
|
use embedded_hal::i2c::ErrorKind;
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
use std::fmt::{Debug, Display, Formatter};
|
use std::fmt::{Debug, Display, Formatter};
|
||||||
use embedded_hal::i2c::ErrorKind;
|
|
||||||
|
|
||||||
#[derive(Copy, Clone, Debug)]
|
#[derive(Copy, Clone, Debug)]
|
||||||
pub struct WrappingError<ERR: Debug>(pub ERR);
|
pub struct WrappingError<ERR: Debug>(pub ERR);
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
|
use crate::hardware::error::WrappingError;
|
||||||
use anyhow::{ensure, Result};
|
use anyhow::{ensure, Result};
|
||||||
use embedded_hal::spi::SpiDevice;
|
use embedded_hal::spi::SpiDevice;
|
||||||
use log::trace;
|
use log::trace;
|
||||||
use std::fmt::{Debug, Formatter};
|
use std::fmt::{Debug, Formatter};
|
||||||
use crate::hardware::error::WrappingError;
|
|
||||||
|
|
||||||
pub struct Mcp3208<SPI> {
|
pub struct Mcp3208<SPI> {
|
||||||
spi: SPI,
|
spi: SPI,
|
||||||
|
|||||||
@@ -4,10 +4,13 @@ use anyhow::Result;
|
|||||||
use embedded_hal::pwm::SetDutyCycle;
|
use embedded_hal::pwm::SetDutyCycle;
|
||||||
|
|
||||||
pub trait Hardware {
|
pub trait Hardware {
|
||||||
|
type Mcp23017<'a>: Mcp23017 + Sync
|
||||||
|
where
|
||||||
|
Self: 'a;
|
||||||
type Pwm: SetDutyCycle<Error: std::error::Error + Sync + Send> + Sync;
|
type Pwm: SetDutyCycle<Error: std::error::Error + Sync + Send> + Sync;
|
||||||
|
|
||||||
fn new_mcp23017_a(&self) -> Result<impl Mcp23017 + Sync>;
|
fn new_mcp23017_a(&self) -> Result<Self::Mcp23017<'_>>;
|
||||||
fn new_mcp23017_b(&self) -> Result<impl Mcp23017 + Sync>;
|
fn new_mcp23017_b(&self) -> Result<Self::Mcp23017<'_>>;
|
||||||
|
|
||||||
fn new_pwm0(&self) -> Result<Self::Pwm>;
|
fn new_pwm0(&self) -> Result<Self::Pwm>;
|
||||||
|
|
||||||
|
|||||||
@@ -1,9 +1,10 @@
|
|||||||
mod pwm;
|
mod pwm;
|
||||||
|
|
||||||
use crate::hardware::mcp23017::{Mcp23017, Mcp23017Driver};
|
use crate::hardware::mcp23017::Mcp23017Driver;
|
||||||
use crate::hardware::mcp3208::Mcp3208;
|
use crate::hardware::mcp3208::Mcp3208;
|
||||||
use crate::hardware::mct8316a::{Mct8316AVDriver, Mct8316a};
|
use crate::hardware::mct8316a::{Mct8316AVDriver, Mct8316a};
|
||||||
use crate::hardware::raspi::pwm::PwmWrapper;
|
use crate::hardware::raspi::pwm::PwmWrapper;
|
||||||
|
use crate::hardware::sim::mct8316a::SimMct8316a;
|
||||||
use crate::hardware::Hardware;
|
use crate::hardware::Hardware;
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use embedded_hal_bus::i2c::MutexDevice;
|
use embedded_hal_bus::i2c::MutexDevice;
|
||||||
@@ -15,7 +16,6 @@ use rpi_pal::spi::SimpleHalSpiDevice;
|
|||||||
use rpi_pal::spi::{Bus, Mode, SlaveSelect, Spi};
|
use rpi_pal::spi::{Bus, Mode, SlaveSelect, Spi};
|
||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
use std::sync::Mutex;
|
use std::sync::Mutex;
|
||||||
use crate::hardware::sim::mct8316a::SimMct8316a;
|
|
||||||
|
|
||||||
const CLOCK_1MHZ: u32 = 1_000_000;
|
const CLOCK_1MHZ: u32 = 1_000_000;
|
||||||
|
|
||||||
@@ -49,14 +49,15 @@ impl RaspiHardware {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Hardware for RaspiHardware {
|
impl Hardware for RaspiHardware {
|
||||||
|
type Mcp23017<'a> = Mcp23017Driver<MutexDevice<'a, I2c>>;
|
||||||
type Pwm = PwmWrapper;
|
type Pwm = PwmWrapper;
|
||||||
|
|
||||||
fn new_mcp23017_a(&self) -> Result<impl Mcp23017> {
|
fn new_mcp23017_a(&self) -> Result<Self::Mcp23017<'_>> {
|
||||||
trace!("RaspiHardware::new_mcp23017_a()");
|
trace!("RaspiHardware::new_mcp23017_a()");
|
||||||
Ok(Mcp23017Driver::new(MutexDevice::new(&self.i2c_bus), 0b0100000))
|
Ok(Mcp23017Driver::new(MutexDevice::new(&self.i2c_bus), 0b0100000))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn new_mcp23017_b(&self) -> Result<impl Mcp23017> {
|
fn new_mcp23017_b(&self) -> Result<Self::Mcp23017<'_>> {
|
||||||
trace!("RaspiHardware::new_mcp23017_b()");
|
trace!("RaspiHardware::new_mcp23017_b()");
|
||||||
Ok(Mcp23017Driver::new(MutexDevice::new(&self.i2c_bus), 0b0100001))
|
Ok(Mcp23017Driver::new(MutexDevice::new(&self.i2c_bus), 0b0100001))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
|
use crate::hardware::error::WrappingError;
|
||||||
use embedded_hal::pwm::{ErrorType, SetDutyCycle};
|
use embedded_hal::pwm::{ErrorType, SetDutyCycle};
|
||||||
use log::trace;
|
use log::trace;
|
||||||
use rpi_pal::pwm::Pwm;
|
use rpi_pal::pwm::Pwm;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use crate::hardware::error::WrappingError;
|
|
||||||
|
|
||||||
const PWM_PERIOD: Duration = Duration::from_micros(1000); // 1kHz
|
const PWM_PERIOD: Duration = Duration::from_micros(1000); // 1kHz
|
||||||
|
|
||||||
|
|||||||
@@ -1,20 +1,19 @@
|
|||||||
|
use crate::hardware::mcp23017::Mcp23017Driver;
|
||||||
use std::sync::Mutex;
|
|
||||||
use crate::hardware::Hardware;
|
|
||||||
use crate::hardware::mcp23017::{Mcp23017, Mcp23017Driver};
|
|
||||||
use crate::hardware::mct8316a::{Mct8316AVDriver, Mct8316a};
|
use crate::hardware::mct8316a::{Mct8316AVDriver, Mct8316a};
|
||||||
use anyhow::Result;
|
|
||||||
use embedded_hal_bus::i2c::MutexDevice;
|
|
||||||
use log::trace;
|
|
||||||
use crate::hardware::sim::mcp23017::SimMcp23017;
|
use crate::hardware::sim::mcp23017::SimMcp23017;
|
||||||
use crate::hardware::sim::mct8316a::SimMct8316a;
|
use crate::hardware::sim::mct8316a::SimMct8316a;
|
||||||
use crate::hardware::sim::pwm::SimPwm;
|
use crate::hardware::sim::pwm::SimPwm;
|
||||||
|
use crate::hardware::Hardware;
|
||||||
|
use anyhow::Result;
|
||||||
|
use embedded_hal_bus::i2c::MutexDevice;
|
||||||
|
use log::trace;
|
||||||
|
use std::sync::Mutex;
|
||||||
|
|
||||||
pub struct SimHardware {
|
pub struct SimHardware {
|
||||||
mcp23017a: Mutex<SimMcp23017>,
|
mcp23017a: Mutex<SimMcp23017>,
|
||||||
mcp23017b: Mutex<SimMcp23017>,
|
mcp23017b: Mutex<SimMcp23017>,
|
||||||
mct8316a: Mutex<SimMct8316a>,
|
mct8316a: Mutex<SimMct8316a>,
|
||||||
battery_voltage: f64
|
battery_voltage: f64,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SimHardware {
|
impl SimHardware {
|
||||||
@@ -29,14 +28,15 @@ impl SimHardware {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Hardware for SimHardware {
|
impl Hardware for SimHardware {
|
||||||
|
type Mcp23017<'a> = Mcp23017Driver<MutexDevice<'a, SimMcp23017>>;
|
||||||
type Pwm = SimPwm;
|
type Pwm = SimPwm;
|
||||||
|
|
||||||
fn new_mcp23017_a(&self) -> Result<impl Mcp23017 + Sync> {
|
fn new_mcp23017_a(&self) -> Result<Self::Mcp23017<'_>> {
|
||||||
trace!("SimHardware::new_mcp23017_a()");
|
trace!("SimHardware::new_mcp23017_a()");
|
||||||
Ok(Mcp23017Driver::new(MutexDevice::new(&self.mcp23017a), 0b0100000))
|
Ok(Mcp23017Driver::new(MutexDevice::new(&self.mcp23017a), 0b0100000))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn new_mcp23017_b(&self) -> Result<impl Mcp23017 + Sync> {
|
fn new_mcp23017_b(&self) -> Result<Self::Mcp23017<'_>> {
|
||||||
trace!("SimHardware::new_mcp23017_b()");
|
trace!("SimHardware::new_mcp23017_b()");
|
||||||
Ok(Mcp23017Driver::new(MutexDevice::new(&self.mcp23017b), 0b0100000))
|
Ok(Mcp23017Driver::new(MutexDevice::new(&self.mcp23017b), 0b0100000))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,15 +1,11 @@
|
|||||||
use std::fmt::{Display, Formatter};
|
|
||||||
use embedded_hal::i2c::{ErrorKind, ErrorType, I2c, Operation, SevenBitAddress};
|
use embedded_hal::i2c::{ErrorKind, ErrorType, I2c, Operation, SevenBitAddress};
|
||||||
|
use std::fmt::{Display, Formatter};
|
||||||
|
|
||||||
pub struct SimMcp23017 {
|
pub struct SimMcp23017 {}
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
impl SimMcp23017 {
|
impl SimMcp23017 {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Self {
|
Self {}
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ use std::collections::HashMap;
|
|||||||
const CRC: crc::Crc<u8> = crc::Crc::<u8>::new(&crc::CRC_8_SMBUS);
|
const CRC: crc::Crc<u8> = crc::Crc::<u8>::new(&crc::CRC_8_SMBUS);
|
||||||
|
|
||||||
pub struct SimMct8316a {
|
pub struct SimMct8316a {
|
||||||
data: HashMap<u32, u32>
|
data: HashMap<u32, u32>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SimMct8316a {
|
impl SimMct8316a {
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
use std::fmt::{Display, Formatter};
|
|
||||||
use embedded_hal::pwm::{ErrorKind, ErrorType, SetDutyCycle};
|
use embedded_hal::pwm::{ErrorKind, ErrorType, SetDutyCycle};
|
||||||
use log::trace;
|
use log::trace;
|
||||||
|
use std::fmt::{Display, Formatter};
|
||||||
|
|
||||||
pub struct SimPwm {
|
pub struct SimPwm {
|
||||||
duty_cycle: u16,
|
duty_cycle: u16,
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
use crate::hardware::channelization::{MCP23017_A_LED, MCP23017_B_LED};
|
use crate::hardware::channelization::{LED_A, LED_B};
|
||||||
use crate::hardware::initialize;
|
use crate::hardware::initialize;
|
||||||
use crate::hardware::mcp23017::Mcp23017OutputPin;
|
use crate::hardware::mcp23017::Mcp23017OutputPin;
|
||||||
use crate::hardware::mcp23017::{Mcp23017, Mcp23017Task};
|
use crate::hardware::mcp23017::{Mcp23017, Mcp23017Task};
|
||||||
@@ -61,13 +61,13 @@ pub fn run() -> Result<()> {
|
|||||||
Mcp23017Task::new(&mcp23017_b, "mcp23017-b".into())
|
Mcp23017Task::new(&mcp23017_b, "mcp23017-b".into())
|
||||||
.start(scope, &running, 10)?;
|
.start(scope, &running, 10)?;
|
||||||
|
|
||||||
let mut led_pin_a = mcp23017_a.new_output_pin(MCP23017_A_LED)?;
|
let mut led_pin_a = LED_A.new_output_pin(&mcp23017_a, &mcp23017_b)?;
|
||||||
led_pin_a.set_state_on_drop(PinState::Low);
|
led_pin_a.set_state_on_drop(PinState::Low);
|
||||||
let mut led_pin_b = mcp23017_b.new_output_pin(MCP23017_B_LED)?;
|
let mut led_pin_b = LED_B.new_output_pin(&mcp23017_a, &mcp23017_b)?;
|
||||||
led_pin_b.set_state_on_drop(PinState::Low);
|
led_pin_b.set_state_on_drop(PinState::Low);
|
||||||
|
|
||||||
info!("Starting Main Loop");
|
info!("Starting Main Loop");
|
||||||
loop {
|
for _ in 0..2 {
|
||||||
debug!("A On");
|
debug!("A On");
|
||||||
led_pin_a.set_state(PinState::High);
|
led_pin_a.set_state(PinState::High);
|
||||||
sleep(Duration::from_secs(1));
|
sleep(Duration::from_secs(1));
|
||||||
|
|||||||
Reference in New Issue
Block a user