//---------------------------------------------------------------------------

#ifndef DLPrinterPortIOH
#define DLPrinterPortIOH
//---------------------------------------------------------------------------
#include <SysUtils.hpp>
#include <Classes.hpp>
#include "LDriver.h"
//---------------------------------------------------------------------------
class PACKAGE TDLPrinterPortIO : public TComponent
{
 TLdriver mDriver;
  static const  unsigned char BIT0 = 0x01;
   static const unsigned char BIT1 = 0x02;
   static const unsigned char BIT2 = 0x04;
   static const unsigned char BIT3 = 0x08;
   static const unsigned char BIT4 = 0x10;
   static const unsigned char BIT5 = 0x20;
   static const unsigned char BIT6 = 0x40;
   static const unsigned char BIT7 = 0x80;

   // Printer Port pin numbers
   static const ACK_PIN       = 10;
   static const BUSY_PIN      = 11;
   static const PAPEREND_PIN  = 12;
   static const SELECTOUT_PIN = 13;
   static const ERROR_PIN     = 15;
   static const STROBE_PIN    = 1;
   static const AUTOFD_PIN    = 14;
   static const INIT_PIN      = 16;
   static const SELECTIN_PIN  = 17;
   short __fastcall GetPortWord(int Address){ return mDriver.DlReadWord(Address); }
   void __fastcall SetPortWord(int Address, short Data){ mDriver.DlWriteWord(Address, Data); }
protected:
   char __fastcall GetLPTNumPorts() { return mDriver.FLPTCount; }
   int __fastcall GetLPTBasePort() { return mDriver.FLPTBase; }
   void __fastcall SetLPTNumber(char Number);
   bool __fastcall GetPin(char index);
   void __fastcall SetPin(char index, bool state);
   bool __fastcall GetLPTAckwl()    { return GetPin(ACK_PIN); }
   bool __fastcall GetLPTBusy()     { return GetPin(BUSY_PIN); }
   bool __fastcall GetLPTPaperEnd() { return GetPin(PAPEREND_PIN); }
   bool __fastcall GetLPTSlct()     { return GetPin(SELECTOUT_PIN); }
   bool __fastcall GetLPTError()    { return GetPin(ERROR_PIN); }
public:
        __fastcall TDLPrinterPortIO(TComponent* Owner); // Sends STROBE signal to the printer
    void __fastcall LPTStrobe();
     // Sends AUTOFD (auto line feed) signal to the printer
    void __fastcall LPTAutofd(bool Flag);
    // Resets printer by sending INIT signal
    void __fastcall LPTInit();
    // Sends SLCTIN signal to the printer
    void __fastcall LPTSlctIn();
    // Sends a character to the printer.
   // Returns true on success. Repeat as neccessary.
    bool __fastcall LPTPrintChar(char CH);
        __fastcall ~TDLPrinterPortIO(){};

    // Index valid is in the range 1-25 only (other values return false)
   // Reading the pin returns true when it is 5V, or false when it at 0V.
   // Writing true sets the pin to 5V, or 0V when false.
   __property bool Pin[char Index]={read=GetPin,write=SetPin};

   // Returns ACKWL state from the printer
   __property bool LPTAckwl={read=GetLPTAckwl};
   // Returns BUSY state from the printer
   __property bool LPTBusy={read=GetLPTBusy};
   // Returns PAPER END state from the printer
   __property bool LPTPaperEnd={read=GetLPTPaperEnd};
   // Returns SLCT state from the printer
   __property bool LPTSlct={read=GetLPTSlct};
   // Returns ERROR state from the printer
   __property bool LPTError={read=GetLPTError};
   __property short Port[int Address]={read=GetPortWord,write=SetPortWord};
__published:
   // Returns how many LPT ports are installed on your PC.
   __property char LPTNumPorts={read=GetLPTNumPorts};
   // Selects the LPT port to use for all LPT operations
   __property char LPTNumber={read=mDriver.FLPTNumber,write=SetLPTNumber,default=1};
   // Returns a base address of the current LPT port.
   __property int LPTBasePort={read=GetLPTBasePort};
};
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
// Inline Functions
//---------------------------------------------------------------------------


//---------------------------------------------------------------------------
// LPTStrobe()
//    Sends STROBE signal to the printer
//---------------------------------------------------------------------------
inline
void __fastcall TDLPrinterPortIO::LPTStrobe()
{
   // Set to strobe pin to 0V
   SetPin(STROBE_PIN, false);
   // Wait one millisecond
   Sleep(1);
   // Set strobe pin back to 5V
   SetPin(STROBE_PIN, true);
}


//---------------------------------------------------------------------------
// LPTAutofd()
//    Sends AUTOFD (auto line feed) signal to the printer
//---------------------------------------------------------------------------
inline
void __fastcall TDLPrinterPortIO::LPTAutofd(bool Flag)
{
   // Set the auto line feed pin
   SetPin(AUTOFD_PIN, Flag);
}


//---------------------------------------------------------------------------
// LPTInit()
//    Resets printer by sending INIT signal
//---------------------------------------------------------------------------
inline
void __fastcall TDLPrinterPortIO::LPTInit()
{
   // Set pin to a 0V
   SetPin(INIT_PIN, false);
   // Wait 1 ms
   Sleep(1);
   // Set pin back to 5V
   SetPin(INIT_PIN, true);
}


//---------------------------------------------------------------------------
// LPTSlctIn()
//    Sends SLCTIN signal to the printer
//---------------------------------------------------------------------------
inline
void __fastcall TDLPrinterPortIO::LPTSlctIn()
{
   // Send the signal (0V)
   SetPin(SELECTIN_PIN, false);
}


#endif
