Skip to content

Commit

Permalink
Merge pull request #25 from andrewdavidmackenzie/pigg-20
Browse files Browse the repository at this point in the history
Separate pin description from pin config
  • Loading branch information
andrewdavidmackenzie authored May 16, 2024
2 parents 4c138b8 + 57551af commit 65c55e8
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 118 deletions.
51 changes: 22 additions & 29 deletions src/gpio/mod.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
mod pins;
mod pin_descriptions;

//use serde::{Deserialize, Serialize};
use pins::*;

pub type PinLevel = bool;

#[derive(Debug)]
#[allow(dead_code)]
pub struct GPIOState {
pub pin_state: [Option<PinLevel>; 40] // TODO make private later
}
use serde::{Deserialize, Serialize};
use pin_descriptions::*;

// All the possible functions a pin can be given
#[derive(Debug, PartialEq, Clone)]
#[derive(Debug, PartialEq, Clone, Copy, Serialize, Deserialize)]
#[allow(non_camel_case_types)]
#[allow(clippy::upper_case_acronyms)]
pub enum PinFunction {
Expand Down Expand Up @@ -43,32 +35,33 @@ pub enum PinFunction {
// these are the numbers after "GPIO"
#[derive(Debug, Clone)]
#[allow(dead_code)] // TODO remove later
pub struct PinConfig {
pub struct PinDescription {
pub board_pin_number: u8,
bcm_pin_number: Option<u8>,
pub name: &'static str,
options: &'static[PinFunction], // The set of functions the pin can have, chosen by user config
config: Option<PinFunction>, // The currently selected function for the pin, if any selected
}

// Model the 40 pin GPIO connections - including Ground, 3.3V and 5V outputs
// If no specific config is set on a pin, it will have None
// I have made array of 40, to the index is "of by one" compared to the [board_pin_number]
// is not used
#[derive(Debug, Clone)]
pub const GPIO_DESCRIPTION : [PinDescription; 40] = [PIN_1, PIN_2, PIN_3, PIN_4, PIN_5, PIN_6, PIN_7, PIN_8, PIN_9, PIN_10,
PIN_11, PIN_12, PIN_13, PIN_14, PIN_15, PIN_16, PIN_17, PIN_18, PIN_19, PIN_20,
PIN_21, PIN_22, PIN_23, PIN_24, PIN_25, PIN_26, PIN_27, PIN_28, PIN_29, PIN_30,
PIN_31, PIN_32, PIN_33, PIN_34, PIN_35, PIN_36, PIN_37, PIN_38, PIN_39, PIN_40];

// A vector of tuples of (board_pin_number, PinFunction)
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct GPIOConfig {
pub pins: [PinConfig; 40], // TODO make private later
pub configured_pins: Vec<(u8, PinFunction)>,
}

impl Default for GPIOConfig {
fn default() -> Self {
GPIOConfig {
pins: [PIN_1, PIN_2, PIN_3, PIN_4, PIN_5, PIN_6, PIN_7, PIN_8, PIN_9, PIN_10,
PIN_11, PIN_12, PIN_13, PIN_14, PIN_15, PIN_16, PIN_17, PIN_18, PIN_19, PIN_20,
PIN_21, PIN_22, PIN_23, PIN_24, PIN_25, PIN_26, PIN_27, PIN_28, PIN_29, PIN_30,
PIN_31, PIN_32, PIN_33, PIN_34, PIN_35, PIN_36, PIN_37, PIN_38, PIN_39, PIN_40],
}
}
pub type PinLevel = bool;

// TBD whether we should merge state with config
// on config load, for an output pin we would set the level...
#[derive(Debug)]
#[allow(dead_code)]
pub struct GPIOState {
pub pin_state: [Option<PinLevel>; 40] // TODO make private later
}

#[cfg(test)]
Expand All @@ -78,6 +71,6 @@ mod test {
#[test]
fn create_a_config() {
let config = gpio::GPIOConfig::default();
assert_eq!(config.pins[1].config, None);
assert!(config.configured_pins.is_empty());
}
}
Loading

0 comments on commit 65c55e8

Please sign in to comment.