summaryrefslogtreecommitdiff
path: root/extra/usb_power/convert_power_log_board.py
blob: f5fb7e925dc754f0ddd48c48eed9ef95b59d51b2 (plain)
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
#!/usr/bin/env python
# Copyright 2018 The ChromiumOS Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""
Program to convert sweetberry config to servod config template.
"""

# Note: This is a py2/3 compatible file.

from __future__ import print_function

import json
import os
import sys

from powerlog import Spower  # pylint:disable=import-error


def fetch_records(board_file):
    """Import records from servo_ina file.

    board files are json files, and have a list of tuples with
    the INA data.
    (name, rs, swetberry_num, net_name, channel)

    Args:
      board_file: board file

    Returns:
      list of tuples as described above.
    """
    data = None
    with open(board_file) as f:
        data = json.load(f)
    return data


def write_to_file(file, sweetberry, inas):
    """Writes records of |sweetberry| to |file|
    Args:
      file: file to write to.
      sweetberry: sweetberry type. A or B.
      inas: list of inas read from board file.
    """

    with open(file, "w") as pyfile:

        pyfile.write("inas = [\n")

        for rec in inas:
            if rec["sweetberry"] != sweetberry:
                continue

            # EX : ('sweetberry', 0x40, 'SB_FW_CAM_2P8', 5.0, 1.000, 3, False),
            channel, i2c_addr = Spower.CHMAP[rec["channel"]]
            record = (
                "    ('sweetberry', 0x%02x, '%s', 5.0, %f, %d, 'True')"
                ",\n"
                % (
                    i2c_addr,
                    rec["name"],
                    rec["rs"],
                    channel,
                )
            )
            pyfile.write(record)

        pyfile.write("]\n")


def main(argv):
    if len(argv) != 2:
        print("usage:")
        print(" %s input.board" % argv[0])
        return

    inputf = argv[1]
    basename = os.path.splitext(inputf)[0]

    inas = fetch_records(inputf)

    sweetberry = set(rec["sweetberry"] for rec in inas)

    if len(sweetberry) == 2:
        print(
            "Converting %s to %s and %s"
            % (inputf, basename + "_a.py", basename + "_b.py")
        )
        write_to_file(basename + "_a.py", "A", inas)
        write_to_file(basename + "_b.py", "B", inas)
    else:
        print("Converting %s to %s" % (inputf, basename + ".py"))
        write_to_file(basename + ".py", sweetberry.pop(), inas)


if __name__ == "__main__":
    main(sys.argv)