output channelization
This commit is contained in:
@@ -1,2 +1,52 @@
|
||||
pub const MCP23017_A_LED: u8 = 7;
|
||||
pub const MCP23017_B_LED: u8 = 7;
|
||||
#![allow(dead_code)]
|
||||
|
||||
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::fmt::{Debug, Display, Formatter};
|
||||
use embedded_hal::i2c::ErrorKind;
|
||||
|
||||
#[derive(Copy, Clone, Debug)]
|
||||
pub struct WrappingError<ERR: Debug>(pub ERR);
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
use crate::hardware::error::WrappingError;
|
||||
use anyhow::{ensure, Result};
|
||||
use embedded_hal::spi::SpiDevice;
|
||||
use log::trace;
|
||||
use std::fmt::{Debug, Formatter};
|
||||
use crate::hardware::error::WrappingError;
|
||||
|
||||
pub struct Mcp3208<SPI> {
|
||||
spi: SPI,
|
||||
|
||||
@@ -4,10 +4,13 @@ use anyhow::Result;
|
||||
use embedded_hal::pwm::SetDutyCycle;
|
||||
|
||||
pub trait Hardware {
|
||||
type Mcp23017<'a>: Mcp23017 + Sync
|
||||
where
|
||||
Self: 'a;
|
||||
type Pwm: SetDutyCycle<Error: std::error::Error + Sync + Send> + Sync;
|
||||
|
||||
fn new_mcp23017_a(&self) -> Result<impl Mcp23017 + Sync>;
|
||||
fn new_mcp23017_b(&self) -> Result<impl Mcp23017 + Sync>;
|
||||
fn new_mcp23017_a(&self) -> Result<Self::Mcp23017<'_>>;
|
||||
fn new_mcp23017_b(&self) -> Result<Self::Mcp23017<'_>>;
|
||||
|
||||
fn new_pwm0(&self) -> Result<Self::Pwm>;
|
||||
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
mod pwm;
|
||||
|
||||
use crate::hardware::mcp23017::{Mcp23017, Mcp23017Driver};
|
||||
use crate::hardware::mcp23017::Mcp23017Driver;
|
||||
use crate::hardware::mcp3208::Mcp3208;
|
||||
use crate::hardware::mct8316a::{Mct8316AVDriver, Mct8316a};
|
||||
use crate::hardware::raspi::pwm::PwmWrapper;
|
||||
use crate::hardware::sim::mct8316a::SimMct8316a;
|
||||
use crate::hardware::Hardware;
|
||||
use anyhow::Result;
|
||||
use embedded_hal_bus::i2c::MutexDevice;
|
||||
@@ -15,7 +16,6 @@ use rpi_pal::spi::SimpleHalSpiDevice;
|
||||
use rpi_pal::spi::{Bus, Mode, SlaveSelect, Spi};
|
||||
use std::cell::RefCell;
|
||||
use std::sync::Mutex;
|
||||
use crate::hardware::sim::mct8316a::SimMct8316a;
|
||||
|
||||
const CLOCK_1MHZ: u32 = 1_000_000;
|
||||
|
||||
@@ -49,14 +49,15 @@ impl RaspiHardware {
|
||||
}
|
||||
|
||||
impl Hardware for RaspiHardware {
|
||||
type Mcp23017<'a> = Mcp23017Driver<MutexDevice<'a, I2c>>;
|
||||
type Pwm = PwmWrapper;
|
||||
|
||||
fn new_mcp23017_a(&self) -> Result<impl Mcp23017> {
|
||||
fn new_mcp23017_a(&self) -> Result<Self::Mcp23017<'_>> {
|
||||
trace!("RaspiHardware::new_mcp23017_a()");
|
||||
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()");
|
||||
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 log::trace;
|
||||
use rpi_pal::pwm::Pwm;
|
||||
use std::time::Duration;
|
||||
use crate::hardware::error::WrappingError;
|
||||
|
||||
const PWM_PERIOD: Duration = Duration::from_micros(1000); // 1kHz
|
||||
|
||||
|
||||
@@ -1,20 +1,19 @@
|
||||
|
||||
use std::sync::Mutex;
|
||||
use crate::hardware::Hardware;
|
||||
use crate::hardware::mcp23017::{Mcp23017, Mcp23017Driver};
|
||||
use crate::hardware::mcp23017::Mcp23017Driver;
|
||||
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::mct8316a::SimMct8316a;
|
||||
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 {
|
||||
mcp23017a: Mutex<SimMcp23017>,
|
||||
mcp23017b: Mutex<SimMcp23017>,
|
||||
mct8316a: Mutex<SimMct8316a>,
|
||||
battery_voltage: f64
|
||||
battery_voltage: f64,
|
||||
}
|
||||
|
||||
impl SimHardware {
|
||||
@@ -29,14 +28,15 @@ impl SimHardware {
|
||||
}
|
||||
|
||||
impl Hardware for SimHardware {
|
||||
type Mcp23017<'a> = Mcp23017Driver<MutexDevice<'a, SimMcp23017>>;
|
||||
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()");
|
||||
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()");
|
||||
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 std::fmt::{Display, Formatter};
|
||||
|
||||
pub struct SimMcp23017 {
|
||||
|
||||
}
|
||||
pub struct SimMcp23017 {}
|
||||
|
||||
impl SimMcp23017 {
|
||||
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);
|
||||
|
||||
pub struct SimMct8316a {
|
||||
data: HashMap<u32, u32>
|
||||
data: HashMap<u32, u32>,
|
||||
}
|
||||
|
||||
impl SimMct8316a {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use std::fmt::{Display, Formatter};
|
||||
use embedded_hal::pwm::{ErrorKind, ErrorType, SetDutyCycle};
|
||||
use log::trace;
|
||||
use std::fmt::{Display, Formatter};
|
||||
|
||||
pub struct SimPwm {
|
||||
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::mcp23017::Mcp23017OutputPin;
|
||||
use crate::hardware::mcp23017::{Mcp23017, Mcp23017Task};
|
||||
@@ -61,13 +61,13 @@ pub fn run() -> Result<()> {
|
||||
Mcp23017Task::new(&mcp23017_b, "mcp23017-b".into())
|
||||
.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);
|
||||
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);
|
||||
|
||||
info!("Starting Main Loop");
|
||||
loop {
|
||||
for _ in 0..2 {
|
||||
debug!("A On");
|
||||
led_pin_a.set_state(PinState::High);
|
||||
sleep(Duration::from_secs(1));
|
||||
|
||||
Reference in New Issue
Block a user