From 14f9966373ce81c1a3a642870000008efeeef35d Mon Sep 17 00:00:00 2001 From: Fabio Battaglia Date: Thu, 22 Aug 2024 20:03:46 +0200 Subject: [PATCH] Use new dpdumperlib to load file into memory --- CHANGELOG.md | 14 +++++++++----- pyproject.toml | 4 ++-- requirements.txt | 2 +- src/dpdumper/frontend.py | 4 +++- src/dpdumper/hl_board_utilities.py | 6 +++--- src/dpdumper/outfile_utilities.py | 15 --------------- 6 files changed, 18 insertions(+), 27 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c516cea..9a6b384 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,24 +1,28 @@ # Changelog Changelog for the dpdumper utility +## [0.3.3] - 2024-08-22 +### Changed +- Depends on dpdumperlib 0.0.2 + ## [0.3.2] - 2024-08-21 -## Added +### Added - Flag to output the Hi-Z mask of every read in binary format -## Changed +### Changed - Use upside-down floor division to calculate number of bytes for data entriess and addresses - Tool description in argsparse ## [0.3.1] - 2024-08-19 -## Changed +### Changed - Depends on dupicolib 0.4.2 - Split out some code in dpdumperlib, now depends on version 0.0.1 -## Removed +### Removed - Example definitions are now moved in the dpdumperlib repo ## [0.3.0] - 2024-08-12 -## Changed +### Changed - Depends on dupicolib 0.4.0 ## [0.2.1] - 2024-08-12 diff --git a/pyproject.toml b/pyproject.toml index e799b1a..6491e20 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = "dpdumper" -version = "0.3.2" +version = "0.3.3" description = "Tool to use the dupico as a dumping device" authors = [ { name = "Fabio Battaglia", email = "hkzlabnet@gmail.com" } @@ -19,7 +19,7 @@ requires-python = ">=3.12" dependencies = [ "pyserial ~= 3.5", "dupicolib >= 0.4.2", - "dpdumperlib >= 0.0.1" + "dpdumperlib >= 0.0.2" ] [project.scripts] diff --git a/requirements.txt b/requirements.txt index 7ab7fb8..2479824 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,3 @@ pyserial>=3.5 dupicolib>=0.4.2 -dpdumperlib>=0.0.1 +dpdumperlib>=0.0.2 diff --git a/src/dpdumper/frontend.py b/src/dpdumper/frontend.py index a8500ad..604bc6a 100644 --- a/src/dpdumper/frontend.py +++ b/src/dpdumper/frontend.py @@ -17,6 +17,7 @@ from dpdumperlib.ic.ic_loader import ICLoader from dpdumperlib.ic.ic_definition import ICDefinition +import dpdumperlib.io.file_utils as FileUtils from dpdumper import __name__, __version__ from dpdumper.dumper_utilities import DumperUtilities @@ -179,7 +180,8 @@ def write_command(ser: serial.Serial, cmd_class: type[HardwareBoardCommands], ic if not skip_note and ic_definition.adapter_notes and bool(ic_definition.adapter_notes.strip()): print_note(ic_definition.adapter_notes) - data_list: list[int] = OutFileUtilities.build_data_list_from_file(inf, ic_definition) + bytes_per_entry: int = -(len(ic_definition.data) // -8) + data_list: list[int] = FileUtils.load_file_data(inf, bytes_per_entry) start_time: float = time.time() HLBoardUtilities.write_ic(ser, cmd_class, ic_definition, data_list) diff --git a/src/dpdumper/hl_board_utilities.py b/src/dpdumper/hl_board_utilities.py index c75ce53..09762f2 100644 --- a/src/dpdumper/hl_board_utilities.py +++ b/src/dpdumper/hl_board_utilities.py @@ -108,13 +108,13 @@ def read_ic(cls, ser: serial.Serial, cmd_class: type[HardwareBoardCommands], ic: @staticmethod def write_ic(ser: serial.Serial, cmd_class: type[HardwareBoardCommands], ic: ICDefinition, data: list[int]) -> None: - data_width: int = int(math.ceil(len(ic.data) / 8.0)) + data_width: int = -(len(ic.data) // -8) addr_combs: int = 1 << len(ic.address) # Calculate the number of addresses that this IC supports _LOGGER.debug(f'write_ic command with definition {ic.name}, IC has {addr_combs} addresses and data width {data_width} bits.') # Check that we have enough data (or not too much) to write - if addr_combs != len(data): - raise ValueError(f'IC definition supports {addr_combs} addresses, but input array has {len(data)}') + if (d_len := len(data)) != addr_combs: + raise ValueError(f'IC definition supports {addr_combs} addresses, but input array has {d_len}') hi_pins_mapped: int = cmd_class.map_value_to_pins(ic.adapter_hi_pins, 0xFFFFFFFFFFFFFFFF) diff --git a/src/dpdumper/outfile_utilities.py b/src/dpdumper/outfile_utilities.py index de67a5b..0d992e4 100644 --- a/src/dpdumper/outfile_utilities.py +++ b/src/dpdumper/outfile_utilities.py @@ -18,21 +18,6 @@ def _bits_iterator(n: int) -> Generator[int, None, None]: yield b n ^= b -def build_data_list_from_file(inf: str, ic: ICDefinition) -> list[int]: - data_list: list[int] = [] - in_content: bytes - with open(inf, 'rb') as f: - in_content = f.read() - - data_width: int = len(ic.data) - # Use upside-down floor division: https://stackoverflow.com/questions/14822184/is-there-a-ceiling-equivalent-of-operator-in-python - bytes_per_entry = -(data_width // -8) - - for block in grouped_iterator(in_content, bytes_per_entry): - data_list.append(int.from_bytes(block, byteorder='big', signed=False)) - - return data_list - def build_binary_array(ic: ICDefinition, elements: list[DataElement], hiz_high: bool = False) -> tuple[bytearray, bytearray, str]: """Builds a binary array out of data read from the IC, and returns it plus the SHA1SUM of the data