Skip to content

Commit 9b62790

Browse files
committed
feat(callback): add set_callback method
1 parent 548b8d0 commit 9b62790

File tree

2 files changed

+25
-13
lines changed

2 files changed

+25
-13
lines changed

examples/polar_h10.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,8 @@ async def main():
3737
# Inspect the device details
3838
inspect(device)
3939

40-
def heartrate_callback(data: HRData):
41-
console.print(f"[bold green]Received Data:[/bold green] {data}")
42-
43-
def data_callback(data: Union[ECGData, ACCData]):
44-
"""
45-
Callback function to handle incoming data from the Polar device.
46-
47-
Args:
48-
data (Union[ECGData, ACCData]): The data received from the Polar device.
49-
"""
50-
console.print(f"[bold green]Received Data:[/bold green] {data}")
51-
5240
# Establish connection to the Polar device
53-
async with PolarDevice(device, data_callback, heartrate_callback) as polar_device:
41+
async with PolarDevice(device) as polar_device:
5442
# Query available features
5543
available_features = await polar_device.available_features()
5644
inspect(available_features)
@@ -79,6 +67,20 @@ def data_callback(data: Union[ECGData, ACCData]):
7967
],
8068
)
8169

70+
def heartrate_callback(data: HRData):
71+
console.print(f"[bold green]Received Data:[/bold green] {data}")
72+
73+
def data_callback(data: Union[ECGData, ACCData]):
74+
"""
75+
Callback function to handle incoming data from the Polar device.
76+
77+
Args:
78+
data (Union[ECGData, ACCData]): The data received from the Polar device.
79+
"""
80+
console.print(f"[bold green]Received Data:[/bold green] {data}")
81+
82+
polar_device.set_callback(data_callback, heartrate_callback)
83+
8284
# Start data streams for ECG and ACC
8385
await polar_device.start_stream(ecg_settings)
8486
await polar_device.start_stream(acc_settings)

polar_python/device.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,16 @@ async def stop_heartrate_stream(self) -> None:
148148
f"Failed to stop heart rate stream: {str(e)}"
149149
) from e
150150

151+
def set_callback(
152+
self,
153+
data_callback: Callable[
154+
[Union[constants.ECGData, constants.ACCData]], None
155+
] = None,
156+
heartrate_callback: Callable[[constants.HRData], None] = None,
157+
) -> None:
158+
self._data_callback = data_callback
159+
self._heartrate_callback = heartrate_callback
160+
151161
def _handle_pmd_control(
152162
self, sender: BleakGATTCharacteristic, data: bytearray
153163
) -> None:

0 commit comments

Comments
 (0)