-
Notifications
You must be signed in to change notification settings - Fork 928
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
machine/pin: add pin.ConfigureAsInput() and pin.ConfigureAsOutput() helper functions #1537
Conversation
Any opinions on this PR @aykevl @conejoninja @sago35 anyone? 😸 |
I like the idea, it's not only shorter (not much) but easier/simpler. Could the code be placed inside machine.go only once? It smells a little that it needs to be copied/pasted in every machine_xxx.go file, we'll need to make sure new devices add those functions too. To make it even simpler, what about : led.ConfigureAsOutput()
led.ConfigureAsInput() |
What about pull up and down? It seems to me that those are rather important and also supported on most chips (I believe AVR is an exception and it only supports pull up, not pull down). |
Also, what problem are you trying to solve exactly? Adding |
This change is not intended to remove dependency on In fact, I much prefer @conejoninja suggestion, and I think I will change this PR to incorporate it. Regarding pull-up vs. pull-down, the proposed change to |
Ah okay, thank you for the explanation. I'm not entirely convinced that adding more API surface will simplify things, to be honest. However, the current API is a bit more complicated than necessary so maybe we can simplify something? This might break existing code depending on what we do. For example: pin.Configure(machine.PinOutput)
pin.Configure(machine.PinInput)
pin.Configure(machine.PinInputPullup) And if we later need more options (which so far don't seem to be necessary), they can be implemented with extra methods, such as: pin.SetDriveStrength(10) // 10mA drive strength |
Although, maybe the suggestion from @conejoninja is better as it also allows using the pins in an interface. For example, a hypothetical button driver could use this interface: type Pin interface {
ConfigureAsInputPullup()
Get() bool
} It still looks a bit ugly to me to have four methods for essentially one thing ( |
That right there does achieve both the simplification I am seeking, and also gives us an interface for testing. So do we want to go with this: type Pinner interface {
ConfigureAsInputPullup() error
ConfigureAsInputPulldown() error
ConfigureAsOutput() error
Get() bool
Set(bool) error
} or this: type Pinner interface {
ConfigureAsInput() error
ConfigureAsOutput() error
Get() bool
Set(bool) error
} Where What do you think? |
I'm more inclined to the simplest |
Seems like pullup is more common then pulldown. We could default to that for |
What do you mean, default? Regarding the interface: I would avoid making one shared interface type. Instead, I think every driver should define the subset that the driver needs as this can vary greatly. One driver might only need |
91f9006
to
6a4c67e
Compare
The interface will be defined in the consumer aka the drivers repo, not here. I just pushed a commit to this branch with the |
6a4c67e
to
833d7aa
Compare
…elper functions to simplify using defaults
833d7aa
to
23d911a
Compare
OK I have rebased and squashed commits. I think this is now ready to go, unless there is additional feedback. |
@aykevl any other feedback on this PR? |
Closing, since we decided not to do this. |
This PR adds the PinConfigInput() and PinConfigOut() helper functions to avoid so much repetition when just using the default values for config:
Before:
After:
You can still use the longer form, this is just a shortened form since the defaults are so often used.