//
//  comunication.h
//  nrf24
//
//  Created by Richard Kuckovsky on 29.4.17.
//  Copyright © 2017 Richard Kuckovsky. All rights reserved.
//

#ifndef comunication_h
#define comunication_h

#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include <avr/eeprom.h>
#include <stdlib.h>

#include "nrf24l01.h"

typedef struct
{
    unsigned rpi   : 4;
    unsigned modul : 8;
    unsigned id    : 12;
    unsigned type   : 8;
    char mes[28];
} payload_data;


typedef struct {
    bool is_init;
    bool is_hash;
    unsigned int hash;
    uint8_t rpi;
    uint8_t address;
    uint8_t rx_pipe[5];
    uint8_t rx2_pipe[5];
    uint8_t tx_pipe[5];
    int srand;
    
} eeprom;

typedef struct {
    uint8_t rpi;
    uint8_t address;
    int msg_ids[20];
    int last_msg_id;
} module_id;


nRF24L01 *comunication_init(void);

bool send_data(nRF24L01 *rf, char *buff, uint8_t rpi, uint8_t address, char data[28], int max_round);
void recieve_data(nRF24L01 *rf, char *buff);
bool module_init(nRF24L01 *rf);

void start_timer(void);
void stop_timer(void);

eeprom get_eeprom(void);
void erase_eeprom(void);
void set_eeprom(eeprom data);

void load_status(void);
void save_status(void);

char status[28];

module_id module;

#endif /* comunication_h */
