Tristate Buttton. Which primitive must I use? (Switch, Pushbutton...) #11674
Replies: 4 comments 10 replies
-
Based on pushbutton.py You can implement usually there should be additional signal form open/close indicator to implement complete state machine, without that for init state (STOP) we do not know in what direction: OPEN or CLOSE we should go, then we have to guess (not to safe). CNT,direction = 1, 1
async def click_on( ms):
global CNT,direction
if CNT ==2:
direction=-1
elif CNT ==0:
direction=1
CNT+=direction
print(CNT)
await asyncio.sleep_ms(ms) edit: I guest you want control 3 state via 2 DIO pins ( for example : 1 0, 0 0, 0 1 ) then instead of "counter" you can directly implement state machine based on a Boole’a sequence machine and manipulate DIO ( still additional open/close indicator should be added) |
Beta Was this translation helpful? Give feedback.
-
Or use an event based approach: import uasyncio as asyncio
from primitives import Switch
from machine import Pin
async def foo(evt):
while True:
for n in range(3):
evt.clear() # re-enable the event
await evt.wait() # minimal resources used while paused
print("Press no.", n + 1)
async def main():
sw = Switch(Pin(1, Pin.IN, Pin.PULL_UP))
sw.close_func(None) # Use event based interface
await foo(sw.close) # Pass the bound event to foo
asyncio.run(main()) |
Beta Was this translation helpful? Give feedback.
-
Re import uasyncio as asyncio
from primitives import ESwitch
from machine import Pin
async def foo(evt):
while True:
for n in range(3):
evt.clear() # re-enable the event
await evt.wait() # minimal resources used while paused
print("Press no.", n + 1)
async def main():
sw = ESwitch(Pin(1, Pin.IN, Pin.PULL_UP))
await foo(sw.close) # Pass the bound event to foo
asyncio.run(main()) The only advantage of |
Beta Was this translation helpful? Give feedback.
-
@2dof A couple of minor points. The methods |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello everyone.
I'm using "Switch" primitive class with polling in my code and it works great to implement opening/closint contacts like relays, but now I want to implement a "tristate" user button. Pushing the same button it have to implement a sequence like this: OPEN-STOP-CLOSE-STOP-OPEN...
But trying to modify my code rising de "debounce_ms" even to 1000, or putting "await time" inside the "close_func"... It doesn't work neither, because the speed of the coros calls are brutal amd my sequence is crazy...
There are an apropiate primitive class to do it?
Thank you very much
Best regards.
Beta Was this translation helpful? Give feedback.
All reactions