-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
DigitalInOut: support for inverting output #4230
Comments
I like the idea, though it may be good alongside a guide (or reference) on buttons and why they are "False" when pressed and how this can invert it. At least as a newbie that got me for a while, maybe I missed something that exists too. But I think it would make code flow a lot better to set some invert type property and the rest of the code can say In terms of CP would potentially inverting |
It would be helpful to have an easier way to indicate that a button is pressed is button = DigitalInOut(board.GP0)
button.direction = Direction.INPUT
button.pull = Pull.UP
# this is counterintuitive for a newbie to electronics
if button.value:
print('button is NOT pressed')
if not button.value:
print('button IS pressed') Using a new property would have the benefit of being more intuitive when using DigitalInOut for buttons, while also requiring less code changes if someone decides to invert their wiring on a project. button = DigitalInOut(board.GP0)
button.direction = Direction.INPUT
button.pull = Pull.UP
if button.on:
print('button IS pressed')
if not button.on:
print('button is NOT pressed') |
I see no reason why you couldn't write a |
You could even have a |
I agree with both of these points, but was offering support that the solution to the original issue could have a benefit with using buttons as well. |
I am a bit concerned about adding it to Digitalio -- at some point, we do want people to understand the hardware. Messing with .value in Digitalio really hides things.... I agree with @deshipu that this should be implemented at a higher level like simpleio. |
I feel like this is related to the discussion we had In The Weeds recently about whether to have a ".voltage" property of AnalogIn objects, or have code need to scale ".value * .reference_voltage / 65535". I think we concluded that while the decision to have a 0..65535 value was also made very early when it was unclear whether CircuitPython would support floating-point numbers at all, it was better to leave the core as-is and put added functionality in Python code. That said, because of some hardware & software I used to use, I miss the "invert" property in digitalio. And it's worth mentioning that at least the RP2040 has a hardware invert. This is used, for instance, to implement the two different clock polarity options of a PIO implementation of SPI (separate programs are needed for the two clock phase options, but in this way only 2 programs are needed rather than 4). But adding an invert to rp2pio is different than adding it to digitalio. |
I've kind of wanted a That kind of abstraction is already done in board-specific libraries like |
We could add A simple The naming convention be a cue to set Another idea would be to add a |
I just want to note that after the whole discussion several years ago about adding a |
I don't really like the idea of changing DigitalInOut. My gut aligns with @jerryneedell's point to do it in Python. However, having native support would be very nice so that What if we added two new native classes |
@ZodiusInfuser asked in #4228 whether
DigitalInOut
has functionality to invert the sense of.value
, so thatTrue
represents a low signal level , andFalse
represents high. The motivation is that Pimoroni has boards where the RGB LEDs are common anode. But this functionality could also be used for buttons that ground when on, etc.MicroPython has a
Signal
class that wrapsPin
and has an optionalinvert
argument:classmachine.Signal(pin_obj, invert=False)
.DigitalInOut
could take an optionalinvert
argument, which would invert the sense of.value
. That might be all that is necessary.We could also leave
.value
alone, and add, say, an.on
property that depends oninvert
. Or we could add a.level
property that always corresponds to high and low, and let.value
be influenced byinvert
. But these extra properties may not be necessary.One still needs to know whether a pin is active-high or active-low. Right now the board-specific pin names do not indicate that. We could start adding
_INV
or some other indicator to their names to make that clearer.The text was updated successfully, but these errors were encountered: