/*
Diplomova prace : Palubni pocitac

keys.c

Atmega128A 10 MHz XTAL

Autor: Ondrej Lutera

2011/2012

*/


#include "keys.h"

static volatile unsigned char key_up=0;
static volatile unsigned char  key_down=0;
uint8_t keyTone = KEY_TONE_ON;
uint8_t ee_keyTone EEMEM;

ROT_ISR()
{
    if(isSleep()){
          CheckWakeUp();
          return ;
    }

    if(bit_is_clear(KEY_PIN,KEY_MENU))return;

    if(bit_is_set(INT_EDGE_MODE_REG,INT_EDGE_SEL))
    {
        if(bit_is_clear(KEY_PIN,ROT_A)) key_up++;
        else key_down++;
    }
    else
    {
        if(bit_is_clear(KEY_PIN,ROT_A)) key_down++;
        else key_up++;
    }

    ROT_INT_CHNG_EDGE();
}


void keysInit(void)
{

    KEY_DDR&=~(1<<KEY_OK|1<<ROT_A|1<<ROT_B|1<<KEY_MENU|1<<KEY_ESC);
    KEY_PULL|=(1<<KEY_OK|1<<ROT_A|1<<ROT_B|1<<KEY_MENU|1<<KEY_ESC);

    KEY_PULL&=~(1<<KEY_ESC);
    KEY_DDR|=1<<KEY_ESC;    /* povolime enkoder uvedenim ESC do log 0 jako vystup */

    ROT_INT_DDR&=~(1<<ROT_INT_PIN);
    ROT_INT_PULL|=(1<<ROT_INT_PIN);

    INT_EDGE_MODE_REG |=INT_EDGE_MODE_INIT;
    INT_ISR_INIT_REG |=INT_ISR_INIT ;


    if(eeprom_read_byte(&ee_keyTone)&(~1))
    {

        eeprom_update_byte(&ee_keyTone,keyTone);

    }

    keyTone=eeprom_read_byte(&ee_keyTone);

}

void GetKeys(key_t * keys, uint8_t delayed)
{

    static unsigned char delay_ok = 0;
    static unsigned char delay_esc = 0;
    static unsigned char delay_menu = 0;
    uint8_t tone = 0;

    if(key_up)
    {

        INT_ISR_INIT_REG &=~INT_ISR_INIT ;

        keys->key_up += key_up;
        key_up= 0;

        INT_ISR_INIT_REG |=INT_ISR_INIT ;

        tone = 1;

    }

    if(key_down)
    {

        INT_ISR_INIT_REG &=~INT_ISR_INIT ;

        keys->key_down += key_down;
        key_down= 0;

        INT_ISR_INIT_REG |=INT_ISR_INIT ;

        tone=1;

    }

    keys->keys = 0;

    if(bit_is_clear(KEY_PIN,KEY_OK) && (delay_ok == 0 || !delayed))
    {
        keys->keys|=OK;
        delay_ok = KEY_DELAY;
    }

    if(delay_esc == 0)
    {
        INT_ISR_INIT_REG &=~INT_ISR_INIT ;

        KEY_DDR &=~(1<<KEY_ESC);
        KEY_PULL|=(1<<KEY_ESC);

        _delay_ms(1);

        if(bit_is_clear(KEY_PIN,KEY_ESC) )
        {
            keys->keys|=ESC;
            delay_esc = KEY_DELAY;


        }

        KEY_PULL&=~(1<<KEY_ESC);
        KEY_DDR|=1<<KEY_ESC;    /* povolime enkoder uvedenim ESC do log 0 jako vystup */

        _delay_ms(1);

        EIFR |=1<<INT5;

        INT_ISR_INIT_REG |=INT_ISR_INIT ;

    }

    if(bit_is_clear(KEY_PIN,KEY_MENU) && (delay_menu == 0 || !delayed))
    {
        keys->keys|=MENU;
        delay_menu = KEY_DELAY;
    }

    if(delay_ok)delay_ok--;
    if(delay_esc)delay_esc--;
    if(delay_menu)delay_menu--;

    if((tone||keys->keys!=0)&&keyTone==KEY_TONE_ON && delayed)
    {

        TONE_ON;
        _delay_ms(TONE_DELAY);
        TONE_OFF;


    }

}

void setKeyTone(uint8_t val)
{

    if(val&~KEY_TONE_ON)return;

    keyTone=val;

    eeprom_update_byte(&ee_keyTone,keyTone);

}

inline uint8_t getKeyTone(void)
{

    return keyTone;
}
