Modifications

Sauter à la navigation Sauter à la recherche
2 374 octets ajoutés ,  29 avril 2025 à 21:27
Ligne 184 : Ligne 184 :  
'''Now, glue the snipset into the final code:'''
 
'''Now, glue the snipset into the final code:'''
   −
The content here below can be found in
+
The content here below can be found in the [https://github.com/mchobby/micropython-mydin/blob/main/examples/din_controlers/common/async-scheduler.py async-scheduler.py] example:
 +
 
 +
<syntaxhighlight lang="python">
 +
from mydin import configure
 +
from mydin.pico import Pico3Mod
 +
from mydin.backplane.relays import TwoRelay3Mod
 +
import time, sys, asyncio
 +
 
 +
import schedule
 +
 
 +
din = configure( Pico3Mod, TwoRelay3Mod )
 +
 
 +
# === Define & schedule Jobs ==================
 +
async def job_relay2():
 +
global din
 +
din.rel2.on()
 +
din.leds[1].on()
 +
await asyncio.sleep(30) # Wait 30 Sec
 +
din.leds[1].off()
 +
 
 +
 
 +
async def job_toggle_led( led_idx ): # Job with parameter
 +
global din
 +
din.leds[led_idx].toggle()
 +
 
 +
schedule.every(1).minutes.do( job_relay2 )
 +
schedule.every(3).seconds.do( job_toggle_led, 2 ) # Toggle led 2 of 0..3
 +
schedule.every(7).seconds.do( job_toggle_led, 3 ) # Toggle led 3 of 0..3
 +
# --- Other examples of scheduling ---
 +
# schedule.every(10).seconds.do(job_with_argument, name="MicroPython")
 +
# for h in range( 8, 17 ):
 +
# schedule.every().day.at("%02i:00" % h).do( upd_auth_file_job, drawer )
 +
# schedule.every().day.at("20:00").do( upl_log_file_job, drawer )
 +
 
 +
# === Main DIN tasks ===================
 +
counter = 0
 +
async def loop( din ):
 +
""" called again and again (like Arduino) """
 +
global counter
 +
counter += 1
 +
# Place your loop code here
 +
print( "loop iteration %3i " % (counter ) )
 +
await asyncio.sleep( 5 )
 +
 
 +
 
 +
# === Running Din Project ==============
 +
def create_tasks( async_evloop ):
 +
scheduler_task = async_evloop.create_task( schedule.run_forever() )
 +
 
 +
din.setup( setup=None, loop=loop, on_tasks_create=create_tasks )
 +
din.run()
 +
</syntaxhighlight>
 +
 
 +
As the tasks and jobs displays {{fname|print()}} statements, we can view the activity in the REPL session:
 +
 
 +
<nowiki>mcu localtime: 1/1/2000 2:22:27
 +
Pico3Mod.run() entering...
 +
USER Loop Setup...
 +
USER Tasks Setup...
 +
running event_loop...
 +
loop iteration  1
 +
DEBUG: Running job <Job object at 20023b40>
 +
DEBUG: Jobs to await: 1
 +
loop iteration  2
 +
DEBUG: Running job <Job object at 20023b40>
 +
DEBUG: Jobs to await: 1
 +
DEBUG: Running job <Job object at 20026530>
 +
DEBUG: Jobs to await: 1
 +
DEBUG: Running job <Job object at 20023b40>
 +
DEBUG: Jobs to await: 1
 +
loop iteration  3
 +
DEBUG: Running job <Job object at 20023b40>
 +
DEBUG: Jobs to await: 1
 +
DEBUG: Running job <Job object at 20026530>
 +
DEBUG: Jobs to await: 1
 +
loop iteration  4
 +
[EXIT] run app exit!
 +
event_loop ends!</nowiki>
32 908

modifications

Menu de navigation