Skip to content

Commit

Permalink
Merge pull request #828 from andrewdavidmackenzie/pigg_756
Browse files Browse the repository at this point in the history
Reduced view that shows only configured pins
  • Loading branch information
andrewdavidmackenzie authored Jan 28, 2025
2 parents f43b7cb + 8d4f656 commit b040696
Show file tree
Hide file tree
Showing 10 changed files with 155 additions and 79 deletions.
5 changes: 1 addition & 4 deletions porky/src/porky.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ use embassy_rp::usb::InterruptHandler as USBInterruptHandler;
use embassy_rp::watchdog::Watchdog;
use embassy_sync::blocking_mutex::raw::{NoopRawMutex, ThreadModeRawMutex};
use embassy_sync::channel::Channel;
use heapless::Vec;
use panic_probe as _;
use static_cell::StaticCell;

Expand Down Expand Up @@ -124,9 +123,7 @@ fn hardware_description(serial: &str) -> HardwareDescription {

HardwareDescription {
details,
pins: PinDescriptionSet {
pins: Vec::from_slice(&PIN_DESCRIPTIONS).unwrap(),
},
pins: PinDescriptionSet::new(&PIN_DESCRIPTIONS),
}
}

Expand Down
25 changes: 14 additions & 11 deletions src/file_helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ async fn save_via_picker(gpio_config: HardwareConfig) -> io::Result<bool> {
if let Some(handle) = rfd::AsyncFileDialog::new()
.add_filter("Pigg Config", &["pigg"])
.set_title("Choose file")
.set_directory(env::current_dir().unwrap())
.set_directory(env::current_dir()?)
.save_file()
.await
{
let path: std::path::PathBuf = handle.path().to_owned();
let path_str = path.display().to_string();
gpio_config.save(&path_str).unwrap();
gpio_config.save(&path_str)?;
Ok(true)
} else {
Ok(false)
Expand All @@ -56,15 +56,18 @@ async fn save_via_picker(gpio_config: HardwareConfig) -> io::Result<bool> {

/// Utility function that saves the [HardwareConfig] to a file using `Task::perform` and uses
/// the result to return correct [Message]
pub fn save(gpio_config: HardwareConfig) -> Task<Message> {
Task::perform(save_via_picker(gpio_config), |result| match result {
Ok(true) => Message::ConfigSaved,
Ok(false) => Message::InfoRow(ShowStatusMessage(Info("File save cancelled".into()))),
Err(e) => Message::InfoRow(ShowStatusMessage(Error(
"Error saving file".into(),
format!("Error saving file. {e}"),
))),
})
pub fn save(gpio_config: &HardwareConfig) -> Task<Message> {
Task::perform(
save_via_picker(gpio_config.clone()),
|result| match result {
Ok(true) => Message::ConfigSaved,
Ok(false) => Message::InfoRow(ShowStatusMessage(Info("File save cancelled".into()))),
Err(e) => Message::InfoRow(ShowStatusMessage(Error(
"Error saving file".into(),
format!("Error saving file. {e}"),
))),
},
)
}

/// Utility function that loads config from a file using `Task::perform` of the load picker
Expand Down
12 changes: 4 additions & 8 deletions src/hw/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,7 @@ impl HW {
pub fn description(&self) -> io::Result<HardwareDescription> {
Ok(HardwareDescription {
details: Self::get_details()?,
pins: PinDescriptionSet {
pins: GPIO_PIN_DESCRIPTIONS.to_vec(),
},
pins: PinDescriptionSet::new(&GPIO_PIN_DESCRIPTIONS),
})
}

Expand Down Expand Up @@ -459,12 +457,10 @@ mod test {
pin7.clone(),
pin7.clone(),
];
let pin_set = PinDescriptionSet {
pins: pins.to_vec(),
};
let pin_set = PinDescriptionSet::new(&pins);
assert_eq!(
pin_set
.pins
.pins()
.first()
.expect("Could not get pin")
.bcm
Expand All @@ -473,7 +469,7 @@ mod test {
);
assert_eq!(
pin_set
.pins
.pins()
.get(1)
.expect("Could not get pin")
.bcm
Expand Down
11 changes: 2 additions & 9 deletions src/hw/hardware_description.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use crate::hw_definition::description::{HardwareDetails, PinDescription, PinDescriptionSet};
use std::fmt;
use std::fmt::{Display, Formatter};

use crate::hw_definition::description::{HardwareDetails, PinDescription, PinDescriptionSet};

impl Display for HardwareDetails {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
writeln!(f, "Application: {}", self.app_name)?;
Expand All @@ -21,18 +20,12 @@ impl Display for HardwareDetails {

/// `PinDescriptionSet` describes a set of Pins on a device, using `PinDescription`s
impl PinDescriptionSet {
/// Return a slice of PinDescriptions
#[allow(dead_code)] // for piglet
pub fn pins(&self) -> &[PinDescription] {
&self.pins
}

/// Return a set of PinDescriptions *only** for pins that have BCM pin numbering, sorted in
/// ascending order of [BCMPinNumber]
#[allow(dead_code)] // for piglet build
pub fn bcm_pins_sorted(&self) -> Vec<&PinDescription> {
let mut pins = self
.pins
.pins()
.iter()
.filter(|pin| pin.bcm.is_some())
.collect::<Vec<&PinDescription>>();
Expand Down
30 changes: 28 additions & 2 deletions src/hw_definition/description.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,10 @@ pub struct WiFiDetails {

#[cfg(not(feature = "no_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
/// Vec indexed from 0 so, index = board_pin_number -1, as pin numbering start at 1
#[derive(Serialize, Debug, Clone, Deserialize)]
pub struct PinDescriptionSet {
pub(crate) pins: Vec<PinDescription>,
pins: Vec<PinDescription>,
}

#[cfg(feature = "no_std")]
Expand All @@ -136,6 +136,32 @@ pub struct PinDescriptionSet<'a> {
pub pins: Vec<PinDescription<'a>, 40>,
}

#[cfg(feature = "no_std")]
impl<'a> PinDescriptionSet<'a> {
/// Create a new [PinDescriptionSet] from a slice of pins
pub fn new(pin_slice: &'a [PinDescription]) -> Self {
Self {
pins: Vec::from_slice(pin_slice).unwrap(),
}
}
}

#[cfg(not(feature = "no_std"))]
impl PinDescriptionSet {
/// Return a slice of PinDescriptions
#[allow(dead_code)] // for piglet
pub fn pins(&self) -> &[PinDescription] {
&self.pins
}

/// Create a new [PinDescriptionSet] from a slice of pins
pub fn new(pin_slice: &[PinDescription]) -> Self {
Self {
pins: pin_slice.to_vec(),
}
}
}

#[cfg(not(feature = "no_std"))]
/// [PinDescription] is used to describe each pin and possible uses it can be put to
/// * [board_pin_number] refer to the pins by the number of the pin printed on the board
Expand Down
10 changes: 7 additions & 3 deletions src/piggui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ mod widgets;

const PIGGUI_ID: &str = "piggui";

pub(crate) const WINDOW_TITLE_AREA_HEIGHT: f32 = 28.0;

/// These are the messages that Piggui responds to
#[derive(Debug, Clone)]
#[allow(clippy::large_enum_variant)]
Expand Down Expand Up @@ -212,11 +214,13 @@ impl Piggui {
}
}

LayoutChanged(layout) => {
let layout = self.layout_selector.update(layout);
LayoutChanged(new_layout) => {
let layout_size = self
.layout_selector
.update(new_layout, self.hardware_view.get_config());
return window::get_latest().then(move |latest| {
if let Some(id) = latest {
window::resize(id, layout)
window::resize(id, layout_size)
} else {
Task::none()
}
Expand Down
2 changes: 2 additions & 0 deletions src/views/hardware_styles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ pub(crate) const SPACE_BETWEEN_PIN_COLUMNS: f32 = 10.0;

pub(crate) const SPACE_BETWEEN_PIN_ROWS: f32 = 5.0;

pub(crate) const PIN_ROW_HEIGHT: f32 = 28.0;

const PIN_RADIUS: Radius = Radius {
top_left: PIN_BUTTON_RADIUS,
top_right: PIN_BUTTON_RADIUS,
Expand Down
43 changes: 39 additions & 4 deletions src/views/hardware_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ impl HardwareView {

/// Get the current [HardwareConfig]
#[must_use]
pub fn get_config(&self) -> HardwareConfig {
self.hardware_config.clone()
pub fn get_config(&self) -> &HardwareConfig {
&self.hardware_config
}

/// Get the current [HardwareConnection]
Expand Down Expand Up @@ -297,8 +297,9 @@ impl HardwareView {

if let Some(hw_description) = &self.hardware_description {
let pin_layout = match layout {
Layout::BoardLayout => self.board_pin_layout_view(&hw_description.pins),
Layout::BCMLayout => self.bcm_pin_layout_view(&hw_description.pins),
Layout::Board => self.board_pin_layout_view(&hw_description.pins),
Layout::Logical => self.bcm_pin_layout_view(&hw_description.pins),
Layout::Reduced => self.reduced_layout_view(&hw_description.pins),
};

return scrollable(pin_layout)
Expand Down Expand Up @@ -370,6 +371,40 @@ impl HardwareView {
.into()
}

/// Reduced size view that only lays out configured pins
pub fn reduced_layout_view<'a>(
&'a self,
pin_set: &'a PinDescriptionSet,
) -> Element<'a, HardwareViewMessage> {
let mut column = Column::new().width(Length::Shrink).height(Length::Shrink);

for pin_description in pin_set.bcm_pins_sorted() {
if let Some(bcm_pin_number) = &pin_description.bcm {
if self
.hardware_config
.pin_functions
.contains_key(bcm_pin_number)
{
let pin_row = create_pin_view_side(
pin_description,
self.hardware_config
.pin_functions
.get(&pin_description.bcm.unwrap()),
Right,
self.pin_states.get(bcm_pin_number),
);

column = column.push(pin_row);
}
}
}

column
.spacing(SPACE_BETWEEN_PIN_ROWS)
.align_x(Alignment::Start)
.into()
}

/// View that draws the pins laid out as they are on the physical Pi board
pub fn board_pin_layout_view<'a>(
&'a self,
Expand Down
2 changes: 2 additions & 0 deletions src/views/info_row.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ use iced_futures::Subscription;
#[cfg(feature = "discovery")]
use std::collections::HashMap;

pub(crate) const INFO_ROW_HEIGHT: f32 = 28.0;

const MENU_BACKGROUND_COLOR: Color = Color::from_rgba(0.15, 0.15, 0.15, 1.0);

const MENU_RADIUS: Radius = Radius {
Expand Down
Loading

0 comments on commit b040696

Please sign in to comment.