colmi_r02_client.real_time_hr

This covers commands for starting and stopping the real time heart rate and blood oxygen (SPO2) measurements, and parsing the results

 1"""
 2This covers commands for starting and stopping the real time
 3heart rate and blood oxygen (SPO2) measurements, and parsing the results
 4"""
 5
 6from dataclasses import dataclass
 7
 8from colmi_r02_client.packet import make_packet
 9
10CMD_REAL_TIME_HEART_RATE = 30  # 0x1E
11CMD_START_HEART_RATE = 105  # 0x69
12CMD_STOP_HEART_RATE = 106  # 0x6A
13
14
15START_HEART_RATE_PACKET = make_packet(
16    CMD_START_HEART_RATE,
17    bytearray(b"\x01\x00"),
18)  # why is this backwards?
19CONTINUE_HEART_RATE_PACKET = make_packet(CMD_REAL_TIME_HEART_RATE, bytearray(b"3"))
20STOP_HEART_RATE_PACKET = make_packet(CMD_STOP_HEART_RATE, bytearray(b"\x01\x00\x00"))
21
22START_SPO2_PACKET = make_packet(CMD_START_HEART_RATE, bytearray(b"\x03\x25"))
23STOP_SPO2_PACKET = make_packet(CMD_STOP_HEART_RATE, bytearray(b"\x03\x00\x00"))
24
25
26@dataclass
27class Reading:
28    kind: int
29    """
30    either heart rate or spo2
31
32    TODO make this an enum and figure out which is which
33    """
34
35    value: int
36
37
38@dataclass
39class ReadingError:
40    code: int
41    kind: int
42
43
44def parse_heart_rate(packet: bytearray) -> Reading | ReadingError:
45    """Parses the heart rate and spo2 packets"""
46
47    assert packet[0] == CMD_START_HEART_RATE
48
49    kind = packet[1]
50    error_code = packet[2]
51    if error_code != 0:
52        return ReadingError(kind=kind, code=error_code)
53
54    return Reading(kind=packet[1], value=packet[3])
CMD_REAL_TIME_HEART_RATE = 30
CMD_START_HEART_RATE = 105
CMD_STOP_HEART_RATE = 106
START_HEART_RATE_PACKET = bytearray(b'i\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00j')
CONTINUE_HEART_RATE_PACKET = bytearray(b'\x1e3\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00Q')
STOP_HEART_RATE_PACKET = bytearray(b'j\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00k')
START_SPO2_PACKET = bytearray(b'i\x03%\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x91')
STOP_SPO2_PACKET = bytearray(b'j\x03\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00m')
@dataclass
class Reading:
27@dataclass
28class Reading:
29    kind: int
30    """
31    either heart rate or spo2
32
33    TODO make this an enum and figure out which is which
34    """
35
36    value: int
Reading(kind: int, value: int)
kind: int

either heart rate or spo2

TODO make this an enum and figure out which is which

value: int
@dataclass
class ReadingError:
39@dataclass
40class ReadingError:
41    code: int
42    kind: int
ReadingError(code: int, kind: int)
code: int
kind: int
def parse_heart_rate( packet: bytearray) -> Reading | ReadingError:
45def parse_heart_rate(packet: bytearray) -> Reading | ReadingError:
46    """Parses the heart rate and spo2 packets"""
47
48    assert packet[0] == CMD_START_HEART_RATE
49
50    kind = packet[1]
51    error_code = packet[2]
52    if error_code != 0:
53        return ReadingError(kind=kind, code=error_code)
54
55    return Reading(kind=packet[1], value=packet[3])

Parses the heart rate and spo2 packets