Coverage for src/bluetooth_sig/gatt/characteristics/magnetic_flux_density_2d.py: 100%

26 statements  

« prev     ^ index     » next       coverage.py v7.11.0, created at 2025-10-30 00:10 +0000

1"""Magnetic Flux Density 2D characteristic implementation.""" 

2 

3from __future__ import annotations 

4 

5from ...types.gatt_enums import ValueType 

6from ...types.units import PhysicalUnit 

7from ..context import CharacteristicContext 

8from .base import BaseCharacteristic 

9from .templates import Vector2DData 

10from .utils import DataParser 

11 

12 

13class MagneticFluxDensity2DCharacteristic(BaseCharacteristic): 

14 """Magnetic Flux Density - 2D characteristic (0x2AA0). 

15 

16 org.bluetooth.characteristic.magnetic_flux_density_2d 

17 

18 Magnetic flux density 2D characteristic. 

19 

20 Represents measurements of magnetic flux density for two orthogonal 

21 axes: X and Y. Note that 1 x 10^-7 Tesla equals 0.001 Gauss. 

22 

23 Format: 2 x sint16 (4 bytes total) with 1e-7 Tesla resolution. 

24 """ 

25 

26 _characteristic_name: str | None = "Magnetic Flux Density - 2D" 

27 # Override YAML since decode_value returns structured dict 

28 _manual_value_type: ValueType | str | None = ValueType.STRING # Override since decode_value returns dict 

29 _manual_unit: str | None = PhysicalUnit.TESLA.value # Tesla 

30 

31 _vector_components: list[str] = ["x_axis", "y_axis"] 

32 resolution: float = 1e-7 

33 

34 def decode_value(self, data: bytearray, ctx: CharacteristicContext | None = None) -> Vector2DData: 

35 """Parse 2D magnetic flux density (2 x sint16 with resolution). 

36 

37 Args: 

38 data: Raw bytearray from BLE characteristic. 

39 ctx: Optional CharacteristicContext providing surrounding context (may be None). 

40 

41 Returns: 

42 Vector2DData with x and y axis values in Tesla. 

43 

44 """ 

45 if len(data) < 4: 

46 raise ValueError("Insufficient data for 2D magnetic flux density (need 4 bytes)") 

47 

48 x_raw = DataParser.parse_int16(data, 0, signed=True) 

49 y_raw = DataParser.parse_int16(data, 2, signed=True) 

50 

51 return Vector2DData(x_axis=x_raw * self.resolution, y_axis=y_raw * self.resolution) 

52 

53 def encode_value(self, data: Vector2DData) -> bytearray: 

54 """Encode 2D magnetic flux density.""" 

55 x_raw = int(data.x_axis / self.resolution) 

56 y_raw = int(data.y_axis / self.resolution) 

57 

58 result = bytearray() 

59 result.extend(DataParser.encode_int16(x_raw, signed=True)) 

60 result.extend(DataParser.encode_int16(y_raw, signed=True)) 

61 return result