Giter VIP home page Giter VIP logo

Comments (2)

Snapstromegon avatar Snapstromegon commented on June 18, 2024 1

I agree, my confusions was, that the InputPin trait of the esp-idf-hal does not implement is_high and similar. Also I wanted to use the subscribe() method on pins, but that one does indeed seem to not be possible in a generic way as far as I can see, since the embedded_hal (version 1.0.0-alpha.8) only offers a blocking way and the esp_idf_hal::gpio::SubscribedPin trait doesn't offer a subscribe function too.

Since this question was resolved, I will close the issue - thanks for your support.

from esp-idf-hal.

tcbennun avatar tcbennun commented on June 18, 2024

In terms of type safety, it makes sense for into_output and so forth to only be implemented on the specific pin types. This allows common mistakes to be caught at compile time, such as trying to use an input-only pin as an output pin.

IMHO, an ergonomic lib function taking pins as arguments would simply do the following...

use embedded_hal::digital::v2::{OutputPin, InputPin};

pub struct SomePeripheral<ResetPin, IntrPin> {
    reset: ResetPin,
    intr: IntrPin,
}

impl<ResetPin, IntrPin> SomePeripheral<ResetPin, IntrPin>
where
    ResetPin: OutputPin,
    IntrPin: InputPin,
{
    pub fn new(reset: ResetPin, intr: IntrPin) -> Self {
        Self { reset, intr, }
    }

    pub fn reset(&mut self) -> Result<(), ResetPin::Error> {
        self.reset.set_high()
    }
}

Note that this library is, so far, also platform-independent.

Then yes, the ESP32 user has to do a little initialization, but nothing absurd:

fn main() {
    let peripherals = Peripherals::take().unwrap();

    let mut my_periph = SomePeripheral::new(
        peripherals.pins.gpio5.into_output().unwrap(),
        peripherals.pins.gpio6.into_input().unwrap(),
    );

    my_periph.reset().unwrap();

    // ...
}

The little extra boilerplate there is worth every penny, frankly.

from esp-idf-hal.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.