Hi,
I am attempting to learn about asyncio with a rotary encoder project. I am not a born programmer so although I have read much of the theory on asyncio, much of it still looks like black magic to me.
I have managed to cobble together, from shameless pilfering of examples in net based tutorials, two small routines, one that accomplishes the reading of the rotary encoder via the appropriate dt_overlay and evdev and a second which reads the associated push button switch via asyncio and RPi.GPIO.
What seems to be eluding me at this point is how to fold these two routines into one cohesive bit of code, where both the value of the encoder position and the state of the switch are known in variables that exist with current values, at the same time and ready to be passed to other code. All of my attempts at this to date have fallen short on at least one of these requirements.
Thoughts & suggestions on how best to accomplish this, would be welcome.
Read the encoder value via evdev:
Read the push button:
Thanks,
Brian H.
I am attempting to learn about asyncio with a rotary encoder project. I am not a born programmer so although I have read much of the theory on asyncio, much of it still looks like black magic to me.
I have managed to cobble together, from shameless pilfering of examples in net based tutorials, two small routines, one that accomplishes the reading of the rotary encoder via the appropriate dt_overlay and evdev and a second which reads the associated push button switch via asyncio and RPi.GPIO.
What seems to be eluding me at this point is how to fold these two routines into one cohesive bit of code, where both the value of the encoder position and the state of the switch are known in variables that exist with current values, at the same time and ready to be passed to other code. All of my attempts at this to date have fallen short on at least one of these requirements.
Thoughts & suggestions on how best to accomplish this, would be welcome.
Read the encoder value via evdev:
Code:
#!/usr/bin/env python3import asyncioimport osimport RPi.GPIO as GPIOfrom evdev import InputDevicedev = InputDevice('/dev/input/event4')# ****************************************************BUTTON_GPIO = 16async def main(dev): async for ev in dev.async_read_loop(): cnt=(repr(ev)) if ", 3, 0, " in cnt: prn=1 else: prn=0 cnt=(cnt[(len(cnt)-4):(len(cnt)-1)]) cnt=cnt.strip(", ") C=int(cnt) if prn==1: print(C)asyncio.run(main(dev))#https://roboticsbackend.com/raspberry-pi-gpio-interrupts-tutorial/#https://python-evdev.readthedocs.io/en/latest/tutorial.html
Code:
import asyncioimport osimport RPi.GPIO as GPIOfrom evdev import InputDevice# ****************************************************BUTTON_GPIO = 16dev = InputDevice('/dev/input/event4')def button_pushed(_): print("Button Pressed!")if __name__ == '__main__': GPIO.setmode(GPIO.BCM) GPIO.setup(BUTTON_GPIO, GPIO.IN, pull_up_down=GPIO.PUD_UP) GPIO.add_event_detect(BUTTON_GPIO, GPIO.FALLING, callback=button_pushed, bouncetime=1000) print('Press Ctrl+{0} to exit'.format('Break' if os.name == 'nt' else 'C')) try: loop = asyncio.get_event_loop() loop.run_forever() except (KeyboardInterrupt, SystemExit): loop.close() GPIO.cleanup()#https://stackoverflow.com/questions/76357252/how-can-i-fix-my-asyncio-python-3-program-to-read-a-button-push-on-gpio26
Brian H.
Statistics: Posted by hambonius — Sun Jan 26, 2025 5:39 pm