-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbreakout.py
62 lines (51 loc) · 1.72 KB
/
breakout.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
import smbus
import struct
from collections import namedtuple
Status = namedtuple('Status', 'ignite flame motor main_temp ambient weight aux_temp0 aux_temp1')
class Breakout(object):
fmt = struct.Struct('<BBH5f')
def __init__(self, addr, bus=1):
self.bus = smbus.SMBus(1)
self.addr = addr
@property
def status(self):
while True:
try:
chars = map(chr, self.bus.read_i2c_block_data(self.addr, 0, self.fmt.size))
return Status._make(self.fmt.unpack(''.join(chars)))
except IOError:
#ignore IOError due to i2c timeout
pass
def __repr__(self):
return repr(self.status)
def _get_cmd(self, cmd, fmt='f'):
s = struct.Struct('<'+fmt)
while True:
try:
data = self.bus.read_i2c_block_data(self.addr, ord(cmd), s.size)
return s.unpack(''.join(map(chr, data)))[0]
except IOError:
#ignore IOError due to i2c timeout
pass
def _set_cmd(self, cmd, value, fmt='f'):
out = map(ord, struct.pack('<'+fmt, value))
while True:
try:
return self.bus.write_i2c_block_data(self.addr, ord(cmd), out)
except IOError:
pass
@property
def motor(self):
return self._get_cmd('M', fmt='H')
@motor.setter
def motor(self, pos):
self._set_cmd('M', pos, fmt='H')
@property
def temperature(self):
return self._get_cmd('T', fmt='f')
@property
def ignite(self):
return self._get_cmd('I', fmt='B')
@ignite.setter
def ignite(self, output):
self._set_cmd('I', output, fmt='B')