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
« prev ^ index » next coverage.py v7.13.1, created at 2026-01-11 20:14 +0000
1"""Force characteristic implementation."""
3from __future__ import annotations
5from ..context import CharacteristicContext
6from .base import BaseCharacteristic
7from .utils.data_parser import DataParser
10class ForceCharacteristic(BaseCharacteristic[float]):
11 """Force characteristic (0x2C07).
13 org.bluetooth.characteristic.force
15 The Force characteristic is used to represent the force being applied to an object along a given axis.
16 """
18 expected_length: int = 4 # sint32
20 def _decode_value(self, data: bytearray, ctx: CharacteristicContext | None = None) -> float:
21 """Decode force characteristic.
23 Decodes a 32-bit signed integer representing force in 0.001 N increments
24 per Bluetooth SIG Force characteristic specification.
26 Args:
27 data: Raw bytes from BLE characteristic (exactly 4 bytes, little-endian)
28 ctx: Optional context for parsing (device info, flags, etc.)
30 Returns:
31 Force in Newtons
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
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)