-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpa1er.cpp
387 lines (329 loc) · 8.31 KB
/
pa1er.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
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/*
* This routines are mend to helps the mail programm of communicating with the SA818 TRX modules
* Use this routines while leaving my callsign and name in the code
*
* Erik-Jan Roggekamp, PA1ER
*
*/
#include "sa818_pa1er.h"
#include "Adafruit_GFX.h"
#include "Adafruit_SSD1306.h"
#include <HardwareSerial.h>
#include <wire.h>
#include "config.h"
#define OLED_RESET 4 // not used
Adafruit_SSD1306 display(OLED_RESET);
//HardwareSerial serialSa818(1); // Define serial pins in configh // use defaults pins, config not used
//SoftwareSerial serialSa818( D7,D8 );
//HardwareSerial serialDopler(2); // Later gebruik voor input dopler
pa1erSa818 pa1er;
pa1erDisplay pa1erDisp;
char s[32];
String resp;
/*
* Initialization of the display params
*/
void pa1erDisplay::init()
{
// display.begin(SSD1306_SWITCHCAPVCC, 0x3c, 0, 21, 20, 800000); //// let op de pinnummer die nog moeten komen
display.begin(SSD1306_SWITCHCAPVCC, 0x3c, 0); // Default pin number, definition not needed address is 078
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(3);
display.setCursor(1,0);
display.println(" PA1ER");
display.display();
sprintf(pa1erDisp.call, "%s", Call);
#ifdef debug
Serial.println(F("Display init done"));
#endif
pa1erDisp.rssi=4;
// pa1erDisp.Vol=8;
}
/*
* This routine will set the display under normal conditions (not during the change of params)
*/
void pa1erDisplay::set()
{
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(2);
display.setCursor(32,0);
sprintf(s, "%s", Call);
display.println(s);
display.setTextSize(1);
display.setCursor(0,16);
display.println("Freq:");
display.setTextSize(2);
display.setCursor(30,16);
if (pa1erDisp.freqOk == true){
display.println(pa1erDisp.freqSa818);
}
else{
display.println("not set");
}
display.setTextSize(1);
display.setCursor(0,40);
display.println("SQL :");
display.setCursor(30,40);
display.println(pa1erDisp.SQ);
display.setCursor(60,40);
display.println("VOL:");
display.setCursor(90,40);
display.println(pa1erDisp.Vol);
display.setCursor(0,50);
display.println("RSSI: ");
display.setCursor(30,50);
if (pa1erDisp.freqOk == true) { display.println(rssi); }
display.setCursor(60,50);
display.println("DIR: ");
display.setCursor(90,50);
display.println(pa1erDisp.dir);
display.display();
}
/*
* Init of the values of the SA818
*/
void pa1erSa818::init()
{
Serial1.begin(9600);
bw = 1;
setMHz=145;
setKHz=2750;
SQ=4;
setGroup();
setVolume(6);
pa1erDisp.SQ = SQ;
pa1erDisp.sqOk = false;
pa1erDisp.freqOk = false;
}
/*
* Starting the communications with the SA818, Also needed after change VHF/UHF (is other module)
*/
bool pa1erStartCom()
{
String readString;
int count;
char c[40];
sprintf (s, "AT+DMOCONNECT\r\n");
Serial1.println(s);
#ifdef debug
Serial.print(F("send: "));
Serial.println(s);
#endif
delay(700);
count =0;
while ( count < 5) // Sometime have to do it a number of times
{
sprintf(c,"+DMOCONNECT:0");
if (pa1er.readSa(c) == true) {
break; // connected"
}
count++;
if (count == 5){
return false;
}
Serial1.println(s);
#ifdef debug
Serial.print(F("send: "));
Serial.println(s);
#endif
delay(500);
}
}
/*
* Set the RX/TX freq, Bandwith, Squels and CTCSS. Lastone is not used for now
*/
bool pa1erSa818::setGroup()
{
String readString;
int count;
char c[40];
// check params, then set SA818
#ifdef debug
Serial.println(F("setGroup."));
#endif
if (bw !=1) // Bandwidh can only be 1(25KHz) or 0 (12.5 KHz)
{
bw = 0;
}
// Don't use Subaudio at this moment, to be used later
sprintf(subAudioTx, "0000");
sprintf(subAudioRx, "0000");
freqOk = false;
if (setMHz > 133 and setMHz < 175) // VHF
{
freqOk=true;
if (band != vhf)
{ // Here we have to activate VHF module
#ifdef debug
Serial.println(F("VHF."));
#endif
pa1erDisp.freqOk = false;
digitalWrite(pinUhf, LOW); // Deactivate UHF madule
digitalWrite(pinVhf, HIGH); //activate VHF modiel
delay(10);
if (pa1erStartCom() == false)
{
Serial.println(F("Starting communication VHF fails"));
return false;
}
band = vhf;
}
}
if (setMHz > 399 and setMHz < 481) // UHF
{
freqOk=true;
if (band != uhf)
{ // Here we have to activate UHF module
#ifdef debug
Serial.println(F("UHF."));
#endif
pa1erDisp.freqOk = false;
digitalWrite(pinVhf, LOW); // Deactivate VHF madule
digitalWrite(pinUhf, HIGH); //activate UHF modiel
delay(10);
if (pa1erStartCom() == false)
{
Serial.println(F("Starting communication UHF fails"));
return false;
}
band = uhf;
}
}
if (freqOk == false)
{
sprintf(s, "Freq out of range: %03d.%04d", setMHz, setKHz);
Serial.println(s);
Serial.println(F("Range VHF: 134.0000 - 174.0000"));
Serial.println(F("Range UHF: 400-0000 - 480.0000"));
return false;
}
sprintf(freqRx, "%03.3d.%04.4d",setMHz, setKHz);
sprintf(freqTx, "%03.3d.%04.4d",setMHz, setKHz); // only work with RX now, so no reason for other TX freq
if (SQ<0){
SQ = 0;
}
if (SQ>8){
SQ = 8;
}
// Now we are able to crate the string to be send to the SA818
// Format:AT+DMOSETGROUP=BW,TX_F,RX_F,Tx_subaudio,SQ,Rx_subaudio
sprintf (s, "AT+DMOSETGROUP=%i,%s,%s,%s,%i, %s\r\n", bw, freqTx,freqRx,subAudioTx,SQ,subAudioRx);
Serial1.println(s);
#ifdef debug
Serial.print(F("send: "));
Serial.println(s);
#endif
delay(700);
count =0;
while ( count < 5) // Moeten soms een paar keer proberen om communicatie op te zetten
{
sprintf(c,"+DMOSETGROUP:1");
if (pa1er.readSa(c) == true) {
Serial.println(F("Freq set"));
sprintf(pa1erDisp.freqSa818, "%s",freqRx);
pa1erDisp.freqOk = true;
pa1erDisp.SQ = SQ;
break; // connected"
}
count++;
if (count == 5){
return false;
}
// Serial1.println(s);
#ifdef debug
Serial.print(F("send: "));
Serial.println(s);
#endif
delay(500);
}
}
/*
* We can set the Valume of the active module
*/
bool pa1erSa818::setVolume(int Vol)
{
if (Vol < 0){
Vol = 0;
}
if (Vol > 8){
Vol = 8;
}
sprintf (s, "AT+DMOSETVOLUME=%i\r\n", Vol);
Serial1.println(s);
#ifdef debug
Serial.println(s);
#endif
pa1erDisp.Vol=Vol;
pa1erDisp.set();
delay(500);
while(Serial1.available()) {
resp = Serial1.readString();// read the incoming data as string
#ifdef debug
Serial.print("responce SA818 volume: ");
Serial.println(resp);
#endif
return true; // No real check but is that needed?
}
}
/*
* This routine will reads the RSSI from the SA818
*/
int pa1erSa818::getRssi()
{
String readString;
readString="";
sprintf (s, "RSSI?\r\n");
Serial1.println(s);
delay(1000);
while (Serial1.available())
{
delay(5); //delay to allow byte to arrive in input buffer
char c = Serial1.read();
if ( (c != '\r') and ( c != '\n')) {
readString += c;
}
}
if (readString.length() >0) {
#ifdef debug
Serial.print("RSSI: ");
Serial.println(readString);
#endif
return readString.substring(5).toInt();
}
}
/*
* This routine is internal to read the serial responce of the SA818 and checks the expected responce
*/
bool pa1erSa818::readSa( char expected[40])
{
String readString;
delay(50);
while (Serial1.available())
{
delay(5); //delay to allow byte to arrive in input buffer
char c = Serial1.read();
if ( (c != '\r') and ( c != '\n')) {
// Serial.println(c);
readString += c;
}
}
if (readString.length() >0) {
if (readString.equals(expected)) {
#ifdef debug
Serial.print("Accepted: ");
Serial.println(readString);
#endif
readString="";
return true;
}// end right answer
} //end there are characters but wrong?
#ifdef debug
Serial.print("Not accepted: ");
Serial.println(readString);
#endif
readString="";
// ff niet voor de test ; return false; // Geen antwoord? Kan bijna niet
return false;
}