Added EnviropHAT support

This commit is contained in:
Fabio Manganiello 2019-08-01 22:40:41 +02:00
parent b8f936e440
commit bdadd2061c
5 changed files with 112 additions and 0 deletions

View file

@ -220,6 +220,7 @@ autodoc_mock_imports = ['googlesamples.assistant.grpc.audio_helpers',
'serial',
'pyHS100',
'grpc',
'envirophat',
]
sys.path.insert(0, os.path.abspath('../..'))

View file

@ -0,0 +1,36 @@
from platypush.backend.sensor import SensorBackend
from platypush.context import get_plugin
class SensorEnvirophatBackend(SensorBackend):
"""
Backend to poll analog sensor values from an MCP3008 chipset
(https://learn.adafruit.com/raspberry-pi-analog-to-digital-converters/mcp3008)
Requires:
* ``adafruit-mcp3008`` (``pip install adafruit-mcp3008``)
* The :mod:`platypush.plugins.gpio.sensor.mcp3008` plugin configured
"""
def __init__(self, temperature=True, pressure=True, altitude=True, luminosity=True,
analog=True, accelerometer=True, magnetometer=True, qnh=1020, **kwargs):
super().__init__(self, **kwargs)
self.qnh = qnh
self.enabled_sensors = {
'temperature': temperature,
'pressure': pressure,
'altitude': altitude,
'luminosity': luminosity,
'analog': analog,
'accelerometer': accelerometer,
'magnetometer': magnetometer,
}
def get_measurement(self):
plugin = get_plugin('gpio.sensor.envirophat')
return plugin.get_data(qnh=self.qnh).output
# vim:sw=4:ts=4:et:

View file

@ -0,0 +1,71 @@
import envirophat
from platypush.plugins import action
from platypush.plugins.gpio.sensor import GpioSensorPlugin
class GpioSensorEnvirophatPlugin(GpioSensorPlugin):
"""
Plugin to interact with a `Pimoroni enviropHAT <https://shop.pimoroni.com/products/enviro-phat>`_ device.
You can use an enviropHAT device to read e.g. temperature, pressure, altitude, accelerometer, magnetometer and
luminosity data, plus control the status of its RGB LEDs.
Requires:
* ``envirophat`` (``pip install envirophat``)
"""
@action
def get_measurement(self, qnh=1020.0):
"""
:param: qnh: Local value for atmospheric pressure adjusted to sea level (default: 1020)
:type qnh: float
:returns: dict. Example::
output = {
"temperature": 21.0, # Celsius
"pressure": 101555.08, # pascals
"altitude": 10, # meters
"luminosity": 426, # lumens
# Measurements from the custom analog channels
"analog": [0.513, 0.519, 0.531, 0.528],
"accelerometer": {
"x": -0.000915,
"y": 0.0760,
"z": 1.026733
},
"magnetometer": {
"x": -2297,
"y": 1226,
"z": -7023
},
}
"""
ret = {}
weather = envirophat.weather
light = envirophat.light
accelerometer = envirophat.motion.accelerometer()
magnetometer = envirophat.motion.magnetometer()
leds = envirophat.leds
analog = envirophat.analog
weather.update()
ret['temperature'] = weather.temperature()
ret['pressure'] = weather.pressure()
ret['altitude'] = weather.altitude(qnh=qnh)
ret['luminosity'] = light.light()
ret['accelerometer'] = {getattr(accelerometer, v) for v in ['x', 'y', 'z']}
ret['magnetometer'] = {getattr(magnetometer, v) for v in ['x', 'y', 'z']}
ret['analog'] = list(analog.read_all())
ret['leds'] = leds.is_on()
return ret
# vim:sw=4:ts=4:et:

View file

@ -147,3 +147,6 @@ pyScss
# nfcpy >= 1.0
# ndef
# Support for enviropHAT
# envirophat

View file

@ -171,6 +171,7 @@ setup(
'Support for mpv player plugin': ['python-mpv'],
'Support for compiling SASS/SCSS styles to CSS': ['pyScss'],
'Support for NFC tags': ['nfcpy>=1.0', 'ndef'],
'Support for enviropHAT': ['envirophat'],
# 'Support for Leap Motion backend': ['git+ssh://git@github.com:BlackLight/leap-sdk-python3.git'],
# 'Support for Flic buttons': ['git+https://@github.com/50ButtonsEach/fliclib-linux-hci.git']
# 'Support for media subtitles': ['git+https://github.com/agonzalezro/python-opensubtitles#egg=python-opensubtitles']