-
Notifications
You must be signed in to change notification settings - Fork 232
set_state and toggle for digital::OutputPin #44
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
Conversation
set_state and toggle with default implementation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I quite like the set_state()
and toggle()
methods, especially since the latter is specialisable in case there's hardware toggle support.
Not so happy about the snuck in default implementation for is_high()
. IMHO we should rather (in a separate topic because this might be controversial!) add a get_state()
function (also for the InputPin) and make is_high()
and is_low()
be default implementations of the get_state()
function. In case of the OutputPin we may even have to make that fallible since it is not on all platforms possible to query the state of output pins (though my memory is faint on which platforms exactly didn't support that).
src/digital.rs
Outdated
fn set_high(&mut self); | ||
fn set_high(&mut self) { | ||
!self.is_low() | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This does not look right.
I personally feel that the if pa0.get_state() { // what do you mean by state? input state? output state? floating state?
// ..
}
if pa0.is_high() { // OTOH, this is totally unambiguous
// ..
} Now if Both I'm of the (unpopular) opinion that if pa1.is_high() {
pa0.set_high()
} else {
pa0.set_low()
} Yes, it's longer but it's pretty clear what it does. I give
And a remainder: adding methods to a trait is a breaking change unless the methods have a default implementation. |
Will |
@japaric Usually the state represents the current interpretation, i.e. the level while the "state" in your reading is called the mode, sometimes also separated into administrative mode and operational mode. |
I came here to propose adding a FWIW, over time I have come to the opinion that boolean input parameters are to be avoided because the intent is not clear. These days I try to prefer using an enum. In this case I would humbly advocate for Can we split out the |
@wez I agree with both of your points. However I wanted to add that using a custom enum makes using the methods a bit more tedious to use unless there's an adapter interface mapping the generally accepted true == high relation. |
239: Add try_set_state method for OutputPin r=therealprof a=eldruin This is an implementation of #200 to gather some opinions and so we can either accept it or close the issue. This was earlier discussed at #44. I added a conversion from `bool` following the usual convention as well as an `ops::Not` implementation as suggested in #200, which seemed appropriate. I also added a default implementation for the `try_set_state` method. This bears the question whether a default implementation for `try_set_high()` / `try_set_low()` by using `try_set_state()` would be useful, so that potential implementors can choose to implement less methods. It should be noted that adding a default implementation for all 3 methods has the somewhat amusing property of generating an endless loop if none is overwritten. Closes #200 Co-authored-by: Diego Barrios Romero <eldruin@gmail.com>
44: Implement transactional I2C interface and add example r=ryankurte a=eldruin I implemented the transactional I2C interface from rust-embedded#223 and added an example with a driver which does a combined Write/Read transaction. This is based on previous work from rust-embedded#33 by @ryankurte, rust-embedded/rust-i2cdev#51 and is similar to rust-embedded#35. Co-authored-by: Diego Barrios Romero <eldruin@gmail.com>
set_state and toggle with default implementation