dweet fix again
This commit is contained in:
217
app/controller/drivers/adafruit/Adafruit_DHT/Beaglebone_Black.py
Normal file
217
app/controller/drivers/adafruit/Adafruit_DHT/Beaglebone_Black.py
Normal file
@@ -0,0 +1,217 @@
|
||||
# Copyright (c) 2014 Adafruit Industries
|
||||
# Author: Tony DiCola
|
||||
|
||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
# of this software and associated documentation files (the "Software"), to deal
|
||||
# in the Software without restriction, including without limitation the rights
|
||||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
# copies of the Software, and to permit persons to whom the Software is
|
||||
# furnished to do so, subject to the following conditions:
|
||||
|
||||
# The above copyright notice and this permission notice shall be included in all
|
||||
# copies or substantial portions of the Software.
|
||||
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
# SOFTWARE.
|
||||
import re
|
||||
|
||||
import common
|
||||
import Beaglebone_Black_Driver as driver
|
||||
|
||||
|
||||
# Define mapping of pin names to GPIO base and number.
|
||||
# Adapted from Adafruit_BBIO code Beaglebone Black system reference.
|
||||
pin_to_gpio = {
|
||||
"P9_11": (0,30),
|
||||
"P9_12": (1,28),
|
||||
"P9_13": (0,31),
|
||||
"P9_14": (1,18),
|
||||
"P9_15": (1,16),
|
||||
"P9_16": (1,19),
|
||||
"P9_17": (0,5),
|
||||
"P9_18": (0,4),
|
||||
"P9_19": (0,13),
|
||||
"P9_20": (0,12),
|
||||
"P9_21": (0,3),
|
||||
"P9_22": (0,2),
|
||||
"P9_23": (1,17),
|
||||
"P9_24": (0,15),
|
||||
"P9_25": (3,21),
|
||||
"P9_26": (0,14),
|
||||
"P9_27": (3,19),
|
||||
"P9_28": (3,17),
|
||||
"P9_29": (3,15),
|
||||
"P9_30": (3,16),
|
||||
"P9_31": (3,14),
|
||||
"P9_41": (0,20),
|
||||
"P9_42": (0,7),
|
||||
"UART4_RXD": (0,30),
|
||||
"UART4_TXD": (0,31),
|
||||
"EHRPWM1A": (1,18),
|
||||
"EHRPWM1B": (1,19),
|
||||
"I2C1_SCL": (0,5),
|
||||
"I2C1_SDA": (0,4),
|
||||
"I2C2_SCL": (0,13),
|
||||
"I2C2_SDA": (0,12),
|
||||
"UART2_TXD": (0,3),
|
||||
"UART2_RXD": (0,2),
|
||||
"UART1_TXD": (0,15),
|
||||
"UART1_RXD": (0,14),
|
||||
"SPI1_CS0": (3,17),
|
||||
"SPI1_D0": (3,15),
|
||||
"SPI1_D1": (3,16),
|
||||
"SPI1_SCLK": (3,14),
|
||||
"CLKOUT2": (0,20),
|
||||
"30": (0,30),
|
||||
"60": (1,28),
|
||||
"31": (0,31),
|
||||
"50": (1,18),
|
||||
"48": (1,16),
|
||||
"51": (1,19),
|
||||
"5": (0,5),
|
||||
"4": (0,4),
|
||||
"13": (0,13),
|
||||
"12": (0,12),
|
||||
"3": (0,3),
|
||||
"2": (0,2),
|
||||
"49": (1,17),
|
||||
"15": (0,15),
|
||||
"117": (3,21),
|
||||
"14": (0,14),
|
||||
"115": (3,19),
|
||||
"113": (3,17),
|
||||
"111": (3,15),
|
||||
"112": (3,16),
|
||||
"110": (3,14),
|
||||
"20": (0,20),
|
||||
"7": (0,7),
|
||||
"P8_3": (1,6),
|
||||
"P8_4": (1,7),
|
||||
"P8_5": (1,2),
|
||||
"P8_6": (1,3),
|
||||
"P8_7": (2,2),
|
||||
"P8_8": (2,3),
|
||||
"P8_9": (2,5),
|
||||
"P8_10": (2,4),
|
||||
"P8_11": (1,13),
|
||||
"P8_12": (1,12),
|
||||
"P8_13": (0,23),
|
||||
"P8_14": (0,26),
|
||||
"P8_15": (1,15),
|
||||
"P8_16": (1,14),
|
||||
"P8_17": (0,27),
|
||||
"P8_18": (2,1),
|
||||
"P8_19": (0,22),
|
||||
"P8_20": (1,31),
|
||||
"P8_21": (1,30),
|
||||
"P8_22": (1,5),
|
||||
"P8_23": (1,4),
|
||||
"P8_24": (1,1),
|
||||
"P8_25": (1,0),
|
||||
"P8_26": (1,29),
|
||||
"P8_27": (2,22),
|
||||
"P8_28": (2,24),
|
||||
"P8_29": (2,23),
|
||||
"P8_30": (2,25),
|
||||
"P8_31": (0,10),
|
||||
"P8_32": (0,11),
|
||||
"P8_33": (0,9),
|
||||
"P8_34": (2,17),
|
||||
"P8_35": (0,8),
|
||||
"P8_36": (2,16),
|
||||
"P8_37": (2,14),
|
||||
"P8_38": (2,15),
|
||||
"P8_39": (2,12),
|
||||
"P8_40": (2,13),
|
||||
"P8_41": (2,10),
|
||||
"P8_42": (2,11),
|
||||
"P8_43": (2,8),
|
||||
"P8_44": (2,9),
|
||||
"P8_45": (2,6),
|
||||
"P8_46": (2,7),
|
||||
"TIMER4": (2,2),
|
||||
"TIMER7": (2,3),
|
||||
"TIMER5": (2,5),
|
||||
"TIMER6": (2,4),
|
||||
"EHRPWM2B": (0,23),
|
||||
"EHRPWM2A": (0,22),
|
||||
"UART5_CTSN": (0,10),
|
||||
"UART5_RTSN": (0,11),
|
||||
"UART4_RTSN": (0,9),
|
||||
"UART3_RTSN": (2,17),
|
||||
"UART4_CTSN": (0,8),
|
||||
"UART3_CTSN": (2,16),
|
||||
"UART5_TXD": (2,14),
|
||||
"UART5_RXD": (2,15),
|
||||
"38": (1,6),
|
||||
"39": (1,7),
|
||||
"34": (1,2),
|
||||
"35": (1,3),
|
||||
"66": (2,2),
|
||||
"67": (2,3),
|
||||
"69": (2,5),
|
||||
"68": (2,4),
|
||||
"45": (1,13),
|
||||
"44": (1,12),
|
||||
"23": (0,23),
|
||||
"26": (0,26),
|
||||
"47": (1,15),
|
||||
"46": (1,14),
|
||||
"27": (0,27),
|
||||
"65": (2,1),
|
||||
"22": (0,22),
|
||||
"63": (1,31),
|
||||
"62": (1,30),
|
||||
"37": (1,5),
|
||||
"36": (1,4),
|
||||
"33": (1,1),
|
||||
"32": (1,0),
|
||||
"61": (1,29),
|
||||
"86": (2,22),
|
||||
"88": (2,24),
|
||||
"87": (2,23),
|
||||
"89": (2,25),
|
||||
"10": (0,10),
|
||||
"11": (0,11),
|
||||
"9": (0,9),
|
||||
"81": (2,17),
|
||||
"8": (0,8),
|
||||
"80": (2,16),
|
||||
"78": (2,14),
|
||||
"79": (2,15),
|
||||
"76": (2,12),
|
||||
"77": (2,13),
|
||||
"74": (2,10),
|
||||
"75": (2,11),
|
||||
"72": (2,8),
|
||||
"73": (2,9),
|
||||
"70": (2,6),
|
||||
"71": (2,7)
|
||||
}
|
||||
|
||||
def read(sensor, pin):
|
||||
# Validate GPIO and map it to GPIO base and number.
|
||||
gpio = pin_to_gpio.get(str(pin).upper(), None)
|
||||
if gpio is None:
|
||||
# Couldn't find in mapping, check if pin looks like GPIO<base>_<number>
|
||||
match = re.match('GPIO([0123])_(\d+)', pin, re.IGNORECASE)
|
||||
if match is not None:
|
||||
gpio = (int(match.group(1)), int(match.group(2)))
|
||||
if gpio is None or gpio[0] < 0 or gpio[0] > 3 or gpio[1] < 0 or gpio[1] > 31:
|
||||
raise ValueError('Pin must be a valid GPIO identifier like P9_12 or GPIO1_28.')
|
||||
# Get a reading from C driver code.
|
||||
result, humidity, temp = driver.read(sensor, gpio[0], gpio[1])
|
||||
if result in common.TRANSIENT_ERRORS:
|
||||
# Signal no result could be obtained, but the caller can retry.
|
||||
return (None, None)
|
||||
elif result == common.DHT_ERROR_GPIO:
|
||||
raise RuntimeError('Error accessing GPIO. Make sure program is run as root with sudo!')
|
||||
elif result != common.DHT_SUCCESS:
|
||||
# Some kind of error occured.
|
||||
raise RuntimeError('Error calling DHT test driver read: {0}'.format(result))
|
||||
return (humidity, temp)
|
||||
38
app/controller/drivers/adafruit/Adafruit_DHT/Raspberry_Pi.py
Normal file
38
app/controller/drivers/adafruit/Adafruit_DHT/Raspberry_Pi.py
Normal file
@@ -0,0 +1,38 @@
|
||||
# Copyright (c) 2014 Adafruit Industries
|
||||
# Author: Tony DiCola
|
||||
|
||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
# of this software and associated documentation files (the "Software"), to deal
|
||||
# in the Software without restriction, including without limitation the rights
|
||||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
# copies of the Software, and to permit persons to whom the Software is
|
||||
# furnished to do so, subject to the following conditions:
|
||||
|
||||
# The above copyright notice and this permission notice shall be included in all
|
||||
# copies or substantial portions of the Software.
|
||||
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
# SOFTWARE.
|
||||
import common
|
||||
import Raspberry_Pi_Driver as driver
|
||||
|
||||
def read(sensor, pin):
|
||||
# Validate pin is a valid GPIO.
|
||||
if pin is None or int(pin) < 0 or int(pin) > 31:
|
||||
raise ValueError('Pin must be a valid GPIO number 0 to 31.')
|
||||
# Get a reading from C driver code.
|
||||
result, humidity, temp = driver.read(sensor, int(pin))
|
||||
if result in common.TRANSIENT_ERRORS:
|
||||
# Signal no result could be obtained, but the caller can retry.
|
||||
return (None, None)
|
||||
elif result == common.DHT_ERROR_GPIO:
|
||||
raise RuntimeError('Error accessing GPIO. Make sure program is run as root with sudo!')
|
||||
elif result != common.DHT_SUCCESS:
|
||||
# Some kind of error occured.
|
||||
raise RuntimeError('Error calling DHT test driver read: {0}'.format(result))
|
||||
return (humidity, temp)
|
||||
@@ -0,0 +1,38 @@
|
||||
# Copyright (c) 2014 Adafruit Industries
|
||||
# Author: Tony DiCola
|
||||
|
||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
# of this software and associated documentation files (the "Software"), to deal
|
||||
# in the Software without restriction, including without limitation the rights
|
||||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
# copies of the Software, and to permit persons to whom the Software is
|
||||
# furnished to do so, subject to the following conditions:
|
||||
|
||||
# The above copyright notice and this permission notice shall be included in all
|
||||
# copies or substantial portions of the Software.
|
||||
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
# SOFTWARE.
|
||||
import common
|
||||
import Raspberry_Pi_2_Driver as driver
|
||||
|
||||
def read(sensor, pin):
|
||||
# Validate pin is a valid GPIO.
|
||||
if pin is None or int(pin) < 0 or int(pin) > 31:
|
||||
raise ValueError('Pin must be a valid GPIO number 0 to 31.')
|
||||
# Get a reading from C driver code.
|
||||
result, humidity, temp = driver.read(sensor, int(pin))
|
||||
if result in common.TRANSIENT_ERRORS:
|
||||
# Signal no result could be obtained, but the caller can retry.
|
||||
return (None, None)
|
||||
elif result == common.DHT_ERROR_GPIO:
|
||||
raise RuntimeError('Error accessing GPIO. Make sure program is run as root with sudo!')
|
||||
elif result != common.DHT_SUCCESS:
|
||||
# Some kind of error occured.
|
||||
raise RuntimeError('Error calling DHT test driver read: {0}'.format(result))
|
||||
return (humidity, temp)
|
||||
33
app/controller/drivers/adafruit/Adafruit_DHT/Test.py
Normal file
33
app/controller/drivers/adafruit/Adafruit_DHT/Test.py
Normal file
@@ -0,0 +1,33 @@
|
||||
# Copyright (c) 2014 Adafruit Industries
|
||||
# Author: Tony DiCola
|
||||
|
||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
# of this software and associated documentation files (the "Software"), to deal
|
||||
# in the Software without restriction, including without limitation the rights
|
||||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
# copies of the Software, and to permit persons to whom the Software is
|
||||
# furnished to do so, subject to the following conditions:
|
||||
|
||||
# The above copyright notice and this permission notice shall be included in all
|
||||
# copies or substantial portions of the Software.
|
||||
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
# SOFTWARE.
|
||||
import common
|
||||
import Test_Driver as driver
|
||||
|
||||
def read(sensor, pin):
|
||||
# Get a reading from C driver code.
|
||||
result, humidity, temp = driver.read(sensor, pin)
|
||||
if result in common.TRANSIENT_ERRORS:
|
||||
# Signal no result could be obtained, but the caller can retry.
|
||||
return (None, None)
|
||||
elif result != common.DHT_SUCCESS:
|
||||
# Some kind of error occured.
|
||||
raise RuntimeError('Error calling DHT test driver read: {0}'.format(result))
|
||||
return (humidity, temp)
|
||||
21
app/controller/drivers/adafruit/Adafruit_DHT/__init__.py
Normal file
21
app/controller/drivers/adafruit/Adafruit_DHT/__init__.py
Normal file
@@ -0,0 +1,21 @@
|
||||
# Copyright (c) 2014 Adafruit Industries
|
||||
# Author: Tony DiCola
|
||||
|
||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
# of this software and associated documentation files (the "Software"), to deal
|
||||
# in the Software without restriction, including without limitation the rights
|
||||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
# copies of the Software, and to permit persons to whom the Software is
|
||||
# furnished to do so, subject to the following conditions:
|
||||
|
||||
# The above copyright notice and this permission notice shall be included in all
|
||||
# copies or substantial portions of the Software.
|
||||
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
# SOFTWARE.
|
||||
from common import DHT11, DHT22, AM2302, read, read_retry
|
||||
94
app/controller/drivers/adafruit/Adafruit_DHT/common.py
Normal file
94
app/controller/drivers/adafruit/Adafruit_DHT/common.py
Normal file
@@ -0,0 +1,94 @@
|
||||
# Copyright (c) 2014 Adafruit Industries
|
||||
# Author: Tony DiCola
|
||||
|
||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
# of this software and associated documentation files (the "Software"), to deal
|
||||
# in the Software without restriction, including without limitation the rights
|
||||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
# copies of the Software, and to permit persons to whom the Software is
|
||||
# furnished to do so, subject to the following conditions:
|
||||
|
||||
# The above copyright notice and this permission notice shall be included in all
|
||||
# copies or substantial portions of the Software.
|
||||
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
# SOFTWARE.
|
||||
import time
|
||||
|
||||
import platform_detect
|
||||
|
||||
|
||||
# Define error constants.
|
||||
DHT_SUCCESS = 0
|
||||
DHT_ERROR_TIMEOUT = -1
|
||||
DHT_ERROR_CHECKSUM = -2
|
||||
DHT_ERROR_ARGUMENT = -3
|
||||
DHT_ERROR_GPIO = -4
|
||||
TRANSIENT_ERRORS = [DHT_ERROR_CHECKSUM, DHT_ERROR_TIMEOUT]
|
||||
|
||||
# Define sensor type constants.
|
||||
DHT11 = 11
|
||||
DHT22 = 22
|
||||
AM2302 = 22
|
||||
SENSORS = [DHT11, DHT22, AM2302]
|
||||
|
||||
|
||||
def get_platform():
|
||||
"""Return a DHT platform interface for the currently detected platform."""
|
||||
plat = platform_detect.platform_detect()
|
||||
if plat == platform_detect.RASPBERRY_PI:
|
||||
# Check for version 1 or 2 of the pi.
|
||||
version = platform_detect.pi_version()
|
||||
if version == 1:
|
||||
import Raspberry_Pi
|
||||
return Raspberry_Pi
|
||||
elif version == 2:
|
||||
import Raspberry_Pi_2
|
||||
return Raspberry_Pi_2
|
||||
else:
|
||||
raise RuntimeError('No driver for detected Raspberry Pi version available!')
|
||||
elif plat == platform_detect.BEAGLEBONE_BLACK:
|
||||
import Beaglebone_Black
|
||||
return Beaglebone_Black
|
||||
else:
|
||||
raise RuntimeError('Unknown platform.')
|
||||
|
||||
def read(sensor, pin, platform=None):
|
||||
"""Read DHT sensor of specified sensor type (DHT11, DHT22, or AM2302) on
|
||||
specified pin and return a tuple of humidity (as a floating point value
|
||||
in percent) and temperature (as a floating point value in Celsius). Note that
|
||||
because the sensor requires strict timing to read and Linux is not a real
|
||||
time OS, a result is not guaranteed to be returned! In some cases this will
|
||||
return the tuple (None, None) which indicates the function should be retried.
|
||||
Also note the DHT sensor cannot be read faster than about once every 2 seconds.
|
||||
Platform is an optional parameter which allows you to override the detected
|
||||
platform interface--ignore this parameter unless you receive unknown platform
|
||||
errors and want to override the detection.
|
||||
"""
|
||||
if sensor not in SENSORS:
|
||||
raise ValueError('Expected DHT11, DHT22, or AM2302 sensor value.')
|
||||
if platform is None:
|
||||
platform = get_platform()
|
||||
return platform.read(sensor, pin)
|
||||
|
||||
def read_retry(sensor, pin, retries=15, delay_seconds=2, platform=None):
|
||||
"""Read DHT sensor of specified sensor type (DHT11, DHT22, or AM2302) on
|
||||
specified pin and return a tuple of humidity (as a floating point value
|
||||
in percent) and temperature (as a floating point value in Celsius).
|
||||
Unlike the read function, this read_retry function will attempt to read
|
||||
multiple times (up to the specified max retries) until a good reading can be
|
||||
found. If a good reading cannot be found after the amount of retries, a tuple
|
||||
of (None, None) is returned. The delay between retries is by default 2
|
||||
seconds, but can be overridden.
|
||||
"""
|
||||
for i in range(retries):
|
||||
humidity, temperature = read(sensor, pin, platform)
|
||||
if humidity is not None and temperature is not None:
|
||||
return (humidity, temperature)
|
||||
time.sleep(delay_seconds)
|
||||
return (None, None)
|
||||
103
app/controller/drivers/adafruit/Adafruit_DHT/platform_detect.py
Normal file
103
app/controller/drivers/adafruit/Adafruit_DHT/platform_detect.py
Normal file
@@ -0,0 +1,103 @@
|
||||
# Copyright (c) 2014 Adafruit Industries
|
||||
# Author: Tony DiCola
|
||||
|
||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
# of this software and associated documentation files (the "Software"), to deal
|
||||
# in the Software without restriction, including without limitation the rights
|
||||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
# copies of the Software, and to permit persons to whom the Software is
|
||||
# furnished to do so, subject to the following conditions:
|
||||
|
||||
# The above copyright notice and this permission notice shall be included in all
|
||||
# copies or substantial portions of the Software.
|
||||
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
# SOFTWARE.
|
||||
|
||||
# This is a direct copy of what's in the Adafruit Python GPIO library:
|
||||
# https://raw.githubusercontent.com/adafruit/Adafruit_Python_GPIO/master/Adafruit_GPIO/Platform.py
|
||||
# TODO: Add dependency on Adafruit Python GPIO and use its platform detect
|
||||
# functions.
|
||||
|
||||
import platform
|
||||
import re
|
||||
|
||||
# Platform identification constants.
|
||||
UNKNOWN = 0
|
||||
RASPBERRY_PI = 1
|
||||
BEAGLEBONE_BLACK = 2
|
||||
|
||||
|
||||
def platform_detect():
|
||||
"""Detect if running on the Raspberry Pi or Beaglebone Black and return the
|
||||
platform type. Will return RASPBERRY_PI, BEAGLEBONE_BLACK, or UNKNOWN."""
|
||||
# Handle Raspberry Pi
|
||||
pi = pi_version()
|
||||
if pi is not None:
|
||||
return RASPBERRY_PI
|
||||
|
||||
# Handle Beaglebone Black
|
||||
# TODO: Check the Beaglebone Black /proc/cpuinfo value instead of reading
|
||||
# the platform.
|
||||
plat = platform.platform()
|
||||
if plat.lower().find('armv7l-with-debian') > -1:
|
||||
return BEAGLEBONE_BLACK
|
||||
elif plat.lower().find('armv7l-with-ubuntu') > -1:
|
||||
return BEAGLEBONE_BLACK
|
||||
elif plat.lower().find('armv7l-with-glibc2.4') > -1:
|
||||
return BEAGLEBONE_BLACK
|
||||
|
||||
# Couldn't figure out the platform, just return unknown.
|
||||
return UNKNOWN
|
||||
|
||||
|
||||
def pi_revision():
|
||||
"""Detect the revision number of a Raspberry Pi, useful for changing
|
||||
functionality like default I2C bus based on revision."""
|
||||
# Revision list available at: http://elinux.org/RPi_HardwareHistory#Board_Revision_History
|
||||
with open('/proc/cpuinfo', 'r') as infile:
|
||||
for line in infile:
|
||||
# Match a line of the form "Revision : 0002" while ignoring extra
|
||||
# info in front of the revsion (like 1000 when the Pi was over-volted).
|
||||
match = re.match('Revision\s+:\s+.*(\w{4})$', line, flags=re.IGNORECASE)
|
||||
if match and match.group(1) in ['0000', '0002', '0003']:
|
||||
# Return revision 1 if revision ends with 0000, 0002 or 0003.
|
||||
return 1
|
||||
elif match:
|
||||
# Assume revision 2 if revision ends with any other 4 chars.
|
||||
return 2
|
||||
# Couldn't find the revision, throw an exception.
|
||||
raise RuntimeError('Could not determine Raspberry Pi revision.')
|
||||
|
||||
|
||||
def pi_version():
|
||||
"""Detect the version of the Raspberry Pi. Returns either 1, 2 or
|
||||
None depending on if it's a Raspberry Pi 1 (model A, B, A+, B+),
|
||||
Raspberry Pi 2 (model B+), or not a Raspberry Pi.
|
||||
"""
|
||||
# Check /proc/cpuinfo for the Hardware field value.
|
||||
# 2708 is pi 1
|
||||
# 2709 is pi 2
|
||||
# Anything else is not a pi.
|
||||
with open('/proc/cpuinfo', 'r') as infile:
|
||||
cpuinfo = infile.read()
|
||||
# Match a line like 'Hardware : BCM2709'
|
||||
match = re.search('^Hardware\s+:\s+(\w+)$', cpuinfo,
|
||||
flags=re.MULTILINE | re.IGNORECASE)
|
||||
if not match:
|
||||
# Couldn't find the hardware, assume it isn't a pi.
|
||||
return None
|
||||
if match.group(1) == 'BCM2708':
|
||||
# Pi 1
|
||||
return 1
|
||||
elif match.group(1) == 'BCM2709':
|
||||
# Pi 2
|
||||
return 2
|
||||
else:
|
||||
# Something else, not a pi.
|
||||
return None
|
||||
Reference in New Issue
Block a user