source: trunk/Platform/Base/PlatformBase.pas

Last change on this file was 60, checked in by chronos, 8 months ago
  • Modified: Remove U prefix from unit names.
File size: 5.9 KB
Line 
1unit PlatformBase;
2
3interface
4
5uses
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
10type
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
80implementation
81
82{ TDeviceVideoBase }
83
84procedure TDeviceVideoBase.DoRedraw;
85begin
86
87end;
88
89procedure TDeviceVideoBase.SetVideoMode(Mode: TVideoMode);
90begin
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;
108end;
109
110procedure TDeviceVideoBase.GetSupportedModes(Modes: TVideoModes);
111var
112 NewMode: TVideoMode;
113begin
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 }
140end;
141
142function TDeviceVideoBase.GetVideoMemory: PByte;
143begin
144 Result := VideoMemory;
145end;
146
147procedure TDeviceVideoBase.VideoMemoryChange;
148begin
149 if Assigned(FOnRedraw) then FOnRedraw(Self);
150end;
151
152constructor TDeviceVideoBase.Create;
153begin
154 inherited Create;
155end;
156
157destructor TDeviceVideoBase.Destroy;
158begin
159 inherited;
160end;
161
162{ TBaseSerialDevice }
163
164constructor TBaseSerialDevice.Create;
165begin
166 inherited Create;
167 ClassName := 'Serial';
168 Lock := TCriticalSection.Create;
169end;
170
171destructor TBaseSerialDevice.Destroy;
172begin
173 FreeAndNil(Lock);
174 inherited;
175end;
176
177procedure TBaseSerialDevice.WriteText(Text: string);
178begin
179 Lock.Acquire;
180 if Assigned(FOnReceiveData) then
181 FOnReceiveData(Text);
182 Lock.Release;
183end;
184
185{ TBaseTask }
186
187constructor TBaseTask.Create;
188begin
189 inherited;
190 Thread := TCustomThread.Create(True);
191 Thread.Task := Self;
192end;
193
194destructor TBaseTask.Destroy;
195begin
196 FreeAndNil(Thread);
197 inherited;
198end;
199
200{ TCustomThread }
201
202procedure TCustomThread.Execute;
203begin
204 Task.EntryPoint(Task);
205end;
206
207{ TBaseScheduler }
208
209procedure TBaseScheduler.SwitchNext;
210var
211 Task: TTask;
212 Duration: TDateTime;
213begin
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;
221end;
222
223function TBaseScheduler.GetCurrentTask: TTask;
224var
225 I: Integer;
226begin
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;
232end;
233
234function TBaseScheduler.AddTask(Name: string; EntryPoint: TTaskEvent): TTask;
235var
236 Task: TBaseTask;
237begin
238 Task := TBaseTask(inherited AddTask(Name, EntryPoint));
239 if Running then Task.Thread.Start;
240 Result := Task;
241end;
242
243constructor TBaseScheduler.Create;
244begin
245 inherited;
246 TaskClass := TBaseTask;
247end;
248
249destructor TBaseScheduler.Destroy;
250begin
251 inherited;
252end;
253
254procedure TBaseScheduler.Start;
255var
256 I: Integer;
257begin
258 inherited Start;
259 for I := 0 to Tasks.Count - 1 do
260 TBaseTask(Tasks[I]).Thread.Start;
261end;
262
263procedure TBaseScheduler.Stop;
264var
265 I: Integer;
266begin
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;
272end;
273
274end.
275
Note: See TracBrowser for help on using the repository browser.