-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdmCommon.pas
198 lines (165 loc) · 3.77 KB
/
dmCommon.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
unit dmCommon;
interface
uses
syncobjs,
dmSerial.Base,
{$IFDEF MSWINDOWS}
dmSerial.Windows,
{$ENDIF}
{$IFDEF android}
dmSerial.Android,
{$ENDIF}
System.SysUtils,
System.Types,
System.UITypes,
System.Classes,
System.Variants,
System.Rtti,
Generics.Collections,
FMX.Memo,
FMX.Dialogs,
FMX.Types,
FMX.Controls,
FMX.StdCtrls,
FMX.TabControl,
FMX.Objects;
// Winsoft.FireMonkey.FComSignal, Winsoft.FireMonkey.FComPort;
type
TNeatoModels = (XV, BotVac, BotVacConnected, neatoUnknown);
TTimerList = TObjectList<TTimer>;
Tdm = class(TDataModule)
procedure DataModuleCreate(Sender: TObject);
procedure DataModuleDestroy(Sender: TObject);
private
//
public
COM: TdmSerialBase; // COM, Communicaitons level depending on OS
chkTestmode: tcheckbox; // allows frames to access the main form object easily!
ActiveTab: TTabItem;
log: tmemo;
procedure TestModeON;
procedure TestModeOFF;
function GetNeatoType: integer; // used for scripts as issue with type exporting
function isComSerial: boolean; // easy way to tell if using serial or tcpip
end;
var
dm: Tdm; // common datamodule
ftimerStarter: TTimer;
CurrentTimer: TTimer; // quickly know what the active TTimer is
NeatoType: TNeatoModels; // what kind of bot model line
onTabChangeEvent: TNotifyEvent;
timerCS: TCriticalSection;
procedure StopTimers(NILEvent: boolean = false); // stop running timer
procedure SetTimer(T: TTimer);
procedure StartTimer;
implementation
uses dmSerial.TCPIP;
{%CLASSGROUP 'FMX.Controls.TControl'}
{$R *.dfm}
procedure Tdm.DataModuleCreate(Sender: TObject);
begin
ActiveTab := nil;
end;
procedure Tdm.DataModuleDestroy(Sender: TObject);
begin
if assigned(COM) then
freeandnil(COM);
end;
procedure Tdm.TestModeON;
begin
chkTestmode.ischecked := true;
end;
procedure Tdm.TestModeOFF;
begin
chkTestmode.ischecked := false;
end;
function Tdm.isComSerial: boolean;
begin
result := true;
if assigned(dm.COM) then
{$IFDEF MSWINDOWS}
result := dm.COM.ClassType = TdmSerialWindows;
{$ENDIF}
{$IFDEF ANDROID}
result := dm.COM.ClassType = TdmSerialAndroid;
{$ENDIF}
end;
function Tdm.GetNeatoType: integer;
begin
result := -1;
case NeatoType of
XV:
result := 0;
BotVac:
result := 1;
BotVacConnected:
result := 2;
neatoUnknown:
result := -1;
end;
end;
procedure SetTimer(T: TTimer);
begin
timerCS.Enter;
ftimerStarter := T;
timerCS.Leave;
end;
procedure StartTimer;
begin
timerCS.Enter;
if assigned(ftimerStarter) then
begin
tthread.CreateAnonymousThread(
procedure
begin
sleep(1000);
tthread.Synchronize(tthread.CurrentThread,
procedure
begin
try
if assigned(ftimerStarter) then
if ftimerStarter <> nil then // icky
begin
if ftimerStarter.enabled = false then
ftimerStarter.enabled := true;
end;
except
on e: exception do
begin
ftimerStarter := nil;
end;
end;
end);
end).start;
end;
timerCS.Leave;
end;
procedure StopTimers(NILEvent: boolean = false);
begin
timerCS.Enter;
try
if assigned(ftimerStarter) then
if ftimerStarter <> nil then
if assigned(ftimerStarter.OnTimer) then
begin
if NILEvent then
ftimerStarter.OnTimer := nil;
ftimerStarter.enabled := false;
ftimerStarter := nil;
end;
except
on e: exception do
begin
// eat the error for now
end;
end;
timerCS.Leave;
end;
initialization
NeatoType := neatoUnknown;
CurrentTimer := nil;
timerCS := TCriticalSection.Create;
finalization
StopTimers;
timerCS.Free;
end.