Coverage for tropicsquare / config / base.py: 100%
17 statements
« prev ^ index » next coverage.py v7.13.5, created at 2026-03-27 21:24 +0000
« prev ^ index » next coverage.py v7.13.5, created at 2026-03-27 21:24 +0000
1"""Base configuration class for TROPIC01 config objects"""
4class BaseConfig:
5 """Base class for all configuration objects.
7 Configuration objects represent 32-bit hardware registers that control
8 various aspects of TROPIC01 operation. Each config object provides
9 bit-level access to individual configuration fields.
11 Default value is 0xFFFFFFFF (all bits set) to prevent accidental
12 erasure of I-CONFIG memory, where bits can only be changed from 1 to 0
13 (irreversible operation).
15 Attributes:
16 _value: 32-bit integer holding the raw configuration value
17 """
19 def __init__(self, value: int = 0xFFFFFFFF) -> None:
20 """Initialize config object.
22 :param value: 32-bit configuration value (default: 0xFFFFFFFF)
23 """
24 self._value = value
26 @classmethod
27 def from_bytes(cls, data: bytes) -> 'BaseConfig':
28 """Create config object from raw bytes.
30 :param data: 4 bytes in big-endian format
32 :returns: New config object instance
34 :raises ValueError: If data is not exactly 4 bytes
35 """
36 if len(data) != 4:
37 raise ValueError("Expected 4 bytes, got {}".format(len(data)))
38 value = int.from_bytes(data, 'little')
39 return cls(value)
41 def to_bytes(self) -> bytes:
42 """Convert config object to raw bytes.
44 :returns: 4 bytes in big-endian format
45 """
46 return self._value.to_bytes(4, 'little')
48 def to_dict(self) -> dict:
49 """Export configuration fields as dictionary.
51 :returns: Dictionary mapping field names to values
52 :rtype: dict
54 :raises NotImplementedError: Must be implemented by subclasses
55 """
56 raise NotImplementedError("Subclasses must implement to_dict()")
58 def __repr__(self) -> str:
59 """Machine-readable representation."""
60 return "{}(0x{:08x})".format(self.__class__.__name__, self._value)
62 def __str__(self) -> str:
63 """Human-readable representation.
65 Default implementation shows class name and hex value.
66 Subclasses should override for more detailed output.
67 """
68 return "{}(0x{:08x})".format(self.__class__.__name__, self._value)