forked from silenty4ng/uv-k5-firmware-chinese-lts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dcs.c
149 lines (130 loc) · 4.09 KB
/
dcs.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
/* Copyright 2023 Dual Tachyon
* https://github.com/DualTachyon
*
* 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.
*/
#include "dcs.h"
#ifndef ARRAY_SIZE
#define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
#endif
#ifdef TEST_UNDE_CTCSS
const uint16_t CTCSS_Options[50] = {
200, 693, 719, 744, 770, 797, 825, 854, 885, 915,
948, 974, 1000, 1035, 1072, 1109, 1148, 1188, 1230, 1273,
1318, 1365, 1413, 1462, 1514, 1567, 1598, 1622, 1655, 1679,
1713, 1738, 1773, 1799, 1835, 1862, 1899, 1928, 1966, 1995,
2035, 2065, 2107, 2181, 2257, 2291, 2336, 2418, 2503, 2541
};
#else
//CTCSS Hz * 10
const uint16_t CTCSS_Options[50] = {
670, 693, 719, 744, 770, 797, 825, 854, 885, 915,
948, 974, 1000, 1035, 1072, 1109, 1148, 1188, 1230, 1273,
1318, 1365, 1413, 1462, 1514, 1567, 1598, 1622, 1655, 1679,
1713, 1738, 1773, 1799, 1835, 1862, 1899, 1928, 1966, 1995,
2035, 2065, 2107, 2181, 2257, 2291, 2336, 2418, 2503, 2541
};
#endif
const uint16_t DCS_Options[104] = {
0x0013, 0x0015, 0x0016, 0x0019, 0x001A, 0x001E, 0x0023, 0x0027,
0x0029, 0x002B, 0x002C, 0x0035, 0x0039, 0x003A, 0x003B, 0x003C,
0x004C, 0x004D, 0x004E, 0x0052, 0x0055, 0x0059, 0x005A, 0x005C,
0x0063, 0x0065, 0x006A, 0x006D, 0x006E, 0x0072, 0x0075, 0x007A,
0x007C, 0x0085, 0x008A, 0x0093, 0x0095, 0x0096, 0x00A3, 0x00A4,
0x00A5, 0x00A6, 0x00A9, 0x00AA, 0x00AD, 0x00B1, 0x00B3, 0x00B5,
0x00B6, 0x00B9, 0x00BC, 0x00C6, 0x00C9, 0x00CD, 0x00D5, 0x00D9,
0x00DA, 0x00E3, 0x00E6, 0x00E9, 0x00EE, 0x00F4, 0x00F5, 0x00F9,
0x0109, 0x010A, 0x010B, 0x0113, 0x0119, 0x011A, 0x0125, 0x0126,
0x012A, 0x012C, 0x012D, 0x0132, 0x0134, 0x0135, 0x0136, 0x0143,
0x0146, 0x014E, 0x0153, 0x0156, 0x015A, 0x0166, 0x0175, 0x0186,
0x018A, 0x0194, 0x0197, 0x0199, 0x019A, 0x01AC, 0x01B2, 0x01B4,
0x01C3, 0x01CA, 0x01D3, 0x01D9, 0x01DA, 0x01DC, 0x01E3, 0x01EC,
};
static uint32_t DCS_CalculateGolay(uint32_t CodeWord)
{
unsigned int i;
uint32_t Word = CodeWord;
for (i = 0; i < 12; i++)
{
Word <<= 1;
if (Word & 0x1000)
Word ^= 0x08EA;
}
return CodeWord | ((Word & 0x0FFE) << 11);
}
uint32_t DCS_GetGolayCodeWord(DCS_CodeType_t CodeType, uint8_t Option)
{
uint32_t Code = DCS_CalculateGolay(DCS_Options[Option] + 0x800U);
if (CodeType == CODE_TYPE_REVERSE_DIGITAL)
Code ^= 0x7FFFFF;
return Code;
}
uint8_t DCS_GetCdcssCode(uint32_t Code)
{
unsigned int i;
for (i = 0; i < 23; i++)
{
uint32_t Shift;
if (((Code >> 9) & 0x7U) == 4)
{
unsigned int j;
for (j = 0; j < ARRAY_SIZE(DCS_Options); j++)
if (DCS_Options[j] == (Code & 0x1FF))
if (DCS_GetGolayCodeWord(2, j) == Code)
return j;
}
Shift = Code >> 1;
if (Code & 1U)
Shift |= 0x400000U;
Code = Shift;
}
return 0xFF;
}
uint8_t DCS_GetCtcssCode(int Code)
{
unsigned int i;
uint8_t Result = 0xFF;
int Smallest = ARRAY_SIZE(CTCSS_Options);
for (i = 0; i < ARRAY_SIZE(CTCSS_Options); i++)
{
int Delta = Code - CTCSS_Options[i];
if (Delta < 0)
Delta = -(Code - CTCSS_Options[i]);
if (Smallest > Delta)
{
Smallest = Delta;
Result = i;
}
}
return Result;
}
#ifdef TEST_UNDE_CTCSS
uint16_t DCS_GetCtcssCode_ALL(int Code)
{
unsigned int i;
uint16_t Result = 0xFFFF;
int Smallest = 50;
for (i = 0; i <2550; i++)
{
int Delta = Code - i;
if (Delta < 0)
Delta = -(Code - i);
if (Smallest > Delta)
{
Smallest = Delta;
Result = i;
}
}
return Result;
}
#endif