
#ifndef STOREINDATA_H
#define STOREINDATA_H

#include <stdint.h>
//#include "tslib.h"
#include <QString>


#define MAXNROF_AO              3
//#define MAXNROF_GENSTR          16
#define MAXNROF_CONTR_PORTS     11
#define MAXNROF_DIports         2
#define MAXNROF_DOports         2
#define MAXNROF_CTR             2
#define MEASCHAN_TEMPERATURE    0
#define MEASCHAN_VOLTAGE        1


// gpi: DC-driver stores data for graphic peripheral interface
// epi: gui reads values from external peripheral interface


// store power on/off condition of the devices to control the data request

void indat_storePrinterPower(bool isOn);
bool indat_isPrinterOn();

void indat_storeMifarePower(bool isOn);
bool indat_isMifareOn();

void indat_storeMDBisOn(bool isOn);
bool indat_isMdbOn();

void gpi_storeSlaveSerParams(uint8_t slaveBaudRate, uint8_t NrDataBits,
                             uint8_t parity, uint8_t NrStopBits);

void epi_getSlaveSerParams(uint8_t *slaveBaudRate, uint8_t *NrDataBits,
                             uint8_t *parity, uint8_t *NrStopBits);

QString epi_getSlaveParamSTR();

void gpi_storeGenerals(uint8_t genNr, QString text);
    // 0=HW   1=SW  2=State
QString epi_loadGenerals(uint8_t genNr);
// genNr=0=HW   1=SW  2=State


void gpi_storeUID(uint8_t const *buf8byteUid);
// buffer size: 8 byte
void epi_getUIDdec(uint8_t *buf8byteUid);
// buffer size: 8 byte
QString epi_getUIDstr();





// ///////////////////////////////////////////////////////////////////////////////////
//          Time and Date
// ///////////////////////////////////////////////////////////////////////////////////

uint8_t epi_getSquareMode();
void gpi_backupSquareMode(uint8_t squMode);

void gpi_backupTime(uint8_t *timeBuffer, uint8_t Leng);     // 104, <=8byte
void epi_getTime(uint8_t *hh, uint8_t *mm, uint8_t *ss);
void epi_getDate(uint8_t *yy, uint8_t *mm, uint8_t *dd);
void epi_getToday(uint8_t *dow, uint16_t *minOfToday, uint32_t *secOfToday);
bool epi_isLeapYear(uint8_t *lastLeapYear, uint8_t *NextLeapYear);
bool epi_isLeapYear();
void epi_getSpecialWeekTimeDate(uint8_t *DayOfWeek, uint8_t *HoursOfWeek, uint16_t *MinutesOfWeek);
void epi_getSpecialMonthTimeDate(uint8_t *DayOfMonth, uint16_t *HoursOfMonth, uint16_t *MinutesOfMonth);
void epi_getSpecialYearTimeDate(uint16_t *DayOfYear, uint16_t *HoursOfYear, uint32_t *MinutesOfYear);


// ///////////////////////////////////////////////////////////////////////////////////
//          Analog values
// ///////////////////////////////////////////////////////////////////////////////////


// #define MAXNROF_AI              4

void gpi_storeAIs(uint8_t aiNr, uint16_t val);   // rs -> Sdata

uint8_t gpi_getMaxNrAIs();

uint16_t epi_loadAIs(uint8_t aiNr);              //       Sdata -> gui
    // return value of one ADC with channel nr: aiNr 0...15

uint32_t epi_loadMeasureValue(uint8_t ValueNr);
    // ValueNr 0=ADC0, 1=ADC1 aso...

void gpi_storeMeasureValue(uint8_t ValueNr, uint32_t val);
    // in mV, also bis 65,535V

QString epi_getSlaveTemperatureStr();

QString epi_getSlaveVoltageStr();
    // value in "meas_volt" in mV, also bis 65,535V. Value range [6000...16000] (6V...16V)

// ///////////////////////////////////////////////////////////////////////////////////
//          digital inputs
// ///////////////////////////////////////////////////////////////////////////////////

void gpi_storeDI_doorSwitches(uint8_t upperDoor, uint8_t lowerDoor, uint8_t vaultDoor);

uint8_t epi_getDI_doorSwitches(void);
    // bit0: upper door  1: low door  2:vault door

void gpi_storeDI_vaultSwitches(uint8_t CashBoxIn, uint8_t BillBoxIn);

uint8_t epi_getDI_vaultSwitches(void);
    // bit0: cash box    1: bill box in

void gpi_storeDI_lockSwitches(uint8_t indatUL, uint8_t indatLL);
    // D5: bit 0: upper lockbar up   bit1:down
    // D6: bit 0: lower lockbar up   bit1:down

uint8_t epi_getDI_lockSwitches(void);
    // retval: bit 0: upper lockbar up   bit1: upper lockbar is down
    //         bit 2: lower lockbar up   bit1: lower lockbar is down


void gpi_storeDI_optos(uint8_t indatOpto);
    // OptoIn bit 0,1: optoin 1,2

uint8_t epi_getDI_optos(void);
    // bit0: opto in 1    1: opto in 2

uint8_t gpi_storeDI_auxIn(uint8_t indatAuxIn); // Aux0...5
uint8_t epi_getDI_auxIn(void);                 // bit0: auxin 1  ...  5: auxin 6

bool gpi_storeDI_ptuWake(bool w);
bool epi_getDI_ptuWake(void);

bool gpi_storeDI_mbdWake(bool w);
bool epi_getDI_mdbWake(void);

bool gpi_storeDI_prnReady(bool ready);
bool epi_getDI_prnReady(void);

bool gpi_storeDI_CoinAttach(bool attach);
bool epi_getDI_CoinAttach(void);

bool gpi_storeDI_CoinEscrow(bool ce);
bool epi_getDI_CoinEscrow(void);

bool gpi_storeDI_mifareCardTapped(bool tapped);
bool epi_getDI_mifareCardTapped(void);

bool gpi_storeDI_modemWake(bool w);
bool epi_getDI_modemWake(void);

bool gpi_storeDI_contactPowerIsOn(bool on);
bool epi_getDI_contactPwr(void);

bool gpi_storeDI_MifarePowerIsOn(bool on);
bool epi_getDI_mifarePwr(void);

bool gpi_storeDI_readbackMdbTxD(bool rdbkMdbTxd);
bool epi_getDI_mdbTxd(void);

bool gpi_storeDI_AuxPowerIsOn(bool on);
bool epi_getDI_auxPwr(void);

bool gpi_storeDI_GsmPowerIsOn(bool on);
bool epi_getDI_gsmPwr(void);

bool gpi_storeDI_CreditPowerIsOn(bool on);
bool epi_getDI_creditPwr(void);

bool gpi_storeDI_PrinterPowerIsOn(bool on);
bool epi_getDI_printerPwr(void);

bool gpi_storeDI_MdbPowerIsOn(bool on);
bool epi_getDI_mdbPwr(void);

bool gpi_storeDI_rejMot_home(bool reject);
bool epi_getDI_rejectMotor_homepos(void);

uint8_t gpi_storeDI_paperLow(uint8_t di);
uint8_t epi_getDI_npe_sensor(void);
    // 0: Sensor sees paper 1: no paper 99: off

// ///////////////////////////////////////////////////////////////////////////////////
//          readback digital outputs
// ///////////////////////////////////////////////////////////////////////////////////

uint8_t gpi_storeDO_mdbRxTst(uint8_t do_mbdRxTst);
bool epi_getDO_mdbRxTestOut(void);

uint8_t gpi_storeDO_motorOutputs(uint8_t Pwr);
uint8_t epi_getDO_motorOuts(void);
    // bit0: upper lock forward    bit 1 backward
    // bit2: lower lock forward    bit 3 backward

uint8_t gpi_storeDO_serialSwitch(uint8_t state);
    // serial drv on/off, Serial mux1, Serial mux2
uint8_t epi_getDO_serialSwitch(void);
    // serial drv on/off, Serial mux1, Serial mux2

bool epi_getDO_serialDriverIsOn(void);
bool epi_getDO_serialMux1isSetToPrinter(void);
    // mux1 off: serial is switched to printer

bool epi_getDO_serialMux1isSetToModem(void);
    // mux1 on: serial is switched to modem
bool epi_getDO_serialMux2isSetToCredit(void);
    // mux2 off: serial is switched to credit card terminal
bool epi_getDO_serialMux2isSetToMifare(void);
    // mux2 on: serial is switched to mifare reader

uint8_t gpi_storeDO_ledsAndFan(uint8_t ledState);
bool epi_getDO_led_coin(void);
bool epi_getDO_led_front(void);
bool epi_getDO_led_ticket(void);
bool epi_getDO_led_pin(void);
bool epi_getDO_led_start(void);
bool epi_getDO_led_inside(void);
bool epi_getDO_fan(void);

uint8_t gpi_storeDO_sirenAndRelay(uint8_t sirenRelay);
bool epi_getDO_sirene(void);
bool epi_getDO_relay(void);

uint8_t gpi_storeDO_ptuWake(uint8_t state);
bool epi_getDO_ptuWake(void);

uint8_t gpi_storeDO_auxPower(uint8_t pwr);
bool epi_getDO_auxPower(void);

uint8_t gpi_storeDO_coinShutter(uint8_t state);
bool epi_getDO_coinShutterOpen(void);
bool epi_getDO_coinShutterTest(void);

uint8_t gpi_storeDO_coinEscrow(uint8_t state);
uint8_t epi_getDO_coinEscrow(void);
    // retval: 1:return flap is open   2:take flap is open    0:closed

uint8_t gpi_storeDO_printerPwrOn(uint8_t state);
uint8_t epi_getDO_printerPwr(void);

// ---------------------------------------------------------------------------------------------
// counterchecks, make sure that DC-outputs are correct

/*
bool epi_cntchk_wakePtu(void);
bool epi_cntchk_enabDrv01(void);        // no communication possible if 0 !!!!!
bool epi_cntchk_swRs1toModem(void);
bool epi_cntchk_modemWake(void);
bool epi_cntchk_enabDrv2(void);
bool epi_cntchk_swRs2toMIF(void);

bool epi_cntchk_shutterIsOpen(void);
    // counter check if shutter is really open,  PJ4 must be OUT and HIGH, PB5 must be OUT and HIGH
    // retval TRUE: shutter is open   FALSE: shutter is closed

bool epi_cntchk_escrowReturnIsOpen(void);
bool epi_cntchk_escrowTakeIsOpen(void);
bool epi_cntchk_aux1DirOut(uint8_t auxNr);
bool epi_cntchk_aux1OutHigh(uint8_t auxNr);

bool epi_cntchk_ledPaperOn(void);
bool epi_cntchk_ledPinpadOn(void);
bool epi_cntchk_ledStartOn(void);
bool epi_cntchk_ledServiceOn(void);
bool epi_cntchk_ledCoinOn(void);
bool epi_cntchk_ledIllumOn(void);
bool epi_cntchk_FanOn(void);
bool epi_cntchk_RelaisOn(void);
bool epi_cntchk_LaermOn(void);
bool epi_cntchk_Mot1Ron(void);
bool epi_cntchk_Mot1Fon(void);
bool epi_cntchk_Mot2Ron(void);
bool epi_cntchk_Mot2Fon(void);
*/


// ------------------------------------------------------------------------------------
// MDB Sendind Data  are store here for next transport to DC (Device Controller)
// Transport to Slave runs every 100ms, answer from mdb-slave (e.g. coin changer) comes right
// with next slave answer

// start with: SENDDIRCMD_EXCHGMDB,
    // send crude data from here to DC, DC to mdb slaves, mdb answer, return here within 50ms

uint8_t gpi_storeMdbRecData(uint8_t length, uint8_t *buf);
    // datif store received mdb data
uint8_t epi_getMdbResponse(void);
     // 0=no response  1=ACK  2=NAK  3=ACK with data
uint8_t epi_getMdbRecLength(void);
    // 0...31
uint8_t epi_restoreMdbRecData(uint8_t *buf);
    // hwapi reads received mdb data from PI

uint8_t gpi_storeMifReaderStateAndCardType(uint8_t const *buf);

/* data description:
byte 0: current read state: 0=power off  1=reader-fault 2=ready
                            3=just reading 4=read complete
                            5=read partial, removed too early
                            6=state unknown
byte 1,2: read data length from card
3: 1=reader is OK (reported serial nr is OK)  0=wrong or no reader
4...15: reader version, expected "SL025-1.8"
byte16: 1=card is present   0:not
17: 0
18: card type reported from reader
19: 1=allowed card type 0=not
20: card size: 1 or 4 (dec) = card size
21: LengthOfUID: 4 or 7 (dec) (byte)
22: UID 8 byte in hex
byte 30: sector logged: 0
byte 31: current sector: 0
byte 32: result, always 0
*/

uint8_t epi_restoreMifState(uint8_t *buf, uint8_t maxBufferSize);
    // retval 0=OK 1=error host buffer too small
bool gpi_storeMifCardData(uint8_t blkNr, uint8_t const *receivedData);
    // blkNr=0...11    receivedData[64]
uint8_t epi_restoreMifData(uint8_t blkNr, uint8_t *buf, uint8_t maxBufferSize);
    // blkNr=0...11    return buf[64]
    // retval: 1=error 0=OK

void epi_restorePrinterState(uint8_t *buf);
void gpi_storePrinterState(uint8_t const *buf);

void epi_restorePrinterFonts(uint8_t *buf);
void gpi_storePrinterFonts(uint8_t const *buf);

bool gpi_storeMdbState(bool busReady, bool V12on, bool V5on);
bool epi_restoreMdbBusReady(void);
bool epi_restoreMdbV12Ready(void);
bool epi_restoreMdbV5Ready(void);

void gpi_storeMdbResponse(uint8_t leng, uint8_t const *data);
void epi_restoreMdbResponse(uint8_t *leng, uint8_t *data);
    // last received mdb answer (from mdb device)
    // only needed if a special command was sent directly
    // DB0: mdb Device-Nr
    // DB1: last sent mdb command
    // DB2: nr of received (payload) data bytes (apart from ACK, can be 0....34)
    // DB3...DB38: rec.data (payload)

void gpi_storeEmpSettings(uint8_t leng, uint8_t const *data);
void epi_restoreEmpSettings(uint8_t *leng, uint8_t *data);

/*
void gpi_storeEmpCoinSignal(uint8_t leng, uint8_t *data);

void epi_restoreEmpCoinSignal(uint8_t *leng, uint8_t *data);
    // return 5 byte:
    //      data[0]=got coin 0xFF=emp reported an error  0=got nothing
    //      data[1]=emp-signal of last inserted coin
    //      data[2,3]=emp-value of last inserted coin
    //      data[4] = emp-error or warning

void epi_clearEmpCoinSignal();
*/


void gpi_storeEmpCoinSignal(uint8_t leng, uint8_t const *data);

uint8_t epi_isNewCoinLeft(void);
    // retval: 0...16 coins left in FIFO
void epi_restoreEmpCoinSignal(uint8_t *valid, uint8_t *signal, uint8_t *error, uint16_t *value);

void gpi_storeRbDeviceSettings(uint8_t leng, uint8_t const *data);
void epi_restoreRbDeviceSettings(uint8_t *leng, uint8_t *data);

void gpi_storeMachineIDsettings(uint8_t leng, uint8_t const *data);
void epi_restoreMachineIDsettings(uint8_t *leng, uint8_t *data);

void epi_clearCurrentPayment(void);
void gpi_storeCurrentPayment(uint32_t insertedAmount, uint16_t lastCoinType, uint16_t lastCoinValue);

uint32_t epi_CurrentPaymentGetAmount(void);
uint16_t epi_CurrentPaymentGetLastCoin(void);

bool epi_CurrentPaymentGetAllCoins(uint16_t *types, uint16_t *values);
    // alle bei diesem Verkauf eingeworfenen Münzen sind gespeichert falls die jmd. braucht

void gpi_storeWakeSources(uint8_t const *receivedData);
uint64_t epi_getWakeSources(void);
uint8_t epi_getWakeReason(void);

void gpi_storeExtendedTime(uint8_t leng, uint8_t const *data);
void epi_restoreExtendedTime(uint8_t *leng, uint8_t *data);

void gpi_storeDeviceConditions(uint8_t leng, uint8_t const *data);
void epi_restoreDeviceConditions(uint8_t *leng, uint8_t *data);

void gpi_storeDynMachineConditions(uint8_t leng, uint8_t const *data);
void epi_restoreDynMachineConditions(uint8_t *leng, uint8_t *data);

void gpi_storeDCbackupAccNr(uint8_t leng, uint8_t const *data);
void epi_restoreDCbackupAccNr(uint8_t *leng, uint16_t *accNrs);
    // return accNrs[0..7]

void epi_iniVRstorage(void);
void gpi_storeVaultRecord(uint8_t blkNr, uint8_t const *data);
bool epi_checkIfVaultRecordAvailable(void);
bool epi_restoreVaultRecord(uint16_t *length, uint8_t *buf);
    // true if completly received

void gpi_storeCBlevel(uint32_t amount, uint16_t nrOfCoins);

uint32_t epi_getCashBoxContent(void);
uint16_t epi_getNrOfCoinsInCashBox(void);

void gpi_storeNewMifareCard(uint8_t typ, uint8_t const *holder);

uint8_t epi_mifGetCardType(uint8_t const *holder);
    //holder[8] = name of card holder
    // retval Type of MifareCard, 1=upper door, 2=lower door 3=test printer 4=test coins

void gpi_storeDcDataValid(bool isVal);
bool gpi_areDcDataValid();



#endif



