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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
|
#!/usr/bin/python
# coding=UTF-8
# ----------------------------------------------------------------------------
#
# DRCONTROL.PY
#
# Copyright (C) 2012 Sebastian Sjoholm, sebastian.sjoholm@gmail.com
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Version history can be found at
# http://code.google.com/p/drcontrol/wiki/VersionHistory
#
# $Rev$
# $Date$
#
# ----------------------------------------------------------------------------
from optparse import OptionParser
from pylibftdi import Driver
from pylibftdi import BitBangDevice
import sys
import time
# ----------------------------------------------------------------------------
# VARIABLE CLASSS
# ----------------------------------------------------------------------------
class app_data:
def __init__(
self,
name = "DRControl",
version = "0.1",
date = "$Date$",
rev = "$Rev$",
author = "Sebastian Sjoholm"
):
self.name = name
self.version = version
self.build = date
self.rev = rev
self.author = author
class cmdarg_data:
def __init__(
self,
device = "",
relay = "",
state = "",
verbose = False
):
self.device = device
self.relay = relay
self.state = state
self.verbose = verbose
class relay_data(dict):
address = {
"1":"2",
"2":"8",
"3":"20",
"4":"80",
"5":"1",
"6":"4",
"7":"10",
"8":"40"
}
def __getitem__(self, key): return self[key]
def keys(self): return self.keys()
# ----------------------------------------------------------------------------
# LIST_DEVICES()
#
# Routine modified from the original pylibftdi example by Ben Bass
# ----------------------------------------------------------------------------
def list_devices():
print "Vendor\t\tProduct\t\t\tSerial"
dev_list = []
for device in Driver().list_devices():
device = map(lambda x: x.decode('latin1'), device)
vendor, product, serial = device
print "%s\t\t%s\t\t%s" % (vendor, product, serial)
# ----------------------------------------------------------------------------
# SET_RELAY()
#
# Set specified relay to chosen state
# ----------------------------------------------------------------------------
def set_relay():
if cmdarg.verbose:
print "Device: " + cmdarg.device
print "Relay: " + cmdarg.relay + " (0x" + relay.address[cmdarg.relay] + ")"
print "State: " + cmdarg.state
try:
with BitBangDevice(cmdarg.device) as bb:
if cmdarg.verbose:
print "Current state: " + bb.port
if cmdarg.state == "on":
bb.port |= int(relay.address[cmdarg.relay], 16)
elif cmdarg.state == "off":
bb.port &= ~int(relay.address[cmdarg.relay], 16)
if cmdarg.verbose:
print "Current state: " + bb.port
except Exception:
print "Error: Problem with device, or device not exists"
sys.exit(1)
def check():
# Check python version
if sys.hexversion < 0x02060000:
print "Error: Your Python need to be 2.6 or newer"
sys.exit(1)
# Check availability on library
if __name__ == '__main__':
# Init objects
cmdarg = cmdarg_data()
relay = relay_data()
app = app_data()
# Do system check
check()
parser = OptionParser()
parser.add_option("-d", "--device", action="store", type="string", dest="device", help="The device serial, example A6VV5PHY")
parser.add_option("-l", "--list", action="store_true", dest="list", default=False, help="List all devices")
parser.add_option("-r", "--relay", action="store", type="string", dest="relay", help="Relay to command by number: 1...8")
parser.add_option("-s", "--state", action="store", type="string", dest="state", help="State: on or off")
parser.add_option("-v", "--verbose", action="store_true", dest="verbose", default=False, help="Verbose, print all info on screen")
(options, args) = parser.parse_args()
if options.verbose:
cmdarg.verbose = options.verbose
print app.name + " " + app.version
else:
cmdarg.verbose = False
if options.list:
list_devices()
sys.exit(0)
if options.device:
if not options.relay:
print "Error: Need to state which relay"
sys.exit(1)
if not options.state:
print "Error: Need to specify which relay state"
sys.exit(1)
cmdarg.device = options.device
cmdarg.relay = options.relay
cmdarg.state = options.state
set_relay()
sys.exit(0)
|