Skip to content

Commit

Permalink
Add all config options and configure Output Pin.
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewdavidmackenzie committed May 20, 2024
1 parent 56995a8 commit 3e59a31
Showing 1 changed file with 39 additions and 14 deletions.
53 changes: 39 additions & 14 deletions src/hw/pi.rs
Original file line number Diff line number Diff line change
@@ -1,42 +1,67 @@
/// Implementation of GPIO for raspberry pi - uses rrpal
#[allow(unused_imports)] // just checking builds work for now...
use rppal::gpio::{InputPin, Level, Trigger};
use rppal::gpio::OutputPin;
use rppal::gpio::Gpio;
use rppal::gpio::OutputPin;

use crate::gpio::{GPIOConfig, GPIOState};
use crate::gpio::PinFunction;

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

// TODO state will be used at some point I imagine, if not remove it. When used remove the next line
#[allow(dead_code)]
enum Pin {
Input(InputPin),
Output(OutputPin)
Output(OutputPin),
}

// Tuples of pin board number and Pin
pub struct PiHW {
configured_pins: Vec<(u8, Pin)>
configured_pins: Vec<(u8, Pin)>,
}

pub fn get() -> impl Hardware {
PiHW {
configured_pins: Default::default()
configured_pins: Default::default(),
}
}

// TODO maybe change trait to allow errors
// -> Result<(), Box<dyn Error>>
impl Hardware for PiHW {
fn apply_config(&mut self, config: &GPIOConfig) {
for pin in &config.configured_pins {
match pin.1 {
for (pin_number, pin_config) in &config.configured_pins {
match pin_config {
PinFunction::Input => {
let input = Gpio::new().unwrap()
.get(pin.0).unwrap().into_input();
self.configured_pins.push((pin.0, Pin::Input(input)))
// TODO handle pull-up/down options
let input = Gpio::new().unwrap().get(*pin_number).unwrap().into_input();
self.configured_pins.push((*pin_number, Pin::Input(input)))
}
PinFunction::Output => {
// TODO check if there are any options on Output pins
let output = Gpio::new().unwrap().get(*pin_number).unwrap().into_output();
self.configured_pins
.push((*pin_number, Pin::Output(output)))
}
_ => {
println!("A different config");
// TODO implement all of these
PinFunction::SDA1 => {}
PinFunction::I2C => {}
PinFunction::SCL1 => {}
PinFunction::SPIO_MOSI => {}
PinFunction::SPIO_MISO => {}
PinFunction::SPIO_SCLK => {}
PinFunction::ID_SD => {}
PinFunction::ID => {}
PinFunction::EEPROM => {}
PinFunction::UART0_TXD => {}
PinFunction::UART0_RXD => {}
PinFunction::PCM_CLK => {}
PinFunction::SPIO_CE0_N => {}
PinFunction::SPIO_CE1_N => {}
PinFunction::ID_SC => {}
PinFunction::Ground | PinFunction::Power3V3 | PinFunction::Power5V => {
eprintln!("Ground, 3V3 or 5V pins cannot be configured");
}
}
}
Expand All @@ -45,7 +70,7 @@ impl Hardware for PiHW {

fn get_state(&self) -> GPIOState {
GPIOState {
pin_state: [None; 40]
pin_state: [None; 40],
}
}
}
}

0 comments on commit 3e59a31

Please sign in to comment.