-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patht6713.py
81 lines (66 loc) · 2.22 KB
/
t6713.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
#
# Code can be found at @ https://www.raspberrypi.org/forums/viewtopic.php?f=32&t=153764
# By user: edwardsnick
# Use at your own risk!
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
# BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
# DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
#!/usr/bin/python
import math, struct, array, time, io, fcntl
bus = 1
addressT6713 = 0x15
I2C_SLAVE=0x0703
class i2c(object):
def __init__(self, device, bus):
self.fr = io.open("/dev/i2c-"+str(bus), "rb", buffering=0)
self.fw = io.open("/dev/i2c-"+str(bus), "wb", buffering=0)
# set device address
fcntl.ioctl(self.fr, I2C_SLAVE, device)
fcntl.ioctl(self.fw, I2C_SLAVE, device)
def write(self, bytes):
self.fw.write(bytes)
def read(self, bytes):
return self.fr.read(bytes)
def close(self):
self.fw.close()
self.fr.close()
class T6713(object):
def __init__(self):
self.dev = i2c(addressT6713, bus)
def status(self):
buffer = array.array('B', [0x04, 0x13, 0x8a, 0x00, 0x01])
self.dev.write(buffer)
time.sleep(0.1)
data = self.dev.read(4)
buffer = array.array('B', data)
return buffer[2]*256+buffer[3]
def gasPPM(self):
buffer = array.array('B', [0x04, 0x13, 0x8b, 0x00, 0x01])
self.dev.write(buffer)
time.sleep(0.1)
data = self.dev.read(4)
buffer = array.array('B', data)
return buffer[2]*256+buffer[3]
def checkABC(self):
buffer = array.array('B', [0x04, 0x03, 0xee, 0x00, 0x01])
self.dev.write(buffer)
time.sleep(0.1)
data = self.dev.read(4)
buffer = array.array('B', data)
return buffer[2]*256+buffer[3]
def calibrate(self):
buffer = array.array('B', [0x05, 0x03, 0xec, 0xff, 0x00])
self.dev.write(buffer)
time.sleep(0.1)
data = self.dev.read(5)
buffer = array.array('B', data)
return buffer[3]*256+buffer[3]
if __name__ == "__main__":
obj = T6713()
print "Status: ", bin(obj.status())
print "PPM: ", obj.gasPPM()
print "ABC State: ", obj.checkABC()