Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More HW configuration on Pi HW #38

Merged
merged 6 commits into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 42 additions & 15 deletions src/gpio/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
mod pin_descriptions;

use pin_descriptions::*;
use serde::{Deserialize, Serialize};
use std::fs::File;
use std::io;
use std::io::{BufReader, Write};

use serde::{Deserialize, Serialize};

use pin_descriptions::*;

mod pin_descriptions;

// An input can be configured to have an optional pull-up or pull-down
#[derive(Debug, PartialEq, Clone, Copy, Serialize, Deserialize)]

Check warning on line 12 in src/gpio/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/gpio/mod.rs#L12

Added line #L12 was not covered by tests
pub enum InputPull {
PullUp,
PullDown,
}

// All the possible functions a pin can be given
#[derive(Debug, PartialEq, Clone, Copy, Serialize, Deserialize)]
#[allow(non_camel_case_types)]
Expand All @@ -14,8 +23,8 @@
Power3V3,
Power5V,
Ground,
Input,
Output,
Input(Option<InputPull>),
Output(Option<bool>),
SDA1,
I2C,
SCL1,
Expand Down Expand Up @@ -97,22 +106,40 @@

#[cfg(test)]
mod test {
use crate::gpio::{GPIOConfig, PinFunction};
use std::fs;
use std::fs::File;
use std::io::Write;
use std::path::PathBuf;

use tempfile::tempdir;

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

#[test]
fn create_a_config() {
let config = GPIOConfig::default();
assert!(config.configured_pins.is_empty());
}

#[test]
fn load_one_pin_config() {
let pin_config = r#"{"configured_pins":[[1,"Input"]]}"#;
fn save_one_pin_config_input_no_pullup() {
let config = GPIOConfig {
configured_pins: vec![(1, PinFunction::Input(None))],
};

let output_dir = tempdir().expect("Could not create a tempdir").into_path();
let test_file = output_dir.join("test.piggui");

config.save(test_file.to_str().unwrap()).unwrap();

let pin_config = r#"{"configured_pins":[[1,{"Input":null}]]}"#;
let contents = fs::read_to_string(test_file).expect("Could not read test file");
assert_eq!(contents, pin_config);
}

#[test]
fn load_one_pin_config_input_no_pull() {
let pin_config = r#"{"configured_pins":[[1,{"Input":null}]]}"#;
let output_dir = tempdir().expect("Could not create a tempdir").into_path();
let test_file = output_dir.join("test.piggui");
let mut file = File::create(&test_file).expect("Could not create test file");
Expand All @@ -121,7 +148,7 @@
let config = GPIOConfig::load(test_file.to_str().unwrap()).unwrap();
assert_eq!(config.configured_pins.len(), 1);
assert_eq!(config.configured_pins[0].0, 1);
assert_eq!(config.configured_pins[0].1, PinFunction::Input);
assert_eq!(config.configured_pins[0].1, PinFunction::Input(None));
}

#[test]
Expand All @@ -131,22 +158,22 @@
path = path.join("tests/one_pin_config.piggui");
let config = GPIOConfig::load(path.to_str().unwrap()).unwrap();
assert_eq!(config.configured_pins.len(), 1);
assert_eq!(config.configured_pins[0].0, 1);
assert_eq!(config.configured_pins[0].1, PinFunction::Input);
assert_eq!(config.configured_pins[0].0, 17); // GPIO17
assert_eq!(config.configured_pins[0].1, PinFunction::Output(Some(true)));
}

#[test]
fn save_one_pin_config() {
fn save_one_pin_config_output_with_level() {
let config = GPIOConfig {
configured_pins: vec![(1, PinFunction::Input)],
configured_pins: vec![(7, PinFunction::Output(Some(true)))], // GPIO7 output set to 1
};

let output_dir = tempdir().expect("Could not create a tempdir").into_path();
let test_file = output_dir.join("test.piggui");

config.save(test_file.to_str().unwrap()).unwrap();

let pin_config = r#"{"configured_pins":[[1,"Input"]]}"#;
let pin_config = r#"{"configured_pins":[[7,{"Output":true}]]}"#;
let contents = fs::read_to_string(test_file).expect("Could not read test file");
assert_eq!(contents, pin_config);
}
Expand Down
Loading
Loading