Skip to content

Commit b3624de

Browse files
authored
Merge pull request #2 from dhalbert/master
use namedtuple for measurement_value
2 parents 504a673 + 72e90cb commit b3624de

File tree

2 files changed

+42
-17
lines changed

2 files changed

+42
-17
lines changed

adafruit_ble_heart_rate.py

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,9 @@
4141
* Adafruit's BLE library: https://github.com/adafruit/Adafruit_CircuitPython_BLE
4242
"""
4343
import struct
44-
import _bleio
44+
from collections import namedtuple
4545

46+
import _bleio
4647
from adafruit_ble.services import Service
4748
from adafruit_ble.uuid import StandardUUID
4849
from adafruit_ble.characteristics import Characteristic, ComplexCharacteristic
@@ -51,6 +52,37 @@
5152
__version__ = "0.0.0-auto.0"
5253
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_BLE_Heart_Rate.git"
5354

55+
HeartRateMeasurementValues = namedtuple(
56+
"HeartRateMeasurementValues",
57+
("heart_rate", "contact", "energy_expended", "rr_intervals"))
58+
"""Namedtuple for measurement values.
59+
60+
.. py:attribute:: HeartRateMeasurementValues.heart_rate
61+
62+
Heart rate (int), in beats per minute.
63+
64+
.. py:attribute:: HeartRateMeasurementValues.contact
65+
66+
``True`` if device is contacting the body, ``False`` if not,
67+
``None`` if device does not support contact detection.
68+
69+
.. py:attribute:: HeartRateMeasurementValues.energy_expended
70+
71+
Energy expended (int), in kilo joules, or ``None`` if no value.
72+
73+
.. py:attribute:: HeartRateMeasurementValues.rr_intervals
74+
75+
Sequence of RR intervals, measuring the time between
76+
beats. Oldest first, in ints that are units of 1024ths of a second.
77+
This sequence will be empty if the device does not report the intervals.
78+
*Caution:* inexpensive heart rate monitors may not measure this
79+
accurately. Do not use for diagnosis.
80+
81+
For example::
82+
83+
bpm = svc.measurement_values.heart_rate
84+
"""
85+
5486
class _HeartRateMeasurement(ComplexCharacteristic):
5587
"""Notify-only characteristic of streaming heart rate data."""
5688
uuid = StandardUUID(0x2A37)
@@ -109,15 +141,10 @@ def __init__(self, service=None):
109141

110142
@property
111143
def measurement_values(self):
112-
"""All the measurement values, as a tuple:
113-
(heart_rate, contact, energy_expended, rr_intervals)
114-
* heart_rate: int (beats per minute)
115-
* contact: True if contacting, False if not, None if unknown
116-
* energy_expended: int (Kilo joules), or None if no value
117-
* rr_intervals: list of RR-intervals, if any,
118-
oldest first, in ints that are1024ths of a second,
119-
120-
Return None if no packet has been read yet.
144+
"""All the measurement values, returned as a HeartRateMeasurementValues
145+
namedtuple.
146+
147+
Return ``None`` if no packet has been read yet.
121148
"""
122149
buf = self._measurement_buf
123150
packet_length = self.heart_rate_measurement.readinto(buf)
@@ -152,7 +179,7 @@ def measurement_values(self):
152179
rr_val = struct.unpack_from("<H", buf, offset)[0]
153180
rr_values.append(rr_val)
154181

155-
return (bpm, contact, energy_expended, rr_values)
182+
return HeartRateMeasurementValues(bpm, contact, energy_expended, rr_values)
156183

157184
@property
158185
def location(self):
@@ -163,6 +190,10 @@ def location(self):
163190
For instance, some armbands are meant to be worn just below the inner elbow,
164191
but that is not a prescribed location. So the sensor will report something
165192
else, such as "Wrist".
193+
194+
Possible values are:
195+
"Other", "Chest", "Wrist", "Finger", "Hand", "Ear Lobe", "Foot", and
196+
"InvalidLocation" (if value returned does not match the specification).
166197
"""
167198

168199
try:

docs/api.rst

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,2 @@
1-
2-
.. If you created a package, create one automodule per module in the package.
3-
4-
.. If your library file(s) are nested in a directory (e.g. /adafruit_foo/foo.py)
5-
.. use this format as the module name: "adafruit_foo.foo"
6-
71
.. automodule:: adafruit_ble_heart_rate
82
:members:

0 commit comments

Comments
 (0)