summaryrefslogtreecommitdiff
path: root/lib/lennart/EnergyInterface.py
blob: 19aae84ebaf53189b1df772ef1ba904a8dc70c20 (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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import re
import subprocess

from dfatool.lennart.DataInterface import DataInterface
import logging

logger = logging.getLogger(__name__)


class EnergyInterface(DataInterface):
    def __init__(
        self,
        duration_seconds=10,
        console_output=False,
        temp_file="temp/energytrace.log",
        fake=False,
    ):
        """
        class is not used in embedded into dfatool.

        :param duration_seconds: seconds the EnergyTrace should be running
        :param console_output: if EnergyTrace output should be printed to the user
        :param temp_file: file path for temporary file
        :param fake: if already existing file should be used
        """
        self.energytrace = None
        self.duration_seconds = duration_seconds
        self.console_output = console_output
        self.temp_file = temp_file
        self.fake = fake

    def runMeasure(self):
        """
        starts the measurement, with waiting for done
        """
        if self.fake:
            return
        self.runMeasureAsynchronously()
        self.waitForAsynchronousMeasure()

    def runMeasureAsynchronously(self):
        """
        starts the measurement, not waiting for done
        """
        if self.fake:
            return
        self.energytrace = subprocess.Popen(
            "msp430-etv --save %s %s %s"
            % (
                self.temp_file,
                self.duration_seconds,
                "" if self.console_output else "> /dev/null",
            ),
            shell=True,
        )
        print(
            "msp430-etv --save %s %s %s"
            % (
                self.temp_file,
                self.duration_seconds,
                "" if self.console_output else "> /dev/null",
            )
        )

    def waitForAsynchronousMeasure(self):
        """
        Wait until is command call is done
        """
        if self.fake:
            return
        self.energytrace.wait()

    def getData(self):
        """
        cleans the string data and creates int list
        :return: list of data, in format [[int,int,int,int], [int,int,int,int], ... ]
        """
        energytrace_log = open(self.temp_file)
        lines = energytrace_log.readlines()[21:]
        data = []
        for line in lines:
            if "MSP430_DisableEnergyTrace" in line:
                break
            else:
                data.append([int(i) for i in line.split()])
        return data

    @classmethod
    def getDataFromString(cls, string, delimiter="\\n"):
        """
        Parsing the data from string

        :param string: input string which will be parsed
        :param delimiter: for normal file its \n
        :return: list of data, in format [[int,int,int,int], [int,int,int,int], ... ]
        """
        lines = string.split(delimiter)[21:]
        data = []
        for line in lines:
            if "MSP430_DisableEnergyTrace" in line:
                break
            else:
                data.append([int(i) for i in line.split()])
        return data

    def setFile(self, path):
        """
        changeing the temporary file

        :param path: file path of new temp file
        :return: None
        """
        self.temp_file = path
        pass

    def forceStopMeasure(self):
        """
        force stops the Measurement, with signals
        :return: None
        """
        self.energytrace.send_signal(subprocess.signal.SIGINT)
        stdout, stderr = self.energytrace.communicate(timeout=15)