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

12 statements  

« prev     ^ index     » next       coverage.py v7.13.1, created at 2026-01-11 20:14 +0000

1"""Force characteristic implementation.""" 

2 

3from __future__ import annotations 

4 

5from ..context import CharacteristicContext 

6from .base import BaseCharacteristic 

7from .utils.data_parser import DataParser 

8 

9 

10class ForceCharacteristic(BaseCharacteristic[float]): 

11 """Force characteristic (0x2C07). 

12 

13 org.bluetooth.characteristic.force 

14 

15 The Force characteristic is used to represent the force being applied to an object along a given axis. 

16 """ 

17 

18 expected_length: int = 4 # sint32 

19 

20 def _decode_value(self, data: bytearray, ctx: CharacteristicContext | None = None) -> float: 

21 """Decode force characteristic. 

22 

23 Decodes a 32-bit signed integer representing force in 0.001 N increments 

24 per Bluetooth SIG Force characteristic specification. 

25 

26 Args: 

27 data: Raw bytes from BLE characteristic (exactly 4 bytes, little-endian) 

28 ctx: Optional context for parsing (device info, flags, etc.) 

29 

30 Returns: 

31 Force in Newtons 

32 

33 Raises: 

34 InsufficientDataError: If data is not exactly 4 bytes 

35 """ 

36 raw_value = DataParser.parse_int32(data, 0, signed=True) 

37 return raw_value * 0.001 

38 

39 def _encode_value(self, data: float) -> bytearray: 

40 """Encode force value.""" 

41 raw_value = int(data / 0.001) 

42 return DataParser.encode_int32(raw_value, signed=True)