-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFSKBitStrGen.cpp
More file actions
265 lines (236 loc) · 5.15 KB
/
Copy pathFSKBitStrGen.cpp
File metadata and controls
265 lines (236 loc) · 5.15 KB
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
#include "StdAfx.h"
#include "FSKBitStrGen.h"
#include "General.h"
#include "math.h"
CFSKBitStrGen::CFSKBitStrGen(void)
{
sampleRate = 8000 ;
shortBuf = NULL ;
mode = MODE_MDMF ;
name[0] = 0 ;
month = 1 ;
day = 1 ;
hour = 0 ;
minute = 0 ;
telNum[0] = 0 ;
ResetFSKArray() ;
}
CFSKBitStrGen::~CFSKBitStrGen(void)
{
if(shortBuf)
delete shortBuf;
shortBuf = NULL;
}
void CFSKBitStrGen::setSampleRate(uint32 s)
{
sampleRate = s;
}
void CFSKBitStrGen::setMode(uint8 m)
{
mode = m;
}
void CFSKBitStrGen::setName (uint8* n)
{
name[0] = 0;
if( strlen((char*)n) < STRING_SIZE )
strcpy((char*)name,(char*)n);
else
strcpy((char*)name,"MR LONG NAME");
}
void CFSKBitStrGen::setDate (uint8 m, uint8 d)
{
month = m ;
day = d ;
}
void CFSKBitStrGen::setTime (uint8 h, uint8 m)
{
hour = h;
minute = m;
}
void CFSKBitStrGen::setTelNum (uint8* tN)
{
telNum[0] = 0 ;
if( strlen((char*)tN) < STRING_SIZE )
{
strcpy((char*)telNum,(char*)tN);
}
else
{
strcpy((char*)telNum,"007");
}
}
int CFSKBitStrGen::Generate ()
{
uint16 cur;
uint32 i;
int t = 0;
int count = 0, residual = 0;
int br;
uint8 bit;
int bits = 0;
double x;
double freq, oldfreq;
int samples = sampleRate;
int rate = sampleRate;
int bitrate = BITRATE;
double one_freq = ONE_RAD;
double zero_freq = ZERO_RAD;
if(telNum[0]==0)
return FAILURE;
ResetFSKArray();
dataLen = 0;
for( i=0 ; i < 29; i++)
AddFSKByte(0x55);
for(i=29; i < 50; i++)
AddFSKByte(0xFF);
switch( mode )
{
case MODE_MDMF:
// check telnum and
dataLen = 10;
dataLen += strlen((char*)telNum)+2;
if( strlen((char*)name) > 0 )
dataLen += strlen((char*)name)+2;
ResetFSKCRC();
AddFSKDataByte(CODE_MDMF);
AddFSKDataByte(dataLen);
AddFSKDataByte(CODE_MDMF_DATE_TIME);
AddFSKDataByte(8);// date len;
AddFSKDataByte(month/10 + 0x30);
AddFSKDataByte(month%10 + 0x30);
AddFSKDataByte(day/10 + 0x30);
AddFSKDataByte(day%10 + 0x30);
AddFSKDataByte(hour/10 + 0x30);
AddFSKDataByte(hour%10 + 0x30);
AddFSKDataByte(minute/10+ 0x30);
AddFSKDataByte(minute%10+ 0x30);
AddFSKDataByte(CODE_MDMF_TELNUM);
AddFSKDataByte((uint8)strlen((char*)telNum));
for(i=0;i<strlen((char*)telNum);i++)
AddFSKDataByte(telNum[i]);
if( strlen((char*)name) )
{
AddFSKDataByte(CODE_MDMF_NAME);
AddFSKDataByte((uint8)strlen((char*)name));
for(i=0;i<strlen((char*)name);i++)
AddFSKDataByte(name[i]);
}
// calculating the crc.
break;
case MODE_SDMF:
dataLen = 8;
dataLen += strlen((char*)telNum);
AddFSKDataByte(CODE_SDMF);
AddFSKDataByte(dataLen );
AddFSKDataByte(month/10+ 0x30);
AddFSKDataByte(month%10+ 0x30);
AddFSKDataByte(day/10 + 0x30);
AddFSKDataByte(day%10 + 0x30);
AddFSKDataByte(hour/10 + 0x30);
AddFSKDataByte(hour%10 + 0x30);
AddFSKDataByte(minute/10+ 0x30);
AddFSKDataByte(minute%10+ 0x30);
for(i=0;i<strlen((char*)telNum);i++)
AddFSKDataByte(telNum[i]);
break;
}
AddFSKDataByte((uint8)(256-crc));
if(shortBuf)
delete []shortBuf;
bufferLen = 5*sampleRate;
shortBuf = new int16[5*sampleRate];
for(i=0; i < sampleRate; i++)
shortBuf[i]=0 ;
cur = sampleRate ;
// main LOOP.
bit = GetFSKBit();
if (bit)
freq = one_freq ;
else
freq = zero_freq;
for (i = 0; bits < GetFSKBitArraySize(); i++)
{
x = (((double)t/rate) * freq);
shortBuf[cur++] = (8191 * sin(x));
t++;
count++;
br = (rate / count) - residual;
if (br <= bitrate)
{
residual = bitrate - br;
count = 0;
if (bit != GetFSKBit())
{
oldfreq = freq;
if (bit == 0)
{
bit = 1;
freq = one_freq;
}
else
{
bit = 0;
freq = zero_freq;
}
double temp = 0.5 + ((double)t * (oldfreq / freq)) ;
t = (int) temp;
}
bits++;
}
}
for(i=cur; i <bufferLen; i++)
shortBuf[i] = 0;
//data is ready in short buffer.
return SUCCESS;
}
int CFSKBitStrGen::GetData (uint16* data)
{
memcpy(data,shortBuf,bufferLen*2);
return SUCCESS;
}
uint32 CFSKBitStrGen::GetBufferSize(void)
{
return bufferLen*2;
}
void CFSKBitStrGen::ResetFSKArray()
{
memset(fskData,0,FSK_BIT_ARRAY_SIZE);
fsk_read_position = 0;
fsk_write_position = 0;
ResetFSKCRC();
}
void CFSKBitStrGen::AddFSKBit(uint8 b)
{
fskData[fsk_write_position++] = b ;
}
int16 CFSKBitStrGen::GetFSKBitArraySize()
{
return fsk_write_position;
}
uint8 CFSKBitStrGen::GetFSKBit()
{
return fskData[fsk_read_position++];
}
void CFSKBitStrGen::AddFSKByte(uint8 by )
{
for(int i=0 ; i<8 ; i++)
{
AddFSKBit((uint8)( (by&(1<<i))>>i ));
}
}
void CFSKBitStrGen::AddFSKDataByte(uint8 by)
{
AddFSKBit(0);// start bit
AddFSKByte(by);
crc+= by;
AddFSKBit(1);// stop bit
// AddFSKBit(1);// stop bit
}
void CFSKBitStrGen::ResetFSKCRC()
{
crc = 0;
}
uint8 CFSKBitStrGen::GetCRC()
{
return crc;
}