summaryrefslogtreecommitdiff
path: root/lib/lennart/EnergyInterface.py
blob: 55bf7c1114530a96338b2edb54da3cbb8c5c08f1 (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
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 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)