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

1"""Base configuration class for TROPIC01 config objects""" 

2 

3 

4class BaseConfig: 

5 """Base class for all configuration objects. 

6 

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. 

10 

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). 

14 

15 Attributes: 

16 _value: 32-bit integer holding the raw configuration value 

17 """ 

18 

19 def __init__(self, value: int = 0xFFFFFFFF) -> None: 

20 """Initialize config object. 

21 

22 :param value: 32-bit configuration value (default: 0xFFFFFFFF) 

23 """ 

24 self._value = value 

25 

26 @classmethod 

27 def from_bytes(cls, data: bytes) -> 'BaseConfig': 

28 """Create config object from raw bytes. 

29 

30 :param data: 4 bytes in big-endian format 

31 

32 :returns: New config object instance 

33 

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) 

40 

41 def to_bytes(self) -> bytes: 

42 """Convert config object to raw bytes. 

43 

44 :returns: 4 bytes in big-endian format 

45 """ 

46 return self._value.to_bytes(4, 'little') 

47 

48 def to_dict(self) -> dict: 

49 """Export configuration fields as dictionary. 

50 

51 :returns: Dictionary mapping field names to values 

52 :rtype: dict 

53 

54 :raises NotImplementedError: Must be implemented by subclasses 

55 """ 

56 raise NotImplementedError("Subclasses must implement to_dict()") 

57 

58 def __repr__(self) -> str: 

59 """Machine-readable representation.""" 

60 return "{}(0x{:08x})".format(self.__class__.__name__, self._value) 

61 

62 def __str__(self) -> str: 

63 """Human-readable representation. 

64 

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)