-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathBLEProgrammingCommands.pas
356 lines (307 loc) · 8.46 KB
/
BLEProgrammingCommands.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
unit BLEProgrammingCommands;
interface
uses
debug, tickcount, commandprocessor, exe, sysutils, systemx, typex, stringx, simplebleconnection, simplecommport, latchcontrol_FTDI;
const
PROG_WAIT = 60000;
type
TBLEProgrammingCommand = class(TCommand)
private
FLoadBat: string;
FIdx: ni;
FPort: string;
FTestPort: TSimpleBLEMIDIConnection;
FMac: string;
FDoNotTest: boolean;
FNoDelay: boolean;
procedure SetIdx(const Value: ni);
public
lc: TLatchControl;
Latchbit: ni;
LAtchAlwaysOn: boolean;
output: string;
property LoadBat: string read FLoadBat write FLoadbat;
property Port: string read FPort write FPort;
property TestPort: TSimpleBLEMIDIConnection read FTestPort write FTestPort;
property Idx: ni read FIdx write SetIdx;
property Mac6: string read FMac write FMac;
procedure DOExecute;override;
function Prog: boolean;
function Test: boolean;
property DoNotTest: boolean read FDoNotTest write FDoNotTest;
procedure CapHook(ccStatus: TConsoleCaptureStatus; sBufferData: string);
property NoDelay: boolean read FNoDelay write FNodelay;
procedure InitExpense; override;
end;
implementation
{ TBLEProgrammingCommand }
procedure TBLEProgrammingCommand.CapHook(ccStatus: TConsoleCaptureStatus;
sBufferData: string);
begin
case ccStatus of
ccStart:
Output := '';
ccProgress:
Output := output + sBufferData;
end;
end;
procedure TBLEProgrammingCommand.DOExecute;
var
cp: TSimpleCommPortConnection;
begin
Debug.Log(self, 'Command--Begin');
if idx < 1 then
raise ECritical.Create('Set IDX on '+self.ClassName);
if mac6 = '' then
raise Ecritical.Create('Set Mac6');
Debug.Log(self, 'Command--Check COMM Port');
try
cp := TSimpleCommPortConnection.create;
try
if port = 'COM34' then
debug.log(self, 'com34');
cp.EndPoint := port;
cp.Connect;
finally
cp.free;
end;
except
on E: exception do begin
cresult := false;
self.status := port+' Failed COM Test: '+E.message;
exit;
end;
end;
CResult := true;
try
if port='COM32' then
debug.log(self, 'here');
if prog then begin
if not DoNotTest then
test;
end;
except
on e: Exception do begin
CResult := false;
self.Status := 'Exception!';
end;
end;
if not CResult then begin
self.Status := 'Retrying...';
if prog then begin
if not DoNotTest then
test;
end;
end;
end;
procedure TBLEProgrammingCommand.InitExpense;
begin
inherited;
CPUExpense := 0.0;
//MemoryExpense := 1.0;
end;
function TBLEProgrammingCommand.Prog: boolean;
var
ex: exe.Tcmd_RunExe;
sProg: string;
sStatusFile: string;
sStatus: string;
begin
inherited;
Debug.Log(self, 'Command--Prog--Latch OFF');
if not LatchAlwaysOn then begin
lc.BitOff(LatchBit);
sleep(1000);
end;
Debug.Log(self, 'Command--Prog--Latch ON');
if not LatchAlwaysOn then begin
lc.BitOn(LatchBit);
sleep(2000);
end else
if assigned(lc) then begin
Debug.Log(self, 'Command--Prog--LATCH IS ALWAYS ON!');
lc.SetValue($ff);
end;
result := true;
CResult := true;
Debug.Log(self, 'Command--Prog--Program...');
Status := 'Program...';
sStatusFile := slash(extractfilepath(loadBat))+'result_'+inttostr(idx)+'.txt';
if fileexists(sStatusFIle) then
deletefile(sStatusFile);
Debug.Log(self, 'Command--Prog--Program...Run...');
RunProgramAndWait(LoadBat, Port+' '+Mac6+' '+inttostr(idx), extractfilepath(LoadBat), true, true, false);
Debug.Log(self, 'Command--Prog--Program...Run...Status...');
if not fileexists(sStatusFile) then begin
CResult := false;
Status := 'Failed to find status of operation';
exit;
end;
sStatus := LoadStringFromFile(sStatusFile);
if zpos('failed', lowercase(sStatus)) >=0 then begin
Status := sStatus;
CResult := false;
result := false;
exit;
end else
CResult := true;
output := sStatus;
Debug.Log(self, 'Command--Prog--Program...Run..Status...'+output);
//RunAndCapture('"'+LoadBat+'" '+Port+' '+inttostr(idx), caphook);
//RunAndCapture('c:\test.bat x y', caphook);
status := output;
// ex := Tcmd_RunExe.Create;
// try
// ex.Prog := LoadBat;
// ex.Params := Port+' '+inttostr(idx);
// ex.Hide := true;
// ex.ConsoleRedirect := true;
// ex.CaptureConsoleoutput := true;
// ex.Start;
// ex.WaitFor;
// output := ex.ConsoleOutput;
//
// finally
// ex.Free;
// end;
//SLEEP(3000);
end;
procedure TBLEProgrammingCommand.SetIdx(const Value: ni);
begin
FIdx := Value;
Resources.SetResourceUsage('BUILD'+inttostr(FIdx),1.0);
end;
function TBLEProgrammingCommand.Test: boolean;
var
bleServer: TSimpleCommportConnection;
bleClient: TSimpleBLEMIDIConnection;
a: array[0..2] of byte;
b: array[0..2] of byte;
t: ni;
tm, tmStart: ticker;
begin
Debug.Log(self, 'Command--Test');
bleClient := TestPort;
bleClient.Lock;
try
result := true;
tmStart := getticker;
tm := tmStart;
{$IFDEF DO_TEST1}
Debug.Log(self, 'Command--Test 1');
Status := 'Test 1...';
bleServer := TSimpleCommPortConnection.create;
try
// bleServer.Endpoint := Port;
// bleServer.Connect;
// bleServer.baudrate := 31250;
bleClient.EndPoint := 'FF736A'+Mac6;
Debug.Log(self, 'Command--Test '+bleClient.EndPoint);
if bleClient.Connect then begin
result := true;
CRESULT := true;
// exit;
end else begin
Debug.Log(self, 'Command--Failed to connect LOG: '+Self.TestPort.selflog.text);
result := false;
CRESULT := false;
STATUS := 'Failed to connect '+Self.TestPort.selflog.text;
// exit;
end;
{ a[0] := $80;
a[1] := $66;
a[2] := $77;
b[0] := 0;
b[1] := 0;
b[2] := 0;
bleClient.SendData(@a[0], 3, true);
if not bleServer.WaitForData(8000) then begin
Cresult := false;
result := false;
Status := 'Failed to receive data that was sent.';
end else begin
bleServer.ReadData(@b[0], 3, true);
for t:= 0 to high(a) do begin
if a[t] <> b[t] then begin
result := false;
Cresult := false;
Status := 'Data did not match';
exit;
end;
end;
end;}
if not NoDelay then begin
tmStart := getticker;
repeat
Status := 'Wait '+inttostr(round(gettimesince(tmStart)/PROG_WAIT * 100))+'%';
Debug.Log(self, status);
sleep(500);
until gettimesince(tmSTart) > PROG_WAIT;
end;
bleClient.Disconnect;
finally
bleServer.free;
end;
{$ENDIF}
bleClient.CommandReset;
tmStart := getticker;
if not NoDelay then begin
repeat
Status := 'Wait '+inttostr(round(gettimesince(tmStart)/PROG_WAIT * 100))+'%';
sleep(200);
until gettimesince(tmSTart) > PROG_WAIT;
end;
bleClient.CommandReset;
Debug.Log(self, 'Command--Test 2');
Status := 'Test 2...';
bleServer := TSimpleCommPortConnection.create;
try
// bleServer.Endpoint := Port;
// bleServer.Connect;
// bleServer.baudrate := 31250;
bleClient.EndPoint := 'FF736A'+Mac6;
Debug.Log(self, 'Command--Test 2...'+bleClient.EndPoint);
if bleClient.Connect then begin
result := true;
CRESULT := true;
if not LatchAlwaysOn then
lc.BitOff(LatchBit);//turn off power to device when successful to reduce interference from devices that get 2nd chances.
// exit;
end else begin
result := false;
CRESULT := false;
STATUS := 'Failed to connect '+Self.TestPort.selflog.text;
Debug.Log(self, 'Command--Test 2...FAILED TO CONNECT LOG: '+status);
// exit;
end;
{ a[0] := $80;
a[1] := $66;
a[2] := $77;
b[0] := 0;
b[1] := 0;
b[2] := 0;
bleClient.SendData(@a[0], 3, true);
if not bleServer.WaitForData(8000) then begin
Cresult := false;
result := false;
Status := 'Failed to receive data that was sent.';
end else begin
bleServer.ReadData(@b[0], 3, true);
for t:= 0 to high(a) do begin
if a[t] <> b[t] then begin
result := false;
Cresult := false;
Status := 'Data did not match';
exit;
end;
end;
end;}
bleClient.Disconnect;
finally
bleServer.free;
end;
finally
bleClient.Unlock;
end;
end;
end.