Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

[r508]: uNewWatch.pas Maximize Restore History

Download this file

uNewWatch.pas    663 lines (618 with data), 17.8 kB

  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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
(*
* The contents of this file are subject to the Mozilla Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
* License for the specific language governing rights and limitations
* under the License.
*
* The Initial Developer of this code is John Hansen.
* Portions created by John Hansen are Copyright (C) 2009 John Hansen.
* All Rights Reserved.
*
*)
unit uNewWatch;
{$IFDEF FPC}
{$MODE Delphi}
{$ENDIF}
interface
uses
{$IFNDEF FPC}
DataAnalysis,
{$ELSE}
LResources,
{$ENDIF}
Classes, Controls, Forms, StdCtrls, ExtCtrls, Buttons, ComCtrls,
uSpirit, uProgram, Dialogs, Menus;
type
TVarControls = record
CheckBox : TCheckBox;
Edit : TEdit;
end;
TfrmNewWatch = class(TForm)
btnPollNow: TButton;
btnCheckAll: TButton;
btnCheckNone: TButton;
btnPollRegular: TSpeedButton;
TimeBox: TComboBox;
Timer1: TTimer;
btnGraph: TSpeedButton;
chkIfActive: TCheckBox;
chkSyncSeries: TCheckBox;
lblSource: TLabel;
cboSource: TComboBox;
lblValue: TLabel;
edtValue: TEdit;
udValue: TUpDown;
btnAddWatch: TButton;
pagMain: TPageControl;
btnRemoveWatch: TButton;
btnClear: TButton;
btnHelp: TButton;
menuPopup: TPopupMenu;
mniOpenSym: TMenuItem;
dlgOpen: TOpenDialog;
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure btnPollNowClick(Sender: TObject);
procedure btnCheckAllClick(Sender: TObject);
procedure btnCheckNoneClick(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
procedure btnPollRegularClick(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure TimeBoxChange(Sender: TObject);
procedure btnGraphClick(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure FormActivate(Sender: TObject);
procedure FormDeactivate(Sender: TObject);
procedure btnAddWatchClick(Sender: TObject);
procedure cboSourceChange(Sender: TObject);
procedure btnRemoveWatchClick(Sender: TObject);
procedure edtValueExit(Sender: TObject);
procedure edtValueKeyPress(Sender: TObject; var Key: Char);
procedure btnClearClick(Sender: TObject);
procedure btnHelpClick(Sender: TObject);
procedure mniOpenSymClick(Sender: TObject);
private
{ Private declarations }
{$IFNDEF FPC}
fGraph : TfrmDataAnalysis;
{$ENDIF}
fNewData : TStrings;
fWatchCount : Integer;
fWatchedProgram : TProgram;
fOldProgram : TProgram;
procedure UpdateGraph;
procedure AdjustRangeOfValueSlider(source : Integer);
procedure InitSources;
function GetSourceIndex: Integer;
procedure UpdateControls;
procedure AddVariableHint(sht : TTabsheet; i : integer);
procedure PopulateVariables(cbo : TCombobox);
procedure cboNXTVarChange(Sender: TObject);
procedure LoadWatchedProgram;
procedure RemoveAllWatches;
public
{ Public declarations }
procedure GraphDestroyed;
end;
var
frmNewWatch: TfrmNewWatch;
implementation
{$IFNDEF FPC}
{$R *.DFM}
{$ENDIF}
uses
SysUtils, Graphics, uBasicPrefs, uMiscDefines,
brick_common, rcx_constants, uSources, uLocalizedStrings, uCommonUtils,
uGlobals, uEditorUtils, Variants;
var
busy : boolean = false;
procedure TfrmNewWatch.btnPollNowClick(Sender: TObject);
var
i, ival : integer;
val : variant;
fval : Single;
tmpStr, tmpStr2 : string;
temp: TComponent;
idx, src, num : Integer;
procedure FormatIntegerValue;
begin
ival := val;
tmpStr := Format(BrickWatchSources[LocalBrickType][idx].Name + '_%3.3d: %d', [num, ival]);
tmpStr2 := Format('%6d', [ival]);
end;
begin
if busy then exit; // Avoid polling while polling
busy := true;
try
fNewData.Clear;
for i := ComponentCount - 1 downto 0 do
begin
temp := Components[i];
if (temp is TCheckBox) and TCheckBox(temp).Visible and
not ((temp = chkIfActive) or (temp = chkSyncSeries)) and
TCheckBox(temp).Checked then
begin
// the sheet this checkbox is on is the source and
// the checkbox is the value
idx := Integer(cboSource.Items.Objects[TComboBox(TCheckBox(temp).Parent.Controls[4]).ItemIndex]);
// idx := TCheckBox(temp).Parent.Tag;
// src := WatchSources[idx].SourceNum;
src := idx;
num := TUpDown(TCheckBox(temp).Parent.Controls[3]).Position;
val := BrickComm.Poll(src, num);
if IsNXT and (idx = 0) then begin
if VarType(val) in [varSingle, varDouble] then begin
fVal := val;
tmpStr2 := StripTrailingZeros(Format('%.4f', [fval]));
tmpStr := TComboBox(TCheckBox(temp).Parent.Controls[5]).Items[num] + ': ' + tmpStr2;
end
else if VarType(val) = varString then
begin
tmpStr2 := val;
tmpStr := TComboBox(TCheckBox(temp).Parent.Controls[5]).Items[num] + ': ' + tmpStr2;
end
else
FormatIntegerValue;
end
else
FormatIntegerValue;
fNewData.Add(tmpStr);
TEdit(TCheckBox(temp).Parent.Controls[1]).Text := tmpStr2;
end;
end;
{
for i := Low(fVarArray) to High(fVarArray) do
begin
if fVarArray[i].CheckBox.Checked then
begin
val := BrickComm.GetVariableValue(i);
tmpStr := Format('Var %d: %d', [i, val]);
fNewData.Add(tmpStr);
fVarArray[i].Edit.Text := Format('%6d',[val]);
end;
end;
}
UpdateGraph;
busy := false;
except
ShowMessage(sWatchError);
Timer1.Enabled := false;
btnPollRegular.Down := false;
busy := false;
end;
end;
procedure TfrmNewWatch.btnCheckAllClick(Sender: TObject);
var
i: Integer;
temp: TComponent;
begin
for i := ComponentCount - 1 downto 0 do
begin
temp := Components[i];
if (temp is TCheckBox) and TCheckBox(temp).Visible and
not ((temp = chkIfActive) or (temp = chkSyncSeries)) then
TCheckBox(temp).Checked := true;
end;
end;
procedure TfrmNewWatch.btnCheckNoneClick(Sender: TObject);
var i: Integer;
temp: TComponent;
begin
for i := ComponentCount - 1 downto 0 do
begin
temp := Components[i];
if (temp is TCheckBox) then
TCheckBox(temp).Checked := false;
end;
end;
procedure TfrmNewWatch.Timer1Timer(Sender: TObject);
begin
btnPollNowClick(Self);
end;
procedure TfrmNewWatch.btnPollRegularClick(Sender: TObject);
begin
Timer1.Enabled := btnPollRegular.Down;
end;
procedure TfrmNewWatch.FormShow(Sender: TObject);
begin
LoadWatchedProgram;
InitSources;
btnPollRegular.Down := false;
Timer1.Enabled := false;
busy := false;
UpdateControls;
end;
procedure TfrmNewWatch.FormCreate(Sender: TObject);
begin
fNewData := TStringList.Create;
fWatchedProgram := TProgram.Create;
{$IFNDEF FPC}
fGraph := nil;
{$ENDIF}
fWatchCount := 0;
TimeBox.ItemIndex := 3;
end;
procedure TfrmNewWatch.TimeBoxChange(Sender: TObject);
begin
case TimeBox.ItemIndex of
0: Timer1.Interval := 100;
1: Timer1.Interval := 200;
2: Timer1.Interval := 500;
3: Timer1.Interval := 1000;
4: Timer1.Interval := 2000;
5: Timer1.Interval := 5000;
6: Timer1.Interval := 10000;
end;
end;
procedure TfrmNewWatch.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Timer1.Enabled := false;
end;
procedure TfrmNewWatch.btnGraphClick(Sender: TObject);
begin
{$IFNDEF FPC}
if not btnGraph.Down then
begin
// close graph form and nil our pointer
if Assigned(fGraph) then
begin
fGraph.Close;
fGraph := nil;
end;
end
else
begin
// create graph form and show it
fGraph := TfrmDataAnalysis.Create(Application);
fGraph.FromNewWatch := True;
fGraph.WatchPoints := WatchPoints;
fGraph.SyncSeries := chkSyncSeries.Checked;
fGraph.DataIsXY := False;
fGraph.Show;
end;
{$ENDIF}
end;
procedure TfrmNewWatch.FormDestroy(Sender: TObject);
begin
FreeAndNil(fWatchedProgram);
fNewData.Free;
end;
procedure TfrmNewWatch.LoadWatchedProgram;
var
fname, name, tmp : string;
begin
fname := GetActiveEditorFilename;
// is there a program running on the NXT?
if BrickComm.GetCurrentProgramName(name) and (name <> '') then
begin
tmp := ExtractFileName(fname);
if Pos(ChangeFileExt(name, ''), tmp) > 0 then
name := tmp;
fname := ExtractFilePath(fname) + name;
end;
ReadSymbolFile(fWatchedProgram, fname);
end;
procedure TfrmNewWatch.UpdateGraph;
begin
{$IFNDEF FPC}
if Assigned(fGraph) then
begin
fGraph.AddNewData(fNewData);
end;
{$ENDIF}
end;
procedure TfrmNewWatch.GraphDestroyed;
begin
{$IFNDEF FPC}
// called by data analysis form when it closes
if Assigned(fGraph) then
begin
fGraph := nil;
btnGraph.Down := False;
end;
{$ENDIF}
end;
procedure TfrmNewWatch.FormActivate(Sender: TObject);
begin
fOldProgram := BrickComm.TheProgram;
// make sure the variable watch event handlers are hooked up
BrickComm.TheProgram := fWatchedProgram;
if chkIfActive.Checked then
Timer1.Enabled := btnPollRegular.Down;
end;
procedure TfrmNewWatch.FormDeactivate(Sender: TObject);
begin
BrickComm.TheProgram := fOldProgram;
if chkIfActive.Checked then
Timer1.Enabled := False;
end;
procedure TfrmNewWatch.btnAddWatchClick(Sender: TObject);
var
sht: TTabSheet;
edtWatch: TEdit;
chkWatch: TCheckBox;
edtVal: TEdit;
udVal: TUpDown;
cboSrc: TComboBox;
cboNXTVar: TComboBox;
num : string;
scale_amount : double;
begin
scale_amount := Screen.PixelsPerInch / 96;
Inc(fWatchCount);
num := Format('%3.3d', [fWatchCount]);
sht := TTabSheet.Create(Self);
edtWatch := TEdit.Create(Self);
chkWatch := TCheckBox.Create(Self);
edtVal := TEdit.Create(Self);
udVal := TUpDown.Create(Self);
cboSrc := TComboBox.Create(Self);
cboNXTVar := TComboBox.Create(Self);
with sht do
begin
Name := 'sht' + num;
Parent := pagMain;
PageControl := pagMain;
Caption := num;
Tag := GetSourceIndex;
HelpContext := 3615;
end;
with chkWatch do
begin
Name := 'chkWatch' + num;
Parent := sht;
Left := Trunc(8 * scale_amount);
Top := Trunc(16 * scale_amount);
Width := Trunc(21 * scale_amount);
Height := Trunc(21 * scale_amount);
TabOrder := 1;
Caption := '';
Tag := udValue.Position;
HelpContext := 3616;
end;
with edtWatch do
begin
Name := 'edtWatch' + num;
Parent := sht;
Text := '';
Left := Trunc(32 * scale_amount);
Top := Trunc(16 * scale_amount);
Width := Trunc(154 * scale_amount);
Height := Trunc(21 * scale_amount);
AutoSize := False;
{$IFNDEF FPC}
Font.Charset := DEFAULT_CHARSET;
{$ENDIF}
Font.Color := clWindowText;
Font.Height := -12;
Font.Name := 'Courier New';
Font.Style := [];
ParentFont := False;
ReadOnly := True;
TabOrder := 2;
HelpContext := 3617;
end;
with edtVal do
begin
Name := 'edtValue'+num;
Parent := sht;
Left := Trunc(190 * scale_amount);
Top := Trunc(16 * scale_amount);
Width := Trunc(50 * scale_amount);
Height := Trunc(21 * scale_amount);
MaxLength := 5;
TabOrder := 3;
Text := IntToStr(udValue.Position);
OnKeyPress := edtValueKeyPress;
HelpContext := 3618;
ReadOnly := True;
Visible := not (IsNXT and (sht.Tag = 0));
end;
with udVal do
begin
Name := 'udValue' + num;
Parent := sht;
Left := Trunc(240 * scale_amount);
Top := Trunc(16 * scale_amount);
Width := Trunc(15 * scale_amount);
Height := Trunc(21 * scale_amount);
Associate := edtVal;
Min := udValue.Min;
Max := udValue.Max;
Position := udValue.Position;
TabOrder := 4;
Thousands := False;
Wrap := False;
HelpContext := 3619;
Visible := not (IsNXT and (sht.Tag = 0));
end;
with cboSrc do
begin
Name := 'cboSource' + num;
Parent := sht;
if IsNXT and (sht.Tag = 0) then
begin
Left := Trunc(340 * scale_amount);
Width := Trunc(140 * scale_amount);
end
else
begin
Left := Trunc(260 * scale_amount);
Width := Trunc(160 * scale_amount);
end;
Top := Trunc(16 * scale_amount);
Height := Trunc(21 * scale_amount);
Style := csDropDownList;
ItemHeight := 13;
TabOrder := 5;
Items.Assign(cboSource.Items);
ItemIndex := cboSource.ItemIndex;
Enabled := False;
HelpContext := 3620;
end;
with cboNXTVar do
begin
Name := 'cboNXTVar'+num;
Parent := sht;
Left := Trunc(190 * scale_amount);
Top := Trunc(16 * scale_amount);
Width := Trunc(146 * scale_amount);
Height := Trunc(21 * scale_amount);
Style := csDropDownList;
ItemHeight := 13;
TabOrder := 3;
Visible := IsNXT and (sht.Tag = 0);
OnChange := cboNXTVarChange;
end;
if IsNXT and (sht.Tag = 0) then
begin
AddVariableHint(sht, udValue.Position);
PopulateVariables(cboNXTVar);
if udValue.Position < cboNXTVar.Items.Count then
cboNXTVar.ItemIndex := udValue.Position;
end;
UpdateControls;
end;
procedure TfrmNewWatch.cboSourceChange(Sender: TObject);
begin
AdjustRangeOfValueSlider(GetSourceIndex);
UpdateControls;
end;
procedure TfrmNewWatch.AdjustRangeOfValueSlider(source: Integer);
begin
udValue.Position := BrickWatchSources[LocalBrickType][source].VMin;
// crud. poll only accepts a byte value 0..255
// udValue.Min := Max(BrickWatchSources[LocalBrickType][source].VMin, 0);
// udValue.Max := Min(BrickWatchSources[LocalBrickType][source].VMax, 255);
udValue.Min := BrickWatchSources[LocalBrickType][source].VMin;
udValue.Max := BrickWatchSources[LocalBrickType][source].VMax;
if (source = 0) and IsNXT then
begin
udValue.Min := 0;
udValue.Max := fWatchedProgram.Dataspace.Count - 1;
end;
end;
procedure TfrmNewWatch.InitSources;
var
i : Integer;
WS : WatchSources;
begin
cboSource.Items.Clear;
WS := BrickWatchSources[LocalBrickType];
for i := low(WS) to high(WS) do
begin
if WS[i].Has then
cboSource.Items.AddObject(WS[i].Name, TObject(i));
end;
end;
function TfrmNewWatch.GetSourceIndex: Integer;
begin
Result := Integer(cboSource.Items.Objects[cboSource.ItemIndex]);
end;
procedure TfrmNewWatch.btnRemoveWatchClick(Sender: TObject);
begin
pagMain.ActivePage.Free;
UpdateControls;
end;
procedure TfrmNewWatch.edtValueExit(Sender: TObject);
var
i : Integer;
begin
// make sure text is valid
i := StrToIntDef(edtValue.Text, -32768);
if i > udValue.Max then
udValue.Position := udValue.Max
else if i < udValue.Min then
udValue.Position := udValue.Min;
edtValue.Text := IntToStr(udValue.Position);
end;
procedure TfrmNewWatch.edtValueKeyPress(Sender: TObject; var Key: Char);
begin
if not (Key in [#8, #16, '0'..'9']) then
Key := #0;
end;
procedure TfrmNewWatch.UpdateControls;
var
bEnable : Boolean;
begin
btnAddWatch.Enabled := cboSource.ItemIndex <> -1;
bEnable := pagMain.PageCount > 0;
btnRemoveWatch.Enabled := bEnable;
btnCheckAll.Enabled := bEnable;
btnCheckNone.Enabled := bEnable;
btnPollNow.Enabled := bEnable;
btnPollRegular.Enabled := bEnable;
end;
procedure TfrmNewWatch.btnClearClick(Sender: TObject);
var
i: Integer;
temp: TComponent;
begin
for i := ComponentCount - 1 downto 0 do
begin
temp := Components[i];
if (temp is TEdit) and (Pos('edtWatch', TEdit(temp).Name) = 1) then
TEdit(temp).Text := '';
end;
end;
procedure TfrmNewWatch.btnHelpClick(Sender: TObject);
begin
Application.HelpContext(HelpContext);
end;
procedure TfrmNewWatch.AddVariableHint(sht: TTabsheet; i : integer);
var
tmp : string;
begin
if IsNXT then
begin
if fWatchedProgram.Dataspace.Count > i then
begin
tmp := fWatchedProgram.Dataspace[i].PrettyName;
sht.Hint := tmp;
end;
end;
end;
procedure TfrmNewWatch.PopulateVariables(cbo: TCombobox);
var
i : integer;
begin
if IsNXT then
begin
cbo.Items.Clear;
for i := 0 to fWatchedProgram.Dataspace.Count - 1 do
cbo.Items.Add(fWatchedProgram.Dataspace[i].PrettyName);
end;
end;
procedure TfrmNewWatch.cboNXTVarChange(Sender: TObject);
var
C : TComboBox;
begin
// change the edit value and ud position
C := TCombobox(Sender);
TUpDown(C.Parent.Controls[3]).Position := C.ItemIndex;
C.Parent.Hint := C.Text;
end;
procedure TfrmNewWatch.mniOpenSymClick(Sender: TObject);
begin
if dlgOpen.Execute then
begin
RemoveAllWatches;
ReadSymbolFile(fWatchedProgram, dlgOpen.Filename);
end;
end;
procedure TfrmNewWatch.RemoveAllWatches;
begin
while pagMain.ActivePage <> nil do
pagMain.ActivePage.Free;
UpdateControls;
end;
{$IFDEF FPC}
initialization
{$i uNewWatch.lrs}
{$ENDIF}
end.