-
Notifications
You must be signed in to change notification settings - Fork 1
/
tmp100.py
74 lines (59 loc) · 2.46 KB
/
tmp100.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
import smbus
class Temp100:
def __init__(self):
self.defaultBus = 0 # 0 for RPiv1, 1 for RPiv2
self.bus = smbus.SMBus(self.defaultBus)
self.defaultAddress = 0b1001000 # 0x48 for EveAlpha
self.tmp100temp = 0b00
self.tmp100config = 0b01
self.tmp100tempL = 0b10
self.tmp100tempH = 0b11
self.tmp100configres = 0b10011111
self.tmp100config9bit = 0b00000000
self.tmp100config10bit = 0b00100000
self.tmp100config11bit = 0b01000000
self.tmp100config12bit = 0b01100000
self.tmp100configShutclear = 0b11111110
self.tmp100configShutdown = 0b00000001
def readByteData(self, register):
return self.bus.read_byte_data(self.defaultAddress, register)
def readWordData(self, register):
return self.bus.read_word_data(self.defaultAddress, register)
def writeByteData(self, register, value):
self.bus.write_byte_data(self.defaultAddress, register, value)
def getConfiguration(self):
return self.readByteData(self.tmp100config)
def setShutdown(self):
config = self.getConfiguration()
config = config | self.tmp100configShutdown
self.writeByteData(self.tmp100config,config)
def setShutclear(self):
config = self.getConfiguration()
config = config & self.tmp100configShutclear
self.writeByteData(self.tmp100config,config)
def setResolution9bit(self):
config = self.getConfiguration()
config = config & self.tmp100configres
config = config | self.tmp100config9bit
self.writeByteData(self.tmp100config,config)
def setResolution10bit(self):
config = self.getConfiguration()
config = config & self.tmp100configres
config = config | self.tmp100config10bit
self.writeByteData(self.tmp100config,config)
def setResolution11bit(self):
config = self.getConfiguration()
config = config & self.tmp100configres
config = config | self.tmp100config11bit
self.writeByteData(self.tmp100config,config)
def setResolution12bit(self):
config = self.getConfiguration()
config = config & self.tmp100configres
config = config | self.tmp100config12bit
self.writeByteData(self.tmp100config,config)
def getTemperature(self):
tempW = self.readWordData(self.tmp100temp)
#print "word: "+bin(tempW)
temp = (tempW&0xFF) + ((tempW>>12)/16.0)
#print "temp: "+str(temp)
return temp