Jump to content
thoga31

[Snippet] Alarm (para pequenos lembretes)

Recommended Posts

thoga31

Esta foi apenas uma pequena brincadeira que fiz. Eu tinha de fazer algo a uma determinada hora, e lembrei-me de fazer este brinquedo.

Gostaria de sugestões para adicionar som a isto. Falo mais especificamente em Linux, que o acesso de baixo nível ao speaker é mais complexo.

Anyway, aqui fica. Não é um código bonito, mas é minimamente funcional :)

(*        === ALARM ===        *
* Version: 1.0.0              *
* Author: Igor Nunes @thoga31 *
* Date: December 22nd, 2013   *)

{$mode objfpc}
program alarm;
uses crt, dos, sysutils;

type TTime = record
       h, m, s, ms : word;
    end;

const DEFAULT_MESSAGE : string = '!!! IT''S TIME !!!';

var asked : TTime;
   i : byte;
   with_sec : boolean;
   counter : longint;
   converter : integer;
   message : string = '';


function StrToInt(s : string) : integer;
var e : byte;
begin
   Val(s, StrToInt, e);
end;


function Now : TTime;
begin
   GetTime(Now.h, Now.m, Now.s, Now.ms);
end;


procedure BlinkMessage(msg : string);
begin
   GotoXY(1, WhereY);
   clreol;
   repeat
       while not keypressed do begin
           GotoXY(1, WhereY);
           textcolor(10);
           write(msg);
           delay(500);
           GotoXY(1, WhereY);
           textcolor(14);
           write(msg);
           delay(500);
       end;
   until readkey = #13;
   textcolor(7);
end;


begin
   try
       writeln('ALARM 1.0.0, by Igor Nunes, December 2013');
       if ParamCount <> 0 then begin
           i := 1;
           repeat
               if LowerCase(ParamStr(i)) = '--hour' then begin
                   inc(i);
                   with_sec := false;
                   if LowerCase(ParamStr(i)) = '+s' then begin
                       with_sec := true;
                       inc(i);
                   end;
                   asked.h := StrToInt(ParamStr(i));
                   inc(i);
                   asked.m := StrToInt(ParamStr(i));
                   if with_sec then begin
                       inc(i);
                       asked.s := StrToInt(ParamStr(i));
                   end else
                       asked.s := 0;
                   asked.ms := 0;
                   inc(i);
                   if LowerCase(ParamStr(i)) = '-m' then begin
                       inc(i);
                       message := ParamStr(i);
                   end else
                       message := DEFAULT_MESSAGE;
                   while not ((asked.h = Now.h) and (asked.m = Now.m) and ((with_sec and (asked.s = Now.s)) or not with_sec)) do begin
                       GotoXY(1, WhereY);
                       clreol;
                       GotoXY(1, WhereY);
                       write(Now.h:2,':',Now.m:2,':',Now.s:2);
                       delay(1000);
                       if keypressed then
                           if readkey in [#27, #13] then
                               raise Exception.Create('Terminated by user');
                   end;
                   BlinkMessage(message);
               end else if LowerCase(ParamStr(i)) = '--countdown' then begin
                   inc(i);
                   converter := 1;
                   if LowerCase(ParamStr(i)) = 's' then begin converter := 1; inc(i); end
                   else if LowerCase(ParamStr(i)) = 'm' then begin converter := 60; inc(i); end
                   else if LowerCase(ParamStr(i)) = 'h' then begin converter := 3600; inc(i); end;
                   counter := abs(StrToInt(ParamStr(i))) * converter;
                   inc(i);
                   if LowerCase(ParamStr(i)) = '-m' then begin
                       inc(i);
                       message := ParamStr(i);
                   end else
                       message := DEFAULT_MESSAGE;
                   while counter > 0 do begin
                       GotoXY(1, WhereY);
                       clreol;
                       GotoXY(1, WhereY);
                       write('Countdown: ', counter,' seconds...');
                       delay(1000);
                       dec(counter);
                       if keypressed then
                           if readkey in [#27, #13] then
                               raise Exception.Create('Aborted by user');
                   end;
                   BlinkMessage(message);
               end else begin
                   raise Exception.Create('Parameters wrongly defined');
               end;
               inc(i);
           until i > ParamCount;
       end else begin
           writeln;
           writeln('Usage: alarm <--countdown [<s|m|h>] time | --hour [+s] hr min [sec]> [-m message]');
           writeln;
           writeln('  Parameter      Description');
           writeln('  -- countdown   Defines the mode ''countdown''.');
           writeln('                 Alarm turns on after a given time.');
           writeln('     <s|m|h>     Optional. Defines time to count in seconds (s), minutes (m)');
           writeln('                 or hours (h). Default = ''s''.');
           writeln('     time        A number, integer. It''s the time to count.');
           writeln('  -- hour        Defines the mode ''hour''.');
           writeln('                 Alarm turns on at a specific hour of the day.');
           writeln('     +s          Optional. Sets precision to second and not minute.');
           writeln('     hr min [sec]  Defines the hour. ''sec'' must be defined only if ''+s'' is set.');
           writeln('  -m message     Optional. Defines the message of the alarm.');
           writeln('                 Default = "!!! IT''S TIME !!!".');
       end;
   except
       on ex:exception do begin
           writeln;
           textcolor(12);
           writeln('ERROR OCCURRED, of class ''', UpperCase(ex.classname), ''', being the message ''', ex.message, '''.');
           textcolor(7);
       end;
   end;
   writeln;
end.

Edited by thoga31

Knowledge is free!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.