/*
Diplomova prace : Palubni pocitac

keys.h

Atmega128A 10 MHz XTAL

Autor: Ondrej Lutera

2011/2012

*/

#ifndef KEYS_H_INCLUDED
#define KEYS_H_INCLUDED

#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <avr/eeprom.h>
#include "pwrctrl.h"
#include "define.h"

#define KEY_OK      PC1
#define ROT_A       PC0
#define ROT_B       PC2
#define KEY_ESC     PC4
#define KEY_MENU    PC3

#define KEY_DDR     DDRC
#define KEY_PULL    PORTC
#define KEY_PIN     PINC

#define TONE_DDR       DDRC
#define TONE_PORT      PORTC
#define TONE           PC5
#define TONE_ON        PORTC|=1<<TONE;
#define TONE_OFF       PORTC&=~(1<<TONE);
#define TONE_DELAY     3
#define KEY_TONE_ON     1
#define KEY_TONE_OFF    0


#define ROT_ISR()     ISR(INT5_vect)


#define INT_EDGE_MODE_REG   EICRB
#define INT_EDGE_MODE_INIT  (1<<ISC51)
#define INT_CHNG_MODE_INIT  (1<<ISC50)
#define INT_ISR_INIT_REG    EIMSK
#define INT_ISR_INIT        (1 <<INT5)
#define INT_EDGE_SEL        ISC50

#define ROT_INT_CHNG_EDGE() INT_EDGE_MODE_REG  ^= (1<<INT_EDGE_SEL)


#define ROT_INT_PIN PE5
#define ROT_INT_DDR DDRE
#define ROT_INT_PULL PORTE

#define KEY_DELAY 5

typedef struct {

    unsigned char keys;         // jednotlive bitove masky ESC OK A MENU
    unsigned char key_up;           // pocet pohybu enkoderu UP
    unsigned char key_down;         // pocet pohybu enkoderu DOWN

}key_t ;

#define UP      1
#define DOWN    (1<<1)
#define LEFT    (1<<2)
#define RIGHT   (1<<3)
#define OK      (1<<4)
#define ESC     (1<<5)
#define MENU    (1<<6)


void keysInit(void);        // inicializace
void GetKeys(key_t * keys, uint8_t delayed);        // vraci stav tlacitek a zaznamenany stav enkoderu
void setKeyTone(uint8_t val);                   // povoluje zvuk tlacitek
uint8_t getKeyTone(void);                       // vraci nastaveni zvuku tlacitek

#endif // KEYS_H_INCLUDED
