Replies: 5 comments 2 replies
-
Congratulations on using Some comments on your code. The def open_switch():
global button_presses
button_presses += 1 Secondly there is no need to repeatedly set the callback print(button_presses)
sw.open_func(open_switch) # Assign the callback outside the loop
while True:
await asyncio.sleep(0) # A sleep of 0 is an entirely valid way to let other tasks be scheduled
# Rest of code |
Beta Was this translation helpful? Give feedback.
-
@peterhinch I have another question to seek better understanding. In your previous comment you explained that there is no need set the callback inside the while loop in the main. Reading carefully I understand that:
|
Beta Was this translation helpful? Give feedback.
-
The above code fragment is fine. The way It is therefore important that any coroutine you write must periodically issue You might like to look at the relevant Switch code where you will see the periodic yield to the scheduler. There are various ways to time the press duration. One way is to use events. Create a coro like this async def time_switch(sw):
while True:
await sw.close.wait() # These await statements yield to the scheduler
ts = time.ticks_ms()
sw.close.clear()
await sw.open.wait()
tclosed = ticks_diff(ticks_ms(), ts)
sw.open.clear()
# Do something with tclosed You'll find the methods in the |
Beta Was this translation helpful? Give feedback.
-
Hi again, I am seeking once more for some help. I am now struggling with implementing a function that read an event and wait for the same event execution under a time window. If execution happens outside the time window, the event should not be registered in the main. More explicit: Since the default of the
In this above example I would like to implement a new function, where the counter only returns if the switch 2 presses twice within the time window (2,5 seconds and 5 seconds) otherwise counter should be the same. I was reading the part on Do you have any suggestion? Thanks in advance. |
Beta Was this translation helpful? Give feedback.
-
I'm not 100% sure I understand your requirement but here is my attempt: from primitives import Delay_ms
d25 = Delay_ms(duration=2500)
d5 = Delay_ms(duration=5000)
def open_switch2():
global button_presses2
if not d5.running(): # First press
d5.trigger()
d25.trigger()
else: # Second press
if not d25.running(): # In the time window
# Do whatever
d25.stop() # After 2nd press it may be wise to stop the timers in case another press occurs
d5.stop() |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
The below code is not working correctly since the push button after the interrupt still bounce in certain cases and increases the counter, e.g. under then the mechanical switch releases. (I will describe later the application)
I have tried several debouncing techniques but still get some odd results when testing the app. Some times the bouncing when releasing the pushbutton increases the value of the counter (means entering the definition on the irq handler).
Anyway, I decided to test the uasyncio script for the switch driver. This works like a charm.
However, I am not sure if my code is efficient or not. Here is a sample:
I am using an M5stamp-c3 based on an esp32-c3 microcontroller. The inbuilt functions are explained on the code.
While are different implemented functions (to save an JSON script to check some values in case of power off the device, or the change of LED color), my main purpouse is to make a stable counter.
Using the coros can be helpful in next step, that is to add a new switch and to time events between switches. In this case I am having issues adding a second switch and my timers are never updating (I think becuse the trigger event for the second switch is not getting activated.)
I would love some help and maybe giving me some help to achieve the code.
PD: I was to rushy when say that the code is working fine. The JSON is not being saving the values in the data dump. I need to check tomorrow (time reason). But will appreciate any help on it.
Beta Was this translation helpful? Give feedback.
All reactions