Skip to content

Commit

Permalink
Merge pull request #18 from andrewdavidmackenzie/pigg-4
Browse files Browse the repository at this point in the history
Virtual GPIO Config and State
  • Loading branch information
andrewdavidmackenzie authored May 14, 2024
2 parents 4dc9351 + ad08702 commit 0b0807d
Show file tree
Hide file tree
Showing 10 changed files with 492 additions and 53 deletions.
405 changes: 372 additions & 33 deletions src/gpio.rs

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion src/gpio/none.rs

This file was deleted.

3 changes: 0 additions & 3 deletions src/gpio/pi.rs

This file was deleted.

1 change: 0 additions & 1 deletion src/gpio/pico.rs

This file was deleted.

16 changes: 16 additions & 0 deletions src/hw/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use crate::gpio::{GPIOConfig, GPIOState};

#[cfg_attr(feature="pico", path="pico.rs")]
#[cfg_attr(feature="pi", path="pi.rs")]
#[cfg_attr(not(any(feature="pico", feature="pi")), path="none.rs")]
mod implementation;

pub fn get() -> impl Hardware {
implementation::get()
}

// TODO placeholder until I figure out what this trait needs to implement
pub trait Hardware {
fn apply_config(&mut self, config: &GPIOConfig);
fn get_state(&self) -> GPIOState;
}
22 changes: 22 additions & 0 deletions src/hw/none.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Implementation of GPIO for hosts that don't have GPIO (Linux, macOS, etc.)

use super::Hardware;
use crate::gpio::{GPIOState, GPIOConfig};

pub struct NoneHW;

pub fn get() -> impl Hardware {
NoneHW {}
}

impl Hardware for NoneHW {
fn apply_config(&mut self, _config: &GPIOConfig) {
println!("GPIO Config has been applied to fake hardware");
}

fn get_state(&self) -> GPIOState {
GPIOState {
pin_state: [None; 40]
}
}
}
25 changes: 25 additions & 0 deletions src/hw/pi.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/// Implementation of GPIO for raspberry pi - uses rrpal
#[cfg(feature = "rppal")]
#[allow(unused_imports)] // just checking builds work for now...
use rppal::gpio::{InputPin, Level, Trigger};

use super::Hardware;
use crate::gpio::{GPIOState, GPIOConfig};

pub struct PiHW;

pub fn get() -> impl Hardware {
PiHW {}
}

impl Hardware for PiHW {
fn apply_config(&mut self, _config: &GPIOConfig) {
println!("GPIO Config has been applied to Pi hardware");
}

fn get_state(&self) -> GPIOState {
GPIOState {
pin_state: [None; 40]
}
}
}
24 changes: 24 additions & 0 deletions src/hw/pico.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/// Implementation of GPIO for pi pico targets
#[cfg(feature = "rppal")]
use rppal::gpio::{InputPin, Level, Trigger};

use super::Hardware;
use crate::gpio::{GPIOState, GPIOConfig};

pub struct PicoHW;

pub fn get() -> impl Hardware {
PicoHW {}
}

impl Hardware for PicoHW {
fn apply_config(&mut self, _config: &GPIOConfig) {
println!("GPIO Config has been applied to Pico hardware");
}

fn get_state(&self) -> GPIOState {
GPIOState {
pin_state: [None; 40]
}
}
}
37 changes: 25 additions & 12 deletions src/piggui.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,35 @@
mod gpio;
mod hw;

use gpio::PinConfig;
// This binary will only be built with the "iced" feature enabled, by use of "required-features"
// in Cargo.toml so no need for the feature to be used here for conditional compiling
use iced::widget::{button, container, row, Column, Text};
use iced::{alignment, window, Element, Length, Sandbox, Settings};

fn main() -> iced::Result {
// Use Hardware via trait
use hw::Hardware;
use crate::gpio::GPIOConfig;

fn main() -> Result<(), iced::Error> {
let window = window::Settings {
resizable: false,
..Default::default()
};

// Serde and load this from saved file, using command line option or later via UI
let config = gpio::GPIOConfig::new();

// TODO maybe this should be done async, or with a Command or something?
let mut hw = hw::get();
hw.apply_config(&config);

// TODO remove println!() and start using the Pin configs in the layout to show current
// Pin setup for each one
println!("Pin configs: {:?}", config);
let state = gpio::GPIOState::get(&config);
println!("OINK: {:?}", state);
println!("Pin1 Config is: {:?}", config.pins[1]);

// TODO show the state of each pin in the layout, if an input pin and/or the state is not None
println!("OINK: {:?}", hw.get_state());

Gpio::run(Settings {
window,
Expand All @@ -24,7 +38,7 @@ fn main() -> iced::Result {
}

struct Gpio {
pins: [Option<PinConfig>; 40],
gpio_config: GPIOConfig,
clicked: bool,
}

Expand All @@ -38,9 +52,8 @@ impl Sandbox for Gpio {
type Message = Message;

fn new() -> Self {
let config = gpio::GPIOConfig::new();
Self {
pins: config.pin_configs,
gpio_config: GPIOConfig::new(),
clicked: false,
}
}
Expand All @@ -56,7 +69,7 @@ impl Sandbox for Gpio {
}

fn view(&self) -> iced::Element<Self::Message> {
container(pin_view(&self.pins))
container(pin_view(&self.gpio_config))
.height(Length::Fill)
.width(Length::Fill)
.align_x(alignment::Horizontal::Center)
Expand All @@ -73,18 +86,18 @@ impl Sandbox for Gpio {
}
}

fn pin_view(pins: &[Option<PinConfig>; 40]) -> Element<'static, Message> {
fn pin_view(config: &GPIOConfig) -> Element<'static, Message> {
let mut column = Column::new()
.spacing(20)
.align_items(iced::Alignment::Center)
.width(Length::Fill)
.height(Length::Fill);

for _i in 0..pins.len() / 2 {
for pair in config.pins.chunks(2) {
let row = row!(
// add radio button
button(Text::new("pin1")).on_press(Message::Activate),
button(Text::new("pin2")).on_press(Message::Activate)
button(Text::new(pair[0].board_pin_number.to_string())).on_press(Message::Activate),
button(Text::new(pair[1].board_pin_number.to_string())).on_press(Message::Activate),
)
.spacing(10);
column = column.push(row);
Expand Down
11 changes: 8 additions & 3 deletions src/piglet.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
mod gpio;
mod hw;

// Use Hardware via trait
use hw::Hardware;

fn main() {
let config = gpio::GPIOConfig::new();
println!("Pin configs: {:?}", config);
println!("Pin1 Config is: {:?}", config.pins[1]);

let state = gpio::GPIOState::get(&config);

println!("Oink: {:?}", state);
let mut hw = hw::get();
hw.apply_config(&config);
println!("Oink: {:?}", hw.get_state());
}

0 comments on commit 0b0807d

Please sign in to comment.