1 | unit PlatformBase;
|
---|
2 |
|
---|
3 | interface
|
---|
4 |
|
---|
5 | uses
|
---|
6 | Classes, SysUtils, Kernel.FileSystem, Kernel.Core, syncobjs, ThreadEx,
|
---|
7 | Kernel.Memory, DateUtils, Kernel.Device, Generics.Collections, Graphics, Forms,
|
---|
8 | Kernel.Screen, Kernel.Scheduler, Kernel.Graphics;
|
---|
9 |
|
---|
10 | type
|
---|
11 | { TCustomThread }
|
---|
12 |
|
---|
13 | TCustomThread = class(TThreadEx)
|
---|
14 | Task: TTask;
|
---|
15 | procedure Execute; override;
|
---|
16 | end;
|
---|
17 |
|
---|
18 | { TBaseTask }
|
---|
19 |
|
---|
20 | TBaseTask = class(TTask)
|
---|
21 | Thread: TCustomThread;
|
---|
22 | constructor Create; override;
|
---|
23 | destructor Destroy; override;
|
---|
24 | end;
|
---|
25 |
|
---|
26 |
|
---|
27 | TBaseFileSystem = class(TFileSystem)
|
---|
28 |
|
---|
29 | end;
|
---|
30 |
|
---|
31 | { TBaseScheduler }
|
---|
32 |
|
---|
33 | TBaseScheduler = class(TScheduler)
|
---|
34 | procedure SwitchNext; override;
|
---|
35 | function GetCurrentTask: TTask; override;
|
---|
36 | function AddTask(Name: string; EntryPoint: TTaskEvent): TTask; override;
|
---|
37 | constructor Create; override;
|
---|
38 | destructor Destroy; override;
|
---|
39 | procedure Start; override;
|
---|
40 | procedure Stop; override;
|
---|
41 | end;
|
---|
42 |
|
---|
43 | TReceiveEvent = procedure(Text: string) of object;
|
---|
44 |
|
---|
45 | { TBaseSerialDevice }
|
---|
46 |
|
---|
47 | TBaseSerialDevice = class(TDeviceSerial)
|
---|
48 | private
|
---|
49 | Lock: TCriticalSection;
|
---|
50 | FOnReceiveData: TReceiveEvent;
|
---|
51 | public
|
---|
52 | constructor Create; override;
|
---|
53 | destructor Destroy; override;
|
---|
54 | procedure WriteText(Text: string); override;
|
---|
55 | property OnReceiveData: TReceiveEvent read FOnReceiveData write FOnReceiveData;
|
---|
56 | end;
|
---|
57 |
|
---|
58 | { TDeviceVideoBase }
|
---|
59 |
|
---|
60 | TDeviceVideoBase = class(TDeviceVideo)
|
---|
61 | private
|
---|
62 | FOnModeChanged: TNotifyEvent;
|
---|
63 | FOnRedraw: TNotifyEvent;
|
---|
64 | procedure DoRedraw;
|
---|
65 | protected
|
---|
66 | procedure SetVideoMode(Mode: TVideoMode); override;
|
---|
67 | function GetVideoMemory: PByte; override;
|
---|
68 | public
|
---|
69 | VideoMemory: PByte;
|
---|
70 | DPI: Integer;
|
---|
71 | Canvas: TCanvas;
|
---|
72 | procedure GetSupportedModes(Modes: TVideoModes); override;
|
---|
73 | procedure VideoMemoryChange; override;
|
---|
74 | constructor Create; override;
|
---|
75 | destructor Destroy; override;
|
---|
76 | property OnRedraw: TNotifyEvent read FOnRedraw write FOnRedraw;
|
---|
77 | property OnModeChanged: TNotifyEvent read FOnModeChanged write FOnModeChanged;
|
---|
78 | end;
|
---|
79 |
|
---|
80 | implementation
|
---|
81 |
|
---|
82 | { TDeviceVideoBase }
|
---|
83 |
|
---|
84 | procedure TDeviceVideoBase.DoRedraw;
|
---|
85 | begin
|
---|
86 |
|
---|
87 | end;
|
---|
88 |
|
---|
89 | procedure TDeviceVideoBase.SetVideoMode(Mode: TVideoMode);
|
---|
90 | begin
|
---|
91 | if (VideoMode.Size.X <> Mode.Size.X) or
|
---|
92 | (VideoMode.Size.Y <> Mode.Size.Y) or
|
---|
93 | (VideoMode.ColorFormat <> Mode.ColorFormat) then begin
|
---|
94 | Lock.Acquire;
|
---|
95 | try
|
---|
96 | if Mode.GetBytesPerImage <> VideoMode.GetBytesPerImage then begin
|
---|
97 | ReAllocMem(VideoMemory, Mode.GetBytesPerImage);
|
---|
98 | FillDWord(VideoMemory^, Mode.GetBytesPerImage div 4, $ffffff);
|
---|
99 | end;
|
---|
100 | VideoMode.Size := Mode.Size;
|
---|
101 | VideoMode.ColorFormat := Mode.ColorFormat;
|
---|
102 | finally
|
---|
103 | Lock.Release;
|
---|
104 | end;
|
---|
105 | if Assigned(FOnModeChanged) then
|
---|
106 | FOnModeChanged(Self);
|
---|
107 | end;
|
---|
108 | end;
|
---|
109 |
|
---|
110 | procedure TDeviceVideoBase.GetSupportedModes(Modes: TVideoModes);
|
---|
111 | var
|
---|
112 | NewMode: TVideoMode;
|
---|
113 | begin
|
---|
114 | Modes.Clear;
|
---|
115 | NewMode := TVideoMode.Create;
|
---|
116 | NewMode.Size := TPoint.Create(320, 240);
|
---|
117 | NewMode.ColorFormat := cfRGBA8;
|
---|
118 | Modes.Add(NewMode);
|
---|
119 | NewMode := TVideoMode.Create;
|
---|
120 | NewMode.Size := TPoint.Create(640, 480);
|
---|
121 | NewMode.ColorFormat := cfRGBA8;
|
---|
122 | Modes.Add(NewMode);
|
---|
123 | { NewMode := TVideoMode.Create;
|
---|
124 | NewMode.Size := Point(800, 600);
|
---|
125 | NewMode.ColorFormat := cfRGBA8;
|
---|
126 | Modes.Add(NewMode);
|
---|
127 | NewMode := TVideoMode.Create;
|
---|
128 | NewMode.Size := Point(1024, 768);
|
---|
129 | NewMode.ColorFormat := cfRGBA8;
|
---|
130 | Modes.Add(NewMode);
|
---|
131 | NewMode := TVideoMode.Create;
|
---|
132 | NewMode.Size := Point(1278, 1024);
|
---|
133 | NewMode.ColorFormat := cfRGBA8;
|
---|
134 | Modes.Add(NewMode);
|
---|
135 | NewMode := TVideoMode.Create;
|
---|
136 | NewMode.Size := Point(1920, 1080);
|
---|
137 | NewMode.ColorFormat := cfRGBA8;
|
---|
138 | Modes.Add(NewMode);
|
---|
139 | }
|
---|
140 | end;
|
---|
141 |
|
---|
142 | function TDeviceVideoBase.GetVideoMemory: PByte;
|
---|
143 | begin
|
---|
144 | Result := VideoMemory;
|
---|
145 | end;
|
---|
146 |
|
---|
147 | procedure TDeviceVideoBase.VideoMemoryChange;
|
---|
148 | begin
|
---|
149 | if Assigned(FOnRedraw) then FOnRedraw(Self);
|
---|
150 | end;
|
---|
151 |
|
---|
152 | constructor TDeviceVideoBase.Create;
|
---|
153 | begin
|
---|
154 | inherited Create;
|
---|
155 | end;
|
---|
156 |
|
---|
157 | destructor TDeviceVideoBase.Destroy;
|
---|
158 | begin
|
---|
159 | inherited;
|
---|
160 | end;
|
---|
161 |
|
---|
162 | { TBaseSerialDevice }
|
---|
163 |
|
---|
164 | constructor TBaseSerialDevice.Create;
|
---|
165 | begin
|
---|
166 | inherited Create;
|
---|
167 | ClassName := 'Serial';
|
---|
168 | Lock := TCriticalSection.Create;
|
---|
169 | end;
|
---|
170 |
|
---|
171 | destructor TBaseSerialDevice.Destroy;
|
---|
172 | begin
|
---|
173 | FreeAndNil(Lock);
|
---|
174 | inherited;
|
---|
175 | end;
|
---|
176 |
|
---|
177 | procedure TBaseSerialDevice.WriteText(Text: string);
|
---|
178 | begin
|
---|
179 | Lock.Acquire;
|
---|
180 | if Assigned(FOnReceiveData) then
|
---|
181 | FOnReceiveData(Text);
|
---|
182 | Lock.Release;
|
---|
183 | end;
|
---|
184 |
|
---|
185 | { TBaseTask }
|
---|
186 |
|
---|
187 | constructor TBaseTask.Create;
|
---|
188 | begin
|
---|
189 | inherited;
|
---|
190 | Thread := TCustomThread.Create(True);
|
---|
191 | Thread.Task := Self;
|
---|
192 | end;
|
---|
193 |
|
---|
194 | destructor TBaseTask.Destroy;
|
---|
195 | begin
|
---|
196 | FreeAndNil(Thread);
|
---|
197 | inherited;
|
---|
198 | end;
|
---|
199 |
|
---|
200 | { TCustomThread }
|
---|
201 |
|
---|
202 | procedure TCustomThread.Execute;
|
---|
203 | begin
|
---|
204 | Task.EntryPoint(Task);
|
---|
205 | end;
|
---|
206 |
|
---|
207 | { TBaseScheduler }
|
---|
208 |
|
---|
209 | procedure TBaseScheduler.SwitchNext;
|
---|
210 | var
|
---|
211 | Task: TTask;
|
---|
212 | Duration: TDateTime;
|
---|
213 | begin
|
---|
214 | Task := GetCurrentTask;
|
---|
215 | if Task.State = tsWaiting then begin
|
---|
216 | if Task.WaitReason = wrSleep then begin
|
---|
217 | Duration := (Task.WakeUpTime - Now) / OneMillisecond;
|
---|
218 | if Duration > 0 then SysUtils.Sleep(Trunc(Duration));
|
---|
219 | end;
|
---|
220 | end;
|
---|
221 | end;
|
---|
222 |
|
---|
223 | function TBaseScheduler.GetCurrentTask: TTask;
|
---|
224 | var
|
---|
225 | I: Integer;
|
---|
226 | begin
|
---|
227 | I := 0;
|
---|
228 | while (I < Tasks.Count) and (TBaseTask(Tasks[I]).Thread.ThreadID <> ThreadID) do
|
---|
229 | Inc(I);
|
---|
230 | if I < Tasks.Count then Result := TBaseTask(Tasks[I])
|
---|
231 | else Result := nil;
|
---|
232 | end;
|
---|
233 |
|
---|
234 | function TBaseScheduler.AddTask(Name: string; EntryPoint: TTaskEvent): TTask;
|
---|
235 | var
|
---|
236 | Task: TBaseTask;
|
---|
237 | begin
|
---|
238 | Task := TBaseTask(inherited AddTask(Name, EntryPoint));
|
---|
239 | if Running then Task.Thread.Start;
|
---|
240 | Result := Task;
|
---|
241 | end;
|
---|
242 |
|
---|
243 | constructor TBaseScheduler.Create;
|
---|
244 | begin
|
---|
245 | inherited;
|
---|
246 | TaskClass := TBaseTask;
|
---|
247 | end;
|
---|
248 |
|
---|
249 | destructor TBaseScheduler.Destroy;
|
---|
250 | begin
|
---|
251 | inherited;
|
---|
252 | end;
|
---|
253 |
|
---|
254 | procedure TBaseScheduler.Start;
|
---|
255 | var
|
---|
256 | I: Integer;
|
---|
257 | begin
|
---|
258 | inherited Start;
|
---|
259 | for I := 0 to Tasks.Count - 1 do
|
---|
260 | TBaseTask(Tasks[I]).Thread.Start;
|
---|
261 | end;
|
---|
262 |
|
---|
263 | procedure TBaseScheduler.Stop;
|
---|
264 | var
|
---|
265 | I: Integer;
|
---|
266 | begin
|
---|
267 | inherited Stop;
|
---|
268 | for I := 0 to Tasks.Count - 1 do begin
|
---|
269 | TBaseTask(Tasks[I]).Terminated := True;
|
---|
270 | TBaseTask(Tasks[I]).Thread.WaitFor;
|
---|
271 | end;
|
---|
272 | end;
|
---|
273 |
|
---|
274 | end.
|
---|
275 |
|
---|