[Lazarus] TTimer woes (newbie)

Flávio Etrusco flavio.etrusco at gmail.com
Mon Mar 31 19:51:02 CEST 2014


On Mon, Mar 31, 2014 at 2:24 PM, Bob Axtell <bob.axtell at gmail.com> wrote:

> mine (a morse-code blinker) just doesn't work:
>
> unit Unit1;
>
> {$mode objfpc}{$H+}
>
> interface
>
> uses
>   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs,
> ExtCtrls;
>
> type
>
>   { TForm1 }
>
>   TForm1 = class(TForm)
>     Panel1: TPanel;
>     Timer1: TTimer;
>     procedure FormKeyPress(Sender: TObject; var Key: char);
>     procedure Timer1Timer(Sender: TObject);
>   private
>     { private declarations }
>     procedure morse;
>     procedure dash;
>     procedure dot;
>     procedure wait(x: longint);
>     { public declarations }
>  end;
>
> var
>   Form1: TForm1;
>   a,b,i: byte;
>   count,dat,
>   ms,ls,mchar: byte;
>   tick: integer;
>   col: integer = clblue;
>   mtbl: string[16] =
>    #$bf#$be#$bc#$b8#$b0#$a0#$a1#$a3#$a7#$af#$42#$81#$85#$61#$20#$84;
>
> implementation
>
> {$R *.lfm}
>
> { TForm1 }
>
> procedure TForm1.Timer1Timer(Sender: TObject);
> begin
>  timer1.enabled:= false;
> end;
>
> procedure TForm1.wait(x: longint);
> begin
>  tick:= x;
>  while tick > 0 do
>   begin
>     timer1.enabled:= true;
>     repeat until not timer1.enabled;
>     dec(tick)
>   end;
> end;
>
> procedure Tform1.dash;
> begin
>  Panel1.color:= col;
>  wait(30);
>  Panel1.color:= clwhite;
>  wait(10);
> end;
>
> procedure Tform1.dot;
> begin
>  panel1.color:= col;
>  wait(10);
>  panel1.color:= clwhite;
>  wait(10);
> end;
>
> procedure tform1.morse;
> begin
>  mchar:= $33;
>  ms:= ord(mtbl[succ((mchar shr 4) and 15)]);
>  ls:= ord(mtbl[succ(mchar and 15)]);
>  count:= (ms shr 5) and 7;
>  dat:= ms and 31;
>  for i:= 1 to count do
>  begin
>   if ((dat and 1) > 0) then dash else dot;
>   wait(10);
>   dat:= dat shr 1;
>  end;
>  wait(50);
>  { ms done, ls begins}
>  count:= (ls shr 5) and 7;
>  dat:= ls and 31;
>  for i:= 1 to count do
>  begin
>   if ((dat and 1) > 0) then dash else dot;
>   wait(10);
>  end;
>  wait(100);
> end;
>
> procedure TForm1.FormKeyPress(Sender: TObject; var Key: char);
> begin
>    case key of
>    'r': begin
>          col:= clred;
>          wait(15);
>         end;
>    'g': begin
>          col:= clgreen;
>          wait(15);
>         end;
>    'b': begin
>          col:= clblue;
>          wait(15);
>         end;
>    ' ': morse;
>   end;
>
> end;
>
> end.
>
> any ideas? using v1.0.14 under WinXP.
>
> --Bob A
>
>
TTimer in Windows (and the basic timer support in Windows ) is implemented
using window messages and you're not allowing the app to process messages
inside your loop.

I guess simply changing your loop to:
+++
   repeat
      Application.ProcessMessages;
   until not timer1.enabled;
+++
should make it work.
If it's no problem that your app freezes the UI during the wait (like it
does right now) you could just use Sleep() and abandon TTimer.
OTOH the usual way to use TTimer is to do processing in the OnTimer
handler, but then you'd have to change your code to store some state.

Regards,
Flávio
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus-ide.org/pipermail/lazarus/attachments/20140331/204894d3/attachment-0003.html>


More information about the Lazarus mailing list