From c6beb6c3bafd3fa62c847024566947857441210b Mon Sep 17 00:00:00 2001 From: Jerry Needell Date: Mon, 20 Apr 2020 19:56:42 -0400 Subject: [PATCH 1/2] revert PR_9 --- adafruit_am2320.py | 21 +------ pi@gjnpi4desk.local | 138 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 141 insertions(+), 18 deletions(-) create mode 100644 pi@gjnpi4desk.local diff --git a/adafruit_am2320.py b/adafruit_am2320.py index 0370773..654b54a 100644 --- a/adafruit_am2320.py +++ b/adafruit_am2320.py @@ -77,21 +77,6 @@ def _crc16(data): return crc -class AM2320Exception(Exception): - """Base class for exceptions.""" - - -class AM2320DeviceNotFound(AM2320Exception, ValueError): - """Indicates that a device couldn't be found.""" - - -class AM2320ReadError(AM2320Exception, RuntimeError): - """indicates that valid data could not be read from the sensor. - - This may be due to a regular I2C read failure, or due to a checksum - mismatch.""" - - class AM2320: """A driver for the AM2320 temperature and humidity sensor. @@ -109,7 +94,7 @@ def __init__(self, i2c_bus, address=AM2320_DEFAULT_ADDR): except ValueError: pass time.sleep(0.25) - raise AM2320DeviceNotFound("AM2320 not found") + raise ValueError("AM2320 not found") def _read_register(self, register, length): with self._i2c as i2c: @@ -130,12 +115,12 @@ def _read_register(self, register, length): # print("$%02X => %s" % (register, [hex(i) for i in result])) # Check preamble indicates correct readings if result[0] != 0x3 or result[1] != length: - raise AM2320ReadError("I2C read failure") + raise RuntimeError("I2C read failure") # Check CRC on all but last 2 bytes crc1 = struct.unpack("`_ (Product ID: 3721) + +**Software and Dependencies:** + +* Adafruit CircuitPython firmware for the ESP8622 and M0-based boards: + https://github.com/adafruit/circuitpython/releases +* Adafruit's Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice + +""" + +# imports +try: + import struct +except ImportError: + import ustruct as struct + +import time + +from adafruit_bus_device.i2c_device import I2CDevice +from micropython import const + +__version__ = "0.0.0-auto.0" +__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_am2320.git" + + +AM2320_DEFAULT_ADDR = const(0x5C) +AM2320_CMD_READREG = const(0x03) +AM2320_REG_TEMP_H = const(0x02) +AM2320_REG_HUM_H = const(0x00) + + +def _crc16(data): + crc = 0xFFFF + for byte in data: + crc ^= byte + for _ in range(8): + if crc & 0x0001: + crc >>= 1 + crc ^= 0xA001 + else: + crc >>= 1 + return crc + + +class AM2320: + """A driver for the AM2320 temperature and humidity sensor. + + :param i2c_bus: The `busio.I2C` object to use. This is the only required parameter. + :param int address: (optional) The I2C address of the device. + + """ + + def __init__(self, i2c_bus, address=AM2320_DEFAULT_ADDR): + for _ in range(3): + # retry since we have to wake up the devices + try: + self._i2c = I2CDevice(i2c_bus, address) + return + except ValueError: + pass + time.sleep(0.25) + raise ValueError("AM2320 not found") + + def _read_register(self, register, length): + with self._i2c as i2c: + # wake up sensor + try: + i2c.write(bytes([0x00])) + except OSError: + pass + time.sleep(0.01) # wait 10 ms + + # Send command to read register + cmd = [AM2320_CMD_READREG, register & 0xFF, length] + # print("cmd: %s" % [hex(i) for i in cmd]) + i2c.write(bytes(cmd)) + time.sleep(0.002) # wait 2 ms for reply + result = bytearray(length + 4) # 2 bytes pre, 2 bytes crc + i2c.readinto(result) + # print("$%02X => %s" % (register, [hex(i) for i in result])) + # Check preamble indicates correct readings + if result[0] != 0x3 or result[1] != length: + raise RuntimeError("I2C read failure") + # Check CRC on all but last 2 bytes + crc1 = struct.unpack("H", self._read_register(AM2320_REG_TEMP_H, 2))[0] + if temperature >= 32768: + temperature = 32768 - temperature + return temperature / 10.0 + + @property + def relative_humidity(self): + """The measured relative humidity in percent.""" + humidity = struct.unpack(">H", self._read_register(AM2320_REG_HUM_H, 2))[0] + return humidity / 10.0 From 26fcc72fe2e889b084b7b95d818cd63cfe179df5 Mon Sep 17 00:00:00 2001 From: Jerry Needell Date: Mon, 20 Apr 2020 20:11:53 -0400 Subject: [PATCH 2/2] remove unecesary file --- pi@gjnpi4desk.local | 138 -------------------------------------------- 1 file changed, 138 deletions(-) delete mode 100644 pi@gjnpi4desk.local diff --git a/pi@gjnpi4desk.local b/pi@gjnpi4desk.local deleted file mode 100644 index 654b54a..0000000 --- a/pi@gjnpi4desk.local +++ /dev/null @@ -1,138 +0,0 @@ -# The MIT License (MIT) -# -# Copyright (c) 2018 Limor Fried -# -# 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. -""" -`adafruit_am2320` -==================================================== - -This is a CircuitPython driver for the AM2320 temperature and humidity sensor. - -* Author(s): Limor Fried - -Implementation Notes --------------------- - -**Hardware:** - -* Adafruit `AM2320 Temperature & Humidity Sensor - `_ (Product ID: 3721) - -**Software and Dependencies:** - -* Adafruit CircuitPython firmware for the ESP8622 and M0-based boards: - https://github.com/adafruit/circuitpython/releases -* Adafruit's Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice - -""" - -# imports -try: - import struct -except ImportError: - import ustruct as struct - -import time - -from adafruit_bus_device.i2c_device import I2CDevice -from micropython import const - -__version__ = "0.0.0-auto.0" -__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_am2320.git" - - -AM2320_DEFAULT_ADDR = const(0x5C) -AM2320_CMD_READREG = const(0x03) -AM2320_REG_TEMP_H = const(0x02) -AM2320_REG_HUM_H = const(0x00) - - -def _crc16(data): - crc = 0xFFFF - for byte in data: - crc ^= byte - for _ in range(8): - if crc & 0x0001: - crc >>= 1 - crc ^= 0xA001 - else: - crc >>= 1 - return crc - - -class AM2320: - """A driver for the AM2320 temperature and humidity sensor. - - :param i2c_bus: The `busio.I2C` object to use. This is the only required parameter. - :param int address: (optional) The I2C address of the device. - - """ - - def __init__(self, i2c_bus, address=AM2320_DEFAULT_ADDR): - for _ in range(3): - # retry since we have to wake up the devices - try: - self._i2c = I2CDevice(i2c_bus, address) - return - except ValueError: - pass - time.sleep(0.25) - raise ValueError("AM2320 not found") - - def _read_register(self, register, length): - with self._i2c as i2c: - # wake up sensor - try: - i2c.write(bytes([0x00])) - except OSError: - pass - time.sleep(0.01) # wait 10 ms - - # Send command to read register - cmd = [AM2320_CMD_READREG, register & 0xFF, length] - # print("cmd: %s" % [hex(i) for i in cmd]) - i2c.write(bytes(cmd)) - time.sleep(0.002) # wait 2 ms for reply - result = bytearray(length + 4) # 2 bytes pre, 2 bytes crc - i2c.readinto(result) - # print("$%02X => %s" % (register, [hex(i) for i in result])) - # Check preamble indicates correct readings - if result[0] != 0x3 or result[1] != length: - raise RuntimeError("I2C read failure") - # Check CRC on all but last 2 bytes - crc1 = struct.unpack("H", self._read_register(AM2320_REG_TEMP_H, 2))[0] - if temperature >= 32768: - temperature = 32768 - temperature - return temperature / 10.0 - - @property - def relative_humidity(self): - """The measured relative humidity in percent.""" - humidity = struct.unpack(">H", self._read_register(AM2320_REG_HUM_H, 2))[0] - return humidity / 10.0