forked from Hamlib/Hamlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHamlibComponents.pas
432 lines (367 loc) · 10.4 KB
/
HamlibComponents.pas
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
unit HamlibComponents;
interface
uses
SysUtils, Types, Classes, QGraphics, QControls, QForms, QDialogs,
hamlib_rigapi, hamlib_rotapi;
type
TFrequencyEvent = procedure(Sender: TObject; vfo: vfo_t; Freq: freq_t) of object;
TVfoEvent = procedure(Sender: TObject; vfo: vfo_t) of object;
TModeEvent = procedure(Sender: TObject; vfo: vfo_t; mode: rmode_t) of object;
TRigComponent = class(TComponent)
private
{ Private declarations }
FRig: PRig;
FOnFrequency: TFrequencyEvent;
FOnVfo : TVfoEvent;
FOnMode : TModeEvent;
function GetModel: integer;
function GetModelName: string;
function GetMfgName: string;
function GetTrn: integer;
procedure SetTrn(Value: integer);
protected
{ Protected declarations }
public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
constructor CreateRig(AOwner: TComponent; AModel: integer);
destructor Destroy; override;
procedure OpenRig;
procedure CloseRig;
procedure SetTransceive(trn: integer);
function GetFreq(vfo: vfo_t): freq_t;
procedure SetFreq(vfo: vfo_t; freq: freq_t);
function checkMagic: boolean;
published
{ Published declarations }
property Model: integer read GetModel;
property ModelName: string read GetModelName;
property MfgName: string read GetMfgName;
property Transceive: integer read GetTrn write SetTrn default RIG_TRN_OFF;
property OnFrequency: TFrequencyEvent read FOnFrequency write FOnFrequency;
property OnVFO: TVfoEvent read FOnVfo write FOnVfo;
property OnMode: TModeEvent read FOnMode write FOnMode;
end;
type
TRotatorComponent = class(TComponent)
private
{ Private declarations }
FRot: PRot;
procedure SetMinAzim(const Value: float);
procedure SetMaxAzim(const Value: float);
procedure SetMinElev(const Value: float);
procedure SetMaxElev(const Value: float);
function GetMinAzim: float;
function GetMaxAzim: float;
function GetMinElev: float;
function GetMaxElev: float;
function GetModel: integer;
protected
{ Protected declarations }
public
{ Public declarations }
constructor Create(AOwner : TComponent); override;
constructor CreateRotator(AOwner : TComponent; AModel : Integer);
destructor Destroy; override;
procedure OpenRotator;
procedure CloseRotator;
procedure GetPosition(var Az: azimuth_t; var El: elevation_t);
procedure SetPosition(var Az: azimuth_t; var El: elevation_t);
procedure Stop;
procedure Park;
procedure Reset;
published
{ Published declarations }
property Model: integer read GetModel;
property MinAzimuth: float read GetMinAzim write SetMinAzim;
property MaxAzimuth: float read GetMaxAzim write SetMaxAzim;
property MinElevation: float read GetMinElev write SetMinElev;
property MaxElevation: float read GetMaxElev write SetMaxElev;
end;
type
ERigException = class(Exception);
ERotatorException = class(Exception);
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('User', [TRigComponent, TRotatorComponent]);
end;
constructor TRotatorComponent.Create(AOwner: TComponent);
begin
CreateRotator(AOwner, 1);
end;
constructor TRotatorComponent.CreateRotator(AOwner: TComponent; AModel: rot_model_t);
begin
inherited Create(AOwner);
{* Initialize the library for the spesified model *}
FRot := rot_init(AModel);
if (FRot = nil)
then raise ERotatorException.Create('Rig initialization error');
with FRot^ do
begin
state.obj := Self; { Pointer back to TRotatorComponent }
//callbacks.position_event := @position_callback; // TODO: have to implement this
end;
end;
destructor TRotatorComponent.Destroy;
begin
rot_cleanup(FRot);
FRot := nil;
inherited Destroy;
end;
procedure TRotatorComponent.OpenRotator;
var
retval: integer;
begin
retval := rot_open(FRot);
if (retval <> RIG_OK)
then raise ERotatorException.Create('rot_open: ' + StrPas(rigerror(retval)));
end;
procedure TRotatorComponent.CloseRotator;
var
retval: integer;
begin
retval := rot_close(FRot);
if (retval <> RIG_OK)
then raise ERotatorException.Create('rot_close: ' + StrPas(rigerror(retval)));
end;
procedure TRotatorComponent.GetPosition;
var
retval: integer;
begin
retval := rot_get_position(FRot, az, el);
if (retval <> RIG_OK)
then raise ERotatorException.Create('rot_get_position: ' + StrPas(rigerror(retval)));
end;
procedure TRotatorComponent.SetPosition;
var
retval: integer;
begin
retval := rot_set_position(FRot, az, el);
if (retval <> RIG_OK)
then raise ERotatorException.Create('rot_set_position: ' + StrPas(rigerror(retval)));
end;
procedure TRotatorComponent.Stop;
var
retval: integer;
begin
retval := rot_stop(FRot);
if (retval <> RIG_OK)
then raise ERotatorException.Create('rot_stop: ' + StrPas(rigerror(retval)));
end;
procedure TRotatorComponent.Park;
var
retval: integer;
begin
retval := rot_park(FRot);
if (retval <> RIG_OK)
then raise ERotatorException.Create('rot_park: ' + StrPas(rigerror(retval)));
end;
procedure TRotatorComponent.Reset;
var
retval: integer;
begin
retval := rot_reset(FRot, 0);
if (retval <> RIG_OK)
then raise ERotatorException.Create('rot_reset: ' + StrPas(rigerror(retval)));
end;
function TRotatorComponent.GetModel: integer;
begin
with FRot^ do
result := Caps^.rot_model;
end;
function TRotatorComponent.GetMinAzim: float;
begin
with FRot^ do
result := State.min_az;
end;
function TRotatorComponent.GetMaxAzim: float;
begin
with FRot^ do
result := State.max_az;
end;
function TRotatorComponent.GetMinElev: float;
begin
with FRot^ do
result := State.min_el;
end;
function TRotatorComponent.GetMaxElev: float;
begin
with FRot^ do
result := State.max_el;
end;
procedure TRotatorComponent.SetMinAzim(const Value: float);
begin
with FRot^ do
State.min_az := Value;
end;
procedure TRotatorComponent.SetMaxAzim(const Value: float);
begin
with FRot^ do
State.max_az := Value;
end;
procedure TRotatorComponent.SetMinElev(const Value: float);
begin
with FRot^ do
State.min_el := Value;
end;
procedure TRotatorComponent.SetMaxElev(const Value: float);
begin
with FRot^ do
State.max_el := Value;
end;
(*
*
*
*
*
*
*
*
*
* TRigComponent
*
*)
function freq_callback(rig: PRig; vfo: vfo_t; freq: freq_t): integer; cdecl;
var
component: TRigComponent;
begin
rig_debug(RIG_DEBUG_TRACE, 'Inside the callback: freq_callback...'+chr($a));
component := rig^.state.obj;
with component do
begin
if Assigned(FOnFrequency)
then FOnFrequency(component, vfo, freq);
end;
result := 1;
end;
{* These callback still have to be implemented
*
function mode_callback(rig: PRig; vfo: vfo_t; mode: rmode; width: pbwidth_t): integer; cdecl;
function vfo_callback(rig: PRig; vfo: vfo_t; freq_t: freq_t): integer; cdecl;
function ptt_callback(rig: PRig; mode: ptt_t): integer; cdecl;
function dcd_callback(rig: PRig; mode: dcd_t): integer; cdecl;
*
*}
constructor TRigComponent.CreateRig(AOwner: TComponent; AModel: integer);
begin
inherited Create(AOwner);
{* Initialize the library for the spesified model *}
FRig := rig_init(AModel);
if (FRig = nil)
then raise ERigException.Create('Rig initialization error');
with FRig^ do
begin
state.obj := Self; { Pointer back to TRigComponent }
callbacks.freq_event := @freq_callback;
end;
end;
constructor TRigComponent.Create(AOwner: TComponent);
begin
CreateRig(AOwner, 1); { Use Dummy by default }
end;
destructor TRigComponent.Destroy;
begin
rig_cleanup(FRig);
FRig := nil;
inherited Destroy;
end;
{*
* Because FRig is initialized with constructor and cleared with the
* destructor, all methods will assume that FRig != NIL during the life
* of the object.
*}
procedure TRigComponent.OpenRig;
var
retval: integer;
begin
retval := rig_open(FRig);
if (retval <> RIG_OK)
then raise ERigException.Create('rig_open: ' + StrPas(rigerror(retval)));
end;
procedure TRigComponent.CloseRig;
var
retval: integer;
begin
retval := rig_close(FRig);
if (retval <> RIG_OK)
then raise ERigException.Create('rig_close: ' + StrPas(rigerror(retval)));
end;
function TRigComponent.GetModel: integer;
begin
result := FRig^.caps^.rig_model;
end;
function TRigComponent.GetModelName: string;
begin
result := StrPas(FRig^.caps^.model_name);
end;
function TRigComponent.GetMfgName: string;
begin
result := StrPas(FRig^.caps^.mfg_name);
end;
function TRigComponent.GetTrn: integer;
var
retval : integer;
trn : integer;
begin
retval := rig_get_trn(FRig, trn);
if (retval <> RIG_OK)
then raise ERigException.Create('rig_get_trn: ' + StrPas(rigerror(retval)));
result := trn;
end;
procedure TRigComponent.SetTrn(Value: integer);
var
retval: integer;
begin
retval := rig_set_trn(FRig, Value);
if (retval <> RIG_OK) and (retval <> -RIG_ENAVAIL)
then raise ERigException.Create('rig_set_trn: ' + StrPas(rigerror(retval)));
end;
function TRigComponent.GetFreq(vfo: vfo_t): freq_t;
var
freq: freq_t;
retval: integer;
begin
freq := 0;
retval := rig_get_freq(FRig, vfo, freq);
if (retval <> RIG_OK)
then raise ERigException.Create('rig_get_freq: ' + StrPas(rigerror(retval)));
result := freq;
end;
procedure TRigComponent.SetFreq(vfo: vfo_t; freq: freq_t);
var
retval: integer;
begin
vfo:=1;
writeln('vfo=',vfo,' freq=',freq);
retval := rig_set_freq(FRig, vfo, freq);
if (retval <> RIG_OK)
then
begin
writeln('Return=',retval);
raise ERigException.Create('rig_set_freq: ' + StrPas(rigerror(retval)));
end;
end;
procedure TRigComponent.SetTransceive(trn: integer);
var
retval: integer;
begin
retval := rig_set_trn(FRig, trn);
if (retval <> RIG_OK)
then raise ERigException.Create('rig_set_trn: ' + StrPas(rigerror(retval)));
{ Note: This will cause a SIGIO interruption. The application will handle
the SIGIO. Please disable SIGIO handling in the debugger, else the
debugger will interrupt the program.
Menu: Tools|Debugger Options|Signals
}
end;
function TRigComponent.checkMagic: boolean;
begin
{$IFDEF HAVE_MAGIC_NUMBERS}
result := rigapi_check_magic(FRig) = 0;
{$ELSE}
result := true;
{$ENDIF}
end;
end.