1# -*- coding: UTF-8 -*-
2
3"""
4The driver for Si7006 chip, it is a temperature and humidity sensor.
5"""
6
7from driver import I2C
8from utime import sleep_ms
9
10
11# The register address in Si7006 controller.
12Si7006_MEAS_REL_HUMIDITY_MASTER_MODE    = 0xE5
13Si7006_MEAS_REL_HUMIDITY_NO_MASTER_MODE = 0xF5
14Si7006_MEAS_TEMP_MASTER_MODE            = 0xE3
15Si7006_MEAS_TEMP_NO_MASTER_MODE         = 0xF3
16Si7006_READ_OLD_TEMP                    = 0xE0
17Si7006_RESET                            = 0xFE
18Si7006_READ_ID_LOW_0                    = 0xFA
19Si7006_READ_ID_LOW_1                    = 0x0F
20Si7006_READ_ID_HIGH_0                   = 0xFC
21Si7006_READ_ID_HIGH_1                   = 0xC9
22Si7006_READ_Firmware_Revision_0         = 0x84
23Si7006_READ_Firmware_Revision_1         = 0xB8
24
25class SI7006Error(Exception):
26    def __init__(self, value=0, msg="si7006 common error"):
27        self.value = value
28        self.msg = msg
29
30    def __str__(self):
31        return "Error code:%d, Error message: %s" % (self.value, str(self.msg))
32
33    __repr__ = __str__
34
35
36class SI7006(object):
37    """
38    This class implements SI7006 chip's functions.
39    """
40    def __init__(self):
41        self.i2cDev = None
42
43    def open(self, devid):
44        self.i2cDev = I2C()
45        self.i2cDev.open(devid)
46
47    def getVer(self):
48        """
49        Get the firmware version of the chip.
50        """
51
52        reg = bytearray([Si7006_READ_Firmware_Revision_0, Si7006_READ_Firmware_Revision_1])
53        self.i2cDev.write(reg)
54        sleep_ms(30)
55        version = bytearray(1)
56        self.i2cDev.read(version)
57        return version[0]
58
59    def getID(self):
60        """Get the chip ID."""
61
62        reg = bytearray([Si7006_READ_ID_LOW_0, Si7006_READ_ID_LOW_1])
63        self.i2cDev.write(reg)
64        sleep_ms(30)
65        id_buf_low = bytearray(4)
66        self.i2cDev.read(id_buf_low)
67
68        reg = bytearray([Si7006_READ_ID_HIGH_0, Si7006_READ_ID_HIGH_1])
69        id_buf_high = bytearray(4)
70        self.i2cDev.read(id_buf_high)
71
72        return id_buf_low + id_buf_high
73
74    def getTemperature(self):
75        """Get temperature."""
76
77        reg = bytearray([Si7006_MEAS_TEMP_NO_MASTER_MODE])
78        self.i2cDev.write(reg)
79        sleep_ms(30)
80        readData = bytearray(2)
81        self.i2cDev.read(readData)
82        value = (readData[0] << 8 | readData[1])
83
84        if (value & 0xFFFC):
85            temperature = (175.72 * value) / 65536.0 - 46.85
86            return temperature
87        else:
88            raise SI7006Error("failed to get temperature.")
89
90
91    def getHumidity(self):
92        """Get humidity."""
93
94        reg = bytearray([Si7006_MEAS_REL_HUMIDITY_NO_MASTER_MODE])
95        self.i2cDev.write(reg)
96        sleep_ms(30)
97        readData = bytearray(2)
98        self.i2cDev.read(readData)
99        value = (readData[0] << 8) | readData[1]
100
101        if (value & 0xFFFE):
102            humidity = (125.0 * value) / 65535.0 - 6.0
103            return humidity
104        else:
105            raise SI7006Error("failed to get humidity.")
106
107    def getTempHumidity(self):
108        """Get temperature and humidity."""
109
110        temphumidity = [0, 0]
111        temphumidity[0] = self.getTemperature()
112        temphumidity[1] = self.getHumidity()
113        return temphumidity
114
115    def close(self):
116        self.i2cDev.close()
117