1 // ----------------------------------------------------------------------------
2 // Copyright (C) 2014
3 //              David Freese, W1HKJ
4 //
5 // This file is part of flrig.
6 //
7 // flrig is free software; you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by
9 // the Free Software Foundation; either version 3 of the License, or
10 // (at your option) any later version.
11 //
12 // flrig is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // aunsigned long int with this program.  If not, see <http://www.gnu.org/licenses/>.
19 // ----------------------------------------------------------------------------
20 
21 #include "TT599.h"
22 
23 //=============================================================================
24 // TT-599
25 
26 const char RIG_TT599name_[] = "Eagle";
27 
28 const char *RIG_TT599modes_[] = {
29 		"USB", "LSB", "CW", "AM", "FM", NULL};
30 const char RIG_TT599mchar_[] = {
31 		'0', '1', '3', '4', '5', 0 };
32 static const int  RIG_TT599_def_bw[] = { 19, 19, 7, 24, 19 };
33 static const char RIG_TT599_mode_type[] = {'U', 'L', 'L', 'U', 'U'};
34 
35 const char *RIG_TT599widths[] = {
36 "100",  "200",  "300",  "400",  "500", "600",  "700",  "800",  "900",  "1000",
37 "1200", "1400", "1600", "1800", "2000", "2200", "2400", "2600", "2800", "3000", NULL};
38 static int TT599_bw_vals[] = {
39  1, 2, 3, 4, 5, 6, 7, 8, 9,10,
40 11,12,13,14,15,16,17,18,19,20,
41 WVALS_LIMIT};
42 
43 const int RIG_TT599w[] = {
44 100, 200, 300, 400, 500,
45 600, 700, 800, 900, 1000,
46 1200, 1400, 1600, 1800, 2000,
47 2200, 2400, 2600, 2800, 3000, 0 };
48 
49 static GUI rig_widgets[]= {
50 	{ (Fl_Widget *)sldrPOWER,   266, 105, 156 },
51 	{ (Fl_Widget *)NULL,          0,   0,   0 }
52 };
53 
RIG_TT599()54 RIG_TT599::RIG_TT599() {
55 	name_ = RIG_TT599name_;
56 	modes_ = RIG_TT599modes_;
57 	bandwidths_ = RIG_TT599widths;
58 	bw_vals_ = TT599_bw_vals;
59 
60 	widgets = rig_widgets;
61 
62 	comm_baudrate = BR57600;
63 	stopbits = 1;
64 	comm_retries = 2;
65 	comm_wait = 10;
66 	comm_timeout = 50;
67 	comm_echo = false;
68 	comm_rtscts = true;
69 	comm_rtsplus = false;
70 	comm_dtrplus = true;
71 	comm_catptt = true;
72 	comm_rtsptt = false;
73 	comm_dtrptt = false;
74 	modeA = 1;
75 	bwA = 16;
76 	can_change_alt_vfo = true;
77 
78 	max_power = 100;
79 
80 	has_smeter =
81 	has_power_out =
82 	has_split =
83 	has_split_AB =
84 	has_swr_control =
85 	has_mode_control =
86 	has_bandwidth_control =
87 	has_ptt_control =
88 	has_preamp_control =
89 	has_auto_notch =
90 	has_attenuator_control =
91 	has_noise_reduction_control =
92 	has_power_control = true;
93 //	has_agc_level = true;
94 
95 	precision = 1;
96 	ndigits = 8;
97 
98 };
99 
wait(int msec)100 static void wait(int msec)
101 {
102 	for (int i = 0; i < msec; i += 10) {
103 		MilliSleep(10);
104 		Fl::awake();
105 	}
106 }
107 
initialize()108 void RIG_TT599::initialize()
109 {
110 	rig_widgets[0].W = sldrPOWER;
111 
112 	cmd = "X\r";
113 	sendCommand(cmd, 0);
114 	wait(200);
115 	readResponse();
116 	showresp(WARN, ASC, "init", cmd, replystr);
117 	split = false;
118 	cmd = "*KVAAA\r";
119 	sendCommand(cmd, 0);
120 	wait(200);
121 	readResponse();
122 	showresp(WARN, ASC, "normal", cmd, replystr);
123 }
124 
shutdown()125 void RIG_TT599::shutdown()
126 {
127 	cmd = "*RMF0\r";
128 	sendCommand(cmd, 0);
129 	wait(200);
130 	readResponse();
131 	showresp(WARN, ASC, "Enable BW control", cmd, replystr);
132 }
133 
check()134 bool RIG_TT599::check ()
135 {
136 	cmd = "?AF\r";
137 	int ret = waitCommand( cmd, 12, "check");
138 	if (ret < 12) return false;
139 	return true;
140 }
141 
get_vfoA()142 unsigned long int RIG_TT599::get_vfoA ()
143 {
144 	size_t p;
145 	cmd = "?AF\r";
146 	if ( waitCommand( cmd, 12, "get vfoA") ) {
147 		if ((p = replystr.rfind("@AF")) != string::npos)
148 			freqA =  atol(&replystr[p+3]);
149 	}
150 	return freqA;
151 }
152 
set_vfoA(unsigned long int freq)153 void RIG_TT599::set_vfoA (unsigned long int freq)
154 {
155 	freqA = freq;
156 	cmd = "*AF";
157 	cmd.append( to_decimal( freq, 8 ) );
158 	cmd += '\r';
159 	sendCommand(cmd);
160 	get_vfoA();
161 }
162 
get_vfoB()163 unsigned long int RIG_TT599::get_vfoB ()
164 {
165 	size_t p;
166 	cmd = "?BF\r";
167 	if ( waitCommand( cmd, 12, "get vfoB") ) {
168 		if ((p = replystr.rfind("@BF")) != string::npos)
169 			freqB =  atol(&replystr[p+3]);
170 	}
171 	return freqB;
172 }
173 
set_vfoB(unsigned long int freq)174 void RIG_TT599::set_vfoB (unsigned long int freq)
175 {
176 	freqB = freq;
177 	cmd = "*BF";
178 	cmd.append( to_decimal( freq, 8 ) );
179 	cmd += '\r';
180 	sendCommand(cmd);
181 	get_vfoB();
182 }
183 
set_PTT_control(int val)184 void RIG_TT599::set_PTT_control(int val)
185 {
186 	cmd = val ? "*TK\r" : "*TU\r";
187 	sendCommand(cmd);
188 	wait(200);
189 }
190 
get_modeA()191 int RIG_TT599::get_modeA()
192 {
193 	size_t p;
194 	cmd = "?RMM\r";
195 	if ( waitCommand( cmd, 6, "get modeA") ) {
196 		if ((p = replystr.rfind("@RMM")) != string::npos) {
197 			modeA = 0;
198 			while (RIG_TT599mchar_[modeA] != replystr[p+4]) {
199 				modeA++;
200 				if (RIG_TT599mchar_[modeA] == 0) {
201 					modeA = 0;
202 					break;
203 				}
204 			}
205 		}
206 	}
207 	return modeA;
208 }
209 
set_modeA(int md)210 void RIG_TT599::set_modeA(int md)
211 {
212 	modeA = md;
213 	cmd = "*RMM";
214 	cmd += RIG_TT599mchar_[md];
215 	cmd += '\r';
216 	sendCommand(cmd);
217 	get_modeA();
218 }
219 
get_modeB()220 int RIG_TT599::get_modeB()
221 {
222 	size_t p;
223 	cmd = "?RMM\r";
224 	if ( waitCommand( cmd, 6, "get modeB") ) {
225 		if ((p = replystr.rfind("@RMM")) != string::npos) {
226 			modeB = 0;
227 			while (RIG_TT599mchar_[modeB] != replystr[p+4]) {
228 				modeB++;
229 				if (RIG_TT599mchar_[modeB] == 0) {
230 					modeB = 0;
231 					break;
232 				}
233 			}
234 		}
235 	}
236 	return modeB;
237 }
238 
set_modeB(int md)239 void RIG_TT599::set_modeB(int md)
240 {
241 	modeB = md;
242 	cmd = "*RMM";
243 	cmd += RIG_TT599mchar_[md];
244 	cmd += '\r';
245 	sendCommand(cmd);
246 	get_modeB();
247 }
248 
get_bwA()249 int RIG_TT599::get_bwA()
250 {
251 	int w = 0;
252 	size_t p;
253 	cmd = "?RMF\r";
254 	if ( waitCommand( cmd, 12, "get bwA") ) {
255 		if ((p = replystr.rfind("@RMF")) != string::npos) {
256 			if ( p != string::npos)
257 				w = atol(&replystr[p+4]);
258 			bwA = 0;
259 			while ( (RIG_TT599w[bwA] < w)  &&
260 					(RIG_TT599w[bwA + 1] != 0) ) bwA++;
261 		}
262 	}
263 	return bwA;
264 }
265 
set_bwA(int bw)266 void RIG_TT599::set_bwA(int bw)
267 {
268 	cmd = "*RMF";
269 	cmd.append(RIG_TT599widths[bw]);
270 	cmd += '\r';
271 	sendCommand(cmd);
272 	get_bwA();
273 }
274 
get_bwB()275 int RIG_TT599::get_bwB()
276 {
277 	int w = 0;
278 	size_t p;
279 	cmd = "?RMF\r";
280 	if ( waitCommand( cmd, 12, "get bwB") ) {
281 		if ((p = replystr.rfind("@RMF")) != string::npos) {
282 			if ( p != string::npos)
283 				w = atol(&replystr[p+4]);
284 			bwB = 0;
285 			while ( (RIG_TT599w[bwB] < w)  &&
286 					(RIG_TT599w[bwB + 1] != 0) ) bwB++;
287 		}
288 	}
289 	return bwB;
290 }
291 
set_bwB(int bw)292 void RIG_TT599::set_bwB(int bw)
293 {
294 	cmd = "*RMF";
295 	cmd.append(RIG_TT599widths[bw]);
296 	cmd += '\r';
297 	sendCommand(cmd);
298 	get_bwB();
299 }
300 
adjust_bandwidth(int m)301 int  RIG_TT599::adjust_bandwidth(int m)
302 {
303 	return RIG_TT599_def_bw[m];
304 }
305 
def_bandwidth(int m)306 int  RIG_TT599::def_bandwidth(int m)
307 {
308 	return RIG_TT599_def_bw[m];
309 }
310 
get_modetype(int n)311 int RIG_TT599::get_modetype(int n)
312 {
313 	return RIG_TT599_mode_type[n];
314 }
315 
get_preamp()316 int RIG_TT599::get_preamp()
317 {
318 	size_t p;
319 	cmd = "?RME\r";
320 	if ( waitCommand( cmd, 6, "get preamp") ) {
321 		if ((p = replystr.rfind("@RME")) != string::npos)
322 			return replystr[p+4] - '0';
323 	}
324 	return 0;
325 }
326 
set_preamp(int val)327 void RIG_TT599::set_preamp(int val)
328 {
329 	cmd = val ? "*RME1\r" : "*RME0\r";
330 	sendCommand(cmd);
331 	get_preamp();
332 }
333 
334 //void RIG_TT599::set_agc_level()
335 //{
336 // *RMAF - fast *RMAM - medium *RMAS - slow
337 //}
338 
get_power_control(void)339 int  RIG_TT599::get_power_control(void)
340 {
341 	size_t p;
342 	cmd = "?TP\r";
343 	if ( waitCommand( cmd, 6, "get power control") ) {
344 		if ((p = replystr.rfind("@TP")) != string::npos) {
345 			int pwr = atol(&replystr[p+3]);
346 			return pwr;
347 		}
348 	}
349 	return progStatus.power_level;
350 }
351 
set_power_control(double val)352 void RIG_TT599::set_power_control(double val)
353 {
354 	char szPwr[4];
355 	snprintf(szPwr, sizeof(szPwr), "%d", (int)val);
356 	cmd.assign("*TP");
357 	cmd.append(szPwr).append("\r");
358 	sendCommand(cmd);
359 	get_power_control();
360 }
361 
get_auto_notch()362 int  RIG_TT599::get_auto_notch()
363 {
364 	cmd = "?RMNA\r";
365 	sendCommand(cmd);
366 	if ( waitCommand( cmd, 7, "get auto notch") ) {
367 		if (replystr.rfind("@RMNA1") == string::npos) return 0;
368 	}
369 	return 1;
370 }
371 
set_auto_notch(int v)372 void RIG_TT599::set_auto_notch(int v)
373 {
374 	cmd = v ? "*RMNA1\r" : "*RMNA0\r";
375 	sendCommand(cmd);
376 	get_auto_notch();
377 }
378 
get_attenuator()379 int  RIG_TT599::get_attenuator()
380 {
381 	size_t p;
382 	cmd = "?RMT\r";
383 	if ( waitCommand( cmd, 6, "get attenuator") ) {
384 		if ((p = replystr.rfind("@RMT1")) != string::npos) return 1;
385 	}
386 	return 0;
387 }
388 
set_attenuator(int val)389 void RIG_TT599::set_attenuator(int val)
390 {
391 	cmd = val ? "*RMT1\r" : "*RMT0\r";
392 	sendCommand(cmd);
393 	get_attenuator();
394 }
395 
396 int smeter_count = 10;
get_smeter()397 int  RIG_TT599::get_smeter()
398 {
399 	size_t p;
400 	int dbm = 0;
401 	cmd = "?S\r";
402 	if ( waitCommand( cmd, 20, "get smeter") ) {
403 		if ((p = replystr.rfind("@SRM")) != string::npos) dbm = atoi(&replystr[p+4]);
404 	}
405 	return 5 * dbm / 6;
406 }
407 
get_swr()408 int  RIG_TT599::get_swr()
409 {
410 	float swr = (sqrtf(fwdpwr) + sqrtf(refpwr/10.0))/(sqrt(fwdpwr) - sqrt(refpwr/10.0) + .0001);
411 	swr -= 1.0;
412 	swr *= 25.0;
413 	if (swr < 0) swr = 0;
414 	if (swr > 100) swr = 100;
415 	return (int)swr;
416 
417 }
418 
get_power_out()419 int  RIG_TT599::get_power_out()
420 {
421 	size_t p;
422 	fwdpwr = 0; refpwr = 0;
423 	cmd = "?S\r";
424 	if ( waitCommand( cmd, 20, "get smeter") ) {
425 		if ((p = replystr.rfind ("@STF")) != string::npos) {
426 			fwdpwr = atol(&replystr[p+4]);
427 			p = replystr.find("R", p+4);
428 			if (p != string::npos)
429 				refpwr = atol(&replystr[p+1]);
430 		}
431 	}
432 	return fwdpwr;
433 }
434 
get_split()435 int RIG_TT599::get_split()
436 {
437 	cmd = "?KV\r";
438 	if ( waitCommand( cmd, 7, "get split") ) {
439 		size_t p = replystr.find("@KVAA");
440 		if ((p != string::npos) && (replystr[p+5] == 'B')) split = 1;
441 		else split = 0;
442 	}
443 	return split;
444 }
445 
set_split(bool val)446 void  RIG_TT599:: set_split(bool val)
447 {
448 	split = val;
449 	cmd = val ? "*KVAAB\r" : "*KVAAA\r";
450 	sendCommand(cmd);
451 	get_split();
452 }
453 
get_noise_reduction_val()454 int  RIG_TT599::get_noise_reduction_val()
455 {
456 	int val = 1;
457 	cmd.assign("?RMNN\r");
458 
459 	if ( waitCommand( cmd, 7, "get noise_reduction_value") ) {
460 		size_t p = replystr.rfind("@RMNN");
461 		if (p == string::npos) return val;
462 		val = atol(&replystr[p+5]);
463 	}
464 	return val;
465 }
466 
set_noise_reduction_val(int val)467 void RIG_TT599::set_noise_reduction_val(int val)
468 {
469 	cmd.assign("*RMNN");
470 	cmd += ('0' + val);
471 	cmd.append("\r");
472 	sendCommand(cmd);
473 	get_noise_reduction_val();
474 }
475