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

Use switch_hal rather than manually working with active-low #4

Open
chrysn opened this issue Jan 31, 2022 · 0 comments
Open

Use switch_hal rather than manually working with active-low #4

chrysn opened this issue Jan 31, 2022 · 0 comments

Comments

@chrysn
Copy link
Contributor

chrysn commented Jan 31, 2022

Issue status: Mostly a note-to-self; if you like the proposed resolution I can put it into a PR at some later point.

The way the active-low and active-high semantics are packed into the blinq constructor is convenient for typical cases, but falls short when a board already provides the active-high/active-low abstraction through switch-hal. (In particular, for RIOT I currently provide fake pins with fake active-high logic, but I'd rather deprecate that in favor of exposing things at the right semantic level).

Concrete suggestion for blinq:

  • Add switch_hal as a dependency
  • Implement blinq in terms of switches, moving the constructor to build a switch from the arguments.
    • This is a bit harder than it seems, because in a switch the level logic is in type state whereas here it's in runtime state. (This could need an own implementation of OutputSwitch that uses runtime polarity, but that's just a few lines).
    • This would be a breaking change as Blinq's G type changes (from pin to switch) -- one of these "yes it's a breaking release, but unless you fixed that type somewhere you're good to upgrade" changes.
  • Provide a new_from_switch constructor that takes an OutputSwitch (with its on/off semantics), rather than an OutputPin (with its high-low semantics) and a boolean

Alternative: HALs keep providing switches with extra GPIO versions, "It's not really a GPIO, but if some API needs it, use it as OutputPin where it simulates active-high logic". (Which may well be viable).

bors bot added a commit to rust-embedded/awesome-embedded-rust that referenced this issue Feb 7, 2023
400: Add switch-hal r=eldruin a=chrysn

The switch-hal crate just extends embedded-hal by a bit (hah!) -- but I find it very useful, for it provides clean "on"/"off" semantics for what is otherwise merely "high"/"low", and thus dependent on the actual wiring (both LEDs and buttons are just as easy to wire either way).

It's not used a lot ([crates assume active-low](jamesmunns/blinq#4) instead), but I hope that this changes once it is more visible through here.

Co-authored-by: chrysn <chrysn@fsfe.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant