forked from paulh002/sdrberry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCatinterface.cpp
203 lines (188 loc) · 3.23 KB
/
Catinterface.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
#include "sdrberry.h"
#include "Catinterface.h"
#include "vfo.h"
Comm::~Comm()
{
if (serialport > 0)
serialClose(serialport);
}
void Comm::Close()
{
if (serialport > 0)
serialClose(serialport);
serialport = 0;
}
bool Comm::begin()
{
device = Settings_file.find_cat("USB");
if (device.length() == 0)
device = "/dev/serial/by-id/usb-Silicon_Labs_CP2102_USB_to_UART_Bridge_Controller_0001-if00-port0";
speed = 115200;
serialport = serialOpen(device.c_str(), speed);
if (serialport < 0)
{
serialport = 0;
return false;
}
printf("Connect to ESP32 CAT interface\n");
return true;
}
void Comm::Send(std::string s)
{
serialPuts(serialport, (const char *)s.c_str());
}
int Comm::Read(char c, std::string& s)
{
int chr; int i = 0;
s.clear();
do
{
//chr = serialGetchar(serialport);
int ret = serialReadchar(serialport, &chr);
if (ret < 0)
return -1;
if (ret == 1)
{
if (chr == '\n' || chr == '\r')
continue;
s.push_back((char)chr);
}
i++;
} while (chr != c && i < 80);
return s.length();
}
bool Comm::available()
{
if (serialDataAvail(serialport))
return true;
else
return false;
}
/* this methode is intened to send range details over volume, gain, bands etc*/
void Comm::SendInformation(int info)
{
switch (info)
{
case 0:
// Volume
{
char str[20];
int range = gbar.get_vol_range();
sprintf(str, "GT0%d", range);
Send((std::string) str);
}
break;
case 1:
// Gain
{
char str[20];
int max_gain, min_gain;
gagc.get_gain_range(max_gain, min_gain);
sprintf(str, "GT1%2d,%2d", max_gain, min_gain);
Send((std::string) str);
}
break;
case 2:
// Band
{
char str[20];
vector<int> bands;
string s;
strcpy(str, "GT2");
s = str;
vfo.return_bands(bands);
for (auto it : bands)
{
sprintf(str, ",%2d", it);
s.append(str);
}
s.push_back(';');
Send((std::string) s);
}
break;
case 3:
// Filter
{
char str[20];
vector<string> filters;
string s;
strcpy(str, "GT3");
s = str;
gbar.get_filter_range(filters);
for (auto it : filters)
{
s.push_back(',');
s.append(it);
}
s.push_back(';');
Send((std::string) s);
}
break;
}
}
void Catinterface::begin()
{
bcomm_port = comm_port.begin();
cat_message.begin(true, &comm_port, true);
m_mode = 0;
}
void Catinterface::checkCAT()
{
if (!bcomm_port)
return;
int ret = cat_message.CheckCAT(false);
if(ret < 0)
{
comm_port.Close();
usleep(1000000);
if (!comm_port.begin())
{
usleep(1000000);
return;
}
}
if (ret == 1)
{
// do something
int count = cat_message.GetFT();
if (count)
{
vfo.step_vfo(count, true);
cat_message.SetFA(vfo.get_active_vfo_freq());
}
count = cat_message.GetAG();
if (count)
gbar.set_vol_slider(count);
count = cat_message.GetRG();
if (count)
gagc.set_gain_slider(count);
count = cat_message.GetTX();
if (m_mode != count)
{
m_mode = count;
switch (m_mode)
{
case 0:
select_mode(mode);
break;
case 1:
select_mode_tx(mode);
break;
case 2:
select_mode_tx(mode, 1);
break;
}
}
}
}
void Catinterface::operator()()
{
while (1)
{
checkCAT();
}
}
void Catinterface::SetBand(uint16_t band)
{
cat_message.SetBand(band);
}