Skip to content

Commit

Permalink
Merge branch 'add-constants-for-i2c-freq-and-voltage' into 'master'
Browse files Browse the repository at this point in the history
Add constants for I2C frequency and voltage

See merge request MSO-SW/drivers/python-i2c-sfm-sensorbridge!10
  • Loading branch information
psachs committed Nov 3, 2021
2 parents b31df79 + b15b65d commit 669b229
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 165 deletions.
18 changes: 12 additions & 6 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
@@ -1,29 +1,36 @@
include: https://gitlab/Sensirion/Python/ci-config-python/raw/master/gitlab-ci-template-v2.yml
include: https://gitlab.sensirion.lokal/Sensirion/Python/ci-config-python/raw/master/gitlab-ci-template-v2.yml

check_rst_syntax:
extends: .check_rst_syntax_v2

check_editorconfig:
extends: .check_editorconfig_v2

sdist:
extends: .py3p8_linux_sdist_v2

py2p7_linux_build:
extends: .py2p7_linux_build_v2

py3p5_linux_build:
extends: .py3p5_linux_build_v2
py3p6_linux_build:
extends: .py3p6_linux_build_v2

py3p8_linux_build:
extends: .py3p8_linux_build_v2

py2p7_32bit_win_build:
extends: .py2p7_32bit_win_docker_build_v2

py3p5_64bit_win_build:
extends: .py3p5_64bit_win_docker_build_v2

py3p8_64bit_win_build:
extends: .py3p8_64bit_win_docker_build_v2

build_docs:
stage: build
tags: [linux, docker]
image: registry.gitlab.sensirion.lokal/sensirion/docker/docker-python:3.6-18.04-1.1.0
image: registry.gitlab.sensirion.lokal/sensirion/docker/docker-python:3.8-20.04-2.7.0
script:
- apt-get update && apt-get install -y graphviz
- python setup.py install
Expand All @@ -37,7 +44,6 @@ build_docs:

deploy_staging:
extends: .deploy_staging_v2
environment: $DEVPI_GROUP/staging

deploy_stable:
extends: .deploy_stable_v2
Expand All @@ -52,7 +58,7 @@ deploy_stable:
deploy_docs:
stage: deploy
tags: [linux, docker]
image: registry.gitlab.sensirion.lokal/sensirion/docker/docker-python:3.6-18.04-1.1.0
image: registry.gitlab.sensirion.lokal/sensirion/docker/docker-python:3.8-20.04-2.7.0
dependencies: []
only: [master, tags]
script:
Expand Down
7 changes: 7 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
CHANGELOG
---------

0.2.0
:::::
- Specify I2C frequency and voltage in SFM3019 constants
- Add official Python 3.8 support
- Use quickstart example from Python file instead of copy-paste the code

0.1.0
:::::
- Initial release with SFM3019 support
91 changes: 22 additions & 69 deletions docs/conf.py
Original file line number Diff line number Diff line change
@@ -1,44 +1,35 @@
# -*- coding: utf-8 -*-
#
# Configuration file for the Sphinx documentation builder.
#
# This file does only contain a selection of the most common options. For a
# full list see the documentation:
# http://www.sphinx-doc.org/en/master/config

# -- Path setup --------------------------------------------------------------

# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
#
# import os
# import sys
# sys.path.insert(0, os.path.abspath('.'))
# This file only contains a selection of the most common options. For a full
# list see the documentation:
# https://www.sphinx-doc.org/en/master/usage/configuration.html

import os
import sys
from datetime import datetime

import pkg_resources
import sphinx.ext.autodoc

import sensirion_sensorbridge_i2c_sfm

# Add project directory such that sphinx can detect the package.
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))

# -- Project information -----------------------------------------------------
distribution = pkg_resources.get_distribution('sensirion_sensorbridge_i2c_sfm')

project = u'Sensirion SFM I²C Python Driver for use with the Sensorbridge'
copyright = u'{} Sensirion AG, Switzerland'.format(datetime.now().year)
author = u'Sensirion AG'

author = 'Sensirion AG'

# The short X.Y version
version = sensirion_sensorbridge_i2c_sfm.__version__
# The full version, including alpha/beta/rc tags
release = sensirion_sensorbridge_i2c_sfm.__version__


# -- General configuration ---------------------------------------------------

# If your documentation needs a minimal Sphinx version, state it here.
#
# needs_sphinx = '1.0'

# Add any Sphinx extension module names here, as strings. They can be
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
Expand All @@ -50,33 +41,16 @@
'sphinx.ext.intersphinx',
]

# Add any paths that contain templates here, relative to this directory.
templates_path = ['_templates']

# The suffix(es) of source filenames.
# You can specify multiple suffix as a list of string:
#
# source_suffix = ['.rst', '.md']
source_suffix = '.rst'

# The master toctree document.
master_doc = 'index'

# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
#
# This is also used if you do content translation via gettext catalogs.
# Usually you set "language" from the command line for these cases.
language = None

# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
# This pattern also affects html_static_path and html_extra_path.
exclude_patterns = [u'_build', 'Thumbs.db', '.DS_Store', 'generated']
exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store']

# The name of the Pygments (syntax highlighting) style to use.
pygments_style = 'sphinx'

# Configure that index.rst is master and not contents.rst
master_doc = 'index'

# -- Options for HTML output -------------------------------------------------

Expand All @@ -85,47 +59,26 @@
#
html_theme = 'sphinx_rtd_theme'

# Theme options are theme-specific and customize the look and feel of a theme
# further. For a list of options available for each theme, see the
# documentation.
#
# html_theme_options = {}

# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css".
html_static_path = ['_static']

# Custom sidebar templates, must be a dictionary that maps document names
# to template names.
#
# The default sidebars (for documents that don't match any pattern) are
# defined by theme itself. Builtin themes are using these templates by
# default: ``['localtoc.html', 'relations.html', 'sourcelink.html',
# 'searchbox.html']``.
#
# html_sidebars = {}
# Workaround for weirdly formatted function parameters. See https://github.com/readthedocs/sphinx_rtd_theme/issues/766
html4_writer = True

html_favicon = 'favicon.ico'


# -- Extension configuration -------------------------------------------------

autodoc_member_order = 'bysource'

autodoc_default_flags = [
'members',
'special-members', # To see __init__()
'inherited-members', # To see the methods from base classes
'special-members', # To see __init__()
'inherited-members', # To see the methods from base classes
]


def autodoc_skip_member(app, what, name, obj, skip, options):
whitelist = ('__init__', '__call__',)
whitelist = ('__init__',)
exclude = name.startswith('__') and (name not in whitelist)
# blacklist members
blacklist = ()
return skip or exclude or name in blacklist
return skip or exclude


def setup(app):
Expand Down
52 changes: 2 additions & 50 deletions docs/quickstart.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,53 +9,5 @@ connected to the computer using a `Sensirion SEK-SensorBridge
<https://sensirion.com/sensorbridge/>`_. The driver for the SensorBridge can be
installed with ``pip install sensirion-shdlc-sensorbridge``.


.. sourcecode:: python

import logging
import time

from sensirion_shdlc_driver import ShdlcSerialPort, ShdlcConnection
from sensirion_shdlc_sensorbridge import SensorBridgePort, SensorBridgeShdlcDevice

from sensirion_sensorbridge_i2c_sfm.sfm3019 import Sfm3019I2cSensorBridgeDevice, MeasurementMode

logging.basicConfig(format='%(asctime)s [%(levelname)s] %(message)s', level=logging.ERROR)

# Connect to the SensorBridge with default settings:
# - baudrate: 460800
# - slave address: 0
with ShdlcSerialPort(port='/dev/ttyUSB0', baudrate=460800) as port:
# Initialize Sensorbridge
bridge = SensorBridgeShdlcDevice(ShdlcConnection(port), slave_address=0)
print("SensorBridge SN: {}".format(bridge.get_serial_number()))

# Configure SensorBridge port 1 for SFM
bridge.set_i2c_frequency(SensorBridgePort.ONE, frequency=400e3)
bridge.set_supply_voltage(SensorBridgePort.ONE, voltage=3.3)
bridge.switch_supply_on(SensorBridgePort.ONE)

# Create SFM device
sfm3019 = Sfm3019I2cSensorBridgeDevice(bridge, SensorBridgePort.ONE, slave_address=0x2E)

# Define gas (or gas mixes)
measure_mode = MeasurementMode.Air
permille = 200 # only applies for gas mixes

# Initialize sensor:
# 1.) Stop any running measurement
# 2.) Request scale factors and unit set on sensor
sfm3019.initialize_sensor(measure_mode)

# Read out product information
pid, sn = sfm3019.read_product_identifier_and_serial_number()
print("SFM3019 SN: {}".format(sn))
print("Flow unit of sensor: {} (Volume at temperature in degree Centigrade)".format(sfm3019.flow_unit))

# Start measurements
sfm3019.start_continuous_measurement(measure_mode, air_o2_mix_fraction_permille=permille)

# Read them out continuously
while True:
time.sleep(0.1)
print("Flow: {}, Temperature: {}".format(*sfm3019.read_continuous_measurement()))
.. literalinclude:: ../quickstart.py
:language: python
6 changes: 4 additions & 2 deletions quickstart.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from sensirion_shdlc_sensorbridge import SensorBridgePort, SensorBridgeShdlcDevice

from sensirion_sensorbridge_i2c_sfm.sfm3019 import Sfm3019I2cSensorBridgeDevice, MeasurementMode
from sensirion_sensorbridge_i2c_sfm.sfm3019.sfm3019_constants import SFM3019_DEFAULT_I2C_FREQUENCY, \
SFM3019_DEFAULT_VOLTAGE

logging.basicConfig(format='%(asctime)s [%(levelname)s] %(message)s', level=logging.ERROR)

Expand All @@ -17,8 +19,8 @@
print("SensorBridge SN: {}".format(bridge.get_serial_number()))

# Configure SensorBridge port 1 for SFM
bridge.set_i2c_frequency(SensorBridgePort.ONE, frequency=400e3)
bridge.set_supply_voltage(SensorBridgePort.ONE, voltage=3.3)
bridge.set_i2c_frequency(SensorBridgePort.ONE, frequency=SFM3019_DEFAULT_I2C_FREQUENCY)
bridge.set_supply_voltage(SensorBridgePort.ONE, voltage=SFM3019_DEFAULT_VOLTAGE)
bridge.switch_supply_on(SensorBridgePort.ONE)

# Create SFM device
Expand Down
14 changes: 7 additions & 7 deletions sensirion_sensorbridge_i2c_sfm/sfm3019/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
Sfm3019I2cCmdStartMeasAir, Sfm3019I2cCmdStartMeasAirO2Mix, \
Sfm3019I2cCmdStartMeasO2, Sfm3019I2cCmdStopMeas, \
Sfm3019I2cCmdGetUnitAndFactors
from .sfm3019_constants import MeasurementMode, flow_unit_prefix, flow_unit, flow_time_base
from .sfm3019_constants import MeasurementMode, FLOW_UNIT_PREFIX, FLOW_UNIT, FLOW_TIME_BASE


class Sfm3019I2cSensorBridgeDevice:
"""
SFM3019 I²C device class to allow executing I²C commands via Sensirion's Sensorbridge.
SFM3019 I²C device class to allow executing I²C commands via Sensirion's SensorBridge.
"""

MeasurementCmds = {
Expand All @@ -27,9 +27,9 @@ def __init__(self, sensor_bridge, sensor_bridge_port, slave_address=0x2E):
Constructs a new SFM3019 I²C device.
:param ~sensirion_shdlc_sensorbridge.device.SensorBridgeShdlcDevice sensor_bridge:
The I²C SHDLC Sensorbridge connection to use for communication.
The I²C SHDLC SensorBridge connection to use for communication.
:param int sensor_bridge_port:
The port on the Sensorbridge which the sensor is connected to.
The port on the SensorBridge which the sensor is connected to.
:param byte slave_address:
The I²C slave address, defaults to 0x2E.
"""
Expand Down Expand Up @@ -92,9 +92,9 @@ def flow_unit(self):
:return: The flow unit as a string according to datasheet
:rtype: str
"""
unit = flow_unit[self._flow_unit >> 8 & 0xf]
time = flow_time_base[self._flow_unit >> 4 & 0xf]
prefix = flow_unit_prefix[self._flow_unit & 0xf]
unit = FLOW_UNIT[self._flow_unit >> 8 & 0xf]
time = FLOW_TIME_BASE[self._flow_unit >> 4 & 0xf]
prefix = FLOW_UNIT_PREFIX[self._flow_unit & 0xf]

return "{}{}{}".format(prefix, unit, time)

Expand Down
Loading

0 comments on commit 669b229

Please sign in to comment.