1#!/usr/bin/env python3.4
2#
3#   Copyright 2016 - The Android Open Source Project
4#
5#   Licensed under the Apache License, Version 2.0 (the "License");
6#   you may not use this file except in compliance with the License.
7#   You may obtain a copy of the License at
8#
9#       http://www.apache.org/licenses/LICENSE-2.0
10#
11#   Unless required by applicable law or agreed to in writing, software
12#   distributed under the License is distributed on an "AS IS" BASIS,
13#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14#   See the License for the specific language governing permissions and
15#   limitations under the License.
16
17"""Interface for a USB-connected Monsoon power meter
18(http://msoon.com/LabEquipment/PowerMonitor/).
19"""
20
21import argparse
22import sys
23import time
24import collections
25
26from acts.controllers.monsoon import Monsoon
27
28def main(FLAGS):
29    """Simple command-line interface for Monsoon."""
30    if FLAGS.avg and FLAGS.avg < 0:
31        print("--avg must be greater than 0")
32        return
33
34    mon = Monsoon(serial=int(FLAGS.serialno[0]))
35
36    if FLAGS.voltage is not None:
37        mon.set_voltage(FLAGS.voltage)
38
39    if FLAGS.current is not None:
40        mon.set_max_current(FLAGS.current)
41
42    if FLAGS.status:
43        items = sorted(mon.status.items())
44        print("\n".join(["%s: %s" % item for item in items]))
45
46    if FLAGS.usbpassthrough:
47        mon.usb(FLAGS.usbpassthrough)
48
49    if FLAGS.startcurrent is not None:
50         mon.set_max_init_current(FLAGS.startcurrent)
51
52    if FLAGS.samples:
53        # Have to sleep a bit here for monsoon to be ready to lower the rate of
54        # socket read timeout.
55        time.sleep(1)
56        result = mon.take_samples(FLAGS.hz, FLAGS.samples,
57            sample_offset=FLAGS.offset, live=True)
58        print(repr(result))
59
60if __name__ == '__main__':
61    parser = argparse.ArgumentParser(description=("This is a python utility "
62                 "tool to control monsoon power measurement boxes."))
63    parser.add_argument("--status", action="store_true",
64        help="Print power meter status.")
65    parser.add_argument("-avg", "--avg", type=int, default=0,
66        help="Also report average over last n data points.")
67    parser.add_argument("-v", "--voltage", type=float,
68        help="Set output voltage (0 for off)")
69    parser.add_argument("-c", "--current", type=float,
70        help="Set max output current.")
71    parser.add_argument("-sc", "--startcurrent", type=float,
72        help="Set max power-up/inital current.")
73    parser.add_argument("-usb", "--usbpassthrough", choices=("on", "off",
74        "auto"), help="USB control (on, off, auto).")
75    parser.add_argument("-sp", "--samples", type=int,
76        help="Collect and print this many samples")
77    parser.add_argument("-hz", "--hz", type=int,
78        help="Sample this many times per second.")
79    parser.add_argument("-d", "--device", help="Use this /dev/ttyACM... file.")
80    parser.add_argument("-sn", "--serialno", type=int, nargs=1, required=True,
81        help="The serial number of the Monsoon to use.")
82    parser.add_argument("--offset", type=int, nargs='?', default=0,
83        help="The number of samples to discard when calculating average.")
84    parser.add_argument("-r", "--ramp", action="store_true", help=("Gradually "
85        "increase voltage to prevent tripping Monsoon overvoltage"))
86    args = parser.parse_args()
87    main(args)
88