forked from FNAL-HCAL-Ph1Upgrade/gui-step1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemp.py
117 lines (97 loc) · 3.17 KB
/
temp.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
from client import webBus
import collections
from checksumClass import Checksum
# bus = webBus("pi5",0)
#bus = webBus("pi7",0)
# binDataHappy = '0 01110000 00111100 10000011'
# intDataHappy = '0 112 60 131'
# value = 28732
# Set last two bits of LSB to 0 (these are status bits).
# message = '0 01100011 01010010 01100100'
# 0110'0011'0101'0000 = 25424.
# Trigger T measurement hold master 1110'0011 0xE3
# Trigger RH measurement hold master 1110'0101 0xE5
# Trigger T measurement no hold master 1111'0011 0xF3
# Trigger RH measurement no hold master 1111'0101 0xF5
def reverse(message):
mlist = message.split()
error = mlist.pop(0)
mlist.reverse()
return error + " " + " ".join(mlist)
def getValue(message):
value = ''
mList = message.split()
mList = mList[1:-1]
for byte in xrange(len(mList)):
initialByte = bin(int(mList[byte]))[2:]
length = len(initialByte)
zeros = "".join(list('0' for i in xrange(8-length)))
fullByte = zeros + initialByte
value += fullByte
value = value[:-2] + '00'
return int(value,2)
def calcTemp(s):
return -46.85 + 175.72 * s/2**16
def calcHumi(s):
return -6 + 125.0 * s/2**16
def mean(myList):
return float(sum(myList))/len(myList)
# triggerList = [0xE3,0xE5,0xF3,0xF5]
trigger = collections.OrderedDict()
trigger['tempHold'] = 0xE3
trigger['humiHold'] = 0xE5
trigger['tempNoHold'] = 0xF3
trigger['humiNoHold'] = 0xF5
triggerDict = {
'Temperature' : {
'hold' : 0xE3,
'nohold' : 0xF3
},
'Humidity' : {
'hold' : 0xE5,
'nohold' : 0xF5
}
}
function = {
'Temperature' : calcTemp,
'Humidity' : calcHumi
}
# We have used 0xF3 for nohold, which has worked for temp.
def readTempHumi(bus, slot, num_bytes, key, hold, verbosity=0):
bus.write(0x00,[0x06])
bus.write(slot,[0x11,0x05,0,0,0])
bus.write(0x40,[triggerDict[key][hold]])
bus.read(0x40, num_bytes + 1) # also read checksum byte
message = bus.sendBatch()[-1]
# crc = cc.checkCRC(message,2)
check = Checksum(message,1)
crc = check.result
value = getValue(message)
if verbosity > 1:
print 'message: ', message
print 'checksum: ', crc
print 'value: ', value
return [crc,function[key](value)]
def readManyTemps(bus, slot,iterations,key,hold,verbosity=0):
tempArray = []
for i in xrange(iterations):
tempList = readTempHumi(bus, slot,2,key,hold,verbosity)
if verbosity > 0:
print tempList
tempArray.append(tempList)
if int(tempList[0]) != 0:
print 'In readTempHumi(): I2C_ERROR for Test ', i,' : ', tempList
transpose = zip(*tempArray)
finalTempList = transpose[1]
tempMin = min(finalTempList)
tempMax = max(finalTempList)
tempMean = mean(finalTempList)
tempCounter = collections.Counter(finalTempList)
tempModeList = tempCounter.most_common()
return tempMean
def run(slot,iterations,verbosity=0):
tempResults = {"Temperature" : -99, "Humidity" : -99}
for key in triggerDict:
hold = 'nohold'
tempResults[key]=readManyTemps(slot,iterations,key,hold,verbosity)
return tempResults