From 1fbfb770839aefbd2b652f53edcf8d7dfb2498ae Mon Sep 17 00:00:00 2001 From: David Beechey Date: Tue, 26 Nov 2024 14:00:20 +0000 Subject: [PATCH] INFRA - Use Rust procedural macros to reduce code duplication in board crates (#52) --- Cargo.lock | 26 +++++++- Cargo.toml | 5 +- _typos.toml | 5 +- boards/stm32f767zi/Cargo.lock | 50 +++++++++++++- boards/stm32f767zi/Cargo.toml | 8 ++- boards/stm32f767zi/src/bin/adc_test.rs | 4 +- boards/stm32f767zi/src/io.rs | 29 ++++++++- boards/stm32f767zi/src/io/adc.rs | 21 ------ boards/stm32f767zi/src/io/gpio.rs | 20 ------ boards/stm32f767zi/src/io/i2c.rs | 51 --------------- boards/stm32f767zi/src/tasks/keyence.rs | 4 +- boards/stm32f767zi/src/tasks/temperature.rs | 2 +- boards/stm32l432kc/Cargo.lock | 13 +++- boards/stm32l432kc/Cargo.toml | 1 - boards/stm32l476rg/Cargo.lock | 50 +++++++++++++- boards/stm32l476rg/Cargo.toml | 8 ++- boards/stm32l476rg/src/io.rs | 29 ++++++++- boards/stm32l476rg/src/io/adc.rs | 21 ------ boards/stm32l476rg/src/io/gpio.rs | 20 ------ boards/stm32l476rg/src/io/i2c.rs | 51 --------------- boards/stm32l476rg/src/tasks/keyence.rs | 4 +- boards/stm32l476rg/src/tasks/temperature.rs | 2 +- lib/io/{ => hyped_adc}/Cargo.toml | 4 +- lib/io/hyped_adc/hyped_adc_derive/Cargo.lock | 46 +++++++++++++ lib/io/hyped_adc/hyped_adc_derive/Cargo.toml | 11 ++++ lib/io/hyped_adc/hyped_adc_derive/src/lib.rs | 33 ++++++++++ lib/io/{src/adc.rs => hyped_adc/src/lib.rs} | 5 +- lib/io/hyped_gpio_input/Cargo.toml | 7 ++ .../hyped_gpio_input_derive/Cargo.lock | 46 +++++++++++++ .../hyped_gpio_input_derive/Cargo.toml | 11 ++++ .../hyped_gpio_input_derive/src/lib.rs | 30 +++++++++ .../gpio.rs => hyped_gpio_input/src/lib.rs} | 14 ++-- lib/io/hyped_i2c/Cargo.toml | 7 ++ lib/io/hyped_i2c/hyped_i2c_derive/Cargo.lock | 46 +++++++++++++ lib/io/hyped_i2c/hyped_i2c_derive/Cargo.toml | 11 ++++ lib/io/hyped_i2c/hyped_i2c_derive/src/lib.rs | 65 +++++++++++++++++++ lib/io/{src/i2c.rs => hyped_i2c/src/lib.rs} | 4 +- lib/io/hyped_spi/Cargo.toml | 7 ++ lib/io/hyped_spi/hyped_spi_derive/Cargo.lock | 46 +++++++++++++ lib/io/hyped_spi/hyped_spi_derive/Cargo.toml | 11 ++++ lib/io/hyped_spi/hyped_spi_derive/src/lib.rs | 19 ++++++ lib/io/{src/spi.rs => hyped_spi/src/lib.rs} | 2 + lib/io/src/lib.rs | 6 -- lib/sensors/Cargo.toml | 4 +- lib/sensors/src/keyence.rs | 18 ++--- lib/sensors/src/temperature.rs | 4 +- 46 files changed, 638 insertions(+), 243 deletions(-) delete mode 100644 boards/stm32f767zi/src/io/adc.rs delete mode 100644 boards/stm32f767zi/src/io/gpio.rs delete mode 100644 boards/stm32f767zi/src/io/i2c.rs delete mode 100644 boards/stm32l476rg/src/io/adc.rs delete mode 100644 boards/stm32l476rg/src/io/gpio.rs delete mode 100644 boards/stm32l476rg/src/io/i2c.rs rename lib/io/{ => hyped_adc}/Cargo.toml (62%) create mode 100644 lib/io/hyped_adc/hyped_adc_derive/Cargo.lock create mode 100644 lib/io/hyped_adc/hyped_adc_derive/Cargo.toml create mode 100644 lib/io/hyped_adc/hyped_adc_derive/src/lib.rs rename lib/io/{src/adc.rs => hyped_adc/src/lib.rs} (91%) create mode 100644 lib/io/hyped_gpio_input/Cargo.toml create mode 100644 lib/io/hyped_gpio_input/hyped_gpio_input_derive/Cargo.lock create mode 100644 lib/io/hyped_gpio_input/hyped_gpio_input_derive/Cargo.toml create mode 100644 lib/io/hyped_gpio_input/hyped_gpio_input_derive/src/lib.rs rename lib/io/{src/gpio.rs => hyped_gpio_input/src/lib.rs} (74%) create mode 100644 lib/io/hyped_i2c/Cargo.toml create mode 100644 lib/io/hyped_i2c/hyped_i2c_derive/Cargo.lock create mode 100644 lib/io/hyped_i2c/hyped_i2c_derive/Cargo.toml create mode 100644 lib/io/hyped_i2c/hyped_i2c_derive/src/lib.rs rename lib/io/{src/i2c.rs => hyped_i2c/src/lib.rs} (97%) create mode 100644 lib/io/hyped_spi/Cargo.toml create mode 100644 lib/io/hyped_spi/hyped_spi_derive/Cargo.lock create mode 100644 lib/io/hyped_spi/hyped_spi_derive/Cargo.toml create mode 100644 lib/io/hyped_spi/hyped_spi_derive/src/lib.rs rename lib/io/{src/spi.rs => hyped_spi/src/lib.rs} (98%) delete mode 100644 lib/io/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index 5d6809b..7114b4a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -273,6 +273,13 @@ dependencies = [ "stable_deref_trait", ] +[[package]] +name = "hyped_adc" +version = "0.1.0" +dependencies = [ + "heapless", +] + [[package]] name = "hyped_config" version = "0.1.0" @@ -295,7 +302,14 @@ dependencies = [ ] [[package]] -name = "hyped_io" +name = "hyped_gpio_input" +version = "0.1.0" +dependencies = [ + "heapless", +] + +[[package]] +name = "hyped_i2c" version = "0.1.0" dependencies = [ "heapless", @@ -316,7 +330,15 @@ version = "0.1.0" dependencies = [ "heapless", "hyped_core", - "hyped_io", + "hyped_gpio_input", + "hyped_i2c", +] + +[[package]] +name = "hyped_spi" +version = "0.1.0" +dependencies = [ + "heapless", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index f299079..a08f4a4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,10 @@ [workspace] members = [ "config", - "lib/*" + "lib/core", + "lib/io/*", + "lib/localisation", + "lib/sensors" ] exclude = [ "boards/stm32l476rg", diff --git a/_typos.toml b/_typos.toml index f5f00ea..1fdebef 100644 --- a/_typos.toml +++ b/_typos.toml @@ -1,2 +1,5 @@ [default.extend-words] -mosquitto = "mosquitto" \ No newline at end of file +mosquitto = "mosquitto" + +[files] +extend-exclude = ["telemetry/pnpm-lock.yaml"] diff --git a/boards/stm32f767zi/Cargo.lock b/boards/stm32f767zi/Cargo.lock index 0a1abc3..6980a7d 100644 --- a/boards/stm32f767zi/Cargo.lock +++ b/boards/stm32f767zi/Cargo.lock @@ -530,6 +530,21 @@ dependencies = [ "stable_deref_trait", ] +[[package]] +name = "hyped_adc" +version = "0.1.0" +dependencies = [ + "heapless", +] + +[[package]] +name = "hyped_adc_derive" +version = "0.1.0" +dependencies = [ + "quote", + "syn 2.0.79", +] + [[package]] name = "hyped_boards_stm32f767zi" version = "0.1.0" @@ -547,7 +562,12 @@ dependencies = [ "embassy-time", "embedded-hal 0.2.7", "embedded-storage", - "hyped_io", + "hyped_adc", + "hyped_adc_derive", + "hyped_gpio_input", + "hyped_gpio_input_derive", + "hyped_i2c", + "hyped_i2c_derive", "hyped_sensors", "panic-probe", "rand_core", @@ -568,19 +588,43 @@ dependencies = [ ] [[package]] -name = "hyped_io" +name = "hyped_gpio_input" version = "0.1.0" dependencies = [ "heapless", ] +[[package]] +name = "hyped_gpio_input_derive" +version = "0.1.0" +dependencies = [ + "quote", + "syn 2.0.79", +] + +[[package]] +name = "hyped_i2c" +version = "0.1.0" +dependencies = [ + "heapless", +] + +[[package]] +name = "hyped_i2c_derive" +version = "0.1.0" +dependencies = [ + "quote", + "syn 2.0.79", +] + [[package]] name = "hyped_sensors" version = "0.1.0" dependencies = [ "heapless", "hyped_core", - "hyped_io", + "hyped_gpio_input", + "hyped_i2c", ] [[package]] diff --git a/boards/stm32f767zi/Cargo.toml b/boards/stm32f767zi/Cargo.toml index a2c008b..42aec3a 100644 --- a/boards/stm32f767zi/Cargo.toml +++ b/boards/stm32f767zi/Cargo.toml @@ -23,5 +23,11 @@ critical-section = "1.1" embedded-storage = "0.3.1" static_cell = "2" -hyped_io = { path = "../../lib/io" } hyped_sensors = { path = "../../lib/sensors" } + +hyped_adc = { path = "../../lib/io/hyped_adc" } +hyped_adc_derive = { path = "../../lib/io/hyped_adc/hyped_adc_derive" } +hyped_i2c = { path = "../../lib/io/hyped_i2c" } +hyped_i2c_derive = { path = "../../lib/io/hyped_i2c/hyped_i2c_derive" } +hyped_gpio_input = { path = "../../lib/io/hyped_gpio_input" } +hyped_gpio_input_derive = { path = "../../lib/io/hyped_gpio_input/hyped_gpio_input_derive" } \ No newline at end of file diff --git a/boards/stm32f767zi/src/bin/adc_test.rs b/boards/stm32f767zi/src/bin/adc_test.rs index bae27e8..87bfe1a 100644 --- a/boards/stm32f767zi/src/bin/adc_test.rs +++ b/boards/stm32f767zi/src/bin/adc_test.rs @@ -6,8 +6,8 @@ use defmt::*; use embassy_executor::Spawner; use embassy_stm32::adc::{Adc, AdcChannel}; use embassy_time::Timer; -use hyped_boards_stm32f767zi::io::adc::Stm32f767ziAdc; -use hyped_io::adc::HypedAdc; +use hyped_adc::HypedAdc; +use hyped_boards_stm32f767zi::io::Stm32f767ziAdc; use {defmt_rtt as _, panic_probe as _}; #[embassy_executor::main] diff --git a/boards/stm32f767zi/src/io.rs b/boards/stm32f767zi/src/io.rs index 3d3539c..468c6b5 100644 --- a/boards/stm32f767zi/src/io.rs +++ b/boards/stm32f767zi/src/io.rs @@ -1,3 +1,26 @@ -pub mod adc; -pub mod gpio; -pub mod i2c; +use embassy_stm32::adc::{Adc, AnyAdcChannel, Instance}; +use embassy_stm32::gpio::Input; +use embassy_stm32::{i2c::I2c, mode::Blocking}; + +use hyped_adc::HypedAdc; +use hyped_adc_derive::HypedAdc; +use hyped_gpio_input::HypedGpioInput; +use hyped_gpio_input_derive::HypedGpioInput; +use hyped_i2c::{HypedI2c, I2cError}; +use hyped_i2c_derive::HypedI2c; + +#[derive(HypedAdc)] +pub struct Stm32f767ziAdc<'d, T: Instance> { + adc: Adc<'d, T>, + channel: AnyAdcChannel, +} + +#[derive(HypedGpioInput)] +pub struct Stm32f767ziGpioInput { + pin: Input<'static>, +} + +#[derive(HypedI2c)] +pub struct Stm32f767ziI2c<'d> { + i2c: I2c<'d, Blocking>, +} diff --git a/boards/stm32f767zi/src/io/adc.rs b/boards/stm32f767zi/src/io/adc.rs deleted file mode 100644 index eef5eaf..0000000 --- a/boards/stm32f767zi/src/io/adc.rs +++ /dev/null @@ -1,21 +0,0 @@ -use embassy_stm32::adc::{Adc, AnyAdcChannel, Instance}; -use hyped_io::adc::HypedAdc; - -pub struct Stm32f767ziAdc<'d, T: Instance> { - adc: Adc<'d, T>, - channel: AnyAdcChannel, -} - -impl<'d, T: Instance> HypedAdc for Stm32f767ziAdc<'d, T> { - /// Read a value from the ADC channel - fn read_value(&mut self) -> u16 { - self.adc.blocking_read(&mut self.channel) - } -} - -impl<'d, T: Instance> Stm32f767ziAdc<'d, T> { - /// Create a new instance of our ADC implementation for the STM32F767ZI - pub fn new(mut adc: Adc<'d, T>, channel: AnyAdcChannel) -> Self { - Self { adc, channel } - } -} diff --git a/boards/stm32f767zi/src/io/gpio.rs b/boards/stm32f767zi/src/io/gpio.rs deleted file mode 100644 index f95577c..0000000 --- a/boards/stm32f767zi/src/io/gpio.rs +++ /dev/null @@ -1,20 +0,0 @@ -use embassy_stm32::gpio::Input; -use hyped_io::gpio::HypedGpioPin; - -/// A GPIO pin on the STM32L476RG. -pub struct Stm32f767ziGpio { - pin: Input<'static>, -} - -impl HypedGpioPin for Stm32f767ziGpio { - fn is_high(&mut self) -> bool { - self.pin.is_high() - } -} - -impl Stm32f767ziGpio { - /// Create a new instance of our GPIO implementation for the STM32L476RG - pub fn new(pin: Input<'static>) -> Self { - Self { pin } - } -} diff --git a/boards/stm32f767zi/src/io/i2c.rs b/boards/stm32f767zi/src/io/i2c.rs deleted file mode 100644 index 844212b..0000000 --- a/boards/stm32f767zi/src/io/i2c.rs +++ /dev/null @@ -1,51 +0,0 @@ -use embassy_stm32::{i2c::I2c, mode::Blocking}; -use hyped_io::i2c::{HypedI2c, I2cError}; - -pub struct Stm32f767ziI2c<'d> { - i2c: I2c<'d, Blocking>, -} - -impl<'d> HypedI2c for Stm32f767ziI2c<'d> { - /// Read a byte from a register on a device - fn read_byte(&mut self, device_address: u8, register_address: u8) -> Option { - let mut read = [0]; - let result = - self.i2c - .blocking_write_read(device_address, [register_address].as_ref(), &mut read); - match result { - Ok(_) => Some(read[0]), - Err(_) => None, - } - } - - /// Write a byte to a register on a device - fn write_byte_to_register( - &mut self, - device_address: u8, - register_address: u8, - data: u8, - ) -> Result<(), I2cError> { - let result = self - .i2c - .blocking_write(device_address, [register_address, data].as_ref()); - match result { - Ok(_) => Ok(()), - Err(e) => Err(match e { - embassy_stm32::i2c::Error::Bus => I2cError::Bus, - embassy_stm32::i2c::Error::Arbitration => I2cError::Arbitration, - embassy_stm32::i2c::Error::Nack => I2cError::Nack, - embassy_stm32::i2c::Error::Timeout => I2cError::Timeout, - embassy_stm32::i2c::Error::Crc => I2cError::Crc, - embassy_stm32::i2c::Error::Overrun => I2cError::Overrun, - embassy_stm32::i2c::Error::ZeroLengthTransfer => I2cError::ZeroLengthTransfer, - }), - } - } -} - -impl<'d> Stm32f767ziI2c<'d> { - /// Create a new instance of our I2C implementation for the STM32L476RG - pub fn new(i2c: I2c<'d, Blocking>) -> Self { - Self { i2c } - } -} diff --git a/boards/stm32f767zi/src/tasks/keyence.rs b/boards/stm32f767zi/src/tasks/keyence.rs index baaa447..f02343f 100644 --- a/boards/stm32f767zi/src/tasks/keyence.rs +++ b/boards/stm32f767zi/src/tasks/keyence.rs @@ -1,4 +1,4 @@ -use crate::io::gpio::Stm32f767ziGpio; +use crate::io::Stm32f767ziGpioInput; use embassy_stm32::gpio::{Input, Pull}; use embassy_time::{Duration, Timer}; use hyped_sensors::keyence::Keyence; @@ -7,7 +7,7 @@ use hyped_sensors::keyence::Keyence; #[embassy_executor::task] pub async fn read_keyence() -> ! { let p = embassy_stm32::init(Default::default()); - let mut keyence = Keyence::new(Stm32f767ziGpio::new(Input::new(p.PC13, Pull::Down))); + let mut keyence = Keyence::new(Stm32f767ziGpioInput::new(Input::new(p.PC13, Pull::Down))); loop { keyence.update_stripe_count(); diff --git a/boards/stm32f767zi/src/tasks/temperature.rs b/boards/stm32f767zi/src/tasks/temperature.rs index c735dfd..3fd3860 100644 --- a/boards/stm32f767zi/src/tasks/temperature.rs +++ b/boards/stm32f767zi/src/tasks/temperature.rs @@ -1,4 +1,4 @@ -use crate::io::i2c::Stm32f767ziI2c; +use crate::io::Stm32f767ziI2c; use defmt_rtt as _; use embassy_stm32::i2c::I2c; use embassy_stm32::time::Hertz; diff --git a/boards/stm32l432kc/Cargo.lock b/boards/stm32l432kc/Cargo.lock index af962ed..9c40aef 100644 --- a/boards/stm32l432kc/Cargo.lock +++ b/boards/stm32l432kc/Cargo.lock @@ -561,7 +561,6 @@ dependencies = [ "embassy-time", "embedded-hal 0.2.7", "embedded-storage", - "hyped_io", "hyped_sensors", "panic-probe", "rand_core", @@ -582,7 +581,14 @@ dependencies = [ ] [[package]] -name = "hyped_io" +name = "hyped_gpio_input" +version = "0.1.0" +dependencies = [ + "heapless", +] + +[[package]] +name = "hyped_i2c" version = "0.1.0" dependencies = [ "heapless", @@ -594,7 +600,8 @@ version = "0.1.0" dependencies = [ "heapless", "hyped_core", - "hyped_io", + "hyped_gpio_input", + "hyped_i2c", ] [[package]] diff --git a/boards/stm32l432kc/Cargo.toml b/boards/stm32l432kc/Cargo.toml index 7ab9fdc..6ac9647 100644 --- a/boards/stm32l432kc/Cargo.toml +++ b/boards/stm32l432kc/Cargo.toml @@ -23,6 +23,5 @@ critical-section = "1.1" embedded-storage = "0.3.1" static_cell = "2" -hyped_io = { path = "../../lib/io" } hyped_sensors = { path = "../../lib/sensors" } advanced-pid = { version = "0.2.2", default-features = false } diff --git a/boards/stm32l476rg/Cargo.lock b/boards/stm32l476rg/Cargo.lock index 108883d..19b1fbb 100644 --- a/boards/stm32l476rg/Cargo.lock +++ b/boards/stm32l476rg/Cargo.lock @@ -530,6 +530,21 @@ dependencies = [ "stable_deref_trait", ] +[[package]] +name = "hyped_adc" +version = "0.1.0" +dependencies = [ + "heapless", +] + +[[package]] +name = "hyped_adc_derive" +version = "0.1.0" +dependencies = [ + "quote", + "syn 2.0.79", +] + [[package]] name = "hyped_boards_stm32l476rg" version = "0.1.0" @@ -547,7 +562,12 @@ dependencies = [ "embassy-time", "embedded-hal 0.2.7", "embedded-storage", - "hyped_io", + "hyped_adc", + "hyped_adc_derive", + "hyped_gpio_input", + "hyped_gpio_input_derive", + "hyped_i2c", + "hyped_i2c_derive", "hyped_sensors", "panic-probe", "rand_core", @@ -568,19 +588,43 @@ dependencies = [ ] [[package]] -name = "hyped_io" +name = "hyped_gpio_input" version = "0.1.0" dependencies = [ "heapless", ] +[[package]] +name = "hyped_gpio_input_derive" +version = "0.1.0" +dependencies = [ + "quote", + "syn 2.0.79", +] + +[[package]] +name = "hyped_i2c" +version = "0.1.0" +dependencies = [ + "heapless", +] + +[[package]] +name = "hyped_i2c_derive" +version = "0.1.0" +dependencies = [ + "quote", + "syn 2.0.79", +] + [[package]] name = "hyped_sensors" version = "0.1.0" dependencies = [ "heapless", "hyped_core", - "hyped_io", + "hyped_gpio_input", + "hyped_i2c", ] [[package]] diff --git a/boards/stm32l476rg/Cargo.toml b/boards/stm32l476rg/Cargo.toml index 55a129f..370664f 100644 --- a/boards/stm32l476rg/Cargo.toml +++ b/boards/stm32l476rg/Cargo.toml @@ -23,5 +23,11 @@ critical-section = "1.1" embedded-storage = "0.3.1" static_cell = "2" -hyped_io = { path = "../../lib/io" } hyped_sensors = { path = "../../lib/sensors" } + +hyped_adc = { path = "../../lib/io/hyped_adc" } +hyped_adc_derive = { path = "../../lib/io/hyped_adc/hyped_adc_derive" } +hyped_i2c = { path = "../../lib/io/hyped_i2c" } +hyped_i2c_derive = { path = "../../lib/io/hyped_i2c/hyped_i2c_derive" } +hyped_gpio_input = { path = "../../lib/io/hyped_gpio_input" } +hyped_gpio_input_derive = { path = "../../lib/io/hyped_gpio_input/hyped_gpio_input_derive" } \ No newline at end of file diff --git a/boards/stm32l476rg/src/io.rs b/boards/stm32l476rg/src/io.rs index 3d3539c..f886a3f 100644 --- a/boards/stm32l476rg/src/io.rs +++ b/boards/stm32l476rg/src/io.rs @@ -1,3 +1,26 @@ -pub mod adc; -pub mod gpio; -pub mod i2c; +use embassy_stm32::adc::{Adc, AnyAdcChannel, Instance}; +use embassy_stm32::gpio::Input; +use embassy_stm32::{i2c::I2c, mode::Blocking}; + +use hyped_adc::HypedAdc; +use hyped_adc_derive::HypedAdc; +use hyped_gpio_input::HypedGpioInput; +use hyped_gpio_input_derive::HypedGpioInput; +use hyped_i2c::{HypedI2c, I2cError}; +use hyped_i2c_derive::HypedI2c; + +#[derive(HypedAdc)] +pub struct Stm32l476rgAdc<'d, T: Instance> { + adc: Adc<'d, T>, + channel: AnyAdcChannel, +} + +#[derive(HypedGpioInput)] +pub struct Stm32l476rgGpioInput { + pin: Input<'static>, +} + +#[derive(HypedI2c)] +pub struct Stm32l476rgI2c<'d> { + i2c: I2c<'d, Blocking>, +} diff --git a/boards/stm32l476rg/src/io/adc.rs b/boards/stm32l476rg/src/io/adc.rs deleted file mode 100644 index 077f7a4..0000000 --- a/boards/stm32l476rg/src/io/adc.rs +++ /dev/null @@ -1,21 +0,0 @@ -use embassy_stm32::adc::{Adc, AnyAdcChannel, Instance}; -use hyped_io::adc::HypedAdc; - -pub struct Stm32l476rgAdc<'d, T: Instance> { - adc: Adc<'d, T>, - channel: AnyAdcChannel, -} - -impl<'d, T: Instance> HypedAdc for Stm32l476rgAdc<'d, T> { - /// Read a value from the ADC channel - fn read_value(&mut self) -> u16 { - self.adc.blocking_read(&mut self.channel) - } -} - -impl<'d, T: Instance> Stm32l476rgAdc<'d, T> { - /// Create a new instance of our ADC implementation for the STM32L476RG - pub fn new(adc: Adc<'d, T>, channel: AnyAdcChannel) -> Self { - Self { adc, channel } - } -} diff --git a/boards/stm32l476rg/src/io/gpio.rs b/boards/stm32l476rg/src/io/gpio.rs deleted file mode 100644 index 8bef3ad..0000000 --- a/boards/stm32l476rg/src/io/gpio.rs +++ /dev/null @@ -1,20 +0,0 @@ -use embassy_stm32::gpio::Input; -use hyped_io::gpio::HypedGpioPin; - -/// A GPIO pin on the STM32L476RG. -pub struct Stm32l476rgGpio { - pin: Input<'static>, -} - -impl HypedGpioPin for Stm32l476rgGpio { - fn is_high(&mut self) -> bool { - self.pin.is_high() - } -} - -impl Stm32l476rgGpio { - /// Create a new instance of our GPIO implementation for the STM32L476RG - pub fn new(pin: Input<'static>) -> Self { - Self { pin } - } -} diff --git a/boards/stm32l476rg/src/io/i2c.rs b/boards/stm32l476rg/src/io/i2c.rs deleted file mode 100644 index c6a7bbb..0000000 --- a/boards/stm32l476rg/src/io/i2c.rs +++ /dev/null @@ -1,51 +0,0 @@ -use embassy_stm32::{i2c::I2c, mode::Blocking}; -use hyped_io::i2c::{HypedI2c, I2cError}; - -pub struct Stm32l476rgI2c<'d> { - i2c: I2c<'d, Blocking>, -} - -impl<'d> HypedI2c for Stm32l476rgI2c<'d> { - /// Read a byte from a register on a device - fn read_byte(&mut self, device_address: u8, register_address: u8) -> Option { - let mut read = [0]; - let result = - self.i2c - .blocking_write_read(device_address, [register_address].as_ref(), &mut read); - match result { - Ok(_) => Some(read[0]), - Err(_) => None, - } - } - - /// Write a byte to a register on a device - fn write_byte_to_register( - &mut self, - device_address: u8, - register_address: u8, - data: u8, - ) -> Result<(), I2cError> { - let result = self - .i2c - .blocking_write(device_address, [register_address, data].as_ref()); - match result { - Ok(_) => Ok(()), - Err(e) => Err(match e { - embassy_stm32::i2c::Error::Bus => I2cError::Bus, - embassy_stm32::i2c::Error::Arbitration => I2cError::Arbitration, - embassy_stm32::i2c::Error::Nack => I2cError::Nack, - embassy_stm32::i2c::Error::Timeout => I2cError::Timeout, - embassy_stm32::i2c::Error::Crc => I2cError::Crc, - embassy_stm32::i2c::Error::Overrun => I2cError::Overrun, - embassy_stm32::i2c::Error::ZeroLengthTransfer => I2cError::ZeroLengthTransfer, - }), - } - } -} - -impl<'d> Stm32l476rgI2c<'d> { - /// Create a new instance of our I2C implementation for the STM32L476RG - pub fn new(i2c: I2c<'d, Blocking>) -> Self { - Self { i2c } - } -} diff --git a/boards/stm32l476rg/src/tasks/keyence.rs b/boards/stm32l476rg/src/tasks/keyence.rs index e45abd9..24b38b8 100644 --- a/boards/stm32l476rg/src/tasks/keyence.rs +++ b/boards/stm32l476rg/src/tasks/keyence.rs @@ -1,4 +1,4 @@ -use crate::io::gpio::Stm32l476rgGpio; +use crate::io::Stm32l476rgGpioInput; use embassy_stm32::gpio::{Input, Pull}; use embassy_time::{Duration, Timer}; use hyped_sensors::keyence::Keyence; @@ -7,7 +7,7 @@ use hyped_sensors::keyence::Keyence; #[embassy_executor::task] pub async fn read_keyence() -> ! { let p = embassy_stm32::init(Default::default()); - let mut keyence = Keyence::new(Stm32l476rgGpio::new(Input::new(p.PC13, Pull::Down))); + let mut keyence = Keyence::new(Stm32l476rgGpioInput::new(Input::new(p.PC13, Pull::Down))); loop { keyence.update_stripe_count(); diff --git a/boards/stm32l476rg/src/tasks/temperature.rs b/boards/stm32l476rg/src/tasks/temperature.rs index d6e1068..c439111 100644 --- a/boards/stm32l476rg/src/tasks/temperature.rs +++ b/boards/stm32l476rg/src/tasks/temperature.rs @@ -1,4 +1,4 @@ -use crate::io::i2c::Stm32l476rgI2c; +use crate::io::Stm32l476rgI2c; use defmt_rtt as _; use embassy_stm32::i2c::I2c; use embassy_stm32::time::Hertz; diff --git a/lib/io/Cargo.toml b/lib/io/hyped_adc/Cargo.toml similarity index 62% rename from lib/io/Cargo.toml rename to lib/io/hyped_adc/Cargo.toml index e618ed1..b12271e 100644 --- a/lib/io/Cargo.toml +++ b/lib/io/hyped_adc/Cargo.toml @@ -1,7 +1,7 @@ [package] -name = "hyped_io" +name = "hyped_adc" version = "0.1.0" edition = "2021" [dependencies] -heapless = "0.8.0" +heapless = "0.8.0" \ No newline at end of file diff --git a/lib/io/hyped_adc/hyped_adc_derive/Cargo.lock b/lib/io/hyped_adc/hyped_adc_derive/Cargo.lock new file mode 100644 index 0000000..38de287 --- /dev/null +++ b/lib/io/hyped_adc/hyped_adc_derive/Cargo.lock @@ -0,0 +1,46 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "hyped_adc_derive" +version = "0.1.0" +dependencies = [ + "quote", + "syn", +] + +[[package]] +name = "proc-macro2" +version = "1.0.91" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "307e3004becf10f5a6e0d59d20f3cd28231b0e0827a96cd3e0ce6d14bc1e4bb3" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.37" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b5b9d34b8991d19d98081b46eacdd8eb58c6f2b201139f7c5f643cc155a633af" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "syn" +version = "2.0.89" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "44d46482f1c1c87acd84dea20c1bf5ebff4c757009ed6bf19cfd36fb10e92c4e" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "unicode-ident" +version = "1.0.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "adb9e6ca4f869e1180728b7950e35922a7fc6397f7b641499e8f3ef06e50dc83" diff --git a/lib/io/hyped_adc/hyped_adc_derive/Cargo.toml b/lib/io/hyped_adc/hyped_adc_derive/Cargo.toml new file mode 100644 index 0000000..fc1427d --- /dev/null +++ b/lib/io/hyped_adc/hyped_adc_derive/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "hyped_adc_derive" +version = "0.1.0" +edition = "2021" + +[lib] +proc-macro = true + +[dependencies] +syn = { version = "2", features = ["full"] } +quote = "1" diff --git a/lib/io/hyped_adc/hyped_adc_derive/src/lib.rs b/lib/io/hyped_adc/hyped_adc_derive/src/lib.rs new file mode 100644 index 0000000..41d1554 --- /dev/null +++ b/lib/io/hyped_adc/hyped_adc_derive/src/lib.rs @@ -0,0 +1,33 @@ +extern crate proc_macro; + +use proc_macro::TokenStream; +use quote::quote; +use syn::{self, Generics}; + +#[proc_macro_derive(HypedAdc)] +pub fn hyped_adc_derive(input: TokenStream) -> TokenStream { + let ast = syn::parse(input).unwrap(); + impl_hyped_adc(&ast) +} + +fn impl_hyped_adc(ast: &syn::DeriveInput) -> TokenStream { + let name = &ast.ident; + let generics: &Generics = &ast.generics; + let (impl_generics, ty_generics, _) = generics.split_for_impl(); + let gen = quote! { + impl #impl_generics HypedAdc for #name #ty_generics { + /// Read a value from the ADC channel + fn read_value(&mut self) -> u16 { + self.adc.blocking_read(&mut self.channel) + } + } + + impl #impl_generics #name #ty_generics { + /// Create a new instance of our ADC implementation for the STM32L476RG + pub fn new(adc: Adc<'d, T>, channel: AnyAdcChannel) -> Self { + Self { adc, channel } + } + } + }; + gen.into() +} diff --git a/lib/io/src/adc.rs b/lib/io/hyped_adc/src/lib.rs similarity index 91% rename from lib/io/src/adc.rs rename to lib/io/hyped_adc/src/lib.rs index 98eb5c5..ab564e4 100644 --- a/lib/io/src/adc.rs +++ b/lib/io/hyped_adc/src/lib.rs @@ -1,9 +1,12 @@ +#![no_std] + /// ADC trait used to abstract the ADC peripheral pub trait HypedAdc { fn read_value(&mut self) -> u16; } pub mod mock_adc { + use core::clone::Clone; use heapless::Vec; /// A mock ADC instance which can be used for testing @@ -12,7 +15,7 @@ pub mod mock_adc { next_values: Vec, } - impl crate::adc::HypedAdc for MockAdc { + impl crate::HypedAdc for MockAdc { /// Reads a value from the ADC fn read_value(&mut self) -> u16 { let next_value: u16 = self.next_values.pop().unwrap_or(self.current_value); diff --git a/lib/io/hyped_gpio_input/Cargo.toml b/lib/io/hyped_gpio_input/Cargo.toml new file mode 100644 index 0000000..a98c450 --- /dev/null +++ b/lib/io/hyped_gpio_input/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "hyped_gpio_input" +version = "0.1.0" +edition = "2021" + +[dependencies] +heapless = "0.8.0" \ No newline at end of file diff --git a/lib/io/hyped_gpio_input/hyped_gpio_input_derive/Cargo.lock b/lib/io/hyped_gpio_input/hyped_gpio_input_derive/Cargo.lock new file mode 100644 index 0000000..38de287 --- /dev/null +++ b/lib/io/hyped_gpio_input/hyped_gpio_input_derive/Cargo.lock @@ -0,0 +1,46 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "hyped_adc_derive" +version = "0.1.0" +dependencies = [ + "quote", + "syn", +] + +[[package]] +name = "proc-macro2" +version = "1.0.91" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "307e3004becf10f5a6e0d59d20f3cd28231b0e0827a96cd3e0ce6d14bc1e4bb3" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.37" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b5b9d34b8991d19d98081b46eacdd8eb58c6f2b201139f7c5f643cc155a633af" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "syn" +version = "2.0.89" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "44d46482f1c1c87acd84dea20c1bf5ebff4c757009ed6bf19cfd36fb10e92c4e" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "unicode-ident" +version = "1.0.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "adb9e6ca4f869e1180728b7950e35922a7fc6397f7b641499e8f3ef06e50dc83" diff --git a/lib/io/hyped_gpio_input/hyped_gpio_input_derive/Cargo.toml b/lib/io/hyped_gpio_input/hyped_gpio_input_derive/Cargo.toml new file mode 100644 index 0000000..a636eee --- /dev/null +++ b/lib/io/hyped_gpio_input/hyped_gpio_input_derive/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "hyped_gpio_input_derive" +version = "0.1.0" +edition = "2021" + +[lib] +proc-macro = true + +[dependencies] +syn = { version = "2", features = ["full"] } +quote = "1" diff --git a/lib/io/hyped_gpio_input/hyped_gpio_input_derive/src/lib.rs b/lib/io/hyped_gpio_input/hyped_gpio_input_derive/src/lib.rs new file mode 100644 index 0000000..fce2194 --- /dev/null +++ b/lib/io/hyped_gpio_input/hyped_gpio_input_derive/src/lib.rs @@ -0,0 +1,30 @@ +extern crate proc_macro; + +use proc_macro::TokenStream; +use quote::quote; +use syn; + +#[proc_macro_derive(HypedGpioInput)] +pub fn hyped_gpio_derive(input: TokenStream) -> TokenStream { + let ast = syn::parse(input).unwrap(); + impl_hyped_gpio_input(&ast) +} + +fn impl_hyped_gpio_input(ast: &syn::DeriveInput) -> TokenStream { + let name = &ast.ident; + let gen = quote! { + impl HypedGpioInput for #name { + fn is_high(&mut self) -> bool { + self.pin.is_high() + } + } + + impl #name { + /// Create a new instance of our GPIO implementation for the STM32L476RG + pub fn new(pin: Input<'static>) -> Self { + Self { pin } + } + } + }; + gen.into() +} diff --git a/lib/io/src/gpio.rs b/lib/io/hyped_gpio_input/src/lib.rs similarity index 74% rename from lib/io/src/gpio.rs rename to lib/io/hyped_gpio_input/src/lib.rs index 15e7707..2b08fc6 100644 --- a/lib/io/src/gpio.rs +++ b/lib/io/hyped_gpio_input/src/lib.rs @@ -1,5 +1,7 @@ +#![no_std] + /// Abstraction for a GPIO pin so that sensors can be tested with a mock GPIO pin -pub trait HypedGpioPin { +pub trait HypedGpioInput { fn is_high(&mut self) -> bool; } @@ -7,12 +9,12 @@ pub mod mock_gpio { use heapless::Vec; /// A mock GPIO pin that can be used for testing - pub struct MockGpio { + pub struct MockGpioInput { current_value: bool, next_values: Vec, } - impl crate::gpio::HypedGpioPin for MockGpio { + impl crate::HypedGpioInput for MockGpioInput { fn is_high(&mut self) -> bool { let next_value = self.next_values.pop().unwrap_or(self.current_value); self.current_value = next_value; @@ -20,11 +22,11 @@ pub mod mock_gpio { } } - impl MockGpio { - pub fn new(values: Vec) -> MockGpio { + impl MockGpioInput { + pub fn new(values: Vec) -> MockGpioInput { let mut next_values = values.clone(); next_values.reverse(); - MockGpio { + MockGpioInput { current_value: false, next_values, } diff --git a/lib/io/hyped_i2c/Cargo.toml b/lib/io/hyped_i2c/Cargo.toml new file mode 100644 index 0000000..bec8a24 --- /dev/null +++ b/lib/io/hyped_i2c/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "hyped_i2c" +version = "0.1.0" +edition = "2021" + +[dependencies] +heapless = "0.8.0" \ No newline at end of file diff --git a/lib/io/hyped_i2c/hyped_i2c_derive/Cargo.lock b/lib/io/hyped_i2c/hyped_i2c_derive/Cargo.lock new file mode 100644 index 0000000..38de287 --- /dev/null +++ b/lib/io/hyped_i2c/hyped_i2c_derive/Cargo.lock @@ -0,0 +1,46 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "hyped_adc_derive" +version = "0.1.0" +dependencies = [ + "quote", + "syn", +] + +[[package]] +name = "proc-macro2" +version = "1.0.91" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "307e3004becf10f5a6e0d59d20f3cd28231b0e0827a96cd3e0ce6d14bc1e4bb3" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.37" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b5b9d34b8991d19d98081b46eacdd8eb58c6f2b201139f7c5f643cc155a633af" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "syn" +version = "2.0.89" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "44d46482f1c1c87acd84dea20c1bf5ebff4c757009ed6bf19cfd36fb10e92c4e" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "unicode-ident" +version = "1.0.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "adb9e6ca4f869e1180728b7950e35922a7fc6397f7b641499e8f3ef06e50dc83" diff --git a/lib/io/hyped_i2c/hyped_i2c_derive/Cargo.toml b/lib/io/hyped_i2c/hyped_i2c_derive/Cargo.toml new file mode 100644 index 0000000..9acd87f --- /dev/null +++ b/lib/io/hyped_i2c/hyped_i2c_derive/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "hyped_i2c_derive" +version = "0.1.0" +edition = "2021" + +[lib] +proc-macro = true + +[dependencies] +syn = { version = "2", features = ["full"] } +quote = "1" diff --git a/lib/io/hyped_i2c/hyped_i2c_derive/src/lib.rs b/lib/io/hyped_i2c/hyped_i2c_derive/src/lib.rs new file mode 100644 index 0000000..602891f --- /dev/null +++ b/lib/io/hyped_i2c/hyped_i2c_derive/src/lib.rs @@ -0,0 +1,65 @@ +extern crate proc_macro; + +use proc_macro::TokenStream; +use quote::quote; +use syn::{self, Generics}; + +#[proc_macro_derive(HypedI2c)] +pub fn hyped_i2c_derive(input: TokenStream) -> TokenStream { + let ast = syn::parse(input).unwrap(); + impl_hyped_i2c(&ast) +} + +fn impl_hyped_i2c(ast: &syn::DeriveInput) -> TokenStream { + let name = &ast.ident; + let generics: &Generics = &ast.generics; + let (impl_generics, ty_generics, _) = generics.split_for_impl(); + let gen = quote! { + impl #impl_generics HypedI2c for #name #ty_generics{ + /// Read a byte from a register on a device + fn read_byte(&mut self, device_address: u8, register_address: u8) -> Option { + let mut read = [0]; + let result = + self.i2c + .blocking_write_read(device_address, [register_address].as_ref(), &mut read); + match result { + Ok(_) => Some(read[0]), + Err(_) => None, + } + } + + /// Write a byte to a register on a device + fn write_byte_to_register( + &mut self, + device_address: u8, + register_address: u8, + data: u8, + ) -> Result<(), I2cError> { + let result = self + .i2c + .blocking_write(device_address, [register_address, data].as_ref()); + match result { + Ok(_) => Ok(()), + Err(e) => Err(match e { + embassy_stm32::i2c::Error::Bus => I2cError::Bus, + embassy_stm32::i2c::Error::Arbitration => I2cError::Arbitration, + embassy_stm32::i2c::Error::Nack => I2cError::Nack, + embassy_stm32::i2c::Error::Timeout => I2cError::Timeout, + embassy_stm32::i2c::Error::Crc => I2cError::Crc, + embassy_stm32::i2c::Error::Overrun => I2cError::Overrun, + embassy_stm32::i2c::Error::ZeroLengthTransfer => I2cError::ZeroLengthTransfer, + }), + } + } + } + + impl #impl_generics #name #ty_generics { + /// Create a new instance of our I2C implementation for the STM32L476RG + pub fn new(i2c: I2c<'d, Blocking>) -> Self { + Self { i2c } + } + } + + }; + gen.into() +} diff --git a/lib/io/src/i2c.rs b/lib/io/hyped_i2c/src/lib.rs similarity index 97% rename from lib/io/src/i2c.rs rename to lib/io/hyped_i2c/src/lib.rs index 360a845..530602c 100644 --- a/lib/io/src/i2c.rs +++ b/lib/io/hyped_i2c/src/lib.rs @@ -1,3 +1,5 @@ +#![no_std] + /// I2C errors that can occur /// From: https://docs.embassy.dev/embassy-stm32/git/stm32g031c8/i2c/enum.Error.html #[derive(Debug)] @@ -35,7 +37,7 @@ pub mod mock_i2c { writes: I2cValues, } - impl crate::i2c::HypedI2c for MockI2c { + impl crate::HypedI2c for MockI2c { /// Reads a byte by looking up the device address and register address in the map fn read_byte(&mut self, device_address: u8, register_address: u8) -> Option { self.values diff --git a/lib/io/hyped_spi/Cargo.toml b/lib/io/hyped_spi/Cargo.toml new file mode 100644 index 0000000..c9773b7 --- /dev/null +++ b/lib/io/hyped_spi/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "hyped_spi" +version = "0.1.0" +edition = "2021" + +[dependencies] +heapless = "0.8.0" \ No newline at end of file diff --git a/lib/io/hyped_spi/hyped_spi_derive/Cargo.lock b/lib/io/hyped_spi/hyped_spi_derive/Cargo.lock new file mode 100644 index 0000000..38de287 --- /dev/null +++ b/lib/io/hyped_spi/hyped_spi_derive/Cargo.lock @@ -0,0 +1,46 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "hyped_adc_derive" +version = "0.1.0" +dependencies = [ + "quote", + "syn", +] + +[[package]] +name = "proc-macro2" +version = "1.0.91" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "307e3004becf10f5a6e0d59d20f3cd28231b0e0827a96cd3e0ce6d14bc1e4bb3" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.37" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b5b9d34b8991d19d98081b46eacdd8eb58c6f2b201139f7c5f643cc155a633af" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "syn" +version = "2.0.89" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "44d46482f1c1c87acd84dea20c1bf5ebff4c757009ed6bf19cfd36fb10e92c4e" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "unicode-ident" +version = "1.0.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "adb9e6ca4f869e1180728b7950e35922a7fc6397f7b641499e8f3ef06e50dc83" diff --git a/lib/io/hyped_spi/hyped_spi_derive/Cargo.toml b/lib/io/hyped_spi/hyped_spi_derive/Cargo.toml new file mode 100644 index 0000000..39e4cb4 --- /dev/null +++ b/lib/io/hyped_spi/hyped_spi_derive/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "hyped_spi_derive" +version = "0.1.0" +edition = "2021" + +[lib] +proc-macro = true + +[dependencies] +syn = { version = "2", features = ["full"] } +quote = "1" diff --git a/lib/io/hyped_spi/hyped_spi_derive/src/lib.rs b/lib/io/hyped_spi/hyped_spi_derive/src/lib.rs new file mode 100644 index 0000000..736b74a --- /dev/null +++ b/lib/io/hyped_spi/hyped_spi_derive/src/lib.rs @@ -0,0 +1,19 @@ +extern crate proc_macro; + +use proc_macro::TokenStream; +use quote::quote; +use syn::{self, Generics}; + +#[proc_macro_derive(HypedSpi)] +pub fn hyped_spi_derive(input: TokenStream) -> TokenStream { + let ast = syn::parse(input).unwrap(); + impl_hyped_spi(&ast) +} + +fn impl_hyped_spi(ast: &syn::DeriveInput) -> TokenStream { + let name = &ast.ident; + let generics: &Generics = &ast.generics; + let (_impl_generics, _ty_generics, _) = generics.split_for_impl(); + let gen = quote! {}; + gen.into() +} diff --git a/lib/io/src/spi.rs b/lib/io/hyped_spi/src/lib.rs similarity index 98% rename from lib/io/src/spi.rs rename to lib/io/hyped_spi/src/lib.rs index 479dc78..0703395 100644 --- a/lib/io/src/spi.rs +++ b/lib/io/hyped_spi/src/lib.rs @@ -1,3 +1,5 @@ +#![no_std] + /// SPI errors that can occur /// From: https://docs.embassy.dev/embassy-stm32/git/stm32f103c8/spi/enum.Error.html pub enum SpiError { diff --git a/lib/io/src/lib.rs b/lib/io/src/lib.rs deleted file mode 100644 index 4b7222a..0000000 --- a/lib/io/src/lib.rs +++ /dev/null @@ -1,6 +0,0 @@ -#![no_std] - -pub mod adc; -pub mod gpio; -pub mod i2c; -pub mod spi; diff --git a/lib/sensors/Cargo.toml b/lib/sensors/Cargo.toml index 1d18e45..52dc817 100644 --- a/lib/sensors/Cargo.toml +++ b/lib/sensors/Cargo.toml @@ -7,5 +7,5 @@ edition = "2021" heapless = "0.8.0" hyped_core = { path = "../core" } -hyped_io = { path = "../io" } - +hyped_i2c = { path = "../io/hyped_i2c" } +hyped_gpio_input = { path = "../io/hyped_gpio_input" } \ No newline at end of file diff --git a/lib/sensors/src/keyence.rs b/lib/sensors/src/keyence.rs index 25d5f61..30bd74c 100644 --- a/lib/sensors/src/keyence.rs +++ b/lib/sensors/src/keyence.rs @@ -1,11 +1,11 @@ use hyped_core::types::DigitalSignal; -use hyped_io::gpio::HypedGpioPin; +use hyped_gpio_input::HypedGpioInput; /// Keyence represents a Keyence sensor which keeps track of the number of stripes that have passed /// by the sensor. The Keyence sensor is connected to a GPIO pin which reads a high signal when a /// stripe is detected and a low signal when no stripe is detected. The stripe count is updated /// whenever the signal changes from low to high (positive edge). -pub struct Keyence { +pub struct Keyence { /// The number of stripes that have passed by the sensor. stripe_count: u32, /// The last signal that was read from the sensor. @@ -13,7 +13,7 @@ pub struct Keyence { gpio: T, } -impl Keyence { +impl Keyence { /// Creates a new Keyence sensor with an initial stripe count of 0 and a last signal of low. pub fn new(gpio: T) -> Keyence { Keyence { @@ -42,18 +42,18 @@ impl Keyence { mod tests { use super::*; use heapless::Vec; - use hyped_io::gpio::mock_gpio::MockGpio; + use hyped_gpio_input::mock_gpio::MockGpioInput; #[test] fn test_keyence_new() { - let gpio = MockGpio::new(Vec::from_slice(&[false]).unwrap()); + let gpio = MockGpioInput::new(Vec::from_slice(&[false]).unwrap()); let keyence = Keyence::new(gpio); assert_eq!(keyence.get_stripe_count(), 0); } #[test] fn test_keyence_update_stripe_count_low_to_high() { - let gpio = MockGpio::new(Vec::from_slice(&[false, true]).unwrap()); + let gpio = MockGpioInput::new(Vec::from_slice(&[false, true]).unwrap()); let mut keyence = Keyence::new(gpio); keyence.update_stripe_count(); @@ -64,7 +64,7 @@ mod tests { #[test] fn test_keyence_update_stripe_count_high_to_low() { - let gpio = MockGpio::new(Vec::from_slice(&[true, false]).unwrap()); + let gpio = MockGpioInput::new(Vec::from_slice(&[true, false]).unwrap()); let mut keyence = Keyence::new(gpio); keyence.update_stripe_count(); @@ -75,7 +75,7 @@ mod tests { #[test] fn test_keyence_update_stripe_count_high_to_high() { - let gpio = MockGpio::new(Vec::from_slice(&[true, true]).unwrap()); + let gpio = MockGpioInput::new(Vec::from_slice(&[true, true]).unwrap()); let mut keyence = Keyence::new(gpio); keyence.update_stripe_count(); @@ -86,7 +86,7 @@ mod tests { #[test] fn test_keyence_update_stripe_count_low_to_low() { - let gpio = MockGpio::new(Vec::from_slice(&[false, false]).unwrap()); + let gpio = MockGpioInput::new(Vec::from_slice(&[false, false]).unwrap()); let mut keyence = Keyence::new(gpio); keyence.update_stripe_count(); diff --git a/lib/sensors/src/temperature.rs b/lib/sensors/src/temperature.rs index 322ea89..3b66881 100644 --- a/lib/sensors/src/temperature.rs +++ b/lib/sensors/src/temperature.rs @@ -1,4 +1,4 @@ -use hyped_io::i2c::{HypedI2c, I2cError}; +use hyped_i2c::{HypedI2c, I2cError}; /// Temperature implements the logic to read the temperature from the STTS22H temperature sensor /// using the I2C peripheral provided by the HypedI2c trait. @@ -128,7 +128,7 @@ const TWO_POWER_16: f32 = 65536.0; mod tests { use super::*; use heapless::FnvIndexMap; - use hyped_io::i2c::mock_i2c::MockI2c; + use hyped_i2c::mock_i2c::MockI2c; #[test] fn test_write_config() {