Changeset 160 for powerbar


Ignore:
Timestamp:
Jul 16, 2010, 1:06:30 PM (14 years ago)
Author:
Rick van der Zwet
Message:

Pinball test to test all ports in some kind of pinball style foo

File:
1 edited

Legend:

Unmodified
Added
Removed
  • powerbar/serial-npm4000.py

    r159 r160  
    127127  return int(bin(number)[2:].zfill(width)[::-1],2)
    128128
    129 #send_command(ser, login_line, 5)
    130 # Note: you will need to wait at least timeout * 24 to complete
    131 #ser.timeout = 13
    132 #send_command(ser, allon_line, 6)
    133 #send_command(ser, alloff_line,6)
    134 
    135 #send_command(ser, power_on_interval_05_line,6)
    136 #send_command(ser,port_on_cmd(24),6)
    137 #send_command(ser,port_off_cmd(24),6)
    138 #retval = send_command(ser, refresh_line, 42)
    139 #print get_port_status(retval)
    140 #while True:
    141 #  port_number = random.randint(1,24)
    142 #  print port_number
    143 #  send_command(ser, login_line, 5)
    144 #  send_command(ser, port_on_cmd(port_number),6)
    145 #  send_command(ser, login_line, 5)
    146 #  send_command(ser, port_off_cmd(port_number),6)
    147129
    148130# Login cycle
     
    213195  return send_command('port_off', port_to_hex(port))
    214196
     197def action_toggle_port(port):
     198  """ Toggle port state """
     199  if get_port_status(port):
     200    action_port_off(port)
     201  else:
     202    action_port_on(port)
     203
    215204def get_ports_status():
    216205   global ports_status, port_status_synced, ports_ampere, grid_status
     
    287276
    288277def usage(msg="",exitcode=None):
    289     print """%s
    290 Usage %s arguments
     278    if msg:
     279      msg = "[ERROR] %s" % msg
     280    print """%(msg)s
     281Usage %(argv)s arguments
    291282Version: $Id: npm4000.py 750 2009-09-27 14:34:55Z rick $
    292283
     
    296287  [-d|--debug]                  Print extra communication output
    297288  [-r|--raw]                    Status(es) is bits like output
    298   --serialport=<path>           Serial Port to connect to [%s]
    299   --password=<hex>              Password to use in hex notation [%s]
    300   --addresscode=<hex>           Internal device number in hex notation [%s]
    301   --delay=<int>                 Delay used between port operations [%s]
    302   --baudrate=<int>              Bautrate used for communication (19200,9600) [%s]
     289  --serialport=<path>           Serial Port to connect to [%(serialport)s]
     290  --password=<hex>              Password to use in hex notation [%(password)s]
     291  --addresscode=<hex>           Internal device number in hex notation [%(addresscode)s]
     292  --delay=<int>                 Delay used between port operations [%(delay)s]
     293  --baudrate=<int>              Bautrate used for communication (19200,9600) [%(baudrate)s]
    303294  [-s <port>|--status=<port>]   Current port(s) configuration
    304295  [-t <port>|--toggle=<port>]   Toggle port(s)
     
    310301  --changeaddresscode=          Change addresscode [TODO: Implement]
    311302  --changetimerdelay=           Change internal timer delay [TODO: Implement]
     303  --pinballtest=<int>           Randomly toggle ports for number of times]
     304  [-p <port>|--port=<port>]     Ports needed to be used
    312305
    313306Note: [TODO: Implement] bit codes are in the source code, feel free to drop me
     
    318311  Actual value of port          A1,..,A8,B1,..,B8,C1,..,C8
    319312  All ports                     all
    320     """ % (msg, sys.argv[0], opt_serial_port, hex(opt_password), hex(opt_address_code), opt_delay, opt_baudrate)
    321     if exitcode:
     313
     314%(msg)s
     315    """ % { 'argv' : sys.argv[0],
     316            'msg' : msg,
     317            'serialport' : opt_serial_port,
     318            'password' : opt_password,
     319            'addresscode' : opt_address_code,
     320            'delay' : opt_delay,
     321            'baudrate' : opt_baudrate,
     322           }
     323    if exitcode != None:
    322324        sys.exit(exitcode)   
    323325
    324326def main():
    325     global DEBUG, ser, opt_serial_port, opt_password, opt_address_code, opt_baudrate, opt_delay
     327    global DEBUG, ser, opt_serial_port, opt_password, opt_address_code, opt_baudrate, opt_delay, opt_pinballtest
    326328    try:
    327329        opts, args = getopt.getopt(sys.argv[1:],
    328             "adf:s:t:ro:v",
    329              ["ampere", "debug", "delay=", "verbose", "serialport=", "password=",
    330              "addresscode=","toggle=","off=", "on=", "status=", "buadrate=", "raw="])
     330            "adhf:s:t:ro:p:v",
     331             ["ampere", "debug", "delay=", "help", "verbose", "serialport=", "port=", "password=",
     332             "addresscode=","toggle=","off=", "on=", "status=", "buadrate=", "raw=", "pinballtest="])
    331333    except getopt.GetoptError, err:
    332334        usage(str(err),2)
     
    336338    opt_raw = False
    337339    opt_ampere = False
     340    opt_pinballtest = None
    338341    for o, a in opts:
    339342        debug("%s : %s" % (o, a))
     
    350353        elif o in ["--password"]:
    351354            opt_passwd = int(a,16)
     355        elif o in ["-p","--port"]:
     356            opt_port = a
     357        elif o in ["--pinballtest"]:
     358            opt_action = 'pinballtest'
     359            opt_pinballtest = int(a)
    352360        elif o in ["--buadrate"]:
    353361            opt_baudrate = a
     
    372380
    373381    if (opt_port == None):
    374         usage("[ERROR] No port defined",2)
     382        usage("No port defined",2)
    375383    elif (opt_action == None):
    376         usage("[ERROR] No action defined",2)
     384        usage("No action defined",2)
    377385
    378386
     
    394402    debug(serial)
    395403
     404    if opt_pinballtest:
     405      for count in range(0,opt_pinballtest):
     406        port = random.choice(ports)
     407        print "[%04i] Toggle port %02i" % (count, port)
     408        action_toggle_port(port)
     409        # Backoff time
     410        time.sleep(opt_delay)
     411      sys.exit(0)
     412
    396413    # Status needs real integers, hack
    397414    for port in ports:
     
    405422          print "Port %02i : %s %s" % (port, get_port_status(port), ampere_str)
    406423      elif opt_action == "toggle":
    407         if get_port_status(port):
    408           action_port_off(port)
    409         else:
    410           action_port_on(port)
     424          action_toggle_port(port)
    411425      elif opt_action == "on":
    412426          action_port_on(port)
Note: See TracChangeset for help on using the changeset viewer.