// Header file for radioreset.c

#define EEPROM_SECTION  __attribute__ ((section (".eeprom")))


// bitove flagy stavu USARTu
#define FRAMING_ERROR (1<<FE)
#define PARITY_ERROR (1<<PE)
#define DATA_OVERRUN (1<<DOR)
#define DATA_REGISTER_EMPTY (1<<UDRE)
#define RX_COMPLETE (1<<RXC) 

// velikosti bufferu
#define RX_BUFFER_SIZE 17
#define DTMF_BUFFER_SIZE 14

// pro lepsi citelnost kodu 
#define MASTER 1
#define SLAVE 0   
 
 // vystupy na rele                 
#define RELE1_ON PORTC |= 0x20     //PC5
#define RELE1_OFF PORTC &= ~0x20
#define RELE2_ON PORTC |= 0x10     //PC4
#define RELE2_OFF PORTC &= ~0x10
#define RELE3_ON PORTB |= 0x02     //PB1
#define RELE3_OFF PORTB &= ~0x02
#define RELE4_ON PORTC |= 0x08     //PC3
#define RELE4_OFF PORTC &= ~0x08
#define RELE5_ON PORTC |= 0x04     //PC2
#define RELE5_OFF PORTC &= ~0x04
#define RELE6_ON PORTC |= 0x02     //PC1
#define RELE6_OFF PORTC &= ~0x02   
#define RELE7_ON PORTB |= 0x04     //PB2
#define RELE7_OFF PORTB &= ~0x04
#define RELE8_ON PORTB |= 0x10     //PD4
#define RELE8_OFF PORTB &= ~0x10
 
// informacni LEDka (invertovana)
#define LED 0x04  
#define LED_OFF PORTD |= LED 
#define LED_ON PORTD &= ~LED
#define LED_DETECT_ON !(PORTD&LED)

// zpusobi bliknuti LED, resp. nastaveni timeru0 a povoleni preruseni
#define LED_BLIK LED_ON; cli(); ledcounter=100; sei()
 
// klicovani stanice (zahajeni vysilani)              
#define PTTOUT 0x10              
#define TX_ON PORTD |= PTTOUT       
#define TX_OFF PORTD &= ~PTTOUT

// povoleni a zakazani preruseni pro generovani casove prodlevy
#define TIMER1_OVF_ENABLE TIMSK|=1<<TOIE1
#define TIMER1_OVF_DISABLE TIMSK&=~(1<<TOIE1) 
 
// povoleni a zakazani preruseni pro skakani po vzorcich sin tabulky pro DTMF  
#define TIMER2_OVF_ENABLE TIMSK|=1<<TOIE2
#define TIMER2_OVF_DISABLE TIMSK&=~(1<<TOIE2)  
// zapnuti/vypnuti PWM 
// not prescalling
// Clear OC2 on Compare Match, set OC2 at BOTTOM,
// Fast PWM 
#define PWM_RUN TCCR2=0b1101001;    
#define PWM_STOP TCCR2=0;      
    
    
// vnejsi preruseni, zap/vyp prijmu DTMF                                   
#define DTMF_RX_ENABLE GICR |= 1<<INTF1                          
#define DTMF_RX_DISABLE GICR &= ~(1<<INTF1) 


// delky prijimanych paketu     0 1 2 3 4 5 6 7 8 9 0 1 2
#define PACKET_A_LENGTH 10   // * C C C M M M A V S
#define PACKET_B_LENGTH 13   // * C C C M M M B V S D D D
#define PACKET_C_LENGTH 8    // * C C C M M M C


    
// k=hodnotka z tabulky 0-39321   a=adresa 0-999
// vystup funkce MUSI byt POUZE unsigned int, tedy 0-65535, jinak overflow  
#define SESSION_KEY_FC ((k+1)/2)+(2*a)-((a+1)%3)+11 
//#define SESSION_KEY_FC k         

// typy
#define DIRECT   0
#define REPEATER 1  

// Microchip like watchdog clearing :-)
#define CLRWDT  asm("wdr")


// prototypy
void delay(unsigned int cas);
unsigned char is_numeric(unsigned char arg) ;
unsigned char is_18(unsigned char arg) ;
unsigned char getznak(unsigned int tmp_klic, unsigned char pozice);
unsigned int decpower(unsigned char exp);
unsigned char rxb2char(unsigned char *value, unsigned char start, unsigned char count);
void SendDTMF(unsigned char znak);     
void dtmf_send_buffer(void);
void dtmf_clear_buffer(void);
void putchar1(char c);
void lfcr(void);
void write(char *sss);
void print_char(unsigned char arg);
unsigned char cmdtest(unsigned char cmd, unsigned char count);
void rs232_showaddr(void);  
void rs232_newaddr(void);
void rs232_readcode(void);   
void rs232_writecode(void);  
void rs232_showdelays(void);
void rs232_setdelays(void);  
void rs232_direct(void);
void rs232_reset(unsigned char lmode);
void rs232_livetest(void);
void rs232_verbose(void);
void rs232_command(void);   
void move_to_relays(char vystupy);     
void set_output(void);
void livetest(void); 
void create_session_key(unsigned int k, char rezim);
unsigned char rxtest(unsigned char pocet);
void master_initpacket(unsigned char lmode);  
unsigned char master_rx_table_index(void);
void master_klic(void);
unsigned char master_potvrzeni(unsigned char lmode);   
unsigned char master_execute(unsigned char lmode);       
unsigned char slave_gen_index(void);
void slave_key_request(unsigned char table_index);
unsigned char slave_receive_key(unsigned char table_index);   
unsigned char slave_auth(void);
void anti_intrusion_timeout(void);
void create_packet_header(void);
void slave_send_status(unsigned char status);
unsigned char rxtest_addr(void);
unsigned char is_packet(unsigned char name, unsigned char pktlen);
unsigned char read_reset_arguments(void);
void packet_a_handler(void); 
void packet_b_handler(void);  
void packet_c_handler(void); 
