program PanelTest;

// TESTPROGRAMM FÜR MENUPANEL

{ $NOSHADOW}
{$W- Warnings}            {Warnings off}
{$TYPEDCONST ON}

Device = mega168, VCC = 5;

Import SysTick, IncrPort4,TWImaster, LCDmultiPort, I2CExpand;   //

Define
  ProcClock      = 16000000;        {Hertz}
  TWIpresc       = TWI_BR400;

  LCDmultiPort   = I2C_TWI;
  LCDTYPE_M      = 44780; // 44780 oder 66712;
  LCDrows_M      = 2;              //rows
  LCDcolumns_M   = 16;             //chars per line
  IncrPort4      = PinC, 1, 16; // pin-reg used, channels, 16 or 32bit integer
  IncrScan4      = Timer1, 4; // timer used, scan rate 1kHz (1..100)


  SysTick        = 2;              //msec

  StackSize      = $0080, iData;
  FrameSize      = $0080, iData;


  I2Cexpand      = I2C_TWI, $38; {use TWIport, 9554A}
  I2CexpPorts = Port2;

Implementation


{--------------------------------------------------------------}
const
//                              ----SFDC             Strobe,FSync,Data,Clk
{ Jumper und LEDs PortBits }
  DDRDinit  :byte= %00001100;            {PortD dir, 0..1=Serial }
  PortDinit :byte= %11111100;            {PortD }
  DDRCinit  :byte= %11111111;            {PortD dir, 0..1=Serial }
  PortCinit :byte= %11111111;            {PortD }

  low: boolean= false;
  high: boolean= true;


var
{$DATA} {Schnelle Register-Variablen}
  i: Integer;
  b, k: Byte;  // k in HW-Prozess benutzt
  t: Boolean;

{$PDATA}  //Ports

  LED1[@PortD, 2]    : bit; {Bit 2 LED Remote-Activity}
  LED2[@PortD, 3]      : bit; {Bit 3 LED über dem Display}

  IO8_LEDs[@Port2] : TI2Cport;       // Out-Port 9554A auf I/O-Platine
  IO8_LEDsDDR[@DDR2]  : TI2Cport;    // Datenrichtung 9554A auf I/O-Platine

{$IDATA}  {Langsamere SRAM-Variablen}
  ButtonTemp: Byte; // invertiert - low=on!
  BtnDown[@ButtonTemp, 5]  : bit;
  BtnUp[@ButtonTemp, 4]  : bit;
  BtnEnter[@ButtonTemp, 3]  : bit;

  IncrValue, OldIncrValue: integer;
  IncrDiffByte: byte;

  FlashTimer, ActivityTimer: Systimer;

{###########################################################################}



{###########################################################################}


procedure initall;
//nach Reset aufgerufen
begin
  DDRC:=  DDRCinit;            {PortD dir}
  PortC:= PortCinit;           {PortD}
  DDRD:=  DDRDinit;            {PortD dir}
  PortD:= PortDinit;           {PortD}
  IO8_LEDsDDR:= $FF;

  EnableInts;
  if LCDsetup_M(LCD_m1) then
    LCDcursor_M(LCD_m1, false, false);
    write(lcdout_m,'PanelTest   0');
    LCDxy_M(LCD_m1, 0, 1);
    write(lcdout_m,'KeyboardPartner');
  endif;
  IO8_LEDs:= 0;
  ResetSysTimer(FlashTimer);
  ResetSysTimer(ActivityTimer);
  IncrCount4start;
  SetIncrVal4(0,0);
end;

{###########################################################################}

begin
  Initall;
//Hauptschleife
  i:= 0;
  t:= false;
  mdelay(100);
  loop
    if isSysTimerZero(FlashTimer) then
      setSysTimer(FlashTimer,50);
      LED1:= t;
      t:= not t;
      LED2:= t;
      k:= k shl 1;
      if k = 0 then
       k:= 1;
      endif;
      IO8_LEDs:= k xor 255;
    endif;

    IncrValue:=GetIncrVal4(0);
    if IncrValue <> OldIncrValue then
      LCDxy_M(LCD_m1, 12, 0);
      write(lcdout_m,IntToSTr(IncrValue div 4) + '   ');
    endif;
    OldIncrValue:= IncrValue;

    mdelay(10); // wichtig für Beschleunigungsfunktion
    ButtonTemp := (LCDportInp_M(LCD_m1) or %11000111);
    if ButtonTemp <> $FF then
      mdelay(10);
      ButtonTemp := (LCDportInp_M(LCD_m1) or %11000111);
      if ButtonTemp <> $FF then
        if not BtnEnter then
           LCDxy_M(LCD_m1, 12, 0);
           write(lcdout_m,'KNOB');
        endif;
        if not BtnDown then
           LCDxy_M(LCD_m1, 12, 0);
           write(lcdout_m,'DOWN');
        endif;
        if not BtnUp then
           LCDxy_M(LCD_m1, 12, 0);
           write(lcdout_m,'UP  ');
        endif;
        repeat
          mdelay(10);
          ButtonTemp := (LCDportInp_M(LCD_m1) or %11000111);
        until ButtonTemp=$ff;
      endif;
    endif;
    if isSysTimerZero(ActivityTimer) then
      LCDxy_M(LCD_m1, 12, 0);
      write(lcdout_m,'    ');
    endif;
endloop;

end.

