I want to write a separate function for each submenu.
When I press the button, I need to start the other task with the callback function.
however, the previous task must be stopped.
Here my codes
I get errors about cancel.
What should I do.
Code: Select all
MicroPython v1.18 on 2022-01-17; ESP32 module with ESP32
Code: Select all
>>> uasyncio.__version__
(3, 0, 0)
Code: Select all
import pcd8544_fb
from machine import Pin, SPI, Timer
from rotary_irq_esp import RotaryIRQ
import time
import uasyncio
from machine import Timer
a1 = None
m1 = None
m2 = None
t1 = None
r = RotaryIRQ(pin_num_clk=34,
pin_num_dt=35,
min_val=0,
max_val=3,
reverse=False,
range_mode=RotaryIRQ.RANGE_WRAP)
spi = SPI(1)
spi.init(baudrate=2000000, polarity=0, phase=0)
cs = Pin(15)
dc = Pin(4)
rst = Pin(27)
lcd = pcd8544_fb.PCD8544_FB(spi, cs, dc, rst)
sw = Pin(39,Pin.IN)
async def menu1_as(r):
print("Menu1 ok")
sw.irq(handler=None)
sw.irq(trigger=Pin.IRQ_RISING, handler=menu2)
while True:
if r.value()==0:
lcd.fill(0)
lcd.fill_rect(0,0,84,7,1)
lcd.text(" MENU1 ", 0, 0, 0)
lcd.show()
await uasyncio.sleep_ms(50)
async def menu2_as(r):
print("Menu2 ok")
sw.irq(handler=None)
sw.irq(trigger=Pin.IRQ_RISING, handler=menu1)
while True:
if r.value()==0:
lcd.fill(0)
lcd.fill_rect(0,0,84,7,1)
lcd.text(" MENU2 ", 0, 0, 0)
lcd.show()
await uasyncio.sleep_ms(50)
async def ana_sayfa():
sw.irq(trigger=Pin.IRQ_RISING, handler=menu1)
while True:
lcd.fill(0)
lcd.fill_rect(0,0,84,7,1)
lcd.text(" ANA SAYFA ", 0, 0, 0)
lcd.show()
await uasyncio.sleep_ms(50)
async def rotar(r):
global val_old
val_old = r.value()
while True:
global val_new
val_new = r.value()
if val_old != val_new:
val_old = val_new
print('result =', val_new)
val_old = val_new
await uasyncio.sleep_ms(10)
async def main():
global a1
global t1
t1 = uasyncio.create_task(rotar(r))
a1 = uasyncio.create_task(ana_sayfa())
await uasyncio.gather(t1,a1)
def menu1(p):
global m1
print("menu1")
m1 = uasyncio.create_task(menu1_as(r))
print("olustur1")
#uasyncio.run(m1)
print("calistir1")
a1.close()
print("m1 kapat")
def menu2(p):
global m2
print("menu2")
m2 = uasyncio.create_task(menu2_as(r))
print("olustur2")
#uasyncio.run(m2)
print("calistir2")
m1.close()
print("m2 kapat")
uasyncio.run(main())
Code: Select all
Menu1 ok
menu2
olustur2
calistir2
Traceback (most recent call last):
File "main.py", line 98, in menu2
AttributeError: 'Task' object has no attribute 'close'
Menu2 ok
menu1
olustur1
calistir1
Traceback (most recent call last):
File "main.py", line 88, in menu1
AttributeError: 'Task' object has no attribute 'close'