This repository has been archived by the owner on Aug 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
PacketReceiver.h
101 lines (85 loc) · 3.29 KB
/
PacketReceiver.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/// PacketReceiver.h
/// This file has call and methos for packet receiver takes the input frames and
/// output the packet measurements on availability
// Copyright (c) 2020 Pez-Globo and the Pufferfish project contributors
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
// either express or implied.
//
// See the License for the specific language governing permissions and
// limitations under the License.
#pragma once
#include <array>
#include "FrameBuffer.h"
#include "FrameReceiver.h"
#include "Types.h"
namespace Pufferfish::Driver::Serial::Nonin {
enum class PacketInputStatus {
output_ready = 0, /// Input is available to read output
waiting, /// Input is wait to read more bytes
frame_loss /// missed one or more frames in previous received packet
};
using Packet = std::array<Frame, packet_size>;
using PacketOutputStatus = Pufferfish::Driver::Serial::Nonin::FrameOutputStatus;
/**
* @brief Inline function to get the SpO2 data
* @param ByteData - Byte of SpO2 Data received from packet
* @return Masked value of SpO2 from input Spo2Data
*/
inline uint16_t get_spo2_data(uint8_t byte_data) {
/* Mask Bit0 to Bit6 for SpO2 data */
return byte_data & ErrorConstants::spo2_missing;
}
/**
* @brief Inline function to get the Heart rate from HR MSB and HR LSB
* @param msbByte - MSB to extract 2 bits
* @param lsbByte - MSB to extract 5 bits
* @return Calculated 9 bit data from MSB and LSB
*/
inline uint16_t get_hr_data(uint8_t msb_byte, uint8_t lsb_byte) {
/* Pack 2 bits of MSB and 6 bits of LSB for 9 bits of heart rate data */
static const uint16_t msb_shift = 7;
static const uint16_t mask_msb = 0x18;
const uint16_t msb =
static_cast<uint16_t>(static_cast<uint16_t>(msb_byte) << msb_shift) & mask_msb;
const uint16_t lsb = static_cast<uint16_t>(lsb_byte) & ErrorConstants::spo2_missing;
return static_cast<uint16_t>(msb | lsb) & ErrorConstants::hr_missing;
}
/*
* PacketReceiver class for
*/
class PacketReceiver {
public:
PacketReceiver() = default;
/**
* @brief Input is called until packet is filled
* @param frame input frame of 5 bytes
* @param frameIndex frame index of a packet
* @return Packet input status on available of packet
*/
PacketInputStatus input(const Frame &frame);
/**
* @brief Output is called after the input status is available to read
* measurements
* @param sensorMeasurements is updated on available of measurements
* @return Packet Output status on available of measurements
*/
PacketOutputStatus output(Sample &sensor_measurements);
private:
Packet packet_data_{};
size_t received_length_ = packet_size;
PacketInputStatus input_status_ = PacketInputStatus::waiting;
};
extern void read_status_byte(
Sample &sensor_measurements, const size_t &frame_index, const uint8_t &byte_value);
bool check_packet_sync(const Frame &frame);
} // namespace Pufferfish::Driver::Serial::Nonin