/*              mija 2009       key     

                source file for process keyboard

        !!!     must be set PINs,PORTs,DDR in lcd.h 
                                                
                                                
                ver.: 0.0       TESTED                          
                        
*/

#include <avr/io.h>
#include "key.h"

void KeyInit()
{
        KEY1Init();
        KEY1Pullup();
        KEY2Init();
        KEY2Pullup();
}

void ProcesKey(uint8_t *key)
{
        //stav
        enum{STABLE_NO_KEY,NO_STABLE,REPEAT};
        static uint8_t stav = STABLE_NO_KEY;
        static uint8_t KeyPrev = NOKEY;
        static uint16_t KeyTimer;
        uint8_t Key;
        
        Key = NOKEY;
        if (KEY1Input()) Key = TL1;
        if (KEY2Input()) Key = TL2;
        if (KEY1Input() && KEY2Input()) Key = TL1TL2;

        switch (stav)
        {
        case STABLE_NO_KEY:     if (Key){
                                                        KeyPrev = Key;
                                                        KeyTimer = KEYSTABLETIME;
                                                        stav = NO_STABLE;
                                                }
                                                *key = NOKEY;
                                                break;
        case NO_STABLE:         if (--KeyTimer){
                                                        *key = NOKEY;
                                                        break;
                                                }
                                                if (Key == KeyPrev){
                                                        *key = Key;
                                                        KeyTimer = KEYFIRSTREPEATTIME;
                                                        stav = REPEAT;
                                                        break;
                                                }
                                                stav = STABLE_NO_KEY;
                                                *key = NOKEY;
                                                break;
/*      case FIRST_REPEAT:      if (--KeyTimer){
                                                        if (Key == NOKEY) stav = STABLE_NO_KEY;
                                                        *key = NOKEY;
                                                        break;
                                                }
                                                if (Key == KeyPrev){
                                                        *key = Key;
                                                        KeyTimer = KEYREPEATTIME;
                                                        stav = REPEAT;
                                                        break;
                                                }
                                                stav = STABLE_NO_KEY;
                                                *key = NOKEY;
                                                break;*/
        case REPEAT:            if (--KeyTimer){
                                                        if (Key != KeyPrev) stav = STABLE_NO_KEY;
                                                        *key = NOKEY;
                                                        break;
                                                }
                                                if (Key == KeyPrev){
                                                        *key = Key;
                                                        KeyTimer = KEYREPEATTIME;
                                                        //stav = REPEAT;
                                                        break;
                                                }
        default:                        stav = STABLE_NO_KEY;
                                                *key = NOKEY;
        }
}