#ifndef CONTROLBUS_H
#define CONTROLBUS_H


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


// ///////////////////////////////////////////////////////////////////////////////////
//          control serial interface   gui <--> serial
// ///////////////////////////////////////////////////////////////////////////////////


void epi_setSerial(int BaudNr, QString BaudStr, QString ComName, uint8_t connect);
    // Actions: open serial port with parameters

void epi_closeSerial(void);
// Actions: close serial port

// Actions, GUI Buttons -> API, start cyclic transmission
void epi_startEmmision(char start);   // 1: start sending activated

//void epi_setPeriodicSendTimeVal(uint16_t val);


//  Port -> API
void gpi_serialChanged(void);
    // serial confirms that port was closed or opened

// Actions, API -> serialPort
uint8_t gpi_getSerialConn(void);      // connect if 1, disconnect if 0

int gpi_getBaudNr(void);

QString gpi_getComPortName(void);


void gpi_serialIsOpen(bool offen);

bool epi_isSerialPortOpen();
    // true: port is open   false: port is closed





// Meldung von TabCom an Datif: starte zyklische Sendung:
bool gpi_isEmmisionOn(void);

//uint16_t gpi_getPeriodicSendTimeVal();

//bool gpi_PeriodicSendTimeHasChanged();

//void epi_setCurrSlavAddr(int slavAd);

//int gpi_getCurrSlavAddr(void);




// ///////////////////////////////////////////////////////////////////////////////////
//          Status Display    gui <--> serial
// ///////////////////////////////////////////////////////////////////////////////////

//---------------------
    // Statuszeile COM Port  (serial Port) (open, closed)
// Display in tab_com
QString epi_getTxt4comStateLine(void);
void epi_clrTxt4comStateLine();
    // GUI: get Text for serial Comport-State Line

//---------------------
// Statuszeile Handshakes (serial Control)   flow.cpp
// geht überhaupt was raus? kommt überhaupt was zurück?
// I
QString epi_getTxt4HsStateLine(void);
void epi_clrTxt4HsStateLine();
    // GUI: get Text

// II Master receive state (empfangenes Telgramm OK? crc? length?  )
// Statuszeile Auswertung der SlaveResponse (serial Frame, CRC usw)  (prot.cpp)
QString epi_getTxt4masterStateLine(void);
void epi_clrTxt4masterStateLine();

// III Slave receive (from Master) OK? if then show results, if not then show errors
// entweder Empfangsfehler anzeigen (crc? length?) oder result OUT-OK, OUT_ERR, IN_OK, IN_ERR
// Hintergrund: wenn der Slave Fehler im Master-Telegramm gefunden hat, dann kann er es auch
// nicht verwenden und nichts ausgeben oder einlesen
QString epi_getTxt4resultStateLine(void);
void epi_clrTxt4resultStateLine();

// IV  Statuszeile Sende- und Empfangsdaten (Datif)
// Display in tab_com
QString epi_getTxt4dataStateLine(void);
void epi_clrTxt4dataStateLine();
// GUI: get Text for serial Comport-State Line

// V, unten, Datif
QString epi_getTxt4datifLine(void);
void epi_clrTxt4datifLine();

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

        // sende-empfangs-Rohdaten-Fenster
// Display in tab_com
QString epi_getTxt4RsDiagWin(void);
void epi_clrTxt4RsDiagWin();

QString epi_get2ndTxt4RsDiagWin(void);
void epi_clr2ndTxt4RsDiagWin();


// Statuszeile COM Port  (serial Port) (open, closed)
// Display in tab_com
void gpi_setTxt4comStateLine(QString txtline);
    // serial: write Text to be displayed in serial Comport-State line (like "connected")
    // used in vcp.cpp, links in tabCom


// Statuszeile Handshakes (serial Control)
// I obere Zeile
void gpi_setTxt4HsStateLine(QString txtline);
    // used in flow.cc

// II
void gpi_setTxt4masterStateLine(QString txtline);

// III
void gpi_setTxt4resultStateLine(QString txtline);

// IV
void gpi_setTxt4dataStateLine(QString txtline);
    // serial: write Text to be displayed in serial Comport-State line (like "connected")
    // used in prot.cpp

// V  unten:
void gpi_setTxt4datifLine(QString txtline);


// sende-empfangs-Rohdaten-Fenster
// Display in tab_com
void gpi_setTxt4RsDiagWin(QString txtline);
void gpi_set2ndTxt4RsDiagWin(QString txtline);

// ///////////////////////////////////////////////////////////////////////////////////
//          Memory for Slave responses, common data
// ///////////////////////////////////////////////////////////////////////////////////

bool epi_getResult_serialTestOK();
// retval: true: test was successful, got right response


// result of serial line test, slave sent fixed string
void gpi_storeResult_serialTestOK(bool wasOn);


// ///////////////////////////////////////////////////////////////////////////////////
//          restore just received data
// ///////////////////////////////////////////////////////////////////////////////////


uint8_t gpi_startNewRequest();
    // called by Datif

uint8_t gpi_storeResultOfLastRequest(bool answisok);
    // written by Datif

uint8_t epi_getResultOfLastRequest();
    // retval: 0: in progress  1: OK  2: error


void gpi_storeRecPayLoad(uint8_t RdDlen, uint8_t const *receivedData);
    // stored by Datif

uint16_t epi_getLastPayLoad(uint16_t plBufSiz, uint8_t *payLoad);
    // get data back in *pl, max 64 byte
    // retval = nr of bytes received. If host buffer too small then
    // only plBufSíz bytes are copied to pl
    // plBufSíz=size of host buffer



#endif