From d972414ef72d5814ce7ebca9d07dfb185b915435 Mon Sep 17 00:00:00 2001 From: RobertGawron Date: Thu, 2 Jan 2025 20:10:11 +0100 Subject: [PATCH] fix some lints --- .../STM32F103RBTx/device_under_test.py | 22 ++++--- Simulation/FirmwarePCSimulator/simulation.py | 57 +++---------------- Software/Common/Crc32.cpp | 16 +++++- Software/Common/Crc32.hpp | 13 ++++- 4 files changed, 42 insertions(+), 66 deletions(-) diff --git a/Simulation/FirmwarePCSimulator/STM32F103RBTx/device_under_test.py b/Simulation/FirmwarePCSimulator/STM32F103RBTx/device_under_test.py index 881d7d3c..89d86a26 100644 --- a/Simulation/FirmwarePCSimulator/STM32F103RBTx/device_under_test.py +++ b/Simulation/FirmwarePCSimulator/STM32F103RBTx/device_under_test.py @@ -11,7 +11,6 @@ from typing import List, Callable - class SimulationKey(Enum): """Enumeration for simulation key events.""" @@ -42,7 +41,6 @@ def __init__(self) -> None: + os.path.sep + dll_name ) self.dut: ctypes.CDLL = ctypes.CDLL(dll_abs_path) - self._serial_tx_callback_c = None def init(self) -> None: @@ -141,21 +139,21 @@ def register_serial_tx_callback(self, callback: Callable[[List[int], int, int], The function should return an integer (`HAL_StatusTypeDef`). """ # Define the C-compatible callback type - # C: typedef int (*SerialTxCallback)(const uint8_t*, uint16_t, uint32_t); - SerialTxCallbackType = ctypes.CFUNCTYPE( - ctypes.c_int, # return type - ctypes.POINTER(ctypes.c_uint8), # const uint8_t* pData - ctypes.c_uint16, # uint16_t size - ctypes.c_uint32 # uint32_t timeout + # C: typedef int (*serial_tx_callback_type)(const uint8_t*, uint16_t, uint32_t); + serial_tx_callback_type = ctypes.CFUNCTYPE( + ctypes.c_int, # return type + ctypes.POINTER(ctypes.c_uint8), # const uint8_t* pdata + ctypes.c_uint16, # uint16_t size + ctypes.c_uint32 # uint32_t timeout ) - self.dut.LibWrapper_RegisterSerialTxCallback.argtypes = [SerialTxCallbackType] + self.dut.LibWrapper_RegisterSerialTxCallback.argtypes = [serial_tx_callback_type] self.dut.LibWrapper_RegisterSerialTxCallback.restype = None # Wrap the Python callback - def wrapper(pData, size, timeout): - data = [pData[i] for i in range(size)] # Convert to Python list + def wrapper(p_data, size, timeout): + data = [p_data[i] for i in range(size)] # Convert to Python list return callback(data, size, timeout) - self._serial_tx_callback_c = SerialTxCallbackType(wrapper) + self._serial_tx_callback_c = serial_tx_callback_type(wrapper) self.dut.LibWrapper_RegisterSerialTxCallback(self._serial_tx_callback_c) diff --git a/Simulation/FirmwarePCSimulator/simulation.py b/Simulation/FirmwarePCSimulator/simulation.py index 9448a8f0..1f107675 100644 --- a/Simulation/FirmwarePCSimulator/simulation.py +++ b/Simulation/FirmwarePCSimulator/simulation.py @@ -10,8 +10,6 @@ import threading import time -import datetime -import random from typing import Tuple from enum import Enum @@ -21,11 +19,7 @@ class SimulationKey(Enum): - """ - Enumeration for simulation key events. - - Defines key mappings for UP, DOWN, LEFT, and RIGHT keys. - """ + """Enumeration for simulation key events.""" UP = 0 DOWN = 1 @@ -35,11 +29,7 @@ class SimulationKey(Enum): class GPIOID(Enum): - """ - Enumeration for GPIO identifiers. - - Maps GPIO pins to their corresponding identifiers. - """ + """Enumeration for GPIO identifiers.""" GPIO1 = 13 GPIO2 = 2 @@ -48,15 +38,7 @@ class GPIOID(Enum): class Simulation: - """ - Simulation class manages the interaction between STM32 and ESP8266 simulations. - - This class provides methods to initialize, control, and interact with - the device-under-test (DUT) simulations. It handles tasks such as - starting and stopping the firmware, managing periodic ticks, updating - pulse counters, and retrieving display information. The class also - facilitates communication via UART and GPIO callbacks. - """ + """Simulation class manages the interaction between STM32 and ESP8266 simulations.""" def __init__(self): """ @@ -98,7 +80,7 @@ def start_firmware(self) -> None: self.esp8266.register_uart0_tx_callback(self.esp8266_uart0_tx_callback) self.esp8266.register_gpio_state_callback(self.my_gpio_state_callback) - self.stm32.register_serial_tx_callback(self.STM32F103RBTx_uart_tx_callback) + self.stm32.register_serial_tx_callback(self.stm32_uart_tx_callback) self.stm32.init() self.esp8266.init() @@ -121,11 +103,7 @@ def reload_firmware(self) -> None: self.start_firmware() def update_pulse_counters(self, values) -> None: - """ - Update the pulse counters in the STM32 simulation. - - :param values: List of pulse counter values. - """ + """Update the pulse counters in the STM32 simulation.""" self.stm32.update_pulse_counters(values) def get_display_width(self) -> int: @@ -154,21 +132,8 @@ def _run_periodic_tick(self) -> None: This internal method handles periodic updates to simulate device behavior. """ while not self._stop_event.is_set(): - """timestamp = datetime.datetime.now().isoformat(timespec='seconds') + 'Z' - value = round(random.uniform(0.0, 10.0), 2) - data_string = f'MEAS:VOLT:DATA "{timestamp},{value}"' - data_bytes = list(data_string.encode('ascii')) - data_to_send = [len(data_bytes)] + data_bytes - - size = len(data_to_send) - timeout = 1000 - - self.esp8266.uart0_tx(data_to_send, size, timeout) - """ - self.stm32.tick() self.esp8266.tick() - time.sleep(self.tick_interval) def _convert_rgb565_to_rgb8(self, rgb565: int) -> Tuple[int, int, int]: @@ -191,21 +156,17 @@ def key_released(self, key: SimulationKey): """Notify the STM32 simulation of a key release event.""" self.stm32.key_released(key) - def STM32F103RBTx_uart_tx_callback(self, data: list, size: int, timeout: int) -> int: + def stm32_uart_tx_callback(self, data: list, size: int, timeout: int) -> int: """ - Callback function for STM32F103RBTx UART transmission. + Print the data transmitted from STM32F103RBTx UART and send it to ESP8266. - This callback prints the data being transmitted. :param data: List of integers representing the transmitted bytes. :param size: Number of bytes transmitted. :param timeout: Timeout in milliseconds. :return: Always returns 0 (HAL_OK) for success. """ data_string = ''.join(map(chr, data)) # Convert byte list to string - print(f"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@UART TX Callback: Transmitting data: '{data_string}', Size: {size}, Timeout: {timeout}") - + print(f"UART TX Callback: Transmitting data: '{data_string}', Size: {size}, Timeout: {timeout}") self.esp8266.uart0_tx(data, size, timeout) - - - return 0 # HAL_OK equivalent \ No newline at end of file + return 0 # HAL_OK equivalent diff --git a/Software/Common/Crc32.cpp b/Software/Common/Crc32.cpp index 46c8d145..f0fdbecc 100644 --- a/Software/Common/Crc32.cpp +++ b/Software/Common/Crc32.cpp @@ -1,19 +1,29 @@ #include "Crc32.hpp" +#include +#include + namespace Device { + namespace + { + constexpr std::uint32_t INITIAL_CRC = 0xFFFFFFFF; + constexpr std::uint32_t POLYNOMIAL = 0xEDB88320; + constexpr std::size_t BITS_PER_BYTE = 8; + } + std::uint32_t Crc32::compute(const uint8_t *data, std::size_t length) { - std::uint32_t crc = 0xFFFFFFFF; + std::uint32_t crc = INITIAL_CRC; for (std::size_t i = 0; i < length; ++i) { crc ^= data[i]; - for (std::size_t j = 0; j < 8; ++j) + for (std::size_t j = 0; j < BITS_PER_BYTE; ++j) { if (crc & 1) { - crc = (crc >> 1) ^ 0xEDB88320; // Standard polynomial + crc = (crc >> 1) ^ POLYNOMIAL; } else { diff --git a/Software/Common/Crc32.hpp b/Software/Common/Crc32.hpp index 79f045a1..f7eeb552 100644 --- a/Software/Common/Crc32.hpp +++ b/Software/Common/Crc32.hpp @@ -1,15 +1,22 @@ #ifndef Crc32_hpp #define Crc32_hpp -#include // For size_t -#include // For uint8_t +#include +#include namespace Device { class Crc32 { public: - std::uint32_t compute(const uint8_t *data, std::size_t length); + /** + * @brief Computes the CRC32 checksum of the given data. + * + * @param data Pointer to the data buffer. + * @param length Length of the data buffer in bytes. + * @return The computed CRC32 checksum. + */ + static std::uint32_t compute(const uint8_t *data, std::size_t length); }; }