| Ligne 72 : |
Ligne 72 : |
| | == Using setup() == | | == Using setup() == |
| | | | |
| − | The "setup" parameter in the {{fname|1=DinControler.setup( self, setup=None, loop=None, on_tasks_create=None )}} | + | The {{fname|setup}} parameter in the {{fname|1=DinControler.setup( self, setup=None, loop=None, on_tasks_create=None )}} is used to define and initialise additional hardware/sensor/bus connected to myDin. |
| | + | |
| | + | The parameter must be the reference to a regular function accepting a "din" object as parameter. |
| | + | |
| | + | '''Good practice:''' instead of defining global variable, it is recommended to add a new attribute on the "din" object. By doing so, the ''userloop''' can easily access the new object. |
| | + | |
| | + | The following [https://github.com/mchobby/micropython-mydin/blob/main/examples/din_controlers/pico3mod/async-qwiic-bme280.py async-qwiic-bme280.py example] show how to create such reference and how to retreive it in the ''userloop''. The sensor is a [https://shop.mchobby.be/fr/cartes-breakout/684-bme280-capteur-temperaturehumiditepression-i2cspi-stemmaqtqwiic-3232100006843-adafruit.html BME280 sensor wired] on the I2C bus. |
| | + | |
| | + | <syntaxhighlight lang="python"> |
| | + | from mydin import configure |
| | + | from mydin.pico import Pico3Mod |
| | + | from mydin.backplane.relays import TwoRelay3Mod |
| | + | import time, sys |
| | + | |
| | + | 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 ): |
| | + | # temperature (celcius), atmospheric_pressure (hPascal), Relative_humidity (percent) |
| | + | temp, hpa, rh = din.sensor.raw_values |
| | + | print( "temperature",temp, "pressure", hpa, "humidity", rh ) |
| | + | |
| | + | din.setup( setup=setup, loop=loop ) |
| | + | din.run() |
| | + | </syntaxhighlight> |
| | + | |
| | + | Notice the definition of the {{fname|def setup():}} function and its referencing in {{fname|din.setup( setup='''setup''', loop=loop )}} call. |
| | | | |
| | == Activating watchdog == | | == Activating watchdog == |
| | | | |
| | == Add Scheduler Task == | | == Add Scheduler Task == |