-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathpacket.h
158 lines (129 loc) · 3.24 KB
/
packet.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
nexus433
Copyright (C) 2018 aquaticus
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <stdint.h>
#include <stdio.h>
#include <cstddef>
/*
* the data is grouped in 9 nibbles
* [id0] [id1] [flags] [temp0] [temp1] [temp2] [const] [humi0] [humi1]
*
* The 8-bit id changes when the battery is changed in the sensor.
* flags are 4 bits B 0 C C, where B is the battery status: 1=OK, 0=LOW
* and CC is the channel: 0=CH1, 1=CH2, 2=CH3
* temp is 12 bit signed scaled by 10
* const is always 1111 (0x0F)
* humidity is 8 bits
*/
class Packet
{
public:
Packet(uint64_t raw)
{
m_Raw = raw;
m_FrameCounter = 0;
}
Packet()
{
m_Raw=0;
m_FrameCounter=0;
}
uint16_t GetSenderId() const
{
return GetId() << 8 | GetChannel();
}
uint8_t GetId() const
{
return (m_Raw >> 28) & 0xFF;
}
uint8_t GetChannel() const
{
return (m_Raw >> 24) & 0x03;
}
// Returns temperature * 10
int16_t GetTemperature10() const
{
int16_t t = m_Raw >> 12 & 0x0FFF;
return 0x0800 & t ? 0xF000 | t : t;
}
// Returns temperature as double
double GetTemperature() const
{
return GetTemperature10() / 10.0;
}
uint8_t GetHumidity() const
{
return m_Raw & 0xFF;
}
uint8_t GetConst() const
{
return (m_Raw >> 8) & 0x0F;
}
uint8_t GetBattery() const
{
return (m_Raw >> 27) & 0x01;
}
bool IsValid() const
{
return
GetConst() == 0x0F &&
GetHumidity() <= 100 &&
GetTemperature10() > -400 &&
GetTemperature10() < 600; //arbitrary chosen valid range
}
uint64_t GetRaw() const
{
return m_Raw;
}
void SetRaw(uint64_t packet)
{
m_Raw = packet;
}
uint8_t IncreaseFrameCounter()
{
if( m_FrameCounter < 0xFF )
{
m_FrameCounter++;
}
return m_FrameCounter;
}
void ResetFrameCounter()
{
m_FrameCounter=0;
}
uint8_t GetFrameCounter() const
{
return m_FrameCounter;
}
uint8_t GetQualityPercent() const
{
return ((m_FrameCounter * 100) / 12);
}
void Substitute(uint16_t newId)
{
uint64_t id = newId >> 8;
uint64_t ch = newId & 0x0003;
m_Raw &= ~(0xFFLLU << 28);
m_Raw |= (id << 28 );
m_Raw &= ~(0x03LLU << 24);
m_Raw |= (ch << 24);
}
inline bool operator< (const Packet& rhs) const { return m_Raw < rhs.GetRaw(); }
inline bool operator== (const Packet& rhs) const { return m_Raw == rhs.GetRaw(); }
inline operator uint64_t() const { return m_Raw; }
protected:
uint64_t m_Raw;
uint8_t m_FrameCounter;
};