-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathunCtrlPlatMain.pas
4043 lines (3520 loc) · 113 KB
/
unCtrlPlatMain.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
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
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
unit unCtrlPlatMain;
interface
uses Vcl.Dialogs, CPort, System.Classes,
System.Actions, Vcl.ActnList, System.ImageList, Vcl.ImgList, Vcl.Controls,
Vcl.Menus, Vcl.ComCtrls, Vcl.Imaging.pngimage, Vcl.Forms, Vcl.StdCtrls,
Vcl.Buttons,Winapi.Windows,unBlock,Winapi.Messages,System.SysUtils, System.Variants,
unContainerBlock,Vcl.Graphics, Vcl.TabNotBk, Vcl.ExtCtrls, Vcl.WinXCtrls,
Vcl.Imaging.jpeg, Vcl.ToolWin, Vcl.Samples.Spin,
Vcl.OleCtrls, SHDocVw, MahUSB;
Const ProjectStart='10/10/2017';
Const Version='0.96 (Βήτα έκδοση 27/02/2022) - ';
Const Programmer='© 2017-2022 Despoinidis Christos';
Const Progname='Έλεγχος συσκευών Arduino';
Const Onlybluetooth=FALSE;
const WM_REFRESH_MSG = WM_USER + 1;
const WM_REFVARS_MSG = WM_USER + 252;
Const DevBckColor=TColor($00003333);
Const StartBckColor=clGreen;
Const MoveBckColor=clBlue;
Const LoopBckColor=clMaroon;
Const CtrlBckColor=TColor($00008282);
Const VarbckColor=TColor($00FF0080);
type
TRoboCmd=record
ComndID:Integer;
Param1:Integer;
Param2:Integer;
end;
RoboCommands=Array of TRoboCmd;
TfrmRoboLang = class(TForm)
ListBox1: TListBox;
MainMenu1: TMainMenu;
N1: TMenuItem;
N2: TMenuItem;
N3: TMenuItem;
N4: TMenuItem;
N5: TMenuItem;
N6: TMenuItem;
N7: TMenuItem;
ImageList1: TImageList;
FlowPanel2: TFlowPanel;
SpeedButton1: TSpeedButton;
ActionList1: TActionList;
acopen: TAction;
acsave: TAction;
acsaveas: TAction;
acexit: TAction;
SpeedButton2: TSpeedButton;
acSerialport: TAction;
N8: TMenuItem;
N9: TMenuItem;
acConnect: TAction;
SpeedButton3: TSpeedButton;
ComPort1: TComPort;
Splitter1: TSplitter;
OpenDialog1: TOpenDialog;
SaveDialog1: TSaveDialog;
SBar: TStatusBar;
acSndLeft: TAction;
acSndRight: TAction;
SpeedButton6: TSpeedButton;
acStop: TAction;
SpeedButton7: TSpeedButton;
acNew: TAction;
SpeedButton8: TSpeedButton;
N10: TMenuItem;
acSetServer: TAction;
Timer1: TTimer;
TNBook: TTabbedNotebook;
ScrollBox1: TScrollBox;
Image2: TImage;
Splitter2: TSplitter;
ScrollBox2: TScrollBox;
pnlLCD: TPanel;
Image1: TImage;
Label1: TLabel;
LCDadd: TBitBtn;
lblLCD: TLabel;
BalloonHint1: TBalloonHint;
pnlLaser: TPanel;
Image3: TImage;
Label2: TLabel;
lblLaser: TLabel;
LaserAdd: TBitBtn;
DevBook: TTabbedNotebook;
RadioGroup1: TRadioGroup;
pnlSound: TPanel;
Image4: TImage;
Label3: TLabel;
lblSound: TLabel;
SoundAdd: TBitBtn;
pnlUSonic: TPanel;
Image5: TImage;
Label4: TLabel;
lblUSonic: TLabel;
USonicAdd: TBitBtn;
pnlServo: TPanel;
Image6: TImage;
Label5: TLabel;
lblServo: TLabel;
ServoAdd: TBitBtn;
pnlSwitch: TPanel;
Image7: TImage;
Label6: TLabel;
lblSwitch: TLabel;
SwitchAdd: TBitBtn;
pnlTemp: TPanel;
Image8: TImage;
Label7: TLabel;
lblTemp: TLabel;
TempAdd: TBitBtn;
CategoryPanelGroup1: TCategoryPanelGroup;
variousPanel: TCategoryPanel;
CtrlPanel: TCategoryPanel;
LoopPanel: TCategoryPanel;
strtPanel: TCategoryPanel;
PageControl1: TPageControl;
TabSheet1: TTabSheet;
TabSheet2: TTabSheet;
CategoryPanelGroup2: TCategoryPanelGroup;
LCDpanel: TCategoryPanel;
LaserPanel: TCategoryPanel;
SoundPanel: TCategoryPanel;
USonicPanel: TCategoryPanel;
ServoPanel: TCategoryPanel;
SwitchPanel: TCategoryPanel;
TempPanel: TCategoryPanel;
TabSheet3: TTabSheet;
CategoryPanelGroup3: TCategoryPanelGroup;
VarPanel: TCategoryPanel;
ToolBar1: TToolBar;
ToolButton1: TToolButton;
ToolButton2: TToolButton;
Splitter3: TSplitter;
TreeView1: TTreeView;
Label8: TLabel;
Panel2: TPanel;
WebBrowser1: TWebBrowser;
ScrollBox3: TScrollBox;
ArduPinout: TImage;
pnlBMP: TPanel;
Image9: TImage;
Label9: TLabel;
lblBMP: TLabel;
BMPAdd: TBitBtn;
BMPPanel: TCategoryPanel;
AnalogInPanel: TCategoryPanel;
pnlAnalogIn: TPanel;
Image10: TImage;
Label10: TLabel;
lblAnalogIn: TLabel;
AnalogInAdd: TBitBtn;
Panel1: TPanel;
Button1: TButton;
Memo1: TMemo;
pnlDcMotor: TPanel;
Image11: TImage;
Label11: TLabel;
lblDcMotor: TLabel;
DcMotorAdd: TBitBtn;
DcMotorPanel: TCategoryPanel;
pnlRobot: TPanel;
Image12: TImage;
Label12: TLabel;
LblRobot: TLabel;
RobotAdd: TBitBtn;
DcRobotPanel: TCategoryPanel;
ArduMemo: TMemo;
acgetcode: TAction;
Timer2: TTimer;
acOptions: TAction;
N11: TMenuItem;
procedure FormCreate(Sender: TObject);
procedure ListBox1DblClick(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure acexitExecute(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure acConnectExecute(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure CategoryPanelGroup1MouseWheel(Sender: TObject; Shift: TShiftState;
WheelDelta: Integer; MousePos: TPoint; var Handled: Boolean);
procedure FormMouseWheel(Sender: TObject; Shift: TShiftState;
WheelDelta: Integer; MousePos: TPoint; var Handled: Boolean);
procedure ScrollBox1MouseWheel(Sender: TObject; Shift: TShiftState;
WheelDelta: Integer; MousePos: TPoint; var Handled: Boolean);
procedure acsaveExecute(Sender: TObject);
procedure acopenExecute(Sender: TObject);
procedure acsaveasExecute(Sender: TObject);
procedure BitBtn1Click(Sender: TObject);
procedure Edit1Exit(Sender: TObject);
procedure CategoryPanelGroup1MouseEnter(Sender: TObject);
procedure ScrollBox1MouseEnter(Sender: TObject);
procedure N7Click(Sender: TObject);
procedure ComPort1RxChar(Sender: TObject; Count: Integer);
procedure acStopExecute(Sender: TObject);
procedure acNewExecute(Sender: TObject);
procedure FormResize(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
procedure SBarDblClick(Sender: TObject);
procedure BitBtn2Click(Sender: TObject);
procedure ComPort1Exception(Sender: TObject; TComException: TComExceptions;
ComportMessage: string; WinError: Int64; WinMessage: string);
procedure LCDaddClick(Sender: TObject);
procedure DevBookChange(Sender: TObject; NewTab: Integer;
var AllowChange: Boolean);
procedure RadioGroup1Click(Sender: TObject);
procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
procedure ToolButton1Click(Sender: TObject);
procedure ToolButton2Click(Sender: TObject);
procedure ScrollBox2MouseWheel(Sender: TObject; Shift: TShiftState;
WheelDelta: Integer; MousePos: TPoint; var Handled: Boolean);
procedure TreeView1Change(Sender: TObject; Node: TTreeNode);
procedure TNBookChange(Sender: TObject; NewTab: Integer;
var AllowChange: Boolean);
procedure acgetcodeExecute(Sender: TObject);
procedure Timer2Timer(Sender: TObject);
procedure acOptionsExecute(Sender: TObject);
private
cmb:TComboBox;
firstopen:boolean;
NewVarName:String;
ComIdle:boolean;
ChkByte:Byte;
srvCodeRec:boolean;
frmLoadPos:Boolean;
FMotors: Integer;
UsbClass:TUsbClass;
procedure OnRefVarRequest(var Msg: TMessage); message WM_REFVARS_MSG;
function GetStartBlock: TDspBlock;
function BTgetChar: Char;
procedure BTPutChar(c: Char;dochk:boolean=true);
function BTOpen:boolean;
function BTCharReady: Boolean;
procedure SendCommands(blck: TDspBlock);
procedure CreateMoveCommands;
procedure CreateStartCommands;
procedure CreateLoopCommands;
procedure CreateControlCommands;
procedure CreateCommands;
procedure CreateVariousCommands;
procedure BTSendInteger(k: integer);
procedure splitInteger(k: integer; var b1, b2: char);
procedure emptycomps;
procedure SendcmdsTest(blck: TDspBlock);
procedure DoCheckByte(c: Char);
procedure DoNewDoc;
function Hasblocks: Boolean;
procedure MySetCaption;
procedure MyMessage(var Msg: TWMMove); message WM_MOVE;
procedure SaveFormPos;
function LoadFormPos:boolean;
procedure SelectNextPort;
procedure LoadBTPort;
procedure SaveBTPort;
function GetBTPortFromFile: string;
procedure CreateLCDCommands;
procedure addDevClick(Sender: TObject);
procedure DeviceChanged;
procedure CreateLaserCommands;
procedure CreateSoundCommands;
procedure CreateUltraSonicCommands;
function GetActiveDeviceParam(DevNo, PrmNo: Integer): Integer;
procedure CreateServoCommands;
procedure ResetArduino;
procedure CreateSwitchCommands;
procedure CreateTempCommands;
procedure CreateBlocksFromVars;
procedure CreateVarCommands;
function MakeInt(v1, v2: byte): integer;
procedure DeleteVarBlocks;
procedure CompReadError(Reader: TReader; const Message: string;
var Handled: Boolean);
procedure LoadNode(Node: TTreeNode);
procedure LoadNodeName(NodeName: String);
procedure NewMaxPins;
procedure Refname(Reader: TReader; var name: string);
procedure CreateComp(Reader: TReader; ComponentClass: TComponentClass;
var Component: TComponent);
procedure CreateBMP180Commands;
procedure CreateAnalogInCommands;
procedure BurnArduino;
function GetArduType: String;
procedure ComPortChanged(Sender: tobject);
procedure CreateDcMotorCommands;
procedure CreateRobotCommands;
function getDeviceInclude(ComID:integer;DevId: integer): String;
procedure AddIncludeForDev(hd: Tstrings;ComID, DevId: Integer);
procedure AddDefinitionForDev(hd: Tstrings;ComID:Integer; DevId: Integer;devnm:string);
procedure AddSetupForDev(hd: Tstrings;ComID:Integer; DevId: Integer;devnm:string);
procedure GetCodeFromCommands(hd:Tstrings;blck: TDspBlock;ident:integer=1);
procedure AddFunctionsForDev(hd: Tstrings;ComID:Integer; DevId: Integer; devnm: string);
function fix(s: string;Devid:Integer): String;
procedure getVars(hd: TStrings);
procedure GetComPorts;
procedure showMyHint;
procedure checkArduino;
function getArduinoPath: String;
procedure VolumeEvent(const bInserted : boolean; const sDrive : string);
procedure ChangeEvent(const bInserted : boolean;
const ADevType,ADriverName, AFriendlyName : string);
{ Private declarations }
public
{ Public declarations }
function GetActiveDevicePins(DevTp:integer=-1): tStrings;
procedure AddDebug(s: String);
function getFullCaption: String;
procedure addDevices;
function getPanelCountLabel(pnl: TPanel): TLabel;
function getPanelAddButton(pnl: TPanel): TBitBtn;
/// <summary>TfrmRoboLang.CommandsByType
/// Enables or Disables Commands based on typeID
/// </summary>
/// <param name="typeid"> (Integer) </param>
/// <param name="Enable"> (Boolean) </param>
procedure CommandsByType(typeid: Integer; Enable: Boolean=True);
end;
Procedure AddDebug(s:String);
var
frmRoboLang: TfrmRoboLang;
blck:TDspBlock;
IsServer:boolean=false; //false;
ipAddr:String='';
BTComList:TStringList;
BTIndex:integer=-1;
MainPath:String;
implementation
uses Registry, system.ioutils,unAbout, inifiles,setupapi,math, unDevices, unHelpForms,DspUtils,unVariables,unBlockVar,
uUpdate,fserialLcd,fLaser,fSound,fUSonic,fServo,fSwitch,fTemp,fBMP180,fAnalogIn,fDcMotor,fRobot
, fOptions;
{$R *.dfm}
function GetComputerName: string;
var
buffer: array[0..MAX_COMPUTERNAME_LENGTH + 1] of Char;
Size: Cardinal;
begin
Size := MAX_COMPUTERNAME_LENGTH + 1;
Winapi.Windows.GetComputerName(buffer, Size);
Result := StrPas(buffer);
end;
Procedure SetupEnumAvailableComPorts(res:TstringList);
// Enumerates all serial communications ports that are available and ready to
// be used.
// For the setupapi unit see
// http://homepages.borland.com/jedi/cms/modules/apilib/visit.php?cid=4&lid=3
var
RequiredSize: Cardinal;
Guid: TGUID;
DevInfoHandle: HDEVINFO;
DeviceInfoData: TSPDevInfoData;
MemberIndex: Cardinal;
PropertyRegDataType: DWord;
RegProperty: Cardinal;
RegTyp: Cardinal;
Key: Hkey;
Info: TRegKeyInfo;
S1,S2: string;
hc: THandle;
GUIDSize : DWord;
begin
//If we cannot access the setupapi.dll then we return a nil pointer.
if not LoadsetupAPI then exit;
try
GUIDSize := 1;
// get 'Ports' class guid from name
if SetupDiClassGuidsFromName('Ports',@Guid,GUIDSize,RequiredSize) then begin
//get object handle of 'Ports' class to interate all devices
DevInfoHandle:=SetupDiGetClassDevs(@Guid,Nil,0,DIGCF_PRESENT);
if Cardinal(DevInfoHandle)<>Invalid_Handle_Value then begin
try
MemberIndex:=0;
//iterate device list
repeat
FillChar(DeviceInfoData,SizeOf(DeviceInfoData),0);
DeviceInfoData.cbSize:=SizeOf(DeviceInfoData);
//get device info that corresponds to the next memberindex
if Not SetupDiEnumDeviceInfo(DevInfoHandle,MemberIndex,DeviceInfoData) then
break;
//query friendly device name LIKE 'BlueTooth Communication Port (COM8)' etc
RegProperty:=SPDRP_FriendlyName;{SPDRP_Driver, SPDRP_SERVICE, SPDRP_ENUMERATOR_NAME,SPDRP_PHYSICAL_DEVICE_OBJECT_NAME,SPDRP_FRIENDLYNAME,}
SetupDiGetDeviceRegistryProperty(DevInfoHandle,DeviceInfoData,
RegProperty,
PropertyRegDataType,
NIL,0,RequiredSize);
SetLength(S1,RequiredSize);
if SetupDiGetDeviceRegistryProperty(DevInfoHandle,DeviceInfoData,
RegProperty,
PropertyRegDataType,
@S1[1],RequiredSize,RequiredSize) then begin
KEY:=SetupDiOpenDevRegKey(DevInfoHandle,DeviceInfoData,DICS_FLAG_GLOBAL,0,DIREG_DEV,KEY_READ);
if key<> INValid_Handle_Value then begin
FillChar(Info, SizeOf(Info), 0);
//query the real port name from the registry value 'PortName'
if RegQueryInfoKey(Key, nil, nil, nil, @Info.NumSubKeys,@Info.MaxSubKeyLen, nil, @Info.NumValues, @Info.MaxValueLen,
@Info.MaxDataLen, nil, @Info.FileTime) = ERROR_SUCCESS then begin
RequiredSize:= Info.MaxValueLen + 1;
SetLength(S2,RequiredSize);
if RegQueryValueEx(KEY,'PortName',Nil,@Regtyp,@s2[1],@RequiredSize)=Error_Success then begin
If (Pos('COM',S2)=1) then begin
//Test if the device can be used
// hc:=CreateFile(pchar('\\.\'+S2+#0),
// GENERIC_READ or GENERIC_WRITE,
// 0,
// nil,
// OPEN_EXISTING,
// FILE_ATTRIBUTE_NORMAL,
// 0);
// if hc <>INVALID_HANDLE_VALUE then begin
Res.Add(Strpas(PChar(S2))+': = '+StrPas(PChar(S1)));
// CloseHandle(hc);
// end;
end;
end;
end;
RegCloseKey(key);
end;
end;
Inc(MemberIndex);
until False;
//If we did not found any free com. port we return a NIL pointer.
finally
SetupDiDestroyDeviceInfoList(DevInfoHandle);
end;
end;
end;
finally
UnloadSetupApi;
end;
end;
procedure getComPortList(retst:TStrings);
var
reg: TRegistry;
st: Tstrings;
i: Integer;
begin
reg := TRegistry.Create;
try
reg.RootKey := HKEY_LOCAL_MACHINE;
reg.OpenKey('hardware\devicemap\serialcomm', False);
st := TstringList.Create;
try
reg.GetValueNames(st);
for i := 0 to st.Count - 1 do
if (Onlybluetooth and (pos('bt',Lowercase(st.strings[i]))>0)) or not Onlybluetooth then
retst.Add(reg.Readstring(st.strings[i]));
finally
st.Free;
end;
reg.CloseKey;
finally
reg.Free;
end;
end;
procedure LoadComponentFromFile(parent: TComponent; const FileName: TFileName; MyStream:TMemoryStream=Nil);forward;
Procedure AddDebug(s:String);
Begin
frmRoboLang.addDebug(s);
End;
function GetBlutoothComPort(BTComList:TStrings): String;
var S, S1: String;
Item,Item2: Integer;
Reg: TRegistry;
KeyList1, KeyList2: TStringList;
begin
result :='';
Reg := TRegistry.Create;
if Reg = nil then begin
AddDebug( 'Error creating Reg ' );
exit; end;
KeyList1 := TStringList.Create;
if KeyList1 = nil then begin
AddDebug( 'Error creating KeyList1 ' );
exit; end;
KeyList2 := TStringList.Create;
if KeyList2 = nil then begin
AddDebug( 'Error creating KeyList2 ' );
exit; end;
try
Reg.RootKey := HKEY_LOCAL_MACHINE;
S := '\SYSTEM\CurrentControlSet\Enum\BTHENUM\';
AddDebug( 'Key: ' + S );
try
Reg.OpenKeyReadOnly(S);
Reg.GetKeyNames(KeyList1);
For Item := 0 to KeyList1.Count-1 do
begin
S1 := KeyList1.Strings[Item];
Reg.OpenKeyReadOnly(S + S1);
Reg.GetKeyNames(KeyList2);
For Item2 := 0 to KeyList2.Count-1 do
begin
S1 := S + KeyList1.Strings[Item] + '\' + KeyList2.Strings[Item2] + '\Device Parameters';
Reg.OpenKeyReadOnly(S1);
If Reg.ValueExists('PortName') then
begin
S1 := Reg.ReadString('PortName');
if S1<>'' then
Begin
BTComList.Add(S1);
AddDebug( 'Bluetooth Port Added: ' + S1 );
End;
end else
AddDebug( 'Bluetooth Port NOT Added: ' + S1 );
end;
end;
except
AddDebug( 'Error finding Bluetooth' );
end;
finally
Reg.Free;
KeyList1.Free;
KeyList2.Free;
end;
if BTComList.Count>0 then
Begin
BTIndex:=0;
Result:=BTComList[0];
End;
end;
procedure TfrmRoboLang.DevBookChange(Sender: TObject; NewTab: Integer;
var AllowChange: Boolean);
begin
// allowchange:= NewTab>0;
end;
procedure TfrmRoboLang.DoCheckByte(c:Char);
Begin
ChkByte := ChkByte xor byte(c);
End;
procedure TfrmRoboLang.BTPutChar(c:Char;dochk:boolean=true);
begin
if dochk then
DoCheckByte(c);
Comport1.Write(c,1)
End;
function TfrmRoboLang.BTgetChar:Char;
var n:integer;
Begin
n:= ComPort1.Read(Result,1);
if n=0 then
result:=' ';
End;
procedure TfrmRoboLang.SendcmdsTest(blck:TDspBlock);
var nxtblock:TDspBlock;
Begin
NxtBlock:=blck;
while Assigned(nxtblock) do
Begin
Adddebug('Sending '+inttostr(NxtBlock.CommndID)
+' '+inttostr(NxtBlock.Param1)
+' '+inttostr(NxtBlock.Param2)
);
if NxtBlock is TDspContainerBlock then
SendcmdsTest(nxtblock.FirstBlock);
NxtBlock:=NxtBlock.LinkTo;
End;
End;
procedure TfrmRoboLang.BitBtn1Click(Sender: TObject);
begin
ScrollBox1.VertScrollBar.Range:=2000;
ScrollBox1.HorzScrollBar.Range:=2000;
end;
procedure TfrmRoboLang.BitBtn2Click(Sender: TObject);
var
AHeight, AWidth: Integer;
begin
AHeight := Max(ScrollBox1.VertScrollBar.Range, ScrollBox1.Height);
AWidth := Max(ScrollBox1.HorzScrollBar.Range, ScrollBox1.Width);
AddDebug(inttostr(AHeight));
AddDebug(inttostr(AWidth));
end;
function TfrmRoboLang.BTCharReady:Boolean;
var c:char;
Begin
result:= ComPort1.InputCount > 0 ; //doesnot work
Comport1.Read(c,1);
adddebug('Char:'+c);
End;
function TfrmRoboLang.BTOpen:boolean;
Var cp:String;
begin
result:=true;
if comport1.connected or (BTIndex=-1) then exit;
try
cp:=BTComList[BTIndex];
Comport1.Port:= cp;
except
end;
adddebug( cp);
try
comport1.Connected:=true;
except
on e:exception do
Begin
AddDebug(e.Message);
if firstopen then //bypass the bug 1st time open throws an exception
Begin
application.ProcessMessages;
firstopen:=false;
Result:=BTOpen;
exit;
End;
result:=false;
exit;
End;
end;
if comport1.connected then
adddebug('Success');
End;
procedure TfrmRoboLang.ScrollBox1MouseEnter(Sender: TObject);
begin
// scrollbox1.SetFocus;
end;
procedure TfrmRoboLang.ScrollBox1MouseWheel(Sender: TObject; Shift: TShiftState;
WheelDelta: Integer; MousePos: TPoint; var Handled: Boolean);
var
I: Integer;
begin
for I := 1 to Mouse.WheelScrollLines do
try
if WheelDelta > 0 then
ScrollBox1.Perform(WM_VSCROLL, SB_LINEUP, 0)
else
ScrollBox1.Perform(WM_VSCROLL, SB_LINEDOWN, 0);
finally
ScrollBox1.Perform(WM_VSCROLL, SB_ENDSCROLL, 0);
end;
end;
procedure TfrmRoboLang.ScrollBox2MouseWheel(Sender: TObject; Shift: TShiftState;
WheelDelta: Integer; MousePos: TPoint; var Handled: Boolean);
var
I: Integer;
begin
for I := 1 to Mouse.WheelScrollLines do
try
if WheelDelta > 0 then
TWinControl(Sender).Perform(WM_VSCROLL, SB_LINEUP, 0)
else
TWinControl(Sender).Perform(WM_VSCROLL, SB_LINEDOWN, 0);
finally
TWinControl(Sender).Perform(WM_VSCROLL, SB_ENDSCROLL, 0);
end;
end;
procedure TfrmRoboLang.splitInteger(k:integer;var b1:char;var b2:char);
Begin
b2:=char(trunc(k/256)); //high byte
b1:=char((k-(byte(b2)*256))); //low byte
End;
procedure TfrmRoboLang.Timer1Timer(Sender: TObject);
var CodeStream:TMemoryStream;
begin
timer1.Enabled:=false;
CodeStream:=TMemoryStream(timer1.tag);
timer1.tag:=0;
LoadComponentFromFile(Scrollbox1,'',CodeStream);
CodeStream.Free;
end;
procedure TfrmRoboLang.Timer2Timer(Sender: TObject);
begin
ArduMemo.Lines[ardumemo.Lines.Count-1]:= ArduMemo.Lines[ardumemo.Lines.Count-1]+'.';
end;
procedure TfrmRoboLang.TNBookChange(Sender: TObject; NewTab: Integer;
var AllowChange: Boolean);
begin
if newtab=2 then
TreeView1.FullExpand;
end;
procedure TfrmRoboLang.ToolButton1Click(Sender: TObject);
var t:TDspVarBlock;
varcolor:TColor;
cmdName:String;
avar:TArduVar;
function getLastBlockBottom:Integer;
var i,mx:integer;
wc:TWinControl;
Begin
mx:=40;
wc:=TWinControl(VarPanel.Controls[0]);
for i := 0 to wc.controlCount-1 do
if wc.Controls[i] is TDspBlock then
mx:=Max(mx,TDspBlock(wc.Controls[i]).Top+TDspBlock(wc.Controls[i]).Height);
Result:=mx+10;
End;
begin
// VarColor:=StringtoColor('$008A4B08');
VarColor:=HTMLtoColor('#084B8A');
//Create new Variable
t:= TDspVarBlock.create(Self);
t.Visible:=false;
t.parent:=VarPanel;
t.Color:=varcolor;
if assigned(sender) then
cmdName:=InputBox('Δώσε όνομα μεταβλητής','Όνομα','Μεταβλητή')
else
cmdName:=NewVarName;
t.CommandText:=cmdName;
t.ArduCmd:=cmdName;
t.param1question:=cmdName;
if assigned(sender) then
avar:=ArduVars.Newvar(cmdName)
else
avar:=ArduVars.GetVarByName(cmdName);
t.CommndID:=avar.VarID;
t.CommandColor:=clWhite;
t.TotalParams:=1;
t.Left:=10;
t.Top:=getLastBlockBottom;
t.Visible:=True;
end;
procedure TfrmRoboLang.ToolButton2Click(Sender: TObject);
//Διαγραφή Μεταβλητής
var i:integer;
wc:TWinControl;
bpos:Integer;
procedure repositionBlocks;
var i:integer;
Begin
for I := 0 to wc.controlCount-1 do
if wc.Controls[i] is TDspVarBlock then
if TDspVarBlock(wc.Controls[i]).Top> bpos then
TDspVarBlock(wc.Controls[i]).Top:=TDspVarBlock(wc.Controls[i]).Top-TDspVarBlock(wc.Controls[i]).Height-10;
End;
procedure KillCloneVarByName(nm:String);
Var i:Integer;
Container:TWinControl;
tb:TDspBlock;
Begin
Container:=Scrollbox1;
for I := Container.ControlCount-1 downto 0 do
if Container.Controls[i] is TDspVarBlock then
Begin
if sametext(nm,TDspVarBlock(Container.Controls[i]).param1question) then
TDspVarBlock(Container.Controls[i]).Free;// destroy only this
End
else if Container.Controls[i] is TDspblock Then
Begin
if assigned(TDspblock(Container.Controls[i]).VarParam1) and sametext(nm,TDspblock(Container.Controls[i]).VarParam1.param1question) then
Begin
tb:=TDspblock(Container.Controls[i]).VarParam1;
TDspblock(Container.Controls[i]).VarParam1:=nil;
tb.Free;
End;
if assigned(TDspblock(Container.Controls[i]).VarParam2) and sametext(nm,TDspblock(Container.Controls[i]).VarParam2.param2question) then
Begin
tb:=TDspblock(Container.Controls[i]).VarParam2;
TDspblock(Container.Controls[i]).VarParam2:=nil;
tb.Free;
End;
End;
End;
Begin
wc:=TWinControl(VarPanel.Controls[0]);
for i := wc.controlCount-1 downto 0 do
if wc.Controls[i] is TDspVarBlock then
if TDspVarBlock(wc.Controls[i]).ToDelete then
Begin
ArduVars.DeleteVar(TDspVarBlock(wc.Controls[i]).param1question);
bpos:=TDspVarBlock(wc.Controls[i]).Top;
KillCloneVarByName(TDspVarBlock(wc.Controls[i]).param1question);
TDspVarBlock(wc.Controls[i]).Free;
RepositionBlocks;
End;
end;
procedure TfrmRoboLang.LoadNodeName(NodeName: String);
var pth:String;
Begin
NodeName:=NodeName+'.htm';
pth:=extractfilepath(NodeName);
if not fileexists(pth) then
CreateDir(pth);
Label8.Caption:=NodeName;
if not fileexists(NodeName) then
NodeName:=Changefileext(NodeName,'.html');
if not fileexists(NodeName) then
NodeName:=Changefileext(NodeName,'.mht');
if not fileexists(NodeName) then
NodeName:=Changefileext(NodeName,'.mhtml');
if fileexists(NodeName) then
WebBrowser1.Navigate(NodeName)
else
Begin
pth:=TDirectory.GetParent(ExcludeTrailingPathDelimiter(pth));
WebBrowser1.Navigate(pth+'\Error.mht');
End;
End;
procedure TfrmRoboLang.LoadNode(Node: TTreeNode);
Var nm,pth:String;
Begin
pth:='';
if assigned(Node.Parent) then
Begin
pth:=Node.Parent.Text;
nm:=Node.Text;
LoadNodeName(Mainpath+'Lessons\'+pth+'\'+nm);
End;
End;
procedure TfrmRoboLang.TreeView1Change(Sender: TObject; Node: TTreeNode);
begin
LoadNode(Node);
end;
procedure TfrmRoboLang.VolumeEvent(const bInserted: boolean;
const sDrive: string);
begin
//
end;
procedure TfrmRoboLang.BTSendInteger(k:integer);
var b1,b2:char;
Begin
splitInteger(k,b1,b2);
BTPutChar(b1);
BTPutChar(b2);
sleep(20);
End;
procedure TfrmRoboLang.SendCommands(blck:TDspBlock);
var nxtblock:TDspBlock;
i,vid,vid2:Integer;
myvar:TArduVar;
Begin
NxtBlock:=blck;
while Assigned(nxtblock) do
Begin
BTSendInteger( NxtBlock.CommndID);
BTSendInteger( NxtBlock.PDevice);//device id -1 for no device
BTSendInteger( NxtBlock.Param1);
vid:=-1;vid2:=-1;
if assigned(NxtBlock.VarParam1) then
Begin
if NxtBlock.VarParam1.CommndID>=75 then //fix variable resolve in Arduino
vid:=NxtBlock.VarParam1.CommndID
else
Begin
myvar:=ArduVars.GetVarByName(NxtBlock.VarParam1.param1question);
vid:=myvar.VarID;
End;
End;
if assigned(NxtBlock.VarParam2) then
Begin
if NxtBlock.VarParam1.CommndID>=75 then //fix variable resolve in Arduino
vid2:=NxtBlock.VarParam1.CommndID
else
Begin
myvar:=ArduVars.GetVarByName(NxtBlock.VarParam2.param1question);
vid2:=myvar.VarID;
End;
End;
BTSendInteger(vid);
BTSendInteger(vid2); //TODO:Make varparam2
Adddebug('Sending CMD='+inttostr(NxtBlock.CommndID)
+' Dev= '+inttostr(NxtBlock.pDevice)
+' p_1='+inttostr(NxtBlock.Param1)
+' v_1='+inttostr(vid)
+' v_1='+inttostr(vid2)
+' p_2='+inttostr(NxtBlock.Param2)
);
if NxtBlock.paramstr<>'' then
Begin
NxtBlock.Param2:=Length(NxtBlock.paramstr);
BTSendInteger( NxtBlock.Param2+1);
for i := 1 to NxtBlock.Param2 do
Begin
Adddebug('Sending ch:'+NxtBlock.paramstr[i]);
BTPutChar(NxtBlock.paramstr[i]);
End;
BTPutChar(#0);
end
else
BTSendInteger( NxtBlock.Param2);
{c:=Char(NxtBlock.CommndID);
BTPutChar(c);
p1:=Char(NxtBlock.Param1);
BTPutChar(p1);
p2:=Char(NxtBlock.Param2);
BTPutChar(p2);}
if NxtBlock is TDspContainerBlock then //send container commands
SendCommands(nxtblock.FirstBlock);
if NxtBlock is TDspControlElseBlock and TDspControlElseBlock(NxtBlock).elsevisible then //send else container commands
SendCommands(TDspControlElseBlock(nxtblock).ElseBlock);
NxtBlock:=NxtBlock.LinkTo;
End;
End;
procedure TfrmRoboLang.ResetArduino;
Begin
if comport1.connected then //reset arduino to clear memory
begin
try
comport1.SetDTR(true);
except
try
comport1.connected:=false;
except
exit;
end;
if BTOpen then ResetArduino;
Exit;