| Ligne 29 : |
Ligne 29 : |
| | == Exception handling == | | == Exception handling == |
| | | | |
| − | '''An exception occuring in the ''userloop'' task''' will be captured and will ends the code execution. | + | '''An exception occurring in the ''userloop'' task''' will be captured and will ends the code execution. |
| | | | |
| − | '''Exception occuring in other tasks''' will be only be reported into REPL. | + | '''Exception occurring in other tasks''' will be only be reported into REPL. |
| | + | |
| | + | When the code execution ends due to an exception then the {{fname|loop_exception}} contains a reference to the error. |
| | + | |
| | + | When the code execution ends for other reason (eg: RUN_APP=Stop) then the {{fname|loop_exception}} value is None. |
| | + | |
| | + | The [https://github.com/mchobby/micropython-mydin/blob/main/examples/din_controlers/common/async-exception.py async-exception.py] test script -visible here below- do raise an exception when the user press the second button (index=1). |
| | + | |
| | + | |
| | + | <syntaxhighlight lang="python"> |
| | + | 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 ) |
| | + | </syntaxhighlight> |
| | | | |
| | == Activating watchdog == | | == Activating watchdog == |
| | | | |
| | == Add Scheduler Task == | | == Add Scheduler Task == |