/***********************************************************************/
/*                                                                     */
/*  FILE        :remote_debug.c                                        */
/*  DATE        :Mon, Feb 12, 2007                                     */
/*  DESCRIPTION :remote debugger for easyRoof.c                        */
/*  CPU TYPE    :R5F21134 (Renesas R8C13)                              */
/*  AUTHOR      :Dirk Siegmund                                         */
/*                                                                     */
/***********************************************************************/

#define LED_DP    0x80
#define LED_DPFL  0x40
#define LED_FL    0x20

const unsigned char LED_Translate[] =
                {   0x3F,    //0
                    0x06,    //1
                    0x5B,    //2
                    0x4F,    //3
                    0x66,    //4
                    0x6D,    //5
                    0x7D,    //6
                    0x07,    //7
                    0x7F,    //8
                    0x6F,    //9
                    0x77,    //A
                    0x7C,    //b
                    0x39,    //C
                    0x5E,    //d
                    0x79,    //E
                    0x71,    //F
                    0x76,    //H               10h
                    0x38,    //L               11h
                    0x73,    //P               12h
                    0x3E,    //U               13h
                    0x5C,    //Quadrat unten   14h
                    0x63,    //Quadrat oben    15h
                    0x40,    //-               16h
                    0x54,    //n               17h
                    0x6E,    //y               18h
                    0x70,    //t               19h
                    0x08,    //- unten         1ah
                    0x4C,    //] unten         1bh
                    0x58,    //[ unten         1ch
                    0x1C,    //u               1dh
                    0x50,    //r               1eh
                    0x00 };  //LEER            1fh

union byte_def Key_bits;
#define     Keys            Key_bits.byte

#define     Key_Plus        Key_bits.bit.b0
#define     Key_Start       Key_bits.bit.b1
#define     Key_Minus       Key_bits.bit.b2
#define     Key_Prog        Key_bits.bit.b3

unsigned char Digit[4]={0x00,0x01,0x02,0x03};

//+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

void i2c_delay(void)
{
int w=10;

    while(--w);
}

void i2c_start()
{
    SDA = 0;
    i2c_delay();
    SCL = 0;
    i2c_delay();
}

void i2c_stop()
{
    SDA = 0;
    i2c_delay();
    SCL = 1;
    i2c_delay();
    SDA = 1;
    i2c_delay();
}

char i2c_send(unsigned char b)
{
    unsigned char mask;
    mask = 0x80;

    do
    {
        if(b & mask)
                SDA = 1;
        else    SDA = 0;

        SCL = 1;
        i2c_delay();
        SCL = 0;
        i2c_delay();
        mask = mask/2;
    }
    while(mask > 0);

    SDA = 1;
    i2c_delay();
    SCL = 1;
    SDAdir  = 0;        // Port wird von Ausgang auf Eingang umgeschalten
    i2c_delay();
    if (SDA==1)
    {

        SCL = 0;
        SDAdir  = 1;        // Port wird von Eingang auf Ausgang umgeschalten
        return 0;
    }
    else
    {
        SCL = 0;
        SDAdir  = 1;        // Port wird von Eingang auf Ausgang umgeschalten
        return 1;
        i2c_delay();
    }
}

unsigned char i2c_read(void)
{
    unsigned char mask,daten;
    daten = 0;
    mask = 0x80;

    SDA     = 1;
    SDAdir  = 0;        // Port wird von Ausgang auf Eingang umgeschalten

    do
    {
        SCL = 1;
        i2c_delay();
        if(SDA ==1)daten = daten | mask;
        SCL = 0;
        i2c_delay();
        mask = mask/2;
    }
    while(mask > 0);

    SDAdir  = 1;        // Port wird von Eingang auf Ausgang umgeschalten

    return daten;
}


void i2c_ack()
{
    SDA = 0;
    i2c_delay();
    SCL = 1;
    i2c_delay();
    SCL = 0;
    i2c_delay();
    SDA = 1;
}

void remote_display(void)
{
unsigned char tempDigit,DigitCount;

    i2c_start();
    if(!i2c_send(0x70)) goto i2c_err;
    if(!i2c_send(0x00)) goto i2c_err;
    if(!i2c_send(0x27)) goto i2c_err;

    for(DigitCount=0;DigitCount<4;DigitCount++)
    {
        tempDigit=LED_Translate[Digit[DigitCount] & 0x1f];
        if( Digit[DigitCount] & LED_DP) tempDigit |= 0x80;
        if(!i2c_send(tempDigit)) goto i2c_err;
    }

    i2c_stop();
    Remote_avail=1;
    return;

i2c_err:
    i2c_stop();
    Remote_avail=0;

}

get_keys(void)
{
unsigned char readval;

    i2c_start();
    if(!i2c_send(0x40)) goto i2c_err2;
    if(!i2c_send(0xFF)) goto i2c_err2;
    i2c_stop();

    i2c_start();
    if(!i2c_send(0x41)) goto i2c_err2;
    readval = i2c_read();

    i2c_stop();

    if (readval & 0x01)
    {
        Edge_Plus=0;
    }
    else
    {
        if (!Edge_Plus) Key_Plus=1;
        Edge_Plus=1;
    }

    if (readval & 0x02)
    {
        Edge_Start=0;
    }
    else
    {
        if (!Edge_Start) Key_Start=1;
        Edge_Start=1;
    }

    if (readval & 0x04)
    {
        Edge_Minus=0;
    }
    else
    {
        if (!Edge_Minus) Key_Minus=1;
        Edge_Minus=1;
    }

    if (readval & 0x08)
    {
        Edge_Prog=0;
    }
    else
    {
        if (!Edge_Prog) Key_Prog=1;
        Edge_Prog=1;
    }

    return;

i2c_err2:
    i2c_stop();


}


void displayData(void)
{

    if (ABS_active & !Programming)
    {
        if (KMH<100)
        {
            Digit[0] = 0x1F;
            Digit[1] = 0x1F;
            Digit[2] = ((KMH %100) / 10)+LED_DP;
            Digit[3] = KMH %10;
        }
        else
        if (KMH<1000)
        {
            Digit[0] = 0x1F;
            Digit[1] = (KMH %1000) / 100;
            Digit[2] = ((KMH %100) / 10)+LED_DP;
            Digit[3] = KMH %10;
        }
        else
        {
            Digit[0] = (KMH %10000) / 1000;
            Digit[1] = (KMH %1000) / 100;
            Digit[2] = ((KMH %100) / 10)+LED_DP;
            Digit[3] = KMH %10;
        }
    }
    else
    {
        if (Programming)
        {
            Digit[0] = (SysVar.SpeedLimit %10000) / 1000;
            Digit[1] = (SysVar.SpeedLimit %1000) / 100;
            Digit[2] = ((SysVar.SpeedLimit %100) / 10)+LED_DP;
            Digit[3] = SysVar.SpeedLimit %10;
        }
        else
        {
            Digit[0] = 0x1F;
            Digit[1] = 0x1F;
            Digit[2] = 0x1F;
            Digit[3] = 0x1F;
        }
    }
}

void remoteProgrammer(void)
{
    if (Key_Prog)
    {
        Programming=~Programming;
        if(Programming)
             chirp_count=1;
        else chirp_count=2;
        Key_Prog=0;
    }

    if (Key_Start)
    {
        Key_Start=0;
        TOPREL_OUT=~TOPREL_OUT;
    }

    if (Key_Plus)
    {
        if (!Programming)
        {
            if (RAISE_OUT && (!Speed_Limiter))
            {
                TOPREL_OUT=1;
                RemoteAction=ROOF_RAISE;
                RemoteTimer=REMOTE_WAIT;
                RAISE_OUT=1;
                LOWER_OUT=1;

            }
            else
            {
                RemoteAction=ROOF_IDLE;
                RAISE_OUT=1;
                LOWER_OUT=1;
            }
        }

        Key_Plus=0;
    }

    if (Key_Minus)
    {
        if (!Programming)
        {
            if (LOWER_OUT && (!Speed_Limiter))
            {
                TOPREL_OUT=1;
                RemoteAction=ROOF_LOWER;
                RemoteTimer=REMOTE_WAIT;
                RAISE_OUT=1;
                LOWER_OUT=1;
            }
            else
            {
                RemoteAction=ROOF_IDLE;
                RAISE_OUT=1;
                LOWER_OUT=1;
            }
        }

        Key_Minus=0;
    }
}