/****************************************Copyright (c)**************************************************
**                               Guangzhou ZHIYUAN electronics Co.,LTD.
**                                     
**                                 http://www.embedtools.com
**
**--------------File Info-------------------------------------------------------------------------------
** File Name:           ZYTP58.h
** Last modified Date:  2010-11-15 
** Last Version:        v1.01
** Description:         ZYTP_LIB driver library header for upper layer machine VC++
** 
**------------------------------------------------------------------------------------------------------
** Created By:          Huangyixin
** Created date:        2010-11-15
** Version:             v1.01
** Descriptions:
**
********************************************************************************************************/
#pragma once
#include "afxwin.h"
#include "CnCOMM.h"
#include <assert.h>

#define MSG_OVERVOLATGE			WM_USER + 700							/* Over-voltage message						*/
#define MSG_OVERVOLATGE_RESUME	WM_USER + 701							        /* Over-voltage resume message					*/
#define MSG_AXOOPEN				WM_USER + 702						/* Platen open message				 		*/
#define MSG_AXOCLOSE			WM_USER + 703							/* Platen close message						*/
#define MSG_OFPAPER				WM_USER + 704						/* Paper end message						*/
#define MSG_OFPAPER_RESUME		WM_USER + 705							/* Paper end resume message					*/
#define MSG_OVERHEAT			WM_USER + 706							/* Over heat message						*/
#define MSG_OVERHEAT_RESUME		WM_USER + 707							/* Over heat resume message					*/
#define MSG_CUTTERON			WM_USER + 708							/* Cutter down message  					*/
#define MSG_CUTTEROFF			WM_USER + 709							/* Cutter resume message					*/ 
#define MSG_TRANSMIT			WM_USER + 710							/* Transimtting data is received   				*/

/*********************************************************************************************************
**  Font type macro, used in fontTypeSet()
*********************************************************************************************************/
#define  FONT_TYPE_NORMAL             0x00                              /* Normal                       */
#define  FONT_TYPE_ITALIC             0x02                              /* Italic                       */
#define  FONT_TYPE_BORDER             0x04                              /* Border                       */
#define  FONT_TYPE_EMPHASIZE          0x08                              /* Bold                         */
#define  FONT_TYPE_DOUBLE_HEIGHT      0x10                              /* Double height                */
#define  FONT_TYPE_DOUBLE_WIDTH       0x20                              /* Double width                 */
#define  FONT_TYPE_REVERSE_VEDIO      0x40                              /* Inverse                      */
#define  FONT_TYPE_UNDERLINE          0x80                              /* Underliine                   */

/*********************************************************************************************************
**  Alignment marco, used in horizontalAlign()
*********************************************************************************************************/
#define  ALIGN_LEFT				  0								    /* Left                         */
#define  ALIGN_CENTER				  1								    /* Center                       */
#define  ALIGN_RIGHT				  2							     	    /* Right                        */	

/*********************************************************************************************************
**  Print speed marco, used in printSpeedSet()
*********************************************************************************************************/
#define  PRINT_SPEED_LOW              0                                 /* Low speed                    */
#define  PRINT_SPEED_MIDIUM           1                                 /* Moderate speed               */
#define  PRINT_SPEED_HIGHT            2                                 /* High speed                   */

/*********************************************************************************************************
**  Print font marco£¬used in fontSizeSet ()
*********************************************************************************************************/
#define  FONT_SIZE_24                 0                                 /* 24¡Á24                       */
#define  FONT_SIZE_16                 1                                 /* 16¡Á16                       */
#define  FONT_SIZE_12                 2                                 /* 12¡Á12                       */

/*********************************************************************************************************
**  Vertical bit-image printing type macro£¬used in picturePrintV()
*********************************************************************************************************/
#define  DOT_8_SINGLE_DENSITY         0                                 /* 8 dots single density        */
#define  DOT_8_DOUBLE_DENSITY         1                                 /* 8 dots double density        */
#define  DOT_24_SINGLE_DENSITY        32                                /* 24 dots single density       */
#define  DOT_24_DOUBLE_DENSITY        33                                /* 24 dots double density       */
  
/*********************************************************************************************************
**  Horizontal bit-image printing type macro£¬used in picturePrintH()
*********************************************************************************************************/
#define  PICTURE_TYPE_NORMAL          0                                 /* Normal                       */
#define  PICTURE_TYPE_DOUBLE_WIDTH    1                                 /* Double width                 */
#define  PICTURE_TYPE_DOUBLE_HEIGHT   2                                 /* Double height                */
#define  PICTURE_TYPE_QUADRUPLE       3                                 /* Double width + Double height */

/*********************************************************************************************************
**  Horizontal tab font type macro£¬used in formPrintV()
*********************************************************************************************************/
#define  VF_FONT_TYPE_NORMAL           0                                 /* Normal                       */
#define  VF_FONT_TYPE_EMPHASIZE       1                                 /* Bold                         */
#define  VF_FONT_TYPE_UNDERLINE       2                                 /* Underline                    */
#define  VF_FONT_TYPE_REVERSE_VEDIO   3                                 /* Inverse                      */

/*********************************************************************************************************
**  Vertical tab items settings structure
*********************************************************************************************************/
typedef struct __formItem {
    unsigned char ucPos;                                                /* Tab item position            */
    unsigned char ucFontType;                                           /* Tab font settings            */
    unsigned char ucDataLen;                                            /* Tab data length              */
    char *pucDataBuf;							/* Tab data                     */
}formItem;

/*********************************************************************************************************
**  Bar code HRI position macro£¬used in barcodeHriPosSet()
*********************************************************************************************************/
#define  BARCODE_HRI_POS_NONE         0                                 /* None                         */
#define  BARCODE_HRI_POS_TOP          1                                 /* Top                          */
#define  BARCODE_HRI_POS_BOTTOM       2                                 /* Botton                       */
#define  BARCODE_HRI_POS_TOP_BOTTOM   3                                 /* Top + Button                 */

/*********************************************************************************************************
**  Bar code HRI font macro£¬used in barcodeHriFontSet()
*********************************************************************************************************/
#define  BARCODE_HRI_FONT_24          0                                 /* 12x24                        */
#define  BARCODE_HRI_FONT_16          1                                 /* 8x16                         */
#define  BARCODE_HRI_FONT_12          2                                 /* 6x12                         */

/*********************************************************************************************************
**  Bar code system macro£¬used in barcodePrint()
*********************************************************************************************************/
#define  BARCODE_SYS_UPCA			  0                                  
#define  BARCODE_SYS_UPCE			  1								 
#define  BARCODE_SYS_EAN13			  2								 
#define  BARCODE_SYS_EAN8			  3								 
#define  BARCODE_SYS_CODE39			  4                                  
#define  BARCODE_SYS_ITF25			  5								 
#define  BARCODE_SYS_CODABAR		          6								 
#define  BARCODE_SYS_CODE93			  7								 
#define  BARCODE_SYS_CODE128	         	  8			
#define  BARCODE_SYS_EAN128			  9	

/*********************************************************************************************************
**  Cutting mode macro£¬used in printerCutPaper()
*********************************************************************************************************/
#define  CUT_TYPE_HALF				  0x42			/* Half cut                     */
#define  CUT_TYPE_ALL				  0x41			/* Full cut                     */

/*********************************************************************************************************
**  Printer state macro£¬used in printerStateRT() and printerStateAutoUpload()
*********************************************************************************************************/
#define  PRINTER_STATE_NORMAL         0x00                              /* None/Disable                 */
#define  PRINTER_STATE_OVERVOLATGE    0x01                              /* Over voltage                 */
#define  PRINTER_STATE_AXOOPEN	      0x02                              /* Platen open                  */
#define  PRINTER_STATE_OFPAPER        0x04                              /* Paper end                    */
#define  PRINTER_STATE_OVERHEAT       0x08                              /* Over heat                    */
#define  PRINTER_STATE_CUTTERON       0x20                              /* Cutter down                  */

/*********************************************************************************************************
**  Flow control mode selection macro, used in printerFlowCtrlSet()
*********************************************************************************************************/
#define  FLOW_CTRL_NONE              0                                  /* None                         */
#define  FLOW_CTRL_SOFTWARE          49                                 /* Software flow control        */
#define  FLOW_CTRL_HARDWARE          48                                 /* Hardware flow control        */

/*********************************************************************************************************
**  Define printer inforamtion 
*********************************************************************************************************/
typedef struct __PRINTER_INFO {
    unsigned char bOverHeat;											/* Over head flag             */
    unsigned char bOfPaper;											/* Paper end flag             */
    unsigned char bOverVoltage;											/* Over voltage flag          */
    unsigned char bAxoOpen;											/* Platen close flag          */
    unsigned char bCutterOn;											/* Cutter down flag           */
    
	unsigned char ucRtState;                                                                                /* Realtime state             */
                                                                                                                /* bit4 is used as a update flag */
    unsigned char ucPowerDown;                                                                                  /* Power control response        */

	bool          bTransmit;										/* General transmission command response */
	char          ucTransmit[256];
	unsigned int  uiTransmitLen;
}PRINTER_INFO;

// Integrated serial port class
class CZyPrtLib : public CnComm
{
	// constructor function
public:
	CZyPrtLib()
	{
		_hZYTPNotifyWnd = NULL;

		PrinterInfo.bOverVoltage = false;												 
		PrinterInfo.bAxoOpen = false;	
		PrinterInfo.bOverHeat = false;												 
		PrinterInfo.bOfPaper = false;		
		PrinterInfo.bCutterOn = false;	

		PrinterInfo.ucRtState = 0x10;
		PrinterInfo.ucPowerDown = 0x00;

		PrinterInfo.bTransmit = false;

		// Set the serial port to synchronous I/O mode
		ModifyOption(EN_OVERLAPPED | EN_THREAD , EN_TX_BUFFER | EN_TX_THREAD | EN_RX_BUFFER | EN_RX_THREAD); 

		// Nofity on receiving 1 byte
		SetNotifyNum(1);
	}
	virtual ~CZyPrtLib()
	{
		
	}

	// Window handle of associated message
	inline void SetZYTPWnd(HWND hWnd)
	{
		assert(::IsWindow(hWnd));
		_hZYTPNotifyWnd = hWnd;

		// Set the serial port message window handle
		SetWnd(hWnd);
	}

	//Driver library function declaration; These functions can be called by user.
	void uartSendStr (const char *pucStr);
	void print (void);
	void printAndFeedNDotLine (unsigned char ucDotLineNum);
	void printAndBackFeedNDotLine (unsigned char ucDotLineNum);
	void printAndFeedNFontLine (unsigned char ucFontLineNum);
	void printAndBackFeedNFontLine (unsigned char ucFontLineNum);
	void lineSpacingSet (unsigned char ucDotLineNum);
	void lineSpacingDefault (void);
	void leftMarginSet (unsigned char ucLeftMargin);
	void rightMarginSet (unsigned char ucRightMargin);
	void abscissaSet (unsigned short ucXNum);
	void fontTypeSet (unsigned char ucFontType);
	void horizontalAlign(unsigned char align);
	void fontGrayscaleSet (unsigned char ucFontGrayscale);
	void printSpeedSet (unsigned char ucPrintSpeed);
	void fontSizeSet (unsigned char ucFontSize);
	void picturePrintV (unsigned char ucType, 
				        unsigned short usHSize, 
				        unsigned char * pucDataBuf);
	void picturePrintH (unsigned char ucType, 
                        unsigned short usHSize, 
                        unsigned short usVSize, 
				        unsigned char * pucDataBuf);
	void horizontalTabSet (unsigned char *pucNBuf, unsigned char ucDataNum);
	void horizontalTab (void);
	void formPrintV (unsigned char ucVLineNum, 
					 unsigned char *pucVLinePos, 
					 unsigned char ucItemNum, 
					 formItem      *psItem);

    void barcodeHeightSet(unsigned char ucHeight);
    void barcodeWidthSet(unsigned char ucWidth);
    void barcodeHriPosSet(unsigned char ucHriPos);
	void barcodeHriFontSet(unsigned char ucHriFont);
    void barcodePrint(unsigned char ucBarcodeSys,
                      unsigned char *pucCodeBuf, 
                      unsigned char ucCodeLen);

    void pdf417SetRow(unsigned char ucRow);
    void pdf417SetCol(unsigned char ucCol);
    void pdf417SetWidth(unsigned char ucWidth);
    void pdf417SetHeight(unsigned char ucHeight);
    void pdf417SetErrorlevel(unsigned char ucErrorlevel);
    void pdf417SetTuncated(unsigned char ucTuncated);
    void pdf417SetData(unsigned char *pucCodeBuf,unsigned char ucCodeLen);
    void pdf417Print(void);

    void qrcodeSetModel(unsigned char ucModel);
    void qrcodeSetScale(unsigned char ucScale);
    void qrcodeSetErrorLevel(unsigned char ucErrorLevel);
    void qrcodeSetData(unsigned char *pucCodeBuf, unsigned char ucCodeLen);
    void qrcodePrint(void);

	void printerInit (void);
	void printerClearBuffer(void);
	void printerCutPaper(unsigned char ucDotLineNum, unsigned char type);
	bool printerStateRT(unsigned char *pucState, unsigned short usTimeOut);
	void printerStateAutoUpload(unsigned char key);
	void printerBaudRateSet(unsigned long ulBaudRate);
	void printerFlowCtrlSet(unsigned char Mode);
	bool printerEnterLowPowerMode (unsigned short usTimeOut);
	bool printerExitLowPowerMode (unsigned short usTimeOut);

    void layoutSet(unsigned char ucSm, char *pucSa_f, unsigned char ucLen);
    void layoutAdGet(void);
    void layoutAdthSet(char *pucAdth, unsigned char ucLen);
    void layoutDisSet(char *pucDis, unsigned char ucLen);
    void feedPaperToPeelPos(void);
    void feedPaperToCutPos(void);
    void feedPaperToPrintPos(void);  
	
	//Driver library function declarations. These are internal called functions.
private:
    void uartRecvProc (unsigned char ucData);
    virtual void OnReceive(void);
	
	volatile HWND _hZYTPNotifyWnd;	

public:
	PRINTER_INFO PrinterInfo;
	void selectKanjiMode(void);
	void cancelKanjiMode (void);
	void selectInternationalChar (unsigned char ucCountry);
	void selectCharCodePage (unsigned char ucPageNum);
};
