//*****************************************************************************
//
// ExtPort.c - Knihovna pro ovladani rozsir.portu
//
//*****************************************************************************

#include <I2Cdriver.h>
#include <ExtPort.h>

//Promenne------------------
unsigned char EPData[2];


//Zapis na rozsirujici 16b port / vice obvodu, nutno predat i ID----------------
void WritePort(unsigned char PortID, unsigned char *pData)
{
                                              //predavam primo adresu obvodu na IIC, ne ID!!!
    EPData[0]=*pData++;                       //ulozeni dat posilanych na port
    EPData[1]=*pData;                         //pokud jde jen o 8mi bitovy ext.port, musi byt stejne byty
  
    I2C_Write(PortID, &EPData[0], 2);         //zapisu nastaveni  
}

//Cteni hodnot z rozsirujiciho portu--------------------------------------------
void ReadPort(unsigned char PortID, unsigned char *pData)
{
                                                //PortID = 0x30 (dig.IO) / 0x40 (LED) / 0x50 (card addr.)
  I2C_Read(DATA_ADDR_NONE,pData,(PortID|0x02)); //Cteni dat z daneho rozsirujiciho portu po IIC sbernici
  
}

//Nastaveni digitalnich vystupu-------------------------------------------------
void WriteDigOut(unsigned char ucDigOut)            //zapisuji na spodni 4bity
{
    unsigned short tempDigOut=0x0000;
    
    tempDigOut|=(ucDigOut<<4);                                      //posun o 4bity doleva / vystupy na rozsir.portu
    tempDigOut=tempDigOut<<8;
    tempDigOut|=(ucDigOut<<4);                                      //posun o 4bity doleva / vystupy na rozsir.portu
    WritePort(ADDRESS_EP_DIGIO, (unsigned char *)&tempDigOut);      //zapis na port s digitalnimi vstupy a vystupy
}

//Cteni hodnot digitalnich vstupu-----------------------------------------------
unsigned char ReadDigIn(void)
{
    unsigned short tempDigIn;
    unsigned char *pUcDigIn;
    
    ReadPort(EP_IO, (unsigned char *)&tempDigIn);                 //cteni z portu stav dig IO / vraci 2 shodne byty
    pUcDigIn=(unsigned char *)&tempDigIn;                         //uvazuji jen spodni byte
    
    return (*pUcDigIn&0x0f);                                      //vracim jen hodnoty dig. vstupu
}

//Ovladani jednotlivych diod zobrazujicich stav prev.karty----------------------
void SetLEDState(unsigned char RedLED, unsigned char GreenLED)
{
    unsigned short LedState;

    LedState=GreenLED;                                            //Ulozim pozadovane hodnoty zelenych LED
    LedState=LedState<<8;                                         //posunu o byte
    LedState|=((RedLED&0x03)|0xfc);                               //ulozim stav cervenych LED
    WritePort(ADDRESS_EP_LED, (unsigned char *)&LedState);        //odeslani pozadovaneho nastaveni na rozsirujici port s LED
}

//Cteni adresy nastavene na DIP prepinacich a pomoci propojek-------------------
unsigned short ReadAddr(void)
{
    unsigned short tempCardAddr;
    
    ReadPort(EP_ADDR, (unsigned char *)&tempCardAddr);    //cteni z portu uzivatelsky nastavitelnou adresu karty
    
    return tempCardAddr;
}