fossasia / pslab-python

Python Library for PSLab Desktop: https://pslab.io
GNU General Public License v3.0
1.62k stars 227 forks source link

External device meta data #200

Open bessman opened 3 years ago

bessman commented 3 years ago

Devices in pslab.external, such as sensors, motors, and displays, should include some kind of meta data so that front ends like pslab-desktop and pslab-cli can use them without large amounts of custom code.

One possible solution is a JSON file for every supported device, with a structure like this:

{
"name": "MLX90614",
"modes":
    {
    "thermometer":
        {
        "input": 
            {
            "name": "source",
            "type": "selection",
            "options": ["object", "ambient"],
            },
        "output": 
            {
            "name": "temperature",
            "type": "float", 
            "range": [-127, 128],
            "unit": "degC",
            },
        },
    },
}

This file specifies that the MLX90614 class has:

A more complex example:

{
"name": "BMP180",
"modes":
    {
    "thermometer":
        {
        "output": 
            {
            "name": "temperature",
            "type": "float", 
            "range": [-127, 128],
            "unit": "degC",
            },
        "default": 1,
        },
    "pressure meter":
        {
        "output": 
            {
            "name": "pressure",
            "type": "float", 
            "range": [0, 1e7],
            "unit": "Pa",
            },
        "default": 0,
        },
    "altitude meter":
        {
        "input":
            {
            "name": "baseline",
            "type": "float",
            "range": [0, 1e7],
            "unit": "Pa",
            },
        "output": 
            {
            "name": "altitude",
            "type": "float", 
            "range": [-1e2, 1e4],
            "unit": "m",
            },
        "default": 0,
        },
    },
}

This file specifies that the BMP180 class has:

By reading these files, the front end does not need to know any details about the sensors and requires no custom code. @orangecms what do you think?

nkpro2000sr commented 3 years ago

Also Adafruit's drivers which uses CircuitPython-busio can be included with external sensors, since pslab-python has busio now.

Example:

# pslab/external/busio/si7021.py
import adafruit_si7021
from pslab.bus import busio

class SI7021(adafruit_si7021.SI7021):
    _ADDRESS = 0x40

    def __init__(self, **args):
        self._ADDRESS = args.get('address', self._ADDRESS)
        self._i2c = args.get('i2c_buss', busio.I2C())
        super().__init__(self._i2c, self._ADDRESS)
{
"name": "SI7021",
"modes":
    {
    "thermometer":
        {
        "output": 
            {
            "name": "temperature",
            "type": "float", 
            "range": [-10, 85],
            "unit": "degC",
            },
        "default": 1,
        },
    "moisture meter":
        {
        "output": 
            {
            "name": "relative_humidity",
            "type": "int", 
            "range": [0, 80],
            "unit": "% RH",
            },
        "default": 0,
        },
    },
"dependencies" :
    [
        "adafruit-circuitpython-si7021",
    ],
}

We also need "dependencies" to check/install/manage dependencies for these sensors.