-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjoystick_int.pas
321 lines (282 loc) · 8.68 KB
/
joystick_int.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
unit joystick_int;
{$mode objfpc}{$H+}
{
Copyright (C) 2015 Patrick Chevalley
http://www.ap-i.net
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 <http://www.gnu.org/licenses/>.
}
interface
uses indibaseclient, indibasedevice, indiapi, indicom,
ExtCtrls, Forms, Classes, SysUtils;
type
TNotifyMsg = procedure(msg:string) of object;
TNotifyNum = procedure(d: double) of object;
type
T_indijoystick = class(TIndiBaseClient)
private
InitTimer: TTimer;
JoystickDevice: Basedevice;
Joystickport: ITextVectorProperty;
connectprop: ISwitchVectorProperty;
swconnected, swdisconnected: ISwitch;
configprop: ISwitchVectorProperty;
configload,configsave,configdefault: ISwitch;
Fready,Fconnected,FAutoloadConfig: boolean;
Findiserver, Findiserverport, Findidevice, Findideviceport: string;
FStatus: TDeviceStatus;
FonDestroy: TNotifyEvent;
FonMsg: TNotifyMsg;
FonStatusChange: TNotifyEvent;
procedure InitTimerTimer(Sender: TObject);
procedure ClearStatus;
procedure CheckStatus;
procedure NewDevice(dp: Basedevice);
procedure NewMessage(mp: IMessage);
procedure NewProperty(indiProp: IndiProperty);
procedure NewNumber(nvp: INumberVectorProperty);
procedure NewText(tvp: ITextVectorProperty);
procedure NewSwitch(svp: ISwitchVectorProperty);
procedure NewLight(lvp: ILightVectorProperty);
procedure DeleteDevice(dp: Basedevice);
procedure DeleteProperty(indiProp: IndiProperty);
procedure ServerConnected(Sender: TObject);
procedure ServerDisconnected(Sender: TObject);
procedure msg(txt: string);
function GetStatusError: string;
public
constructor Create;
destructor Destroy; override;
Procedure Connect;
Procedure Disconnect;
procedure SaveConfig;
procedure LoadConfig;
property indiserver: string read Findiserver write Findiserver;
property indiserverport: string read Findiserverport write Findiserverport;
property indidevice: string read Findidevice write Findidevice;
property indideviceport: string read Findideviceport write Findideviceport;
property AutoloadConfig: boolean read FAutoloadConfig write FAutoloadConfig;
property Status: TDeviceStatus read FStatus;
property StatusError: string read GetStatusError;
property onDestroy: TNotifyEvent read FonDestroy write FonDestroy;
property onMsg: TNotifyMsg read FonMsg write FonMsg;
property onStatusChange: TNotifyEvent read FonStatusChange write FonStatusChange;
end;
implementation
constructor T_indijoystick.Create;
begin
inherited Create;
ClearStatus;
Findiserver:='localhost';
Findiserverport:='7624';
Findidevice:='Joystick';
Findideviceport:='';
InitTimer:=TTimer.Create(nil);
InitTimer.Enabled:=false;
InitTimer.Interval:=3000;
InitTimer.OnTimer:=@InitTimerTimer;
onNewDevice:=@NewDevice;
onNewMessage:=@NewMessage;
onNewProperty:=@NewProperty;
onNewNumber:=@NewNumber;
onNewText:=@NewText;
onNewSwitch:=@NewSwitch;
onNewLight:=@NewLight;
onDeleteDevice:=@DeleteDevice;
onDeleteProperty:=@DeleteProperty;
onServerConnected:=@ServerConnected;
onServerDisconnected:=@ServerDisconnected;
end;
destructor T_indijoystick.Destroy;
begin
if assigned(FonDestroy) then FonDestroy(self);
onNewDevice:=nil;
onNewMessage:=nil;
onNewProperty:=nil;
onNewNumber:=nil;
onNewText:=nil;
onNewSwitch:=nil;
onNewLight:=nil;
onNewBlob:=nil;
onServerConnected:=nil;
onServerDisconnected:=nil;
if InitTimer<>nil then FreeAndNil(InitTimer);
inherited Destroy;
end;
procedure T_indijoystick.ClearStatus;
begin
configprop:=nil;
connectprop:=nil;
Fready:=false;
Fconnected := false;
FStatus := devDisconnected;
if Assigned(FonStatusChange) then FonStatusChange(self);
end;
procedure T_indijoystick.CheckStatus;
begin
if Fconnected and
(not Fready) and
(connectprop<>nil) and
(configprop<>nil)
then begin
if swconnected.s=ISS_ON then FStatus := devConnected;
if swdisconnected.s=ISS_ON then FStatus := devDisconnected;
if (not Fready) and Assigned(FonStatusChange) then FonStatusChange(self);
Fready:=true;
if FAutoloadConfig then begin
LoadConfig;
end;
end;
end;
function T_indijoystick.GetStatusError: string;
begin
if Fconnected then begin
Result:='Joystick missing properties: ';
if (configprop=nil) then Result:=Result+'CONFIG_PROCESS, ';
end else
Result:='Joystick not connected';
end;
procedure T_indijoystick.msg(txt: string);
begin
if Assigned(FonMsg) then FonMsg(txt);
end;
Procedure T_indijoystick.Connect;
begin
if not Connected then begin
FStatus := devDisconnected;
if Assigned(FonStatusChange) then FonStatusChange(self);
SetServer(indiserver,indiserverport);
watchDevice(indidevice);
ConnectServer;
FStatus := devConnecting;
if Assigned(FonStatusChange) then FonStatusChange(self);
InitTimer.Enabled:=true;
end
else msg('Joystick already connected');
end;
procedure T_indijoystick.InitTimerTimer(Sender: TObject);
begin
InitTimer.Enabled:=false;
if (JoystickDevice=nil)or(not Fready) then begin
msg('Joystick initialisation failed.');
msg(GetStatusError);
Disconnect;
end;
end;
Procedure T_indijoystick.Disconnect;
begin
Terminate;
ClearStatus;
end;
procedure T_indijoystick.ServerConnected(Sender: TObject);
begin
if (Joystickport<>nil)and(Findideviceport<>'') then begin
Joystickport.tp[0].text:=Findideviceport;
sendNewText(Joystickport);
end;
connectDevice(Findidevice);
end;
procedure T_indijoystick.ServerDisconnected(Sender: TObject);
begin
FStatus := devDisconnected;
if Assigned(FonStatusChange) then FonStatusChange(self);
msg('Joystick server disconnected');
end;
procedure T_indijoystick.NewDevice(dp: Basedevice);
begin
//writeln('Newdev: '+dp.getDeviceName);
if dp.getDeviceName=Findidevice then begin
Fconnected:=true;
JoystickDevice:=dp;
end;
end;
procedure T_indijoystick.DeleteDevice(dp: Basedevice);
begin
if dp.getDeviceName=Findidevice then begin
Disconnect;
end;
end;
procedure T_indijoystick.DeleteProperty(indiProp: IndiProperty);
begin
{ TODO : check if a vital property is removed ? }
end;
procedure T_indijoystick.NewMessage(mp: IMessage);
begin
msg(mp.msg);
mp.Free;
end;
procedure T_indijoystick.NewProperty(indiProp: IndiProperty);
var propname: string;
proptype: INDI_TYPE;
i: integer;
begin
propname:=indiProp.getName;
proptype:=indiProp.getType;
if (proptype=INDI_TEXT)and(propname='DEVICE_PORT') then begin
Joystickport:=indiProp.getText;
end
else if (proptype=INDI_SWITCH)and(propname='CONNECTION') then begin
connectprop:=indiProp.getSwitch;
swconnected:=IUFindSwitch(connectprop,'CONNECT');
swdisconnected:=IUFindSwitch(connectprop,'DISCONNECT');
if (swconnected=nil)or(swdisconnected=nil) then connectprop:=nil;
end
else if (proptype=INDI_SWITCH)and(propname='CONFIG_PROCESS') then begin
configprop:=indiProp.getSwitch;
configload:=IUFindSwitch(configprop,'CONFIG_LOAD');
configsave:=IUFindSwitch(configprop,'CONFIG_SAVE');
configdefault:=IUFindSwitch(configprop,'CONFIG_DEFAULT');
if (configload=nil)or(configsave=nil)or(configdefault=nil) then configprop:=nil;
end
;
CheckStatus;
end;
procedure T_indijoystick.NewNumber(nvp: INumberVectorProperty);
begin
end;
procedure T_indijoystick.NewText(tvp: ITextVectorProperty);
begin
// writeln('NewText: '+tvp.name+' '+tvp.tp[0].text);
end;
procedure T_indijoystick.NewSwitch(svp: ISwitchVectorProperty);
var propname: string;
sw: ISwitch;
ok:boolean;
begin
// writeln('NewSwitch: '+svp.name);
if (svp=connectprop) then begin
if swconnected.s=ISS_ON then FStatus := devConnected;
if swdisconnected.s=ISS_ON then FStatus := devDisconnected;
if Assigned(FonStatusChange) then FonStatusChange(self);
end;
end;
procedure T_indijoystick.NewLight(lvp: ILightVectorProperty);
begin
// writeln('NewLight: '+lvp.name);
end;
procedure T_indijoystick.SaveConfig;
begin
if configprop<>nil then begin
IUResetSwitch(configprop);
configsave.s:=ISS_ON;
sendNewSwitch(configprop);
end;
end;
procedure T_indijoystick.LoadConfig;
begin
if configprop<>nil then begin
IUResetSwitch(configprop);
configload.s:=ISS_ON;
sendNewSwitch(configprop);
end;
end;
end.