aboutsummaryrefslogtreecommitdiffstats
path: root/src/base/ULCD.pas
blob: 82f7ba2ff42b0857a0e673209b2fd6dc5e477636 (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
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
unit ULCD;

interface

{$I switches.inc}

type
  TLCD = class
    private
      Enabled:      boolean;
      Text:         array[1..6] of string;
      StartPos:     integer;
      LineBR:       integer;
      Position:     integer;
      procedure WriteCommand(B: byte);
      procedure WriteData(B: byte);
      procedure WriteString(S: string);
    public
      HalfInterface:    boolean;
      constructor Create;
      procedure Enable;
      procedure Clear;
      procedure WriteText(Line: integer; S: string);
      procedure MoveCursor(Line, Pos: integer);
      procedure ShowCursor;
      procedure HideCursor;

      // for 2x16
      procedure AddTextBR(S: string);
      procedure MoveCursorBR(Pos: integer);
      procedure ScrollUpBR;
      procedure AddTextArray(Line:integer; S: string);
  end;

var
  LCD:      TLCD;

const
  Data    = $378; // domy�lny adres portu
  Status  = Data + 1;
  Control = Data + 2;

implementation

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

procedure TLCD.WriteCommand(B: Byte);
// Wysylanie komend sterujacych
begin
{$IFDEF UseSerialPort}
  if not HalfInterface then
  begin
    zlioportwrite(Control, 0, $02);
    zlioportwrite(Data, 0, B);
    zlioportwrite(Control, 0, $03);
  end
  else
  begin
    zlioportwrite(Control, 0, $02);
    zlioportwrite(Data, 0, B and $F0);
    zlioportwrite(Control, 0, $03);

    SDL_Delay( 100 );

    zlioportwrite(Control, 0, $02);
    zlioportwrite(Data, 0, (B * 16) and $F0);
    zlioportwrite(Control, 0, $03);
  end;

  if (B=1) or (B=2) then
    Sleep(2)
  else
    SDL_Delay( 100 );
{$ENDIF}
end;

procedure TLCD.WriteData(B: Byte);
// Wysylanie danych
begin
{$IFDEF UseSerialPort} 
  if not HalfInterface then
  begin
    zlioportwrite(Control, 0, $06);
    zlioportwrite(Data, 0, B);
    zlioportwrite(Control, 0, $07);
  end
  else
  begin
    zlioportwrite(Control, 0, $06);
    zlioportwrite(Data, 0, B and $F0);
    zlioportwrite(Control, 0, $07);

    SDL_Delay( 100 );

    zlioportwrite(Control, 0, $06);
    zlioportwrite(Data, 0, (B * 16) and $F0);
    zlioportwrite(Control, 0, $07);
  end;

  SDL_Delay( 100 );
  Inc(Position);
{$ENDIF}
end;

procedure TLCD.WriteString(S: string);
// Wysylanie slow
var
  I:    integer;
begin
  for I := 1 to Length(S) do
    WriteData(Ord(S[I]));
end;

constructor TLCD.Create;
begin
  inherited;
end;

procedure TLCD.Enable;
{var
  A:  byte;
  B:  byte;}
begin
  Enabled := true;
  if not HalfInterface then
    WriteCommand($38)
  else begin
    WriteCommand($33);
    WriteCommand($32);
    WriteCommand($28);
  end;

//  WriteCommand($06);
//  WriteCommand($0C);
//  sleep(10);
end;

procedure TLCD.Clear;
begin
  if Enabled then begin
  WriteCommand(1);
  WriteCommand(2);
  Text[1] := '';
  Text[2] := '';
  Text[3] := '';
  Text[4] := '';
  Text[5] := '';
  Text[6] := '';
  StartPos := 1;
  LineBR := 1;
  end;
end;

procedure TLCD.WriteText(Line: integer; S: string);
begin
  if Enabled then begin
  if Line <= 2 then begin
    MoveCursor(Line, 1);
    WriteString(S);
  end;

  Text[Line] := '';
  AddTextArray(Line, S);
  end;
end;

procedure TLCD.MoveCursor(Line, Pos: integer);
var
  I:    integer;
begin
  if Enabled then begin
  Pos := Pos + (Line-1) * 40;

  if Position > Pos then begin
    WriteCommand(2);
    for I := 1 to Pos-1 do
      WriteCommand(20);
  end;

  if Position < Pos then
    for I := 1 to Pos - Position do
      WriteCommand(20);

  Position := Pos;
  end;
end;

procedure TLCD.ShowCursor;
begin
  if Enabled then begin
  WriteCommand(14);
  end;
end;

procedure TLCD.HideCursor;
begin
  if Enabled then begin
  WriteCommand(12);
  end;
end;

procedure TLCD.AddTextBR(S: string);
var
  Word:     string;
//  W:        integer;
  P:        integer;
  L:        integer;
begin
  if Enabled then begin
  if LineBR <= 6 then begin
  L := LineBR;
  P := Pos(' ', S);

  if L <= 2 then
    MoveCursor(L, 1);

  while (L <= 6) and (P > 0) do begin
    Word := Copy(S, 1, P);
    if (Length(Text[L]) + Length(Word)-1) > 16 then begin
      L := L + 1;
      if L <= 2 then
        MoveCursor(L, 1);
    end;

    if L <= 6 then begin
      if L <= 2 then
        WriteString(Word);
      AddTextArray(L, Word);
    end;

    Delete(S, 1, P);
    P := Pos(' ', S)
  end;

  LineBR := L + 1;
  end;
  end;
end;

procedure TLCD.MoveCursorBR(Pos: integer);
{var
  I:    integer;
  L:    integer;}
begin
  if Enabled then begin
  Pos := Pos - (StartPos-1);
  if Pos <= Length(Text[1]) then
    MoveCursor(1, Pos);

  if Pos > Length(Text[1]) then begin
    // bez zawijania
//    Pos := Pos - Length(Text[1]);
//    MoveCursor(2, Pos);

    // z zawijaniem
    Pos := Pos - Length(Text[1]);
    ScrollUpBR;
    MoveCursor(1, Pos);
  end;
  end;
end;

procedure TLCD.ScrollUpBR;
var
  T:      array[1..5] of string;
  SP:     integer;
  LBR:    integer;
begin
  if Enabled then begin
  T[1] := Text[2];
  T[2] := Text[3];
  T[3] := Text[4];
  T[4] := Text[5];
  T[5] := Text[6];
  SP := StartPos + Length(Text[1]);
  LBR := LineBR;

  Clear;

  StartPos := SP;
  WriteText(1, T[1]);
  WriteText(2, T[2]);
  WriteText(3, T[3]);
  WriteText(4, T[4]);
  WriteText(5, T[5]);
  LineBR := LBR-1;
  end;
end;

procedure TLCD.AddTextArray(Line: integer; S: string);
begin
  if Enabled then begin
  Text[Line] := Text[Line] + S;
  end;
end;

end.