aboutsummaryrefslogtreecommitdiffstats
path: root/src/base/ULight.pas
blob: a0a399abf31b6119afeea43f460236d2b1c38462 (plain) (blame)
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
unit ULight;

interface

{$IFDEF FPC}
  {$MODE Delphi}
{$ENDIF}

{$I switches.inc}

type
  TLight = class
    private
      Enabled:      boolean;
      Light:        array[0..7] of boolean;
      LightTime:    array[0..7] of real;    // time to stop, need to call update to change state
      LastTime:     real;
    public
      constructor Create;
      procedure Enable;
      procedure SetState(State: integer);
      procedure AutoSetState;
      procedure TurnOn;
      procedure TurnOff;
      procedure LightOne(Number: integer; Time: real);
      procedure Refresh;
  end;

var
  Light:      TLight;

const
  Data    = $378; // default port address
  Status  = Data + 1;
  Control = Data + 2;

implementation

uses
  SysUtils,
  {$IFDEF UseSerialPort}
  zlportio,
  {$ENDIF}
  UTime;
  
{$IFDEF FPC}

  function GetTime: TDateTime;
  var
    SystemTime: TSystemTime;
  begin
    GetLocalTime(SystemTime);
    with SystemTime do
    begin
      {$IFDEF UNIX}
      Result := EncodeTime(Hour, Minute, Second, MilliSecond);
      {$ELSE}
      Result := EncodeTime(wHour, wMinute, wSecond, wMilliSeconds);
      {$ENDIF}
    end;
  end;

{$ENDIF}


constructor TLight.Create;
begin
  inherited;
  Enabled := false;
end;

procedure TLight.Enable;
begin
  Enabled  := true;
  LastTime := GetTime;
end;

procedure TLight.SetState(State: integer);
begin
  {$IFDEF UseSerialPort}
  if Enabled then
    PortWriteB($378, State);
  {$ENDIF}
end;

procedure TLight.AutoSetState;
var
  State:    integer;
begin
  if Enabled then begin
    State := 0;
    if Light[0] then State := State + 2;
    if Light[1] then State := State + 1;
    // etc
    SetState(State);
  end;
end;

procedure TLight.TurnOn;
begin
  if Enabled then
    SetState(3);
end;

procedure TLight.TurnOff;
begin
  if Enabled then
    SetState(0);
end;

procedure TLight.LightOne(Number: integer; Time: real);
begin
  if Enabled then begin
    if Light[Number] = false then begin
      Light[Number] := true;
      AutoSetState;
    end;

    LightTime[Number] := GetTime + Time/1000; // [s]
  end;
end;

procedure TLight.Refresh;
var
  Time:     real;
  L:        integer;
begin
  if Enabled then begin
    Time := GetTime;
    for L := 0 to 7 do begin
      if Light[L] = true then begin
        if LightTime[L] > Time then begin
        end else begin
          Light[L] := false;
        end;
      end;
    end;
    LastTime := Time;
    AutoSetState;
  end;
end;

end.