Différences entre versions de « Mydin-async-or-not »

De MCHobby - Wiki
Sauter à la navigation Sauter à la recherche
Ligne 73 : Ligne 73 :
  
 
<syntaxhighlight lang="python">
 
<syntaxhighlight lang="python">
 +
from mydin import configure
 +
from mydin.pico import Pico3Mod
 +
from mydin.backplane.relays import TwoRelay3Mod
 +
 +
# Which Controler + Backplane to use
 +
din = configure( Pico3Mod, TwoRelay3Mod )
 +
 
async def loop( din ):
 
async def loop( din ):
 
     """ called again and again (like Arduino) """
 
     """ called again and again (like Arduino) """

Version du 22 avril 2025 à 08:35

Introduction

MyDin can be programmed:

  1. with standard Python code (classes, functions, types, etc)... name it "the procedural way".
  2. with asynchronous code (known as AsyncIO)... a kind of multitasking!

The libraries provided with myDin support the both approaches for programming myDin.

In any cases, the myDin library will simplifies the access to the underlaying hardware.

For sure, experimented MicroPython user can also use the schematics and creates their own software stack from the top to the bottom.

By creating and configuring the myDin classes (see later), your code already get a direct access to all the features in the procedural way.

When calling the additional setup() and run() methods the myDin will starts asynchronous processing.

We strongly encourage to use the asynchronous approach for your projects.

Creating myDin (async way)

Step 1: identification

To properly instantiate the DIN controller, you must identifies:

  1. controller board
  2. back-plane board

These information will determine the Python modules to load and the classes names to be used.

The picture shown below combines a

  1. Pico based controller (3 modules DIN)
  2. two relay board (3 modules DIN)

MyDin-3modules.jpg

The "Pico based controller (3 modules DIN)" is known under the product code DINCASE-MB3PICO (Middle Board 3 modules with Pico).

  • The module to load is mydin.pico
  • The class to instantiate is Pico3Mod

The "two relay board (3 modules DIN)" is known under the product code DINCASE-2R-BP3MOD (2 Relays BackPlane 3 Modules).

  • The module to load is mydin.backplane.relays
  • The class to instantiate is TwoRelay3Mod

The information can be retrieved from the Modules and classes reference grid

Step 2: create instances

from mydin import configure
from mydin.pico import Pico3Mod
from mydin.backplane.relays import TwoRelay3Mod 

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

Creating the din object will show initialization information onto the REPL session.

In this case, this shows the local time as reloaded from the real time clock.

mcu localtime: 1/1/2000 0:1:44

from now, the din instance allows the user script to access all the behavior of controler and the backplane.

Up to this point, the code is still running the procedural way.

This is where to "procedural" user code could be inserted.

Step 3: going asynchronous

If you want to develop asynchronous code for myDin then following declaration must be added.

from mydin import configure
from mydin.pico import Pico3Mod
from mydin.backplane.relays import TwoRelay3Mod 

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

async def loop( din ):
    """ called again and again (like Arduino) """
    # Asynchronous user script here 

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


The setup() call is used to specify a setup method (optional) and a loop method to be used by the AsyncIO implementation.

The optional setup parameter is used to specify some initialization routine. setup is quite useful to declare sensors and components in your project.

Once din.run() is called:

  • the def setup( din ) is called once (if any declared)
  • the async def loop( din ) is continuously executed!

The loop execution can even be monitored with a watchdog.

The din parameter passed to setup and loop methods provide a full access to all the behaviors and features of the DIN case.

In normal condition, the din.run() never exits. However, exit can happens:

  • When an exception is raised in loop
  • RUN_APP switch placed on STOP position

Step 4: setup parameter

When calling the setup() method, the setup parameter is used to reference a configuration routine. That routine is used to configure additional hardware (or sensors).

The example here below use a setup() routine that creates a BMP280 athmospheric pressure sensor then attach it to the din instance as sensor attribute.

Then, the loop() routine can access the sensor attribute to query the data.

from mydin import configure
from mydin.pico import Pico3Mod
from mydin.backplane.relays import TwoRelay3Mod 

from bme280 import *

din = configure( Pico3Mod, TwoRelay3Mod )

def setup( din ):
    # Add a new attribute to "din" instance
    din.sensor = BME280( i2c=din.i2c, address=BMP280_I2CADDR )

async def loop( din ):
    # accessing sensor
    temp, hpa, rh = din.sensor.raw_values
    # ... user code here ...
    print( "temperature %s" % temp )

din.setup( setup=setup, loop=loop )
din.run()

Step 5 : Read the doc

It is now time to read the programming reference about controler and backplane boards.

Creating myDin (NO Async!)

Modèle:MyDin-TRAILER