From 26271fcb1726a4ea8adce9606b897d671da3a332 Mon Sep 17 00:00:00 2001 From: Sergey Savelyev Date: Mon, 20 Oct 2025 21:02:57 -0700 Subject: [PATCH] output channelization --- flight/src/hardware/channelization.rs | 54 ++++++++++++++++++++++++++- flight/src/hardware/error/wrapping.rs | 2 +- flight/src/hardware/mcp3208/mod.rs | 2 +- flight/src/hardware/mod.rs | 7 +++- flight/src/hardware/raspi/mod.rs | 9 +++-- flight/src/hardware/raspi/pwm.rs | 2 +- flight/src/hardware/sim/hardware.rs | 20 +++++----- flight/src/hardware/sim/mcp23017.rs | 10 ++--- flight/src/hardware/sim/mct8316a.rs | 2 +- flight/src/hardware/sim/pwm.rs | 2 +- flight/src/lib.rs | 8 ++-- 11 files changed, 84 insertions(+), 34 deletions(-) diff --git a/flight/src/hardware/channelization.rs b/flight/src/hardware/channelization.rs index 7cbf35a..629bf50 100644 --- a/flight/src/hardware/channelization.rs +++ b/flight/src/hardware/channelization.rs @@ -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 { + match self { + PinoutChannel::ExtA(pin) => ext_a.new_output_pin(pin), + PinoutChannel::ExtB(pin) => ext_b.new_output_pin(pin), + } + } +} diff --git a/flight/src/hardware/error/wrapping.rs b/flight/src/hardware/error/wrapping.rs index 42f827d..a29ac4b 100644 --- a/flight/src/hardware/error/wrapping.rs +++ b/flight/src/hardware/error/wrapping.rs @@ -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(pub ERR); diff --git a/flight/src/hardware/mcp3208/mod.rs b/flight/src/hardware/mcp3208/mod.rs index e8fc433..e6228e8 100644 --- a/flight/src/hardware/mcp3208/mod.rs +++ b/flight/src/hardware/mcp3208/mod.rs @@ -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, diff --git a/flight/src/hardware/mod.rs b/flight/src/hardware/mod.rs index 92b112c..0318fe8 100644 --- a/flight/src/hardware/mod.rs +++ b/flight/src/hardware/mod.rs @@ -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 + Sync; - fn new_mcp23017_a(&self) -> Result; - fn new_mcp23017_b(&self) -> Result; + fn new_mcp23017_a(&self) -> Result>; + fn new_mcp23017_b(&self) -> Result>; fn new_pwm0(&self) -> Result; diff --git a/flight/src/hardware/raspi/mod.rs b/flight/src/hardware/raspi/mod.rs index 231192c..bd68175 100644 --- a/flight/src/hardware/raspi/mod.rs +++ b/flight/src/hardware/raspi/mod.rs @@ -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>; type Pwm = PwmWrapper; - fn new_mcp23017_a(&self) -> Result { + fn new_mcp23017_a(&self) -> Result> { trace!("RaspiHardware::new_mcp23017_a()"); Ok(Mcp23017Driver::new(MutexDevice::new(&self.i2c_bus), 0b0100000)) } - fn new_mcp23017_b(&self) -> Result { + fn new_mcp23017_b(&self) -> Result> { trace!("RaspiHardware::new_mcp23017_b()"); Ok(Mcp23017Driver::new(MutexDevice::new(&self.i2c_bus), 0b0100001)) } diff --git a/flight/src/hardware/raspi/pwm.rs b/flight/src/hardware/raspi/pwm.rs index d57b3ba..4ace1b3 100644 --- a/flight/src/hardware/raspi/pwm.rs +++ b/flight/src/hardware/raspi/pwm.rs @@ -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 diff --git a/flight/src/hardware/sim/hardware.rs b/flight/src/hardware/sim/hardware.rs index 77d95af..6f8661d 100644 --- a/flight/src/hardware/sim/hardware.rs +++ b/flight/src/hardware/sim/hardware.rs @@ -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, mcp23017b: Mutex, mct8316a: Mutex, - battery_voltage: f64 + battery_voltage: f64, } impl SimHardware { @@ -29,14 +28,15 @@ impl SimHardware { } impl Hardware for SimHardware { + type Mcp23017<'a> = Mcp23017Driver>; type Pwm = SimPwm; - fn new_mcp23017_a(&self) -> Result { + fn new_mcp23017_a(&self) -> Result> { trace!("SimHardware::new_mcp23017_a()"); Ok(Mcp23017Driver::new(MutexDevice::new(&self.mcp23017a), 0b0100000)) } - fn new_mcp23017_b(&self) -> Result { + fn new_mcp23017_b(&self) -> Result> { trace!("SimHardware::new_mcp23017_b()"); Ok(Mcp23017Driver::new(MutexDevice::new(&self.mcp23017b), 0b0100000)) } diff --git a/flight/src/hardware/sim/mcp23017.rs b/flight/src/hardware/sim/mcp23017.rs index b5805ed..b416a42 100644 --- a/flight/src/hardware/sim/mcp23017.rs +++ b/flight/src/hardware/sim/mcp23017.rs @@ -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 {} } } diff --git a/flight/src/hardware/sim/mct8316a.rs b/flight/src/hardware/sim/mct8316a.rs index 9e173b5..26293d1 100644 --- a/flight/src/hardware/sim/mct8316a.rs +++ b/flight/src/hardware/sim/mct8316a.rs @@ -6,7 +6,7 @@ use std::collections::HashMap; const CRC: crc::Crc = crc::Crc::::new(&crc::CRC_8_SMBUS); pub struct SimMct8316a { - data: HashMap + data: HashMap, } impl SimMct8316a { diff --git a/flight/src/hardware/sim/pwm.rs b/flight/src/hardware/sim/pwm.rs index 3624c30..7ba97ab 100644 --- a/flight/src/hardware/sim/pwm.rs +++ b/flight/src/hardware/sim/pwm.rs @@ -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, diff --git a/flight/src/lib.rs b/flight/src/lib.rs index 63fe036..93dd3a0 100644 --- a/flight/src/lib.rs +++ b/flight/src/lib.rs @@ -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));