-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathunDevices.pas
178 lines (154 loc) · 4.46 KB
/
unDevices.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
unit unDevices;
interface
uses Vcl.Forms,Vcl.ExtCtrls,Vcl.Buttons;
Const MaxDevParams=7-1;//zero based
Type
TDeviceTypes=(LCD,LASER,SOUND,USONIC,SERVO,SWITCH,TEMP,BMP,ANIN,DCMOTOR,ROBOT);
TDevice=record
DeviceName:String;
DeviceFormClass:TFormClass;
DevicePanel:TPanel;
DeviceCmdId:Integer;
DeviceParamCount:Integer;
DeviceMax:Integer;
end;
TArduino=record
Id:integer;
MaxPins:Integer;
end;
TActiveDevice=Record
DeviceTypeID:Integer; //IDX from TDevices
ActDevForm:TForm;
ActDevParams:array[0..MaxDevParams] of integer;
ActDevParamCount:Integer;
End;
TDevices=array[0..20] of TDevice; //Up to 20 Devices
TActiveDevices=array[0..50] of TActiveDevice;
var ArduinoDevices:TDevices;
ActDevs:TActiveDevices;
ActDevsCount:Integer=-1;
Arduino:TArduino;
Procedure SaveActiveDevices(nm:String);
Procedure LoadActiveDevices(nm:String);
procedure ClearActiveDevices;
Procedure DeleteActiveDevice(ActDevId:integer);
Procedure NewActiveDevice;
function getFirstDeviceOfType(tpid:Integer):integer;
implementation
uses sysutils,classes,inifiles,unCtrlPlatMain,unHelpForms;
Procedure NewActiveDevice;
Begin
inc(ActDevsCount);
End;
Procedure DeleteActiveDevice(ActDevId:integer);
Var i,j:Integer;
Begin
if ActDevId=-1 then exit;
for I := ActDevId+1 to ActDevsCount do
Begin
ActDevs[i-1].DeviceTypeID:=ActDevs[i].DeviceTypeID;
ActDevs[i-1].ActDevForm:=ActDevs[i].ActDevForm;
for j := 0 to MaxDevParams do
ActDevs[i-1].ActDevParams[j]:=ActDevs[i].ActDevParams[j];
ActDevs[i-1].ActDevParamCount:=ActDevs[i].ActDevParamCount;
End;
ActDevsCount:=ActDevsCount-1;
End;
Procedure SaveActiveDevices(nm:String);
Var Newnm:String;
inif:TIniFile;
i,j:Integer;
Begin
Newnm:=ChangeFileExt(nm,'.adv');
inif:=TIniFile.create(Newnm);
try
inif.WriteInteger('ArduinoDevice','TypeId',Arduino.ID);
inif.WriteInteger('ActiveDevices','Count',ActDevsCount);
for i := 0 to ActDevsCount do
Begin
inif.WriteInteger('ADev_'+inttostr(i),'DeviceTypeID',ActDevs[i].DeviceTypeID);
inif.WriteInteger('ADev_'+inttostr(i),'ActDevParamCount',ActDevs[i].ActDevParamCount);
for j := 0 to MaxDevParams do
inif.WriteInteger('ADev_'+inttostr(i),'ActDevParam'+inttostr(J+1),ActDevs[i].ActDevParams[j]);
End;
finally
inif.Free;
end;
End;
Procedure ClearActiveDevices;
Var i,j:integer;
Begin
for i := ActDevsCount downto 0 do
Begin
TDefDevForm(ActDevs[i].ActDevForm).KillDevice;
ActDevs[i].DeviceTypeID:=0;
ActDevs[i].ActDevParamCount:=0;
for j := 0 to MaxDevParams do
ActDevs[i].ActDevParams[j]:=0;
ActDevs[i].ActDevForm:=nil;
End;
ActDevsCount:=-1;
End;
function getPanelFromDeviceTypeId(DevTypeid:Integer):TPanel;
var i:Integer;
p:TPanel;
Begin
p:=nil;
for i:=0 to frmRoboLang.ScrollBox2.ControlCount-1 do
if frmRoboLang.ScrollBox2.Controls[i] is TPanel then
Begin
p:=frmRoboLang.ScrollBox2.Controls[i] as TPanel;
if p.Tag=DevTypeid then Break
else p:=nil;
End;
result:=p;
End;
Procedure LoadActiveDevices(nm:String);
Var Newnm:String;
inif:TIniFile;
i,j:Integer;
p:TPanel;
b:TBitBtn;
Adevcnt:Integer;
Begin
ClearActiveDevices;
Newnm:=ChangeFileExt(nm,'.adv');
inif:=TIniFile.create(Newnm);
try
Arduino.ID:=inif.ReadInteger('ArduinoDevice','TypeId',0);
//Set Arduino type
frmRoboLang.RadioGroup1.ItemIndex:=Arduino.ID;
frmRoboLang.RadioGroup1Click(frmRoboLang.RadioGroup1);
Adevcnt:=inif.ReadInteger('ActiveDevices','Count',-1); //ActDevsCount it will increase when we add a page form
for i := 0 to Adevcnt do
Begin
ActDevs[i].DeviceTypeID:=inif.ReadInteger('ADev_'+inttostr(i),'DeviceTypeID',-1);
ActDevs[i].ActDevParamCount:=inif.ReadInteger('ADev_'+inttostr(i),'ActDevParamCount',0);
for j := 0 to MaxDevParams do
ActDevs[i].ActDevParams[j]:=inif.ReadInteger('ADev_'+inttostr(i),'ActDevParam'+inttostr(J+1),0);
//Create Page
p:=getPanelFromDeviceTypeId(ActDevs[i].DeviceTypeID);
if p<>nil then
Begin
b:=frmRoboLang.getPanelAddButton(p);
b.Click; //Add a new page increase ActDevsCount
End;
End;
finally
inif.Free;
end;
End;
function getFirstDeviceOfType(tpid:Integer):integer;
var i:integer;
Begin
result:=-1;
for i := 0 to ActDevsCount do
Begin
if ActDevs[i].DeviceTypeID=tpid then
Begin
Result:=i;
break;
End;
End;
End;
end.