Skip to content

Commit

Permalink
Pass the pin numbering scheme from hw to UI as per #397
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewdavidmackenzie committed Sep 8, 2024
1 parent 85765a4 commit 98817fc
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 6 deletions.
5 changes: 4 additions & 1 deletion porky/src/porky.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ use heapless::FnvIndexMap;
use heapless::Vec;
use hw_definition::config::HardwareConfigMessage::*;
use hw_definition::config::{HardwareConfig, HardwareConfigMessage, InputPull, LevelChange};
use hw_definition::description::{HardwareDescription, HardwareDetails, PinDescriptionSet};
use hw_definition::description::{
HardwareDescription, HardwareDetails, PinDescriptionSet, PinNumberingScheme,
};
use hw_definition::pin_function::PinFunction;
use hw_definition::{BCMPinNumber, PinLevel};
use panic_probe as _;
Expand Down Expand Up @@ -341,6 +343,7 @@ async fn tcp_accept(socket: &mut TcpSocket<'_>, ip_address: &Ipv4Address, device
let hw_desc = HardwareDescription {
details,
pins: PinDescriptionSet {
pin_numbering: PinNumberingScheme::CounterClockwise,
pins: Vec::from_slice(&PIN_DESCRIPTIONS).unwrap(),
},
};
Expand Down
5 changes: 4 additions & 1 deletion src/hw/fake_hw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ use std::time::{Duration, SystemTime, UNIX_EPOCH};
use std::{io, thread};

use crate::hw::{BCMPinNumber, PinFunction, PinLevel};
use crate::hw_definition::description::{HardwareDetails, PinDescription, PinDescriptionSet};
use crate::hw_definition::description::{
HardwareDetails, PinDescription, PinDescriptionSet, PinNumberingScheme,
};

use super::Hardware;
use super::HardwareDescription;
Expand Down Expand Up @@ -36,6 +38,7 @@ impl Hardware for HW {
model: "Fake Hardware".to_string(),
},
pins: PinDescriptionSet {
pin_numbering: PinNumberingScheme::Rows,
pins: FAKE_PIN_DESCRIPTIONS.to_vec(),
},
})
Expand Down
5 changes: 4 additions & 1 deletion src/hw/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@ pub trait Hardware {
mod test {
use crate::hw;
use crate::hw::Hardware;
use crate::hw_definition::description::{PinDescription, PinDescriptionSet};
use crate::hw_definition::description::{
PinDescription, PinDescriptionSet, PinNumberingScheme,
};
use crate::hw_definition::pin_function::PinFunction;
use std::borrow::Cow;

Expand Down Expand Up @@ -209,6 +211,7 @@ mod test {
pin7.clone(),
];
let pin_set = PinDescriptionSet {
pin_numbering: PinNumberingScheme::Rows,
pins: pins.to_vec(),
};
assert_eq!(
Expand Down
1 change: 1 addition & 0 deletions src/hw/pi_hw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ impl Hardware for HW {
Ok(HardwareDescription {
details: Self::get_details()?,
pins: PinDescriptionSet {
pin_numbering: PinNumberingScheme::Rows,
pins: GPIO_PIN_DESCRIPTIONS.to_vec(),
},
})
Expand Down
29 changes: 26 additions & 3 deletions src/hw_definition/description.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,18 @@ pub struct HardwareDetails<'a> {
#[cfg(feature = "std")]
/// [PinDescription] describes a pins in the connected hardware.
/// Array indexed from 0 so, index = board_pin_number -1, as pin numbering start at 1
#[derive(Serialize)]
#[cfg_attr(feature = "std", derive(Debug, Clone, Deserialize))]
#[derive(Serialize, Debug, Clone, Deserialize)]
pub struct PinDescriptionSet {
pub(crate) pin_numbering: PinNumberingScheme,
pub(crate) pins: Vec<PinDescription>,
}

#[cfg(not(feature = "std"))]
/// [PinDescription] describes a pins in the connected hardware.
/// Array indexed from 0 so, index = board_pin_number -1, as pin numbering start at 1
#[derive(Serialize)]
#[cfg_attr(feature = "std", derive(Debug, Clone, Deserialize))]
pub struct PinDescriptionSet<'a> {
pub(crate) pin_numbering: PinNumberingScheme,
pub(crate) pins: Vec<PinDescription<'a>, 40>,
}

Expand All @@ -95,3 +95,26 @@ pub struct PinDescription<'a> {
pub name: &'static str,
pub options: &'a [PinFunction],
}

/// RaspberryPi and Raspberry Pi Pico use two different pin numbering schemes
///
/// RPi uses "Rows":
/// - top-left is pin 1
/// - top-right is pin 2
/// and so on down row-by-row
/// - bottom-left is pin 39
/// - bottom-right is pin 40
///
/// RPi Pico numbers pins from one (top left) down the left column then up the right column
/// ending at 40 at the top right
/// - top-left is pin 1
/// - bottom-left is pin 20
/// - bottom-right is pin 21
/// - top-right is pin 40
#[derive(Serialize)]
#[cfg_attr(feature = "std", derive(Debug, Clone, Deserialize))]
#[allow(dead_code)] // for porky
pub enum PinNumberingScheme {
Rows,
CounterClockwise,
}

0 comments on commit 98817fc

Please sign in to comment.