Index: powerbar/serial-npm4000.py
===================================================================
--- powerbar/serial-npm4000.py	(revision 159)
+++ powerbar/serial-npm4000.py	(revision 160)
@@ -127,22 +127,4 @@
   return int(bin(number)[2:].zfill(width)[::-1],2)
 
-#send_command(ser, login_line, 5)
-# Note: you will need to wait at least timeout * 24 to complete
-#ser.timeout = 13
-#send_command(ser, allon_line, 6)
-#send_command(ser, alloff_line,6)
-
-#send_command(ser, power_on_interval_05_line,6)
-#send_command(ser,port_on_cmd(24),6)
-#send_command(ser,port_off_cmd(24),6)
-#retval = send_command(ser, refresh_line, 42)
-#print get_port_status(retval)
-#while True:
-#  port_number = random.randint(1,24)
-#  print port_number
-#  send_command(ser, login_line, 5)
-#  send_command(ser, port_on_cmd(port_number),6)
-#  send_command(ser, login_line, 5)
-#  send_command(ser, port_off_cmd(port_number),6)
 
 # Login cycle
@@ -213,4 +195,11 @@
   return send_command('port_off', port_to_hex(port))
 
+def action_toggle_port(port):
+  """ Toggle port state """
+  if get_port_status(port):
+    action_port_off(port)
+  else:
+    action_port_on(port)
+
 def get_ports_status():
    global ports_status, port_status_synced, ports_ampere, grid_status
@@ -287,6 +276,8 @@
 
 def usage(msg="",exitcode=None):
-    print """%s
-Usage %s arguments
+    if msg:
+      msg = "[ERROR] %s" % msg
+    print """%(msg)s
+Usage %(argv)s arguments
 Version: $Id: npm4000.py 750 2009-09-27 14:34:55Z rick $
 
@@ -296,9 +287,9 @@
   [-d|--debug]                  Print extra communication output
   [-r|--raw]                    Status(es) is bits like output
-  --serialport=<path>           Serial Port to connect to [%s]
-  --password=<hex>              Password to use in hex notation [%s]
-  --addresscode=<hex>           Internal device number in hex notation [%s]
-  --delay=<int>			Delay used between port operations [%s]
-  --baudrate=<int>		Bautrate used for communication (19200,9600) [%s]
+  --serialport=<path>           Serial Port to connect to [%(serialport)s]
+  --password=<hex>              Password to use in hex notation [%(password)s]
+  --addresscode=<hex>           Internal device number in hex notation [%(addresscode)s]
+  --delay=<int>			Delay used between port operations [%(delay)s]
+  --baudrate=<int>		Bautrate used for communication (19200,9600) [%(baudrate)s]
   [-s <port>|--status=<port>]   Current port(s) configuration
   [-t <port>|--toggle=<port>]   Toggle port(s)
@@ -310,4 +301,6 @@
   --changeaddresscode=		Change addresscode [TODO: Implement]
   --changetimerdelay=		Change internal timer delay [TODO: Implement]
+  --pinballtest=<int>		Randomly toggle ports for number of times]
+  [-p <port>|--port=<port>]     Ports needed to be used
 
 Note: [TODO: Implement] bit codes are in the source code, feel free to drop me
@@ -318,15 +311,24 @@
   Actual value of port          A1,..,A8,B1,..,B8,C1,..,C8 
   All ports                     all
-    """ % (msg, sys.argv[0], opt_serial_port, hex(opt_password), hex(opt_address_code), opt_delay, opt_baudrate)
-    if exitcode:
+
+%(msg)s
+    """ % { 'argv' : sys.argv[0],
+            'msg' : msg,
+            'serialport' : opt_serial_port,
+            'password' : opt_password,
+            'addresscode' : opt_address_code,
+            'delay' : opt_delay,
+            'baudrate' : opt_baudrate,
+           }
+    if exitcode != None:
         sys.exit(exitcode)   
 
 def main():
-    global DEBUG, ser, opt_serial_port, opt_password, opt_address_code, opt_baudrate, opt_delay
+    global DEBUG, ser, opt_serial_port, opt_password, opt_address_code, opt_baudrate, opt_delay, opt_pinballtest
     try:
         opts, args = getopt.getopt(sys.argv[1:], 
-            "adf:s:t:ro:v", 
-             ["ampere", "debug", "delay=", "verbose", "serialport=", "password=", 
-             "addresscode=","toggle=","off=", "on=", "status=", "buadrate=", "raw="])
+            "adhf:s:t:ro:p:v", 
+             ["ampere", "debug", "delay=", "help", "verbose", "serialport=", "port=", "password=", 
+             "addresscode=","toggle=","off=", "on=", "status=", "buadrate=", "raw=", "pinballtest="])
     except getopt.GetoptError, err:
         usage(str(err),2)
@@ -336,4 +338,5 @@
     opt_raw = False
     opt_ampere = False
+    opt_pinballtest = None
     for o, a in opts:
         debug("%s : %s" % (o, a))
@@ -350,4 +353,9 @@
         elif o in ["--password"]:
             opt_passwd = int(a,16)
+        elif o in ["-p","--port"]:
+            opt_port = a
+        elif o in ["--pinballtest"]:
+            opt_action = 'pinballtest'
+            opt_pinballtest = int(a)
         elif o in ["--buadrate"]:
             opt_baudrate = a
@@ -372,7 +380,7 @@
 
     if (opt_port == None):
-        usage("[ERROR] No port defined",2)
+        usage("No port defined",2)
     elif (opt_action == None):
-        usage("[ERROR] No action defined",2)
+        usage("No action defined",2)
 
 
@@ -394,4 +402,13 @@
     debug(serial)
 
+    if opt_pinballtest:
+      for count in range(0,opt_pinballtest): 
+        port = random.choice(ports)
+        print "[%04i] Toggle port %02i" % (count, port)
+        action_toggle_port(port)
+        # Backoff time
+        time.sleep(opt_delay)
+      sys.exit(0)
+
     # Status needs real integers, hack
     for port in ports:
@@ -405,8 +422,5 @@
           print "Port %02i : %s %s" % (port, get_port_status(port), ampere_str)
       elif opt_action == "toggle":
-        if get_port_status(port):
-          action_port_off(port)
-        else:
-          action_port_on(port)
+          action_toggle_port(port)
       elif opt_action == "on":
           action_port_on(port)
