From 81082d00444d3bfd644f2417ef5c1a34d9569a28 Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Fri, 16 Oct 2020 13:58:20 +0200 Subject: Code aus Lennarts BA-repo --- lib/lennart/SigrokAPIInterface.py | 152 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 152 insertions(+) create mode 100644 lib/lennart/SigrokAPIInterface.py (limited to 'lib/lennart/SigrokAPIInterface.py') diff --git a/lib/lennart/SigrokAPIInterface.py b/lib/lennart/SigrokAPIInterface.py new file mode 100644 index 0000000..a8765ba --- /dev/null +++ b/lib/lennart/SigrokAPIInterface.py @@ -0,0 +1,152 @@ +import time + +from dfatool.lennart.SigrokInterface import SigrokInterface + +import sigrok.core as sr +from sigrok.core.classes import * + +from util.ByteHelper import ByteHelper + + +class SigrokAPIInterface(SigrokInterface): + def datafeed_changes(self, device, packet): + """ + Callback type with changes analysis + :param device: device object + :param packet: data (String with binary data) + """ + data = ByteHelper.rawbytes(self.output.receive(packet)) + if data: + # only using every second byte, + # because only every second contains the useful information. + for x in data[1::2]: + self.analyzeData(x) + + def datafeed_in_all(self, device, packet): + """ + Callback type which writes all data into the array + :param device: device object + :param packet: data (String with binary data) + """ + data = ByteHelper.rawbytes(self.output.receive(packet)) + if data: + # only using every second byte, + # because only every second contains the useful information. + self.all_data += data[1::2] + + def datafeed_file(self, device, packet): + """ + Callback type which writes all data into a file + :param device: device object + :param packet: data (String with binary data) + """ + data = ByteHelper.rawbytes(self.output.receive(packet)) + if data: + # only using every second byte, + # because only every second contains the useful information. + for x in data[1::2]: + self.file.write(str(x) + "\n") + + def __init__( + self, + driver="fx2lafw", + sample_rate=100_000, + sample_count=1_000_000, + debug_output=False, + used_datafeed=datafeed_changes, + fake=False, + ): + """ + + :param driver: Driver that should be used + :param sample_rate: The sample rate of the Logic analyzer + :param sample_count: The sample count of the Logic analyzer + :param debug_output: Should be true if output should be displayed to user + :param used_datafeed: one of the datafeeds above, user later as callback. + :param fake: + """ + super(SigrokAPIInterface, self).__init__(sample_rate, sample_count) + if fake: + raise NotImplementedError("Not implemented!") + self.used_datafeed = used_datafeed + + self.debug_output = debug_output + self.session = None + + def forceStopMeasure(self): + """ + Force stopping the measurement + :return: None + """ + self.session.stop() + + def runMeasure(self): + """ + Start the Measurement and set all settings + """ + context = sr.Context_create() + + devs = context.drivers[self.driver].scan() + # print(devs) + if len(devs) == 0: + raise RuntimeError("No device with that driver found!") + sigrokDevice = devs[0] + if len(devs) > 1: + raise Warning( + "Attention! Multiple devices with that driver found! Using ", + sigrokDevice.connection_id(), + ) + + sigrokDevice.open() + sigrokDevice.config_set(ConfigKey.LIMIT_SAMPLES, self.sample_count) + sigrokDevice.config_set(ConfigKey.SAMPLERATE, self.sample_rate) + + enabled_channels = ["D1"] + for channel in sigrokDevice.channels: + channel.enabled = channel.name in enabled_channels + + self.session = context.create_session() + self.session.add_device(sigrokDevice) + self.session.start() + + self.output = context.output_formats["binary"].create_output(sigrokDevice) + + print(context.output_formats) + self.all_data = b"" + + def datafeed(device, packet): + self.used_datafeed(self, device, packet) + + self.session.add_datafeed_callback(datafeed) + time_running = time.time() + self.session.run() + total_time = time.time() - time_running + print( + "Used time: ", + total_time * 1_000_000, + "µs | sample/s: ", + self.sample_count / (total_time), + "Hz ", + ) + self.session.stop() + + if self.debug_output: + # if self.used_datafeed == self.datafeed_in_change: + if True: + changes = [x / self.sample_rate for x in self.changes] + print(changes) + is_on = self.start == 0xFF + print("0", " - ", changes[0], " # Pin ", "HIGH" if is_on else "LOW") + for x in range(len(changes) - 1): + is_on = not is_on + print( + changes[x], + " - ", + changes[x + 1], + " / ", + changes[x + 1] - changes[x], + " # Pin ", + "HIGH" if is_on else "LOW", + ) + elif self.used_datafeed == self.datafeed_in_all: + print(self.all_data) -- cgit v1.2.3 From 675032e99a02a1d1157305eb4e200b373a196f2a Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Mon, 19 Oct 2020 11:47:04 +0200 Subject: Sigrok: Continuous measurement; fix sigrok-cli termination --- lib/lennart/SigrokAPIInterface.py | 9 ++------- lib/lennart/SigrokCLIInterface.py | 36 ++++++++++++++++++++++-------------- lib/lennart/SigrokInterface.py | 6 +----- lib/runner.py | 4 +--- 4 files changed, 26 insertions(+), 29 deletions(-) (limited to 'lib/lennart/SigrokAPIInterface.py') diff --git a/lib/lennart/SigrokAPIInterface.py b/lib/lennart/SigrokAPIInterface.py index a8765ba..a2c087a 100644 --- a/lib/lennart/SigrokAPIInterface.py +++ b/lib/lennart/SigrokAPIInterface.py @@ -51,7 +51,6 @@ class SigrokAPIInterface(SigrokInterface): self, driver="fx2lafw", sample_rate=100_000, - sample_count=1_000_000, debug_output=False, used_datafeed=datafeed_changes, fake=False, @@ -60,12 +59,11 @@ class SigrokAPIInterface(SigrokInterface): :param driver: Driver that should be used :param sample_rate: The sample rate of the Logic analyzer - :param sample_count: The sample count of the Logic analyzer :param debug_output: Should be true if output should be displayed to user :param used_datafeed: one of the datafeeds above, user later as callback. :param fake: """ - super(SigrokAPIInterface, self).__init__(sample_rate, sample_count) + super(SigrokAPIInterface, self).__init__(sample_rate) if fake: raise NotImplementedError("Not implemented!") self.used_datafeed = used_datafeed @@ -98,7 +96,6 @@ class SigrokAPIInterface(SigrokInterface): ) sigrokDevice.open() - sigrokDevice.config_set(ConfigKey.LIMIT_SAMPLES, self.sample_count) sigrokDevice.config_set(ConfigKey.SAMPLERATE, self.sample_rate) enabled_channels = ["D1"] @@ -124,9 +121,7 @@ class SigrokAPIInterface(SigrokInterface): print( "Used time: ", total_time * 1_000_000, - "µs | sample/s: ", - self.sample_count / (total_time), - "Hz ", + "µs", ) self.session.stop() diff --git a/lib/lennart/SigrokCLIInterface.py b/lib/lennart/SigrokCLIInterface.py index 873b226..d7347ca 100644 --- a/lib/lennart/SigrokCLIInterface.py +++ b/lib/lennart/SigrokCLIInterface.py @@ -9,7 +9,6 @@ class SigrokCLIInterface(SigrokInterface): self, bin_temp_file="temp/out.bin", sample_rate=100_000, - sample_count=1_000_000, fake=False, ): """ @@ -17,10 +16,9 @@ class SigrokCLIInterface(SigrokInterface): :param bin_temp_file: temporary file for binary output :param sample_rate: The sample rate of the Logic analyzer - :param sample_count: The sample count of the Logic analyzer :param fake: if it should use existing data """ - super(SigrokCLIInterface, self).__init__(sample_rate, sample_count) + super(SigrokCLIInterface, self).__init__(sample_rate) self.fake = fake self.bin_temp_file = bin_temp_file self.sigrok_cli_thread = None @@ -30,13 +28,15 @@ class SigrokCLIInterface(SigrokInterface): Force stopping measure, sometimes needs pkill for killing definitly :return: None """ - self.sigrok_cli_thread.send_signal(subprocess.signal.SIGKILL) - stdout, stderr = self.sigrok_cli_thread.communicate(timeout=15) - time.sleep(5) - # TODO not nice solution, make better - import os + self.sigrok_cli_thread.terminate() - os.system("pkill -f sigrok") + try: + self.sigrok_cli_thread.wait(timeout=10) + except subprocess.TimeoutExpired: + logger.warning("sigrok-cli has not stopped. Killing it.") + self.sigrok_cli_thread.kill() + + self.sigrok_cli_thread.communicate() self.runOpenAnalyze() def runMeasure(self): @@ -51,11 +51,19 @@ class SigrokCLIInterface(SigrokInterface): """ starts the measurement, not waiting for done """ - shellcommand = ( - 'sigrok-cli --output-file %s --output-format binary --samples %s -d %s --config "samplerate=%s Hz"' - % (self.bin_temp_file, self.sample_count, self.driver, self.sample_rate) - ) - self.sigrok_cli_thread = subprocess.Popen(shellcommand, shell=True) + shellcommand = [ + "sigrok-cli", + "--output-file", + self.bin_temp_file, + "--output-format", + "binary", + "--continuous", + "-d", + self.driver, + "--config", + f"samplerate={self.sample_rate} Hz", + ] + self.sigrok_cli_thread = subprocess.Popen(shellcommand) def waitForAsynchronousMeasure(self): """ diff --git a/lib/lennart/SigrokInterface.py b/lib/lennart/SigrokInterface.py index 555a25f..a5eaffc 100644 --- a/lib/lennart/SigrokInterface.py +++ b/lib/lennart/SigrokInterface.py @@ -103,18 +103,14 @@ class SigrokResult: class SigrokInterface(DataInterface): - def __init__( - self, sample_rate, sample_count, driver="fx2lafw", filename="temp/sigrok.log" - ): + def __init__(self, sample_rate, driver="fx2lafw", filename="temp/sigrok.log"): """ :param sample_rate: Samplerate of the Logic Analyzer - :param sample_count: Count of samples :param driver: for many Logic Analyzer from Saleae the "fx2lafw" should be working :param filename: temporary file name """ # options - self.sample_count = sample_count self.sample_rate = sample_rate self.file = open(filename, "w+") self.driver = driver diff --git a/lib/runner.py b/lib/runner.py index 0f44e97..72d222d 100644 --- a/lib/runner.py +++ b/lib/runner.py @@ -189,14 +189,12 @@ class EnergyTraceLogicAnalyzerMonitor(EnergyTraceMonitor): def __init__(self, port: str, baud: int, callback=None, voltage=3.3): super().__init__(port=port, baud=baud, callback=callback, voltage=voltage) - # TODO Max length - options = {"length": 90, "fake": False, "sample_rate": 1_000_000} + options = {"fake": False, "sample_rate": 1_000_000} self.log_file = "logic_output_log_%s.json" % (time.strftime("%Y%m%d-%H%M%S")) # Initialization of Interfaces self.sig = SigrokCLIInterface( sample_rate=options["sample_rate"], - sample_count=options["length"] * options["sample_rate"], fake=options["fake"], ) -- cgit v1.2.3 From f308a519edecd8ee92f2fe18552620a569f48d3b Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Mon, 19 Oct 2020 11:47:53 +0200 Subject: debug log --- lib/lennart/DataProcessor.py | 17 ++++++++++++++--- lib/lennart/EnergyInterface.py | 3 +++ lib/lennart/SigrokAPIInterface.py | 3 +++ lib/lennart/SigrokCLIInterface.py | 3 +++ lib/lennart/SigrokInterface.py | 3 +++ 5 files changed, 26 insertions(+), 3 deletions(-) (limited to 'lib/lennart/SigrokAPIInterface.py') diff --git a/lib/lennart/DataProcessor.py b/lib/lennart/DataProcessor.py index df3f41c..58cc705 100644 --- a/lib/lennart/DataProcessor.py +++ b/lib/lennart/DataProcessor.py @@ -1,3 +1,8 @@ +import logging + +logger = logging.getLogger(__name__) + + class DataProcessor: def __init__(self, sync_data, energy_data): """ @@ -84,7 +89,7 @@ class DataProcessor: (sync_start / 1_000_000, pre_outliers_ts / 1_000_000) ) - # print("SYNC SPOTS: ", datasync_timestamps) + logger.debug(f"Synchronization areas: {datasync_timestamps}") # print(time_stamp_data[2]) start_offset = datasync_timestamps[0][1] - time_stamp_data[2] @@ -92,9 +97,15 @@ class DataProcessor: end_offset = datasync_timestamps[-2][0] - (time_stamp_data[-8] + start_offset) end_timestamp = datasync_timestamps[-2][0] - print(start_timestamp, end_timestamp) + logger.debug( + f"Measurement area: LA timestamp range [{start_timestamp}, {end_timestamp}]" + ) + logger.debug(f"Start/End offsets: {start_offset} / {end_offset}") - # print(start_offset, start_timestamp, end_offset, end_timestamp) + if end_offset > 10: + logger.warning( + f"synchronization end_offset == {end_offset}. It should be no more than a few seconds." + ) with_offset = self.addOffset(time_stamp_data, start_offset) diff --git a/lib/lennart/EnergyInterface.py b/lib/lennart/EnergyInterface.py index 2b23667..19aae84 100644 --- a/lib/lennart/EnergyInterface.py +++ b/lib/lennart/EnergyInterface.py @@ -2,6 +2,9 @@ import re import subprocess from dfatool.lennart.DataInterface import DataInterface +import logging + +logger = logging.getLogger(__name__) class EnergyInterface(DataInterface): diff --git a/lib/lennart/SigrokAPIInterface.py b/lib/lennart/SigrokAPIInterface.py index a2c087a..44da678 100644 --- a/lib/lennart/SigrokAPIInterface.py +++ b/lib/lennart/SigrokAPIInterface.py @@ -6,6 +6,9 @@ import sigrok.core as sr from sigrok.core.classes import * from util.ByteHelper import ByteHelper +import logging + +logger = logging.getLogger(__name__) class SigrokAPIInterface(SigrokInterface): diff --git a/lib/lennart/SigrokCLIInterface.py b/lib/lennart/SigrokCLIInterface.py index d7347ca..b28a8a9 100644 --- a/lib/lennart/SigrokCLIInterface.py +++ b/lib/lennart/SigrokCLIInterface.py @@ -2,6 +2,9 @@ import subprocess import time from dfatool.lennart.SigrokInterface import SigrokInterface +import logging + +logger = logging.getLogger(__name__) class SigrokCLIInterface(SigrokInterface): diff --git a/lib/lennart/SigrokInterface.py b/lib/lennart/SigrokInterface.py index a5eaffc..1733b68 100644 --- a/lib/lennart/SigrokInterface.py +++ b/lib/lennart/SigrokInterface.py @@ -1,6 +1,9 @@ import json from dfatool.lennart.DataInterface import DataInterface +import logging + +logger = logging.getLogger(__name__) # Adding additional parsing functionality -- cgit v1.2.3