#ifndef XP_INC_SENS_H
#define XP_INC_SENS_H

#include "x_basic_types.h"
#include "xp_cds_in.h"
#include "xp_id_plate_info.h"



//Дискретизацию, при которой расчитываетсю длительность импульсов
#define SAMPLING_TIME_NS  1  // 16,666667ns
#define SAMPLING_TIME_MS  0  // 1,666667us

//Автоматически переключает счётчик, когда количество отсчётов
// на 1 импульс становитсЯ слишком большим или маленьким.
#define AUTO_CHANGE_SAMPLING_TIME	1
/*
	Что бы прочитать данные из датчика, нужно вызвать
	rotation_sensor.read_sensors(&rotation_sensor);
	Результат платы IN будет в 
	rotation_sensor.in_plane.out....
*/

/////////////////////////////////////////////////////////////
// IN plane
/////////////////////////////////////////////////////////////
// Registers with data for incremental sensor
/////////////////////////////////////////////////////////////
typedef union {
	unsigned int all;
	struct {
		unsigned int filter_sensitivity:12;
		unsigned int set_sampling_time:1;	//(1)-16,666667ns (0)-1,666667us
		unsigned int sampling_time2:1;	//(1)-16,666667ns (0)-1,666667us
		unsigned int sampling_time1:1;
		unsigned int update_registers:1; //0 - updated
	}bit;
}T_inc_sensor_comand;

#define T_INC_COMAND_DEFAULT 0
////////////////////////////////////////////////////////////
typedef struct {
	unsigned int time_line1;
    unsigned int n_impulses_line1;
    unsigned int time_line2;
    unsigned int n_impulses_line2;

    unsigned int zero_time_line1;
    unsigned int one_time_line1;
    unsigned int zero_time_line2;
    unsigned int one_time_line2;

	T_inc_sensor_comand 	write_comand_reg;
	T_inc_sensor_comand 	read_comand_reg;

} T_inc_sensor_regs;

#define T_INC_SENSOR_REGS_DEFAULTS {0,0,0,0, 0,0,0,0, T_INC_COMAND_DEFAULT, T_INC_COMAND_DEFAULT}


////////////////////////////////////////////////
////// incremental sensors with IN plane
///////////////////////////////////////////////
typedef struct {
	//UInt16 plane_address;
	unsigned int count_wait_for_update_registers;
	unsigned int error_update;
	unsigned int use_sensor1;
	unsigned int use_sensor2;
    unsigned int break_sensor1;
    unsigned int break_sensor2;
    unsigned int break_direction;


	struct {

		unsigned int Time1;			// Sensor's survey time in mksec
		unsigned int Impulses1;		// Quantity of full impulses during survey time
		unsigned int CountZero1; 	// Value of the zero-half-period counter 
		unsigned int CountOne1;		// Value of the one-half-period counter
        unsigned int prev_CountZero1;    // Value of the prev zero-half-period counter
        unsigned int prev_CountOne1;     // Value of the prev one-half-period counter
        unsigned int countCountZero1;    // Value of the zero-half-period counter
        unsigned int countCountOne1;     // Value of the one-half-period counter
		unsigned int counter_freq1;	// 1 - 60MHz; 0 - 600KHz
		unsigned long TimeCalcFromImpulses1; // Пересчет времени импульса из количества Impulses1 и времени Time1
		int direction1;	// 1 - direct; 0 - reverse

		unsigned int Time2;			// Sensor's survey time in mksec
		unsigned int Impulses2;		// Quantity of full impulses during survey time
		unsigned int CountZero2;		// Value of the zero-half-period counter 
		unsigned int CountOne2;		// Value of the one-half-period counter
        unsigned int prev_CountZero2;    // Value of the prev zero-half-period counter
        unsigned int prev_CountOne2;     // Value of the prev one-half-period counter
        unsigned int countCountZero2;    // Value of the zero-half-period counter
        unsigned int countCountOne2;     // Value of the one-half-period counter
		unsigned int counter_freq2;	// 1 - 60MHz; 0 - 600KHz
		unsigned long TimeCalcFromImpulses2; // Пересчет времени импульса из количества Impulses1 и времени Time1
		int direction2;	// 1 - direct; 0 - reverse
	} data;

	T_inc_sensor_regs pm67regs;

    void (*set)();	    	// Pointer to calculation function

	void (*update_sensors)();
	void (*read_sensors)();
	void (*read_sensor1)();
	void (*read_sensor2)();

} T_inc_sensor;

#define T_INC_SENSOR_DEFAULT {0, 0, 0, 0, 0, 0, 0, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0,0,0, 0,0,0,0}, \
    T_INC_SENSOR_REGS_DEFAULTS, \
    inc_sensor_set,\
    update_sensors_data_s, \
    inc_sensor_read, \
    inc_sensor_read1, \
    inc_sensor_read2 \
    }

//////////////////////////////////////////////////////////////////////////////////
////
//////////////////////////////////////////////////////////////////////////////////

//Public functions

void inc_sensor_set(T_inc_sensor *inc_s);
void inc_sensor_read1(T_inc_sensor *inc_s);
void inc_sensor_read2(T_inc_sensor *inc_s);
void inc_sensor_read(T_inc_sensor *inc_s);
void update_sensors_data_s(T_inc_sensor *inc_s);


extern T_inc_sensor inc_sensor;

#endif //XP_INC_SENS_H
