-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspsave.cpp
318 lines (289 loc) · 9.1 KB
/
spsave.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
/*
* spsave: Save spectrum data from tinySA / tinySA Ultra to log files
* Copyright (C) 2023 Kelei Chen
*
* 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 3 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, see <https://www.gnu.org/licenses/>.
*/
#include "common.hpp"
#include "config.hpp"
#include <fcntl.h>
#include <termios.h>
int send_cmd(int fd, string cmd)
{
// Send commands though fd
//cerr << "<< " << cmd << endl;
cmd += "\r";
write(fd, cmd.c_str(), cmd.length());
return 0;
}
const string read_response(int fd)
{
// Read response from fd
string response;
char c;
while(read(fd, &c, 1) > 0)
{
response += c;
// If we get a 'ch> ' prompt, we're done
//fprintf(stderr, "response (%hhx) = %s\n", c, response.c_str());
if(response.length() >= 4 && response.substr(response.length() - 4) == "ch> ")
break;
}
// remove the last 4 characters
response.erase(response.length() - 4);
cout << ">> " << response << endl;
return response;
}
// TODO: the argument list is becoming too long, consider using a struct
const string read_scanraw(
int fd, int zero_level, logheader_t &h, fstream &output)
{
string response;
uint8_t c;
cout << format("[{}] Reading... ", time_str()) << flush;
while(read(fd, &c, 1) > 0)
{
response += c;
// If we get a 'ch> ' prompt, we're done
if(response.length() >= 4 && response.substr(response.length() - 4) == "ch> ")
break;
}
// count x & print out CSV
int x_count = 0;
// make data header
// # <start_freq>,<stop_freq>,<steps>,<RBW>,<start_time>,<end_time>
output << format("$ {:.06f},{:.06f},{},{:.03f},{},{}\n",
h.start_freq, h.stop_freq, h.steps, h.rbw, h.start_time, time_str());
// first '{' + 1 is x
for(unsigned int i = response.find_first_of('{') + 1; i < response.length(); i += 3)
{
if(response[i] == 'x')
{
uint16_t data;
// x_count starts from 0
x_count++;
data = response[i+1] & 0xff; // avoid sign extension
data |= response[i+2] << 8;
// freq in MHz, data in dBm
output << format("{:.1f}\n", data / 32.0 - zero_level);
}
else
{
break;
}
}
output << endl; // one empty line between each scan
cout << format("Done. {} points read.\t", x_count) << flush; // don't do newline here
return response;
}
// Credits: https://stackoverflow.com/questions/54591636/ceiling-time-point-to-runtime-defined-duration/54634050#54634050
template <class Clock, class Duration1, class Duration2>
constexpr auto ceil(std::chrono::time_point<Clock, Duration1> t, Duration2 m) noexcept
{
using R = std::chrono::time_point<Clock, Duration2>;
auto r = std::chrono::time_point_cast<Duration2>(R{} + (t - R{}) / m * m);
if (r < t)
r += m;
return r;
}
auto awake_time(int interval)
{
return ceil(now(), std::chrono::seconds(interval));
}
void help_msg(char *argv[])
{
cout << "Usage: " << argv[0] << " [options]" << endl <<
"\t-t <ttydev>\n"
"\t-m <tinySA Model> \"tinySA\" or \"tinySA4\" (default)\n"
"\t-s <start freq MHz> default: 1\n"
"\t-e <stop freq MHz> default: 30\n"
"\t-k <step freq kHz> default: 10\n"
"\t-r <RBW in kHz>\t default: 10, consult tinySA.org for supported RBW values\n"
"\t-p <filename prefix> default \"sp\"\n"
"\t-l <loop?> 0 is false (default), any other value is true\n"
"\t-x <max records> default: 1440, 0 means no log rotation\n"
"\t-i <interval>\t sweep interval in seconds (default: 60)" << endl << endl;
}
const string new_logfile(fstream &output, const string &filename_prefix, const string &start_time)
{
const string filename = {filename_prefix + '.' + start_time + ".log"};
if(output.is_open())
output.close();
output.open(filename, std::ios::out);
if_error(!output.is_open(), "Error: cannot open output file");
return filename;
}
int main(int argc, char *argv[])
{
string ttydev = "";
double step_freq_kHz = 10;
logheader_t h =
{
/* start freq */ 1,
/* stop freq */ 30,
/* steps */ 2901,
/* rbw */ 10,
/* start time */ "",
/* end time */ ""
};
string filename_prefix = "sp";
bool loop = 0; // whether to run in a loop or not
int interval = 60; // interval in seconds
string model = "tinySA4"; // tinySA or tinySA4 (Ultra)
size_t max_records = 1440; // 1 day of 1-minute records
// Parse arguments
int opt;
while((opt = getopt(argc, argv, "t:s:e:k:r:p:l:i:m:x:h")) != -1)
{
switch(opt)
{
case 't':
ttydev = optarg;
break;
case 's':
h.start_freq = atof(optarg);
break;
case 'e':
h.stop_freq = atof(optarg);
break;
case 'k':
step_freq_kHz = atof(optarg);
break;
case 'r':
h.rbw = atof(optarg);
break;
case 'p':
filename_prefix = optarg;
break;
case 'l':
loop = atoi(optarg) == 0 ? false : true;
break;
case 'i':
interval = atoi(optarg);
if(60 % interval != 0)
{
cerr <<
"Warning: interval " + to_string(interval) +
" is not a factor of 60, correct behavior of log2png is not guaranteed"
<< endl;
}
break;
case 'm':
model = optarg;
break;
case 'x':
max_records = atoll(optarg);
break;
case 'h':
help_msg(argv);
return 0;
default:
help_msg(argv);
return 1;
}
}
// Sanity check
if_error(h.start_freq >= h.stop_freq, "Error: start freq > stop freq");
if_error(ttydev.empty(), "Error: no tty device specified");
// Open the serial port
int fd = open(ttydev.c_str(), O_RDWR | O_NOCTTY);
if_error(!isatty(fd), "Error: " + ttydev + " is not a tty");
// set baudrate to 115200 8N1, no flow control, no modem control, no echo & CR/LF translation
struct termios tty;
tcgetattr(fd, &tty);
cfsetospeed(&tty, B115200);
cfsetispeed(&tty, B115200);
tty.c_cflag &= ~PARENB; // no parity
tty.c_cflag &= ~CSTOPB; // 1 stop bit
tty.c_cflag &= ~CSIZE;
tty.c_cflag |= CS8; // 8 bits
tty.c_cflag &= ~CRTSCTS; // no flow control
tty.c_cflag |= CREAD | CLOCAL; // turn on READ & ignore ctrl lines
tty.c_lflag &= ~ICANON; // no canonical mode
tty.c_lflag &= ~ECHO; // no echo
tty.c_lflag &= ~ECHOE; // no echo erase
tty.c_lflag &= ~ECHONL; // no echo new line
tty.c_lflag &= ~ISIG; // no interpretation of INTR, QUIT and SUSP
tty.c_iflag &= ~(IXON | IXOFF | IXANY); // no software flow control
tty.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL); // no any special handling of received bytes
tty.c_oflag &= ~OPOST; // no output processing
tty.c_oflag &= ~ONLCR; // no CR -> NL translation
tty.c_cc[VTIME] = 0; // no timeout
tty.c_cc[VMIN] = 1; // no minimum number of bytes to read
tcsetattr(fd, TCSANOW, &tty);
cerr << format("tty = {}, start = {:.6f}MHz, stop = {:.6f}MHz, step = {:.3f}kHz, rbw = {:.3f}kHz, filename prefix = \"{}\"\n",
ttydev, h.start_freq, h.stop_freq, step_freq_kHz, h.rbw, filename_prefix);
print("Initializing...\n\n");
// Send init command
send_cmd(fd, "");
read_response(fd);
send_cmd(fd, "pause");
read_response(fd);
//send_cmd(fd, "rbw "+ to_string(h.rbw));
send_cmd(fd, format("rbw {:.1f}", h.rbw));
read_response(fd);
print("Sweeping...\n\n");
// Calculate the number of steps
double steps_floating = (h.stop_freq - h.start_freq) / (step_freq_kHz / 1e3) + 1;
if(ceil(steps_floating) != steps_floating)
print("Warning: the number of steps will not be an integer, the actual number of steps would be {}\n", ceil(steps_floating));
h.steps = ceil(steps_floating);
// construct the sweep command
const string scanraw_cmd = format("scanraw {:.0f} {:.0f} {}", h.start_freq * 1e6, h.stop_freq * 1e6, h.steps);
fstream output;
string start_time = time_str();
h.start_time = start_time;
string filename = new_logfile(output, filename_prefix, start_time);
int zero_level = ZERO_LEVEL_ULTRA;
if(model == "tinySA")
zero_level = ZERO_LEVEL;
else if(model == "tinySA4")
zero_level = ZERO_LEVEL_ULTRA;
else
if_error(true, "Error: unknown model " + model);
print("\nOpened log file: {}\n", filename);
// initiate sweep
if(loop)
{
// number of records written to file, will rotate file when it reaches MAX_RECORDS
size_t record_count = 0;
while(1)
{
cout << format("\r[{:8d}] ", record_count + 1) << flush; // Displayed value is 1-based
std::this_thread::sleep_until(awake_time(interval));
start_time = time_str();
h.start_time = start_time;
send_cmd(fd, scanraw_cmd);
read_scanraw(fd, zero_level, h, output);
record_count++;
// rotate file
if(max_records != 0 && record_count >= max_records)
{
record_count = 0;
// old log file will be closed in new_logfile()
filename = new_logfile(output, filename_prefix, time_str());
print("\n\nNew log file: {}\n", filename);
}
}
}
else
{
send_cmd(fd, scanraw_cmd);
read_scanraw(fd, zero_level, h, output);
send_cmd(fd, "resume");
}
output.close();
cout << endl;
return 0;
}