Mydin-advanced-usage

De MCHobby - Wiki
Sauter à la navigation Sauter à la recherche

Abstract

Before going into the details of the various controlers and backplanes, it may be opportune to learn some advanced behaviors of the MyDin asyncio implementation.

RUN_APP

RUN_APP pin is used to stop the MyDin software stack (or script execution after a reboot).

It is a convenient way to recover the control on the module when script goes wrong. It would also offer the full access to REPL.

When setting the RUN_APP pin to the "Stop" position (so tied to ground) while the MyDin software is already running, the code may takes up to 3 seconds to terminates.

It is also a good practice to check the RUN_APP pin in the boot.py startup file. This may allows debugging operations when Network operation goes wrong!

from machine import Pin
# Use the RUN_APP pin as defined on your controler board!
RUN_APP = Pin.board.GP3  # High=Run, Low=Stop

if Pin( RUN_APP, Pin.IN, Pin.PULL_UP ).value()==False:
  print( '[BOOT] skip execution (RUN_APP is false)' )
else:
  print( '[BOOT] entering...' )
  # >>> PLACE BOOT CODE HERE <<<
  print( '[BOOT] exit' )

Exception handling

An exception occurring in the userloop task will be captured and will ends the code execution.

Exception occurring in other tasks will be only be reported into REPL.

When the code execution ends due to an exception then the loop_exception contains a reference to the error.

When the code execution ends for other reason (eg: RUN_APP=Stop) then the loop_exception value is None.

The async-exception.py test script -visible here below- do raise an exception when the user press the second button (index=1).

from mydin import configure
from mydin.pico import Pico3Mod
from mydin.backplane.relays import TwoRelay3Mod 
import time, sys

class KaboomError( Exception ):
	pass

# Which Controler + Backplane to use
din = configure( Pico3Mod, TwoRelay3Mod )

async def loop( din ):
  for i in range( 4 ): # 0..3 = 4 buttons
    if din.was_pressed( i ):
      din.leds[i].toggle()
      # raise en exception is button 2 is pressed
      if i==1: 
        raise KaboomError( "This exception will exits the code")


din.setup( setup=None, loop=loop )
din.run()
print( "="*40 )
print( "din.run() did exit!!!")

if din.loop_exception != None:
  print( "An error %s occured!" % (din.loop_exception.__class__.__name__ ) )
  print( "with message: %s" % din.loop_exception )

Activating watchdog

Add Scheduler Task