Skip to content

Defer packet size #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 4, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions adafruit_ble_heart_rate.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,20 +58,20 @@
)
"""Namedtuple for measurement values.

.. :attribute:: heart_rate:
* `HeartRateMeasurementValues.heart_rate`

Heart rate (int), in beats per minute.

.. :attribute:: contact:
* `HeartRateMeasurementValues.contact`

``True`` if device is contacting the body, ``False`` if not,
``None`` if device does not support contact detection.

.. :attribute:: energy_expended:
* `HeartRateMeasurementValues.energy_expended`

Energy expended (int), in kilo joules, or ``None`` if no value.

.. :attribute:: rr_intervals:
* `HeartRateMeasurementValues.rr_intervals`

Sequence of RR intervals, measuring the time between
beats. Oldest first, in ints that are units of 1024ths of a second.
Expand Down Expand Up @@ -139,9 +139,8 @@ class HeartRateService(Service):

def __init__(self, service=None):
super().__init__(service=service)
self._measurement_buf = bytearray(
self.heart_rate_measurement.packet_size # pylint: disable=no-member
)
# Defer creating buffer until needed.
self._measurement_buf = None

@property
def measurement_values(self):
Expand All @@ -150,6 +149,10 @@ def measurement_values(self):

Return ``None`` if no packet has been read yet.
"""
if self._measurement_buf is None:
self._measurement_buf = bytearray(
self.heart_rate_measurement.packet_size # pylint: disable=no-member
)
buf = self._measurement_buf
packet_length = self.heart_rate_measurement.readinto( # pylint: disable=no-member
buf
Expand Down