forked from juribeparada/MMDVM_HS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DMRIdleRX.cpp
130 lines (108 loc) · 3.69 KB
/
DMRIdleRX.cpp
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
/*
* Copyright (C) 2009-2017 by Jonathan Naylor G4KLX
* Copyright (C) 2017 by Andy Uribe CA6JAU
*
* 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 2 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, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "Config.h"
#if defined(DUPLEX)
#include "Globals.h"
#include "DMRIdleRX.h"
#include "DMRSlotType.h"
#include "Utils.h"
const uint8_t MAX_SYNC_SYMBOLS_ERRS = 2U;
const uint8_t MAX_SYNC_BYTES_ERRS = 3U;
const uint16_t NOENDPTR = 9999U;
const uint8_t CONTROL_IDLE = 0x80U;
const uint8_t CONTROL_DATA = 0x40U;
CDMRIdleRX::CDMRIdleRX() :
m_buffer(),
m_dataPtr(0U),
m_endPtr(NOENDPTR),
m_colorCode(0U)
{
}
void CDMRIdleRX::reset()
{
m_dataPtr = 0U;
m_endPtr = NOENDPTR;
}
void CDMRIdleRX::databit(bool bit)
{
m_buffer[m_dataPtr] = bit;
m_patternBuffer <<= 1;
if (bit)
m_patternBuffer |= 0x01U;
if (countBits64((m_patternBuffer & DMR_SYNC_BITS_MASK) ^ DMR_MS_DATA_SYNC_BITS) <= MAX_SYNC_BYTES_ERRS) {
m_endPtr = m_dataPtr + DMR_SLOT_TYPE_LENGTH_BITS / 2U + DMR_INFO_LENGTH_BITS / 2U;
if (m_endPtr >= DMR_FRAME_LENGTH_BITS)
m_endPtr -= DMR_FRAME_LENGTH_BITS;
DEBUG3("SYNC MS Data found pos/end:", m_dataPtr, m_endPtr);
}
if (m_dataPtr == m_endPtr) {
uint16_t ptr = m_endPtr + 1;
if (ptr >= DMR_FRAME_LENGTH_BITS)
ptr -= DMR_FRAME_LENGTH_BITS;
uint8_t frame[DMR_FRAME_LENGTH_BYTES + 1U];
bitsToBytes(ptr, DMR_FRAME_LENGTH_BYTES, frame + 1U);
uint8_t colorCode;
uint8_t dataType;
CDMRSlotType slotType;
slotType.decode(frame + 1U, colorCode, dataType);
if (colorCode == m_colorCode && dataType == DT_CSBK) {
frame[0U] = CONTROL_IDLE | CONTROL_DATA | DT_CSBK;
serial.writeDMRData(false, frame, DMR_FRAME_LENGTH_BYTES + 1U);
}
m_endPtr = NOENDPTR;
}
m_dataPtr++;
if (m_dataPtr >= DMR_FRAME_LENGTH_BITS)
m_dataPtr = 0U;
}
void CDMRIdleRX::bitsToBytes(uint16_t start, uint8_t count, uint8_t* buffer)
{
for (uint8_t i = 0U; i < count; i++) {
buffer[i] = 0U;
buffer[i] |= ((m_buffer[start++] & 0x01) << 7);
if (start >= DMR_FRAME_LENGTH_BITS)
start -= DMR_FRAME_LENGTH_BITS;
buffer[i] |= ((m_buffer[start++] & 0x01) << 6);
if (start >= DMR_FRAME_LENGTH_BITS)
start -= DMR_FRAME_LENGTH_BITS;
buffer[i] |= ((m_buffer[start++] & 0x01) << 5);
if (start >= DMR_FRAME_LENGTH_BITS)
start -= DMR_FRAME_LENGTH_BITS;
buffer[i] |= ((m_buffer[start++] & 0x01) << 4);
if (start >= DMR_FRAME_LENGTH_BITS)
start -= DMR_FRAME_LENGTH_BITS;
buffer[i] |= ((m_buffer[start++] & 0x01) << 3);
if (start >= DMR_FRAME_LENGTH_BITS)
start -= DMR_FRAME_LENGTH_BITS;
buffer[i] |= ((m_buffer[start++] & 0x01) << 2);
if (start >= DMR_FRAME_LENGTH_BITS)
start -= DMR_FRAME_LENGTH_BITS;
buffer[i] |= ((m_buffer[start++] & 0x01) << 1);
if (start >= DMR_FRAME_LENGTH_BITS)
start -= DMR_FRAME_LENGTH_BITS;
buffer[i] |= ((m_buffer[start++] & 0x01) << 0);
if (start >= DMR_FRAME_LENGTH_BITS)
start -= DMR_FRAME_LENGTH_BITS;
}
}
void CDMRIdleRX::setColorCode(uint8_t colorCode)
{
m_colorCode = colorCode;
}
#endif