Skip to content

Commit

Permalink
fix some lints
Browse files Browse the repository at this point in the history
  • Loading branch information
RobertGawron committed Jan 2, 2025
1 parent 79c78ec commit d972414
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 66 deletions.
22 changes: 10 additions & 12 deletions Simulation/FirmwarePCSimulator/STM32F103RBTx/device_under_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
from typing import List, Callable



class SimulationKey(Enum):

"""Enumeration for simulation key events."""
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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)
57 changes: 9 additions & 48 deletions Simulation/FirmwarePCSimulator/simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@

import threading
import time
import datetime
import random
from typing import Tuple
from enum import Enum

Expand All @@ -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
Expand All @@ -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
Expand All @@ -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):
"""
Expand Down Expand Up @@ -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()
Expand All @@ -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:
Expand Down Expand Up @@ -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]:
Expand All @@ -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
return 0 # HAL_OK equivalent
16 changes: 13 additions & 3 deletions Software/Common/Crc32.cpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@

#include "Crc32.hpp"

#include <cstdint>
#include <cstddef>

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
{
Expand Down
13 changes: 10 additions & 3 deletions Software/Common/Crc32.hpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
#ifndef Crc32_hpp
#define Crc32_hpp

#include <cstddef> // For size_t
#include <cstdint> // For uint8_t
#include <cstdint>
#include <cstddef>

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);
};
}

Expand Down

0 comments on commit d972414

Please sign in to comment.