aboutsummaryrefslogtreecommitdiffstats
path: root/src/base/ULight.pas
blob: b85a958a2d87e8c4865331b8971084a54c4dede4 (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
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
{* UltraStar Deluxe - Karaoke Game
 *
 * UltraStar Deluxe is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; see the file COPYING. If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 *
 * $URL$
 * $Id$
 *}

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.