diff options
Diffstat (limited to 'drcontrol.py')
-rwxr-xr-x | drcontrol.py | 78 |
1 files changed, 59 insertions, 19 deletions
diff --git a/drcontrol.py b/drcontrol.py index 135cbc0..722cd00 100755 --- a/drcontrol.py +++ b/drcontrol.py @@ -33,6 +33,8 @@ from optparse import OptionParser from pylibftdi import Driver from pylibftdi import BitBangDevice +from ctypes.util import find_library + import sys import time @@ -44,7 +46,7 @@ class app_data: def __init__( self, name = "DRControl", - version = "0.1", + version = "0.11", date = "$Date$", rev = "$Rev$", author = "Sebastian Sjoholm" @@ -61,13 +63,13 @@ class cmdarg_data: self, device = "", relay = "", - state = "", + command = "", verbose = False ): self.device = device self.relay = relay - self.state = state + self.command = command self.verbose = verbose class relay_data(dict): @@ -80,13 +82,33 @@ class relay_data(dict): "5":"1", "6":"4", "7":"10", - "8":"40" + "8":"40", + "all":"FF" } def __getitem__(self, key): return self[key] def keys(self): return self.keys() # ---------------------------------------------------------------------------- +# testBit() returns a nonzero result, 2**offset, if the bit at 'offset' is one. +# http://wiki.python.org/moin/BitManipulation +# ---------------------------------------------------------------------------- + +def testBit(int_type, offset): + mask = 1 << offset + return(int_type & mask) + +def get_relay_state( data, relay ): + if relay == "1": + return testBit(data, 1) + if relay == "2": + return testBit(data, 3) + if relay == "3": + return testBit(data, 5) + if relay == "4": + return testBit(data, 7) + +# ---------------------------------------------------------------------------- # LIST_DEVICES() # # Routine modified from the original pylibftdi example by Ben Bass @@ -109,26 +131,37 @@ def list_devices(): def set_relay(): if cmdarg.verbose: - print "Device: " + cmdarg.device - print "Command: Set relay " + cmdarg.relay + " (0x" + relay.address[cmdarg.relay] + ") to " + cmdarg.state.upper() + print "Device:\t\t" + cmdarg.device + print "Send command:\tRelay " + cmdarg.relay + " (0x" + relay.address[cmdarg.relay] + ") to " + cmdarg.command.upper() try: with BitBangDevice(cmdarg.device) as bb: - if cmdarg.verbose: - print "Relay state: " + str(bb.port) - - if cmdarg.state == "on": + # Turn relay ON + if cmdarg.command == "on": if cmdarg.verbose: print "Relay " + str(cmdarg.relay) + " to ON" bb.port |= int(relay.address[cmdarg.relay], 16) - elif cmdarg.state == "off": + + # Turn relay OFF + elif cmdarg.command == "off": if cmdarg.verbose: print "Relay " + str(cmdarg.relay) + " to OFF" bb.port &= ~int(relay.address[cmdarg.relay], 16) - if cmdarg.verbose: - print "Relay state: " + str(bb.port) + # Print relay status + elif cmdarg.command == "state": + state = get_relay_state( bb.port, cmdarg.relay ) + if state == 0: + if cmdarg.verbose: + print "Relay " + cmdarg.relay + " state:\tOFF (" + str(state) + ")" + else: + print "OFF" + else: + if cmdarg.verbose: + print "Relay " + cmdarg.relay + " state:\tON (" + str(state) + ")" + else: + print "ON" except Exception, err: print "Error: " + str(err) @@ -141,8 +174,11 @@ def check(): print "Error: Your Python need to be 2.6 or newer" sys.exit(1) - # Check availability on library - + # Check availability on library, this check is also done in pylibftdi + ftdi_lib = find_library('ftdi') + if ftdi_lib is None: + print "Error: The pylibftdi library not found" + sys.exit(1) if __name__ == '__main__': @@ -157,8 +193,8 @@ if __name__ == '__main__': 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("-r", "--relay", action="store", type="string", dest="relay", help="Relay to command by number: 1...8 or all") + parser.add_option("-c", "--command", action="store", type="string", dest="command", help="State: on, off, state") parser.add_option("-v", "--verbose", action="store_true", dest="verbose", default=False, help="Verbose, print all info on screen") (options, args) = parser.parse_args() @@ -173,17 +209,21 @@ if __name__ == '__main__': list_devices() sys.exit(0) + if options.relay or options.command: + if not options.device: + print "Error: Device missing" + if options.device: if not options.relay: print "Error: Need to state which relay" sys.exit(1) - if not options.state: + if not options.command: print "Error: Need to specify which relay state" sys.exit(1) cmdarg.device = options.device cmdarg.relay = options.relay - cmdarg.state = options.state + cmdarg.command = options.command set_relay() sys.exit(0) |