//*****************************************************************************
//
// UARTCmds.h - Funkce pro zpracovani prikazu prijatych po UART
//
//*****************************************************************************

#ifndef __UARTCMDS_H__
#define __UARTCMDS_H__

//Definice konstant-----------------------
#define UART_MAX_COUNT  0x0a                                      //Maximalni delka dat v prikazu
#define UART_PREAM      0xff                                      //Hodnota preambule, ktera startuje komunikaci    
#define CMD_COUNT       0x05                                      //Pocet podporovanych prikazu
  
//definice konstant pro prijimaci stavovy automat
#define UART_STATE_IDLE   0x00                                    //Klidovy stav
#define UART_STATE_PREAM  0x01                                    //Prijata preambule, vysilani preambule
#define UART_STATE_CMD    0x02                                    //Prijat prikaz, vysilani
#define UART_STATE_COUNT  0x03                                    //Prijat pocet dat.bytu, vysilani
#define UART_STATE_DATA   0x04                                    //Prijaty datove byty, vysilani
#define UART_STATE_CRC    0x05                                    //Prijato CRC, vysilani
#define UART_STATE_PROCCESS   0x06                                //Zpracovavani prikazu 
#define UART_STATE_TX_PREAM   0x07                                //vysilani preambule
#define UART_STATE_TX_CMD     0x08                                //vysilani cisla prikazu
#define UART_STATE_TX_COUNT   0x09                                //vysilani poctu datu
#define UART_STATE_TX_DATA    0x0a                                //vysilani datovych bytu
#define UART_STATE_TX_CRC     0x0b                                //vysilani CRC
#define UART_STATE_TX_END     0x0c                                //Vysilani zakoncovaciho znaku
#define UART_STATE_TX_DONE    0x0d                                //Vysilani dokonceno

//definice konstant jednotlivych prikazu pres UART
#define UART_CMD_RD_STATE   0x00                                  //Prikaz pro cteni stavu prevodnikove karty
#define UART_CMD_OPT        0x01                                  //Cteni hodnoty z AD prevodniku
#define UART_CMD_TEMP       0x02                                  //Cteni teploty
#define UART_CMD_WR_EEPROM  0x03                                  //Zapis dat do EEPROM
#define UART_CMD_RD_EEPROM  0x04                                  //Cteni dat z EEPROM

//Struktura pro komunikaci pres UART / USB------------------------------------
struct UARTData{
  unsigned char State;                                          //Stav stavoveho automatu pro prijem prikazu
  unsigned char Preamble;                                       //Preambule
  unsigned char Cmd;                                            //Cislo prikazu
  unsigned char Count;                                          //Pocet datovych bytu
  unsigned char Data[UART_MAX_COUNT];                           //Pole datovych bytu
  unsigned char CRC;                                            //Prijaty kontrolni soucet
  unsigned char CompCRC;                                        //Vypocteny kontrolni soucet
};
extern struct UARTData UARTComm;

//Deklarace funkci pro zpracovani prikazu----------
extern void RecieveSM(unsigned char RecByte);                   //Stavovy automat pro prijem ramce pres UART
extern void UARTProcessCmd(void);                               //Aktivace prikazu
extern void SendReply(void);                                    //Odeslani odpovedi na prikaz
extern void UARTCmd_00(void);                                   //Prikaz 0 - cteni stavu karty
extern void UARTCmd_01(void);                                   //Prikaz 1 - ziskani hodnoty z AD prevodniku
extern void UARTCmd_02(void);                                   //Prikaz 2 - ziskani hodnoty teploty merene na karte
extern void UARTCmd_03(void);                                   //Prikaz 3 - zapis konfigurace karty do EEPROM
extern void UARTCmd_04(void);                                   //Prikaz 4 - cteni konfigurace karty z EEPROM

#endif    //__UARTCMDS_H__