-
Notifications
You must be signed in to change notification settings - Fork 0
/
dispenser_select.py
80 lines (63 loc) · 2.21 KB
/
dispenser_select.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
#!/usr/bin/env python
import sys
import os
from time import sleep
#from bartendro.error import I2CIOError
ROUTER_BUS = 1
ROUTER_ADDRESS = 4
ROUTER_SELECT_CMD_BEGIN = 0
ROUTER_CMD_SYNC_ON = 251
ROUTER_CMD_SYNC_OFF = 252
ROUTER_CMD_PING = 253
ROUTER_CMD_COUNT = 254
ROUTER_CMD_RESET = 255
try:
import smbus
smbus_missing = 0
except ImportError, e:
if e.message != 'No module named smbus':
raise
smbus_missing = 1
class DispenserSelect(object):
'''This object interacts with the bartendro router controller to select dispensers'''
def __init__(self, max_dispensers, software_only):
self.software_only = software_only
self.max_dispensers = max_dispensers
self.router = None
self.num_dispensers = 3
self.selected = 255
def reset(self):
if self.software_only: return
self.router.write_byte(ROUTER_ADDRESS, ROUTER_CMD_RESET)
sleep(.15)
def select(self, dispenser):
if self.software_only: return
# NOTE: This code used to only send the select message if the dispenser changed.
# but tracking which dispenser was last selected across many web server threads
# is an extra pain I dont care to handle now. For now we'll just set the dispenser
# for each packet we send.
if dispenser < self.max_dispensers:
self.selected = dispenser
self.router.write_byte(ROUTER_ADDRESS, dispenser)
sleep(.01)
def sync(self, state):
if self.software_only: return
if (state):
self.router.write_byte(ROUTER_ADDRESS, ROUTER_CMD_SYNC_ON)
else:
self.router.write_byte(ROUTER_ADDRESS, ROUTER_CMD_SYNC_OFF)
def count(self):
return self.num_dispensers
def open(self):
'''Open the i2c connection to the router'''
if self.software_only: return
if smbus_missing:
print "error: You must install the smbus module!"
sys.exit(-1)
print "Opening I2C bus to router"
self.router = smbus.SMBus(ROUTER_BUS)
print "Done."
if __name__ == "__main__":
ds = DispenserSelect(15, 0)
ds.open()
ds.reset()