Skip to content
This repository was archived by the owner on Sep 10, 2024. It is now read-only.

Commit 14b8eee

Browse files
author
Alex Bucknall
committed
Cleaned up SI7006A20.py using suggestions from Anders Borg
1 parent 8fc232b commit 14b8eee

File tree

1 file changed

+10
-21
lines changed

1 file changed

+10
-21
lines changed

pysense/lib/SI7006A20.py

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,32 @@
11
import time
2+
from machine import I2C
23

34
class SI7006A20:
4-
55
SI7006A20_I2C_ADDR = const(0x40)
66
TEMP_NOHOLDMASTER = const(0xF3)
77
HUMD_NOHOLDMASTER = const(0xF5)
88

9-
def __init__(self, pysense=None, sda='P22', scl='P21'):
9+
def __init__(self, pysense = None, sda = 'P22', scl = 'P21'):
1010
if pysense is not None:
1111
self.i2c = pysense.i2c
1212
else:
13-
from machine import I2C
1413
self.i2c = I2C(0, mode=I2C.MASTER, pins=(sda, scl))
1514

16-
def _concat_hex(self, a, b):
17-
sizeof_b = 0
18-
while((b >> sizeof_b) > 0):
19-
sizeof_b += 1
20-
sizeof_b += sizeof_b % 8
21-
return (a << sizeof_b) | b
15+
def _getWord(self, high, low):
16+
return ((high & 0xFF) << 8) + (low & 0xFF)
2217

23-
def temp(self, unit=None):
18+
def temperature(self):
2419
self.i2c.writeto(SI7006A20_I2C_ADDR, bytearray([0xF3]))
2520
time.sleep(0.5)
26-
data = self.i2c.readfrom(SI7006A20_I2C_ADDR,2)
27-
data0 = data[0]
28-
data1 = data[1]
29-
data = self._concat_hex(data0, data1)
21+
data = self.i2c.readfrom(SI7006A20_I2C_ADDR, 2)
22+
data = self._getWord(data[0], data[1])
3023
temp = ((175.72 * data) / 65536.0) - 46.85
31-
if unit == 'F':
32-
temp = temp * 1.8 + 32
3324
return temp
3425

3526
def humidity(self):
3627
self.i2c.writeto(SI7006A20_I2C_ADDR, bytearray([0xF5]))
3728
time.sleep(0.5)
38-
data = self.i2c.readfrom(SI7006A20_I2C_ADDR,2)
39-
data0 = data[0]
40-
data1 = data[1]
41-
data = self._concat_hex(data0, data1)
42-
humidity = ((125.0 * data) / 65536.0) + 6.0
29+
data = self.i2c.readfrom(SI7006A20_I2C_ADDR, 2)
30+
data = self._getWord(data[0], data[1])
31+
humidity = ((125.0 * data) / 65536.0) - 6.0
4332
return humidity

0 commit comments

Comments
 (0)