-
Notifications
You must be signed in to change notification settings - Fork 75
/
STTS751.py
82 lines (69 loc) · 2.41 KB
/
STTS751.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# STTS751 temperature seneor micropython drive
# ver: 1.0
# License: MIT
# Author: shaoziyang ([email protected])
# v1.0 2019.7
STTS751_RESOLUTION = (8, 0, 4, 12, 10, 11, 9, 12)
STTS751_REG_STATUS = const(1)
STTS751_REG_CONFIG = const(3)
STTS751_REG_CONRAT = const(4)
STTS751_REG_TEMPVH = const(0)
STTS751_REG_TEMPVL = const(2)
STTS751_REG_TEMPHH = const(5)
STTS751_REG_TEMPHL = const(6)
STTS751_REG_TEMPLH = const(7)
STTS751_REG_TEMPLL = const(8)
STTS751_REG_ONESHOT = const(15)
STTS751_REG_THERM = const(32)
STTS751_REG_THERMHYS = const(33)
class STTS751():
def __init__(self, i2c):
self.i2c = i2c
self.addr = 0x4A
self.tb = bytearray(1)
self.rb = bytearray(1)
self.oneshot = False
self.oneshot_mode(False)
def int16(self, d):
return d if d < 0x8000 else d - 0x10000
def setreg(self, reg, dat):
self.tb[0] = dat
self.i2c.writeto_mem(self.addr, reg, self.tb)
def getreg(self, reg):
self.i2c.readfrom_mem_into(self.addr, reg, self.rb)
return self.rb[0]
def get2reg(self, reg):
return self.getreg(reg) + self.getreg(reg+1) * 256
def resolution(self, res = None):
self.getreg(STTS751_REG_CONFIG)
if res is None:
return STTS751_RESOLUTION[(self.rb[0] & 0x0C)//4 + 4]
else:
if res > 12 or res < 9: return
self.rb[0] = (self.rb[0] & 0xF3) | STTS751_RESOLUTION[res-9]
self.setreg(STTS751_REG_CONFIG, self.rb[0])
def oneshot_mode(self, oneshot=None):
if oneshot is None:
return self.oneshot
else:
self.getreg(STTS751_REG_CONFIG)
self.oneshot = oneshot
if oneshot: self.rb[0] |= 0x40
else: self.rb[0] &= 0xBF
self.setreg(STTS751_REG_CONFIG, self.rb[0])
def ONE_SHOT(self):
if self.oneshot:
self.setreg(STTS751_REG_ONESHOT, 1)
while 1:
if self.getreg(STTS751_REG_STATUS) < 0x80:
return
def temperature(self):
try:
self.ONE_SHOT()
return self.int16((self.getreg(STTS751_REG_TEMPVH)<<8) + self.getreg(STTS751_REG_TEMPVL))/256
except MemoryError:
return self.temperature_irq()
def temperature_irq(self):
self.ONE_SHOT()
self.getreg(STTS751_REG_TEMPVH)
return self.rb[0] if self.rb[0] < 128 else self.rb[0] - 256