Repository: andrewdavidmackenzie/pigg (rust)

Issue:
Make GPIOConfig and GPIOState independant of rrpal

Make GPIOConfig and GPIOState will need to be independant of rrpal, not just for UI development, but in order to have them on pi pico also.

Pull request:
virtual gpio on all HW
Fixes #15

Patch:
diff --git a/Cargo.toml b/Cargo.toml
index dab9d4d7..159999a1 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -14,12 +14,18 @@ readme = "README.md"
 [[bin]]
 name = "piggui"
 path = "src/piggui.rs"
-required-features = ["iced"]
+required-features = ["gui"]
 
 [[bin]]
 name = "piglet"
 path = "src/piglet.rs"
-required-features = ["rppal"]
+required-features = ["pi"]
+
+[features]
+default = []
+pi = ["rppal"]
+pico = [] #embassy
+gui = ["iced"]
 
 [dependencies]
 # for interacting with GPIO on the Raspberry Pi
diff --git a/Makefile b/Makefile
index 8a975191..5f20ba08 100644
--- a/Makefile
+++ b/Makefile
@@ -17,27 +17,27 @@ release: release-build pibuild
 
 .PHONY: piclippy
 piclippy:
-	CROSS_CONTAINER_OPTS="--platform linux/amd64" cross clippy --release --features "rppal","iced" --tests --no-deps --target=aarch64-unknown-linux-gnu
+	CROSS_CONTAINER_OPTS="--platform linux/amd64" cross clippy --release --features "pi","gui" --tests --no-deps --target=aarch64-unknown-linux-gnu
 
 .PHONY: clippy
 clippy: piclippy
-	cargo clippy --features "iced" --tests --no-deps
+	cargo clippy --features "gui" --tests --no-deps
 #-- --warn clippy::pedantic -D warnings
 
 # Enable the "iced" feature so we only build the "piggui" binary on the current host (macos, linux or raspberry pi)
 # To build both binaries on a Pi directly, we will need to modify this
 .PHONY: build
 build:
-	cargo build --features "iced"
+	cargo build --features "gui"
 
 .PHONY: run
 run:
-	cargo run --features "iced"
+	cargo run --features "gui"
 
 # This will build all binaries on the current host, be it macos, linux or raspberry pi - with release profile
 .PHONY: release-build
 release-build:
-	cargo build --release --features "iced"
+	cargo build --release --features "gui"
 
 # I'm currently building using release profile for Pi, as not debugging natively on it. If we want to do that then
 # we may need to add another make target
@@ -45,13 +45,13 @@ release-build:
 # That should build both the "piggui" and "piglet" binaries, with GUI and GPIO in "piggui" and GPIO in "piglet"
 .PHONY: pibuild
 pibuild:
-	CROSS_CONTAINER_OPTS="--platform linux/amd64" cross build --release --features "rppal","iced" --target=aarch64-unknown-linux-gnu
+	CROSS_CONTAINER_OPTS="--platform linux/amd64" cross build --release --features "pi","gui" --target=aarch64-unknown-linux-gnu
 
 # This will only test GUI tests in piggui on the local host, whatever that is
 # We'd need to think how to run tests on RPi, on piggui with GUI and GPIO functionality, and piglet with GPIO functionality
 .PHONY: test
 test:
-	cargo test --features "iced"
+	cargo test --features "gui"
 
 .PHONY: copy
 copy: pibuild
diff --git a/codecov.yml b/codecov.yml
new file mode 100644
index 00000000..bee11d83
--- /dev/null
+++ b/codecov.yml
@@ -0,0 +1,15 @@
+codecov:
+  token: c53ff4f3-0633-441a-b737-b8ee9980ee7a
+
+coverage:
+  round: up
+  precision: 0
+  status:
+    project:
+      default:
+        target: auto
+        threshold: 20%
+    patch:
+      default:
+        target: auto
+        threshold: 20%
\ No newline at end of file
diff --git a/src/gpio/mod.rs b/src/gpio.rs
similarity index 53%
rename from src/gpio/mod.rs
rename to src/gpio.rs
index e45d707c..ca10a681 100644
--- a/src/gpio/mod.rs
+++ b/src/gpio.rs
@@ -1,15 +1,14 @@
-/// When built with the "rppal" feature for interacting with GPIO - can only be built for RPi
-#[cfg(feature = "rppal")]
-use rppal::gpio::{InputPin, Level, Trigger};
 
-// TODO will need to create a GPIOConfig and state that is NOT related to rrpal -
-// for use in the UI when not compiled for Pi
+#[cfg_attr(feature = "pi", path="gpio/pi.rs")]
+#[cfg_attr(feature = "pico", path="gpio/pico.rs")]
+#[cfg_attr(not(any(feature = "pico", feature = "pico")), path="gpio/none.rs")]
+pub(crate) mod gpio_sys;
+
+pub type PinLevel = bool;
 
-// TODO do this for real
 #[derive(Debug)]
-#[allow(dead_code)] // TODO remove later
 pub struct GPIOState {
-    pin_state: [Option<Level>; 40]
+    pin_state: [Option<PinLevel>; 40]
 }
 
 impl GPIOState {
@@ -21,13 +20,13 @@ impl GPIOState {
 }
 
 // TODO - add in here all the possible config options for GPIO
-#[derive(Debug)]
-#[allow(dead_code)] // TODO remove later
+#[derive(Debug, PartialEq)]
 pub enum PinConfig {
-    Input(InputPin, Option<Trigger>),
+    Input,
     Output
 }
 
+// Pin Names
 const SDA1: usize = 2;
 
 const NO_CONFIG: Option<PinConfig> = None;
@@ -35,7 +34,6 @@ const NO_CONFIG: Option<PinConfig> = None;
 // 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
 #[derive(Debug)]
-#[allow(dead_code)] // TODO remove later
 pub struct GPIOConfig {
     pin_configs: [Option<PinConfig>; 40]
 }
@@ -50,4 +48,21 @@ impl GPIOConfig {
             pin_configs,
         }
     }
+}
+
+// TODO placeholder until I figure out what this trait needs to implement
+pub trait GPIO {
+    fn apply_config(config: &GPIOConfig);
+    fn get_state() -> GPIOState;
+}
+
+#[cfg(test)]
+mod test {
+    use crate::gpio;
+
+    #[test]
+    fn create_a_config() {
+        let config = gpio::GPIOConfig::new();
+        assert_eq!(config.pin_configs[0], None);
+    }
 }
\ No newline at end of file
diff --git a/src/gpio/none.rs b/src/gpio/none.rs
new file mode 100644
index 00000000..51e2dc5b
--- /dev/null
+++ b/src/gpio/none.rs
@@ -0,0 +1,1 @@
+// Implementation of GPIO for hosts that don't have GPIO (Linux, macOS, etc.)
\ No newline at end of file
diff --git a/src/gpio/pi.rs b/src/gpio/pi.rs
new file mode 100644
index 00000000..a02e765f
--- /dev/null
+++ b/src/gpio/pi.rs
@@ -0,0 +1,3 @@
+/// Implementation of GPIO for raspberry pi - uses rrpal
+#[cfg(feature = "rppal")]
+use rppal::gpio::{InputPin, Level, Trigger};
diff --git a/src/gpio/pico.rs b/src/gpio/pico.rs
new file mode 100644
index 00000000..f04c8b22
--- /dev/null
+++ b/src/gpio/pico.rs
@@ -0,0 +1,1 @@
+/// Implementation of GPIO for pi pico targets
\ No newline at end of file
diff --git a/src/piggui.rs b/src/piggui.rs
index 01fc3c26..140aa569 100644
--- a/src/piggui.rs
+++ b/src/piggui.rs
@@ -1,5 +1,3 @@
-/// When built with the "rppal" feature include code for interacting with GPIO
-#[cfg(feature = "rppal")]
 mod gpio;
 
 // This binary will only be built with the "iced" feature enabled, by use of "required-features"
@@ -13,14 +11,10 @@ fn main() -> Result<(), iced::Error> {
         ..Default::default()
     };
 
-    // TODO figure out how to connect config and state to the UI....
-    #[cfg(feature = "rppal")]
-    {
     let config = gpio::GPIOConfig::new();
     println!("Pin configs: {:?}", config);
     let state = gpio::GPIOState::get(&config);
     println!("OINK: {:?}", state);
-    }
 
     Gpio::run(Settings {
         window,
diff --git a/src/piglet.rs b/src/piglet.rs
index 21ecde4b..a7e8d4a6 100644
--- a/src/piglet.rs
+++ b/src/piglet.rs
@@ -1,6 +1,3 @@
-// This binary will only be built when the "rppal" feature for interacting with GPIO is enabled
-// so no need for conditional compilation here
-
 mod gpio;
 
 fn main() {


Test patch:
diff --git a/.github/workflows/build_and_test.yml b/.github/workflows/build_and_test.yml
index 78050c9e..042687f5 100644
--- a/.github/workflows/build_and_test.yml
+++ b/.github/workflows/build_and_test.yml
@@ -71,7 +71,9 @@ jobs:
       - name: test
         run: make test
 
-      - name: Upload code coverage
-        uses: codecov/codecov-action@v4.0.1
-        with:
-          token: ${{ secrets.CODECOV_TOKEN }}
+      - name: UploadCoverage
+        run: |
+          grcov . --binary-path target/debug/ -s . -t lcov --branch --ignore-not-existing --ignore "/*" -o lcov.info
+          #lcov --remove lcov.info 'target/debug/build/**' '**/errors.rs' '*tests/*' -o lcov.info
+          bash <(curl -s https://codecov.io/bash) -f lcov.info
+          rm -f lcov.info
\ No newline at end of file