Ok, made some progress with multithread
in the main I have this code
Code: Select all
from machine import ADC
import utime
import _thread
import appselect_module
mode = 0
def readVoltage():
analogValue = ADC(26)
return analogValue.read_u16() / 65536 * 3.3
def main():
global mode
while True:
if mode == 1:
print("MODE 1")
utime.sleep(2)
if mode == 2:
print("MODE 2")
utime.sleep(2)
if mode == 0:
print("MODE 0")
utime.sleep(1)
appselect_module.appSelect(mode)
def mode():
global mode
while True:
reading = readVoltage()
print(reading)
if reading > 1 and reading < 3:
"""
MODE 1: App run
"""
mode = 1
utime.sleep(1)
elif reading < 1:
"""
MODE 2: Riserva
"""
mode = 2
utime.sleep(1)
else:
"""
MODE 0: App selection
"""
mode = 0
utime.sleep(1)
_thread.start_new_thread(mode, ())
while True:
main()
while in the appselect_module.py I have
Code: Select all
import machine
import time
import _thread
from primitives.pushbutton import Pushbutton
import uasyncio as asyncio
import hal
import functions
prev_app = 1
next_app = 0
total_apps = 6
functions.allLedOff()
machine.Pin(hal.leds[prev_app]).low()
def appSelect():
while True:
def app_select():
global prev_app
global next_app
global total_apps
machine.Pin(hal.leds[prev_app]).low() #program precedente
if prev_app == 0:
machine.Pin(hal.leds[total_apps]).high()
machine.Pin(hal.leds[0]).high()
else:
machine.Pin(hal.leds[prev_app]).high()
next_app = prev_app + 1
machine.Pin(hal.leds[next_app]).low()
if next_app >= total_apps:
prev_app = 0
else:
prev_app = next_app
def app_confirm():
print("confirm")
def button():
pb = Pushbutton(hal.button)
pb.press_func(app_select, ()) # Callback on button press
pb.long_func(app_confirm, ()) # Callback on long press
while True:
await asyncio.sleep(1)
try:
asyncio.run(button())
finally:
asyncio.new_event_loop()
Now, partially work
I correctly run the app_slection function
but then, when I change my switch, I need to go to MODE 1
instead I'm stuck in the app_selection (even though I see that the change of state has been read correctly)