// Data Interface between slave (DC) and pi buffer
// determines sending and receiving order of data
// cares for storing input data and restoring output data



#ifndef DIF_H
#define DIF_H
#include <stdint.h>
#include "tslib.h"
#include "prot.h"
#include "dcBL.h"


#include <QMainWindow>
#include <QString>
#include <QTimer>
#include <QDebug>
#include <QDateTime>
#include <QDate>
#include <QTime>



#define CMD2DC_sendTime             20
#define CMD2DC_setWakeFrequ         112
#define CMD2DC_MOV_UPLOCK           113
#define CMD2DC_MOV_DNLOCK           114
#define CMD2DC_UPPER_DOOR           115
#define CMD2DC_LOWER_DOOR           116
#define CMD2DC_VAULT_DOOR           117
#define CMD2DC_REJMOT_ON            118
#define CMD2DC_REJMOT_RUN           119
#define CMD2DC_LED_COIN             100
#define CMD2DC_LED_ILLU             101
#define CMD2DC_LED_TICKET           102
#define CMD2DC_LED_START            104
#define CMD2DC_LED_PIN              103
#define CMD2DC_LED_IN               105
#define CMD2DC_FAN                  106
#define CMD2DC_SIREN                107
#define CMD2DC_BARRIER              108
#define CMD2DC_WAKEPTU              109
#define CMD2DC_SWITCHAUXPWR         110
#define CMD2DC_SWITCHAUXDDR         18
#define CMD2DC_SWITCHAUXOUT         19
#define CMD2DC_UCONTACTON           111
#define CMD2DC_DEVICE_PARAM         23
#define CMD2DC_SEND_MACH_ID         11
#define CMD2DC_RDBK_DEV_PARA        14

#define CMD2DC_RDBK_MACH_ID         15

#define CMD2DC_MDB_ON               120
#define CMD2DC_MDB_GET_STATE        107  // REQ
#define CMD2DC_MDB_DORESET          121
#define CMD2DC_MDB_SETWAK           122
//#define CMD2DC_MDB_GETWAK           0x2812  // REQ not nec.
#define CMD2DC_MDB_SENDCMD          123
#define CMD2DC_MDB_SENDMSG          12
#define CMD2DC_MDB_GETRESP          22  // REQ

#define CMD2DC_EMP_SET              24
#define CMD2DC_EMP_GET_ALL          23  // REQ
#define CMD2DC_EMP_STARTPOLL        124
#define CMD2DC_EMP_STARTPAY         125
#define CMD2DC_EMP_STOPPAY          126
#define CMD2DC_EMP_GOTCOIN          108  // REQ
#define CMD2DC_SHUTTER_OPEN         129
#define CMD2DC_ESCR_OPEN            132
#define CMD2DC_ESCR_TAKE            133
#define CMD2DC_ESCR_RETURN          134
#define CMD2DC_MOD_ON               135
#define CMD2DC_MOD_WAK              136
#define CMD2DC_CRED_ON              137
#define CMD2DC_CRED_WAK             138

// READ Commands ((e.g. get input)
#define CMD2DC_TestSerial           10
#define CMD2DC_GetSerialConfig      105
#define CMD2DC_RdBkHWversion        11
#define CMD2DC_RdBkSWversion        12
#define CMD2DC_RdBkDCstate          101
#define CMD2DC_RdBkUID              18
#define CMD2DC_RdBkTime             104
#define CMD2DC_RdBkAnalog           106
#define CMD2DC_GetAllInputs         102
#define CMD2DC_RdBkAllOutputs       103
#define CMD2DC_MIFREADERON          127
#define CMD2DC_ATB_CREATE           128




// Mif read data:
#define CMD2DC_RdBk_MifState        109
#define CMD2DC_RdBk_MifData         24




#define CMD2DC_RdBk_AtbCardType     25
#define CMD2DC_SHUTTER_COIN         131
#define CMD2DC_SHUTTER_OPEN3S       130
#define CMD2DC_SEND_SHUT_TIME       0x2915
#define CMD2DC_ESCR_TAKE            133
#define CMD2DC_ESCR_RETURN          134
#define CMD2DC_PRINTERON            139
#define CMD2DC_RdBk_PrnState        110
#define CMD2DC_RdBk_PrnFonts        26
#define CMD2DC_RdBk_AllPrnData      27
                                           // nr of params:
#define CMD2DC_PRI_SYS_CMD          25  // 3
#define CMD2DC_PRI_ESC_CMD          26  // 4
#define CMD2DC_PRI_SETUP            27  // 5
#define CMD2DC_PRI_MOVE             140  // 2
#define CMD2DC_PRI_SETFONT          141  // 4
#define CMD2DC_PRI_SETLETTER        142  // 3
#define CMD2DC_PRI_CUT              143  // 1
#define CMD2DC_PRI_PRINT_TXT        13  // 64
#define CMD2DC_PRI_LF               144  // 1
#define CMD2DC_PRI_PRIFONTTABLE     145
#define CMD2DC_PRI_BARCODE          14  // ca 15...25
#define CMD2DC_STOR_QR_DATA         15  // 150
#define CMD2DC_PRI_QR_CODE          146  // 0
#define CMD2DC_PRI_LOGOFROMFLASH    147  // 2
#define CMD2DC_PRI_STORE_DOC        16  // 1
#define CMD2DC_PRI_DOCUMENT_NR      17  // 1 + 64
#define CMD2DC_PRI_CLEAR_DOC        148  // 1


/*
// WRITE Commands (e.g. switch relay)
#define CMD2DC_sendTime             0x1310
#define CMD2DC_setWakeFrequ         0x1320

#define CMD2DC_MOV_UPLOCK           0x1801
#define CMD2DC_MOV_DNLOCK           0x1802
#define CMD2DC_UPPER_DOOR           0x1810
#define CMD2DC_LOWER_DOOR           0x1811
#define CMD2DC_VAULT_DOOR           0x1812

// neu 7.10.21:
#define CMD2DC_REJMOT_ON            0x1813
#define CMD2DC_REJMOT_RUN           0x1814


#define CMD2DC_LED_COIN             0x1204
#define CMD2DC_LED_ILLU             0x1205
#define CMD2DC_LED_TICKET           0x1206
#define CMD2DC_LED_START            0x1208
#define CMD2DC_LED_PIN              0x1207
#define CMD2DC_LED_IN               0x1209
#define CMD2DC_FAN                  0x1210
#define CMD2DC_SIREN                0x1211
#define CMD2DC_BARRIER              0x1212
#define CMD2DC_WAKEPTU              0x1218
#define CMD2DC_SWITCHAUXPWR         0x1220
#define CMD2DC_SWITCHAUXDDR         0x1222
#define CMD2DC_SWITCHAUXOUT         0x1224
#define CMD2DC_UCONTACTON           0x1226

#define CMD2DC_DEVICE_PARAM         0x2000
#define CMD2DC_SEND_MACH_ID         0x2002
#define CMD2DC_RDBK_DEV_PARA        0x2001
#define CMD2DC_RDBK_MACH_ID         0x2003



// --------------------------- MDB --------------
#define CMD2DC_MDB_ON               0x2800
#define CMD2DC_MDB_GET_STATE        0x2801  // REQ
#define CMD2DC_MDB_DORESET          0x2802
#define CMD2DC_MDB_SETWAK           0x2811
//#define CMD2DC_MDB_GETWAK           0x2812  // REQ not nec.
#define CMD2DC_MDB_SENDCMD          0x2820
#define CMD2DC_MDB_SENDMSG          0x2821
#define CMD2DC_MDB_GETRESP          0x2822  // REQ

// --------------------------- EMP --------------
#define CMD2DC_EMP_SET              0x2830
#define CMD2DC_EMP_GET_ALL          0x2831  // REQ
#define CMD2DC_EMP_STARTPOLL        0x2832
#define CMD2DC_EMP_STARTPAY         0x2834
#define CMD2DC_EMP_STOPPAY          0x2836
#define CMD2DC_EMP_GOTCOIN          0x2837  // REQ








#define CMD2DC_SHUTTER_OPEN         0x2911
#define CMD2DC_ESCR_OPEN            0x2920
#define CMD2DC_ESCR_TAKE            0x2921
#define CMD2DC_ESCR_RETURN          0x2922
#define CMD2DC_MOD_ON               0x2940
#define CMD2DC_MOD_WAK              0x2941
#define CMD2DC_CRED_ON              0x2960
#define CMD2DC_CRED_WAK             0x2961


// READ Commands ((e.g. get input)
#define CMD2DC_TestSerial           0x1101
#define CMD2DC_GetSerialConfig      0x1107
#define CMD2DC_RdBkHWversion        0x110A
#define CMD2DC_RdBkSWversion        0x110B
#define CMD2DC_RdBkDCstate          0x110C

#define CMD2DC_RdBkUID              0x1305
#define CMD2DC_RdBkTime             0x1313
#define CMD2DC_RdBkAnalog           0x1550
#define CMD2DC_GetAllInputs         0x1201
#define CMD2DC_RdBkAllOutputs       0x1202


#define CMD2DC_MIFREADERON          0x2900
#define CMD2DC_ATB_CREATE           0x2907
// Mif read data:
#define CMD2DC_RdBk_MifState       0x2902
#define CMD2DC_RdBk_MifData         0x2903
#define CMD2DC_RdBk_AtbCardType     0x2905
//#define CMD2DC_RdBk_CardData        0x2906




// higher Level operation commands
//#define CMD2DC_SHUTTER_ONE          0x2912
#define CMD2DC_SHUTTER_COIN         0x2913
#define CMD2DC_SHUTTER_OPEN3S       0x2912
#define CMD2DC_SEND_SHUT_TIME       0x2915

#define CMD2DC_ESCR_TAKE            0x2921
#define CMD2DC_ESCR_RETURN          0x2922


#define CMD2DC_PRINTERON            0x2A01
#define CMD2DC_RdBk_PrnState        0x2A02
#define CMD2DC_RdBk_PrnFonts        0x2A12
#define CMD2DC_RdBk_AllPrnData      0x2A40

                                            // nr of params:
#define CMD2DC_PRI_SYS_CMD          0x2A03  // 3
#define CMD2DC_PRI_ESC_CMD          0x2A04  // 4
#define CMD2DC_PRI_SETUP            0x2A05  // 5
#define CMD2DC_PRI_MOVE             0x2A06  // 2
#define CMD2DC_PRI_SETFONT          0x2A10  // 4
#define CMD2DC_PRI_SETLETTER        0x2A11  // 3
#define CMD2DC_PRI_CUT              0x2A13  // 1

#define CMD2DC_PRI_PRINT_TXT        0x2A14  // 64

#define CMD2DC_PRI_LF               0x2A15  // 1
#define CMD2DC_PRI_PRIFONTTABLE     0x2A16
#define CMD2DC_PRI_BARCODE          0x2A17  // ca 15...25

#define CMD2DC_STOR_QR_DATA         0x2A18  // 150
#define CMD2DC_PRI_QR_CODE          0x2A19  // 0

#define CMD2DC_PRI_LOGOFROMFLASH    0x2A1A  // 2

#define CMD2DC_PRI_STORE_DOC        0x2A41  // 1
#define CMD2DC_PRI_DOCUMENT_NR      0x2A42  // 1 + 64
#define CMD2DC_PRI_CLEAR_DOC        0x2A43  // 1

*/




#define FIX_SLAVE_ADDR          0
#define SEND_ATONCE             1
#define SENDCOMBINED            0




class T_datif : public QMainWindow
{
    Q_OBJECT    

    char sendINrequestsAutomatic(void);
        // sende alle Befehle um die Eingangsdaten abzufragen der Reihe nach
    char loadRecDataFromFrame();
    void datif_startSending(void);
    void datif_sendIOrequest(uint16_t WRcmd, uint16_t RDcmd, uint8_t nrOfWrData);

    void datif_send8byteOutCmd(uint16_t WRcmd, uint16_t RDcmd);

    bool verifyLineTestresponse(uint8_t RdDlen, uint8_t *receivedData);
    void datif_OUT_setTime(void);
    uint8_t datif_OUT_SendRandomData(uint8_t *buf, uint8_t Length);

    void datif_send64byteOutCmd(uint16_t WRcmd, uint16_t addr, uint16_t RDcmd);

    void datif_sendToMemory(uint16_t WRcmd, uint16_t docNr, uint16_t blockNr, uint8_t *data64);
        // send printer documents to DC2 memory
        // docNr: 0...15(31)    with 1280 byte each (20 blocks a 64byte)
        // blockNr=0...19   with 64byte each
        // docNr =transmitted in WRITEADDRESS high byte
        // blockNr=transmitted in WRITEADDRESS low byte


    T_prot *myDCIF;
    QTimer *datif_trigger;
    uint8_t selectedSlaveAddr;

private slots:
    char datif_cycleSend();
    void StoredRecData();

public:
    T_datif(QWidget *parent = nullptr);

    T_prot *getProt() { return myDCIF; }
    T_prot const *getProt() const { return myDCIF; }

    void resetChain(void);
    char isPortOpen(void);
    void sendWRcommand(uint16_t nxtAsCmd);
        // Sende Schreibbefehle die bereits vorher asynchron gespeichert wurden
    void send_requests(uint16_t nextWrCmd);
    void sendHighLevel(uint16_t nxtHLCmd);
    bool areDataValid(void);

signals:
    void ResponseRecieved();
        //the requested data are stored in peripheral image
        // can be loaded with epi

    void datif_templatePrintFinished_OK();
    void datif_templatePrintFinished_Err();

    void datif_gotNewCoin();

};

#endif // CI_H