From 3e59a31b76e723c45162122c1613a71e81b58981 Mon Sep 17 00:00:00 2001 From: Andrew Mackenzie Date: Mon, 20 May 2024 23:31:59 +0200 Subject: [PATCH] Add all config options and configure Output Pin. --- src/hw/pi.rs | 53 ++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 39 insertions(+), 14 deletions(-) diff --git a/src/hw/pi.rs b/src/hw/pi.rs index e1d7a70ab..4bad452ee 100644 --- a/src/hw/pi.rs +++ b/src/hw/pi.rs @@ -1,26 +1,29 @@ /// 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(), } } @@ -28,15 +31,37 @@ pub fn get() -> impl Hardware { // -> Result<(), Box> 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"); } } } @@ -45,7 +70,7 @@ impl Hardware for PiHW { fn get_state(&self) -> GPIOState { GPIOState { - pin_state: [None; 40] + pin_state: [None; 40], } } -} \ No newline at end of file +}