//----------------------------------------------------------------------------
// HACIINC                                                      19980407 CHG
// 2 Incremental Encoder inputs
//
// HACBUS:
//   Device type: 0x0D               ; HACIO type
//   Address    : 0x01..0x0F         ; Address 1..15
//   Commands   : 0x00               ; Poll
//          Resp: 0x80 [XX XX YY YY] ; XX XX is "changed mask" and YY YY is new data
//                                   ; for the changed inputs, no data if no change
//              : 0x01 XX XX         ; Set all 16 outputs to value XX XX
//          Resp: 0x81               ;
//----------------------------------------------------------------------------
#include <reg52.h>
#include <stdio.h>
#include "..\HACNET\HACNET.H"

//----------------------------------------------------------------------------
// Constants...
//----------------------------------------------------------------------------
#define TRUE   1
#define FALSE  0
#define MyDeviceType 0x0D
#define RxSize 3+3             // Maximum size of RxBuffer

//----------------------------------------------------------------------------
// I/O definitions...
//----------------------------------------------------------------------------
#define DATABUS P1
#define nRST    P25
#define SEL     P27
#define nOE1    P24
#define nOE2    P26

#define SW5     P04
#define SW6     P05
#define SW7     P06
#define SW8     P07

//----------------------------------------------------------------------------
// Variables...
//----------------------------------------------------------------------------
unsigned char RxBuffer[RxSize];       // RxBuffer
extern bit RxAvail;                   // Message available in RX Buffer
extern bit Resend;                    // Resend last response


//----------------------------------------------------------------------------
// wait for a specific time in 10 mSec
// Based on a 11.059 MHz crystal
//----------------------------------------------------------------------------
void Delay(unsigned char t) {
  unsigned int i;
  while (t--) for(i=0;i<774; i++);
}

//----------------------------------------------------------------------------
// Read one of the Inc counters
//----------------------------------------------------------------------------
unsigned int GetCount(unsigned char No) {
  unsigned char TmpH, TmpL;
  SEL=0;
  if (No==0)
    nOE1=0;
  else 
    nOE2=0;
  TmpL=DATABUS;
  SEL=1;
  TmpH=DATABUS;
  nOE1=1;
  nOE2=1;
  return (((unsigned int)(TmpH) << 8) + (unsigned int)TmpL);
}

//----------------------------------------------------------------------------
// Main part
//----------------------------------------------------------------------------
void main() {
  unsigned char Rsp[5];         // Buffer for responses
  bit PwrUp=TRUE;
  unsigned char MyAddress;      // My assigned address on the Dip switches.
  unsigned int c1,c2;
  unsigned int Oldc1=0xfede,Oldc2=0xdeadbeef;

 //----------------------------------------------------------------------------
 // Initialize HACNET driver...
 //----------------------------------------------------------------------------
  MyAddress=InitHACNET(MyDeviceType, RxSize);

 //---------------------------------
 // Initialize outputs...
 //---------------------------------

 //------------------------------
 // Configure onchip resources...
 //------------------------------
 EA=1;        // Global int enable
  nRST=0;
  nRST=1;
  //----------------------------------------------------------------------------
  // Forever
  //----------------------------------------------------------------------------
  while(1) {
    //----------------------------------------------------------------------------
    // Diagnostic mode, Address is 0x00
    //----------------------------------------------------------------------------
    if (MyAddress==0) {
      Delay(50);
      HacTransmit("DIAG\n\r",6);

    } else {
    //----------------------------------------------------------------------------
    // Normal mode
    //----------------------------------------------------------------------------
      // Check if we have to resend last response...
      if (Resend) {
        Resend=FALSE;
        HacTransmit(Rsp, 0xFF);
      }
      if (RxAvail) {
        switch (RxBuffer[0]) {
          case 0 :
            Rsp[0]=0x80; // Response code
            c1=GetCount(0);
            c2=GetCount(1);
            if ((c1!=Oldc1) || (c2!=Oldc2)) {
              Oldc1=c1;
              Oldc2=c2;
              Rsp[1]=(c1 >> 8);
              Rsp[2]=c1;
              Rsp[3]=(c2 >> 8);
              Rsp[4]=c2;
              HacTransmit(Rsp, 5); // Response with data
            } else 
              HacTransmit(Rsp, 1); // Empty response
            break;
          case 1 :
            Rsp[0]=0x81; // Response code
            c1=GetCount(0);
            c2=GetCount(1);
            Oldc1=c1;
            Oldc2=c2;
            Rsp[1]=(c1 >> 8);
            Rsp[2]=c1;
            Rsp[3]=(c2 >> 8);
            Rsp[4]=c2;
            HacTransmit(Rsp, 5); // Response with data
            break;
        }
        RxAvail=0;
      }
    }
  }
}

