-
Notifications
You must be signed in to change notification settings - Fork 2
/
ErriezOregonTHN128Receive.c
337 lines (306 loc) · 8.25 KB
/
ErriezOregonTHN128Receive.c
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/*
* MIT License
*
* Copyright (c) 2020 Erriez
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
/*!
* \file ErriezOregonTHN128Receive.c
* \brief Oregon THN128 433MHz temperature transmit/receive library for Arduino
* \details
* Source: https://github.com/Erriez/ErriezOregonTHN128
* Documentation: https://erriez.github.io/ErriezOregonTHN128
*/
#include <Arduino.h>
#include <avr/interrupt.h>
#include "ErriezOregonTHN128Receive.h"
/*!
* \brief Receive state
*/
typedef enum {
StateSearchSync = 0, /*!< Search for sync */
StateMid0 = 1, /*!< Sample at the middle of a pulse part 1 */
StateMid1 = 2, /*!< Sample at the middle of a pulse part 2 */
StateEnd = 3, /*!< Sample at the end of a pulse to store bit */
StateRxComplete = 4 /*!< Receive complete */
} RxState_t;
/* Static variables */
static uint8_t _rxPinPort;
static uint8_t _rxPinBit;
static uint8_t _rxInt;
static uint32_t _tPulseBegin;
static uint16_t _tPinHigh;
static uint16_t _tPinLow;
static int8_t _rxBit;
static volatile uint32_t _rxData;
static volatile RxState_t _rxState = StateSearchSync;
/* Forward declaration */
void rfPinChange(void);
/*!
* \brief Receive enable
*/
static void rxEnable()
{
/* Enable INTx change interrupt */
attachInterrupt(_rxInt, rfPinChange, CHANGE);
/* Initialize with search for sync state */
_rxState = StateSearchSync;
}
/*!
* \brief Receive disable
*/
static void rxDisable()
{
/* Disable INTx change interrupt */
detachInterrupt(_rxInt);
}
/*!
* \brief Check is pulse duration is within range
* \param tPulse
* Measured pulse length in us
* \param tMin
* Minimum pulse length in us
* \param tMax
* Maximum pulse length in us
* \retval true
* Pulse is in range
* \retval false
* Pulse not in range
*/
static bool isPulseInRange(uint16_t tPulse, uint16_t tMin, uint16_t tMax)
{
/* Check is pulse length between min and max time */
if ((tPulse >= tMin) && (tPulse <= tMax)) {
return true;
} else {
return false;
}
}
/*!
* \brief Find synchronisation
* \retval true
* Sync found
* \retval false
* Sync not found
*/
static bool findSync()
{
/* Read sync pulse */
if (isPulseInRange(_tPinHigh, T_SYNC_H_MIN, T_SYNC_H_MAX)) {
if (isPulseInRange(_tPinLow, T_SYNC_L_MIN_0, T_SYNC_L_MAX_0)) {
_rxData = 0;
_rxState = StateMid1;
_rxBit = 1;
return true;
} else if (isPulseInRange(_tPinLow, T_SYNC_L_MIN_1, T_SYNC_L_MAX_1)) {
_rxData = 0;
_rxState = StateEnd;
_rxBit = 0;
return true;
}
}
return false;
}
/*!
* \brief Store a logical bit 1 or 0
* \param one
* true: Bit 1\n
* false: Bit 0
*/
static void storeBit(bool one)
{
/* Store received bit */
if (one) {
_rxData |= (1UL << _rxBit);
}
/* Check if all 32 data bits are received */
_rxBit++;
if (_rxBit >= 32) {
if (OregonTHN128_CheckCRC(_rxData)) {
_rxState = StateRxComplete;
/* Disable receive */
rxDisable();
} else {
_rxState = StateSearchSync;
}
}
}
/*!
* \brief Handle pulse RF receive pin
*/
static void handlePulse()
{
if (isPulseInRange(_tPinHigh, T_BIT_SHORT_MIN, T_BIT_SHORT_MAX)) {
if (_rxState == StateEnd) {
_rxState = StateMid0;
storeBit(1);
} else if (_rxState == StateMid1) {
_rxState = StateEnd;
} else {
_rxState = StateSearchSync;
}
} else if (isPulseInRange(_tPinHigh, T_BIT_LONG_MIN, T_BIT_LONG_MAX)) {
if (_rxState == StateMid1) {
_rxState = StateMid0;
storeBit(1);
} else {
_rxState = StateSearchSync;
}
} else {
_rxState = StateSearchSync;
}
}
/*!
* \brief Handle space RF receive pin
*/
static void handleSpace()
{
/* State machine */
if (isPulseInRange(_tPinLow, T_BIT_SHORT_MIN, T_BIT_SHORT_MAX)) {
if (_rxState == StateEnd) {
_rxState = StateMid1;
storeBit(0);
} else if (_rxState == StateMid0) {
_rxState = StateEnd;
} else {
_rxState = StateSearchSync;
}
} else if (isPulseInRange(_tPinLow, T_BIT_LONG_MIN, T_BIT_LONG_MAX)) {
if (_rxState == StateMid0) {
_rxState = StateMid1;
storeBit(0);
} else {
_rxState = StateSearchSync;
}
} else {
_rxState = StateSearchSync;
}
}
/*!
* \brief RF pin level change
*/
void rfPinChange(void)
{
uint32_t tNow;
uint16_t _tPulseLength;
uint8_t rfPinHigh;
/* Return when previous completed receive is not read */
if (_rxState == StateRxComplete) {
return;
}
/* Read absolute pulse time in us for sync */
tNow = micros();
if (tNow > _tPulseBegin) {
_tPulseLength = tNow - _tPulseBegin;
} else {
_tPulseLength = _tPulseBegin - tNow;
}
/* Ignore short pulses */
if (_tPulseLength < T_RX_TOLERANCE_US) {
return;
}
_tPulseBegin = tNow;
/* Get RF pin state */
rfPinHigh = *portInputRegister(_rxPinPort) & _rxPinBit;
/* Store pulse (high) or space (low) length */
if (rfPinHigh) {
_tPinLow = _tPulseLength;
} else {
_tPinHigh = _tPulseLength;
}
/* Always search for sync */
if (findSync()) {
return;
}
/* Handle received pulse */
if (_rxState != StateSearchSync) {
if (rfPinHigh) {
handleSpace();
} else {
handlePulse();
}
}
}
/*------------------------------------------------------------------------------------------------*/
/* Public functions */
/*------------------------------------------------------------------------------------------------*/
/*!
* \brief Initialize receiver pin
* \details
* Connect RX pin to an external interrupt pin such as INT0 (D2) or INT1 (D3)
* \param extIntPin
*/
void OregonTHN128_RxBegin(uint8_t extIntPin)
{
/* Save interrupt number of the RF pin */
_rxInt = digitalPinToInterrupt(extIntPin);
/* Save pin port and bit */
_rxPinPort = digitalPinToPort(extIntPin);
_rxPinBit = digitalPinToBitMask(extIntPin);
/* Enable receive */
rxEnable();
}
/*!
* \brief Receive enable
*/
void OregonTHN128_RxEnable()
{
/* Enable receive */
rxEnable();
}
/*!
* \brief Receive disable
*/
void OregonTHN128_RxDisable()
{
/* Disable receive */
rxDisable();
}
/*!
* \brief Check if data received
* \retval true
* Data received
* \retval false
* No data available
*/
bool OregonTHN128_Available()
{
/* Return receive complete */
return (_rxState == StateRxComplete) ? true : false;
}
/*!
* \brief Read data
* \param data
* Structure OregonTHN128Data_t output
* \retval true
* Data received
* \retval false
* No data available
*/
bool OregonTHN128_Read(OregonTHN128Data_t *data)
{
if (OregonTHN128_Available()) {
/* Convert raw 32-bit data to data structure */
OregonTHN128_RawToData(_rxData, data);
return true;
} else {
return false;
}
}