Mydin-async-or-not

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

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.

myDin : Asynchronous

Ready to start cooperative multi-tasking with MyDin ?

You are at the right place!

The following steps details the information to benefit from AsyncIO implementation of MyDin.

Despite the numerous details presented here below, user script only need to implement a async def loop( din ) function.

More advanced user script will also implements the def setup( din ) function.

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.

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.

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 : asynchronous tasks

The graphic here below shows what's happening behind the scene when calling run() method.

A fine understanding is not necessary for working with the a asynchronous stack. However, having a rough idea of what's happening may help a lot.

Mydin-DinControler-run.jpg

Notice:

  • the various calls of setup, loop parameter are visible.
  • the newer on_create_task parameter allowing the user code to add its own tasks.
  • the userloop task taking care of loop execution.

As the Pico3Mod class derives from DinControler class, the task_setup() method is override to inject the Pico3Mod "Monitoring" and "ds18b20" specifics tasks.

The simplified picture here below shows the Pico3Mod class, the specialized DinControler class for the DINCASE-MB3PICO product (Middle Board 3 modules with Pico).

Mydin-Pico3Mod.class.jpg

Step 6 : Read the doc

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

myDin : procedural

It is also possible to develop solution with MyDin in a procedural way.

Despite the fact that user script lost all the benefits of cooperative multitasking, the myDin library also allow procedural development.

Steps 1 & 2

Those steps are identical to the "Asynchronous" approach.

Once those steps are done, the din object is available and offers access to all the behaviors of both boards.

Step 3

For a procedural implementation, the user script is encapsulated within an infinite while loop.

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

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

while True:
    # ... place user code is this section.
    # 
    # ... user code can freely access the "din" object.

Step 4

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

Modèle:MyDin-TRAILER