program distri_graines;

{$NOSHADOW}
{ $W+ Warnings}            {Warnings off}

Device = mega8, VCC = 5;

Import SysTick, LCDport, RTclock,  ServoPort;

From System Import;

From RTclock Import RTCtimer, RTCalarm;

Define
  ProcClock      = 16000000;        {Hertz}
  SysTick        = 10, Timer2;     {msec}   // ATTENTION: UTILISE LE Timer2 pour l'horloge RTC
  StackSize      = 100, iData;     // remarque: le prog utilise 44 bytes sur les 100 alloués (voir affi en runtime)
  FrameSize      = $0020, iData;
  LCDport        = PortD;
  LCDtype        = 44780;
  LCDrows        = 2;              {rows}
  LCDcolumns     = 16;             {columns per line}
  RTclock        = iData, DateTime; {Time, DateTime}
  RTCsource      = SysTick {, adj}; { adj = +/-100}
  RTCtimer       = 4;

//  SysTick        = 10;             {msec}
//  StackSize      = $0020, iData;
//  FrameSize      = $0010, iData;
  ServoPort      = PortC, 0, iData; {use Portx, startbit n, RAMpage}
  ServoChans     = 1, positive;     {channel count, pulse polarity}
  ServoNeutral   = 2.0, Timer1;     {msec neutral pulse width, Timer used}


  Define_usr
  bouton_inc_h   = %00100000;
  bouton_dec_h   = %00010000;
  bouton_inc_mn  = %00001000;
  bouton_dec_mn  = %00000100;
  bouton_manuel  = %00000010;

Implementation

{$IDATA}

{--------------------------------------------------------------}
{ Type Declarations }
type
  tWeekDays      = (Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday);

  tWeekDArr      = array[tWeekDays] of string[10];

{--------------------------------------------------------------}
{ Const Declarations }
const
  cWeekDArr      : tWeekDArr = ('Dimanche',
  'Lundi',
  'Mardi',
  'Mercredi',
  'Jeudi',
  'Vendredi',
  'Samedi');

  p1             : integer   =  90;
  p2             : integer   =  60;
  p3             : integer   =  30;
  p4             : integer   =   0;
  p5             : integer   = -30;


  {--------------------------------------------------------------}
{ Var Declarations }
{$IDATA}
var
  delay          : SysTimer8;
  ticked         : boolean;
  timeout        : boolean;
  timeout1       : boolean;
  alarm          : boolean;
  bool           : boolean;
//  i, j           : byte;

  j1, cyc1       : byte;
  h1, m1, s1     : byte;
  stk1           : word;


{--------------------------------------------------------------}
{ functions }

procedure init_ports;  // ports perso
begin
  ddrc:= ddrc and %11000001; // 5 entrées pour les boutons
end;


procedure RTCtimer(chan : byte); // CallBack from RTCtimer
begin
{
  for i:= 0 to 10 do
    inc(j);
  endfor;

  if chan = 0 then
    timeout:= true;
  endif;
}
end;


procedure RTCalarm;            // CallBack from RTCalarm
begin
  alarm:= true;
end;

procedure RTCtickSecond;       // CallBack from RTClock
begin
  ticked:= true;
end;

procedure RTCtickMinute;       // CallBack from RTClock
begin
 cyc1:=0; // mode auto
end;

procedure RTCtickHour;         // CallBack from RTClock
begin
end;

procedure MAJ_LCD;
begin
//  Write(LCDout, '      ' + cWeekDArr[tWeekDays(RTCgetWeekDay)]);

  LCDxy(0, 0);
  LCDclrEol;

  // date
  LCDxy(0, 0);
  Write(LCDout, 'jour=' + ByteToStr(j1 : 2 : ' ') );
  
  LCDxy(10, 0);
  if cyc1=0 then Write(LCDout, 'AUTO');
  else  Write(LCDout, 'manu');
  endif;
  
  
{ test et affiche la pile (stack) en runtime
  stk1:= GetStackFree;
  LCDxy(8, 0);
  Write(LCDout, 'stack=' + IntToStr(stk1 : 2 : '0') );
}
  
  LCDclrEol;

//  Write(LCDout, ByteToStr(RTCgetDay : 2 : '0') + '.');
//  Write(LCDout, ByteToStr(RTCgetMonth : 2 : '0') + '.');
//  Write(LCDout, '20' + ByteToStr(RTCgetYear : 2 : '0') + ' ');

  // time
  LCDxy(0, 1);
  Write(LCDout, ByteToStr(RTCgetHour : 2 : '0') + ':');
  Write(LCDout, ByteToStr(RTCgetMinute : 2 : '0') + ':');
  Write(LCDout, ByteToStr(RTCgetSecond : 2 : '0'));
end;

{--------------------------------------------------------------}
{ Main Program }
{$IDATA}

begin
  init_ports;

  j1:= 1;
  cyc1:=0;  //mode auto
  
  RTCsetSecond(0);
  RTCsetMinute(0);
  RTCsetHour(0);

  RTCsetDay(21);
  RTCsetWeekDay(02);
  RTCsetMonth(08);
  RTCsetYear(07);

//  LCDCharSet(#0, $0E, $1F, $04, $0E, $11, $1D, $15, $0E);
//  LCDCharSet(#1, $00, $0E, $15, $17, $11, $0E, $00, $00);

  LCDclr;                                  { clear display }
  LCDcursor(false, false);                 { display on, cursor off & no blink }
//  Write(LCDout, ' '+#0+' E-LAB RTC Demo '+#1);
  ticked:= true;
{
  RTCalarm_Time(00, 01, 25);         // 00:01:25
  RTCalarm_Date(01, 01, 01);         // 01.01.2001
  RTCalarm_Start(2);                 // 0 = stop, 1 = time, 2 = date+time

  RTCtimer_Load(0, 15);
  RTCtimer_Start(0);
  RTCtimer_Load(1, 10);
  RTCtimer_Start(1);
}
  EnableInts;

  SetServoChan(0, p1);

  loop
    if ticked then
      ticked:= false;

      MAJ_LCD;

      mdelay(100);

      h1:= RTCgetHour;
      m1:= RTCgetMinute;
      s1:= RTCgetSecond;
    if (h1 = 23 ) and (m1 = 59) and (s1 = 59 )then j1:= j1 + 1; endif;

    if (j1 = 1) and (h1 = 18 ) and (m1 = 0)  then SetServoChan(0, p2); endif;
    if (j1 = 2) and (h1 =  6 ) and (m1 = 0)  then SetServoChan(0, p3); endif;
    if (j1 = 2) and (h1 = 18 ) and (m1 = 0)  then SetServoChan(0, p4); endif;
    if (j1 = 3) and (h1 =  6 ) and (m1 = 0)  then SetServoChan(0, p5); endif;

    endif;


// SET TIME
    if (PinC and bouton_inc_mn ) = 0 then
      disableInts;
      RTCsetSecond(0);
      RTCsetMinute(RTCgetMinute + 1);
      EnableInts;
      MAJ_LCD;
    while (PinC and bouton_inc_mn ) = 0 do mdelay(10);   endwhile;  // wait relache + anti-rebond
      j1:= 1;
    endif;

    if (PinC and bouton_dec_mn ) = 0 then
      disableInts;
      RTCsetSecond(0);
      RTCsetMinute(RTCgetMinute - 1);
      EnableInts;
      MAJ_LCD;
    while (PinC and bouton_dec_mn ) = 0 do mdelay(10);   endwhile;  // wait relache + anti-rebond
      j1:= 1;
    endif;

    if (PinC and bouton_inc_h ) = 0 then
      disableInts;
      RTCsetSecond(0);
      RTCsetHour(RTCgetHour + 1);
      EnableInts;
      MAJ_LCD;
    while (PinC and bouton_inc_h ) = 0 do mdelay(10);   endwhile;  // wait relache + anti-rebond
      j1:= 1;
    endif;

    if (PinC and bouton_dec_h ) = 0 then
      disableInts;
      RTCsetSecond(0);
      RTCsetHour(RTCgetHour - 1);
      EnableInts;
      MAJ_LCD;
    while (PinC and bouton_dec_h ) = 0 do mdelay(10);   endwhile;  // wait relache + anti-rebond
      j1:= 1;
    endif;

    if (PinC and bouton_manuel ) = 0 then
      cyc1:=cyc1+1;
      if cyc1>4 then SetServoChan(0, p1); cyc1:=0; endif;
      case cyc1 of
        0: nop;                 |
        1: SetServoChan(0, p2); |
        2: SetServoChan(0, p3); |
        3: SetServoChan(0, p4); |
        4: SetServoChan(0, p5); |
      endcase;


    while (PinC and bouton_manuel ) = 0 do mdelay(10);   endwhile;  // wait relache + anti-rebond
      j1:= 1;
    endif;



  endloop;

  RTCtimer_Stop(0);
end distri_graines.




