import sys
from rotary_irq_rp2 import RotaryIRQ
import uasyncio as asyncio
from machine import I2C, Pin
from pico_i2c_lcd import I2cLcd
i2c = I2C(1, sda=Pin(14), scl=Pin(15), freq=100000)
I2C_ADDR = i2c.scan()[0]
lcd = I2cLcd(i2c, I2C_ADDR, 2, 16)
while True:
print(I2C_ADDR)
lcd.putstr("I2CAddress:"+str(I2C_ADDR)+"\n")
lcd.putstr("AUTO MOTO BY GOVIN")
class Application1():
def __init__(self, r1):
self.r1 = r1
self.myevent = asyncio.Event()
asyncio.create_task(self.action())
r1.add_listener(self.callback)
def callback(self):
self.myevent.set()
async def action(self):
while True:
await self.myevent.wait()
print('App 1: rotary 1 = {}'. format(self.r1.value()))
# do something with the encoder result ...
self.myevent.clear()
async def main():
rotary_encoder_1 = RotaryIRQ(pin_num_clk=16,
pin_num_dt=17,
min_val=0,
max_val=60,
reverse=True,
range_mode=RotaryIRQ.RANGE_WRAP)
# create tasks that use the rotary encoders
app1 = Application1(rotary_encoder_1)
# keep the event loop active
while True:
await asyncio.sleep_ms(10)
try:
asyncio.run(main())
except (KeyboardInterrupt, Exception) as e:
print('Exception {} {}\n'.format(type(e).__name__, e))
finally:
ret = asyncio.new_event_loop() # Clear retained uasyncio state