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

Add digital::IoPin to resolve issue 29 #269

Merged
merged 1 commit into from
Apr 27, 2021
Merged

Conversation

MorganR
Copy link
Contributor

@MorganR MorganR commented Apr 25, 2021

Closes #29.

This implementation is inspired by chrismoos's comment in #29.

I've demonstrated using this trait for a no-std DHT11 driver, and verified this on a Raspberry Pi 3B+ using an implementation in linux-embedded-hal (PR to follow).

I'll also be sending a PR to implement this in at least one additional HAL implementation library (recommendations welcome).

One question I have is how copyright/authorship is tracked in this repo? I believe the copyright owner for my code would technically be Google LLC. When patching repos, I'm meant to add that to the appropriate copyright/authors list.

@rust-highfive
Copy link

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @therealprof (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

@MorganR
Copy link
Contributor Author

MorganR commented Apr 25, 2021

PR on linux-embedded-hal: rust-embedded/linux-embedded-hal#60

Copy link
Member

@eldruin eldruin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this proposal!
Could you add some examples on how to use this to the docs?
I do not know about copyright / authorship attribution matters. The current crate author list is missing many people.

CHANGELOG.md Outdated Show resolved Hide resolved
src/digital.rs Outdated Show resolved Hide resolved
src/digital.rs Outdated Show resolved Hide resolved
src/digital.rs Outdated Show resolved Hide resolved
src/digital.rs Outdated
///
/// If the pin is already in the requested state, this method should
/// succeed.
fn try_switch_to_output_pin(self, state: PinState) -> Result<TOutput, Self::Error>;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would not setting an initial state also make sense?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the user didn't specify the state, then the result would be a pin in output mode that's either high or low, but the user doesn't know which it is. I think it's better to make the user specify which state they want rather than to leave it ambiguous.

That being, said I'm no expert in the embedded space. Do you see a valid use case?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was just wondering. From a software perspective it makes sense to always change to a defined state. However, from a hardware perspective, it may not always make sense to set a pin, since it has electrical implications.
Does anybody else here have an use case for changing the pin type to output but leaving it unset?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIUC, an example use-case might be an output pin in open drain mode (I could be wrong; like I said, I'm not an embedded expert). However, the current digital traits don't seem to provide any support around these states either. Maybe it makes sense to look at that separately, eg. as an addition to PinState? Definitely interested in what other folks think here, as @eldruin said.

@MorganR
Copy link
Contributor Author

MorganR commented Apr 26, 2021

I've added an example to the IoPin docs.

I am not a lawyer, but I expect that if there isn't a formal process for tracking copyright at the moment, then the PR/commit history will be fine.

Copy link
Member

@eldruin eldruin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, although I do not have the whole discussion about this in #29 in mind at the moment.

src/digital.rs Outdated Show resolved Hide resolved
src/digital.rs Outdated Show resolved Hide resolved
@therealprof
Copy link
Contributor

@MorganR This is looking great.

I'm wondering if we can make the type stuff a little bit easier on the eyes and the mind but other than that I like it.

@MorganR
Copy link
Contributor Author

MorganR commented Apr 27, 2021

Thanks @therealprof! I agree the type system becomes a bit verbose when using the abstract trait. However, it's actually fairly clean when implementing the trait, and in user code that gives a concrete implementation to something using the abstraction.

To simplify things a little more, I've removed the TIoPin parameter from the example function. This is superfluous because the function can instead accept a pin that's already in either TInputPin or TOutputPin format.

@therealprof
Copy link
Contributor

If you could squash the commits and maybe add a "Closes #29" to your initial post, that would be great.

@MorganR
Copy link
Contributor Author

MorganR commented Apr 27, 2021

If you could squash the commits and maybe add a "Closes #29" to your initial post, that would be great.

Done

therealprof
therealprof previously approved these changes Apr 27, 2021
Copy link
Contributor

@therealprof therealprof left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot for your efforts!

bors r+

bors bot added a commit that referenced this pull request Apr 27, 2021
269: Add digital::IoPin to resolve issue 29 r=therealprof a=MorganR

Closes #29.

This implementation is inspired by [chrismoos's comment](#29 (comment)) in #29.

I've demonstrated using this trait for a [no-std DHT11 driver](https://github.com/MorganR/rust-simple-sensors/blob/main/src/dht11.rs), and verified this on a Raspberry Pi 3B+ using an implementation in linux-embedded-hal (PR to follow). 

I'll also be sending a PR to implement this in at least one additional HAL implementation library (recommendations welcome).

One question I have is how copyright/authorship is tracked in this repo? I believe the copyright owner for my code would technically be Google LLC. When patching repos, I'm meant to add that to the appropriate copyright/authors list.

Co-authored-by: Morgan Roff <mroff@google.com>
@bors
Copy link
Contributor

bors bot commented Apr 27, 2021

Build failed:

@MorganR
Copy link
Contributor Author

MorganR commented Apr 27, 2021

Sorry, fixed that remaining fmt error. Should be good now.

@therealprof
Copy link
Contributor

@MorganR I guess rustfmt doesn't like your formatting. 😅 It's a pity the new GH protection mechanisms seem to silently prevent actions from running...

Copy link
Contributor

@therealprof therealprof left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's try this again.

bors r+

@bors bors bot merged commit 1d22385 into rust-embedded:master Apr 27, 2021
@burrbull
Copy link
Member

Which combinations of output and input modes this should be implemented?
PushPull, OpenDrain
PullUp, PullDown, Floating
Alternate

bors bot added a commit to rust-embedded/linux-embedded-hal that referenced this pull request Oct 4, 2021
60: Implementation of embedded_hal::digital::IoPin r=eldruin a=MorganR

This is just a draft, and is pending the PR merge of an [IoPin implementation](rust-embedded/embedded-hal#269) into embedded_hal.

I've verified it with a [no-std DHT11 driver based on the IoPin PR](https://github.com/MorganR/rust-simple-sensors/blob/main/src/dht11.rs).

Co-authored-by: Morgan Roff <mroff@google.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Review is incomplete T-hal
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[RFC] digital::IoPin, pins that can switch between input and output modes at runtime
5 participants