#include "DOG_rwr.h"
#include "math.h"

// ״ƽɨ
void radar_scanning(struct DOG_RWR* this){
	if(this -> radar_scanning_enable_flag == True){
		// ʹʱı־λΪ Falseǰʱʱʹʱ,ñ־λΪTrueʾ޸Ĺ
		if(this -> enable_time_change_flag == False){
			this -> enable_time = *this -> time;
			this -> enable_time_change_flag = True;
		}
		uint32 time = fmod(*this -> time-this -> enable_time, 1000);
		// 0-100ms,440Hz
		if(time >= 0 && time < 100){
			pwm_init(this -> pin, 440, PWM_DUTY_MAX / 2);
		}
		// 100-1000ms,200000Hz
		if(time >= 100 && time < 1000){
			pwm_init(this -> pin, 200000, 0);
		}
	}
	if(this -> radar_scanning_enable_flag == False && this -> radar_tracking_enable_flag == False && this -> missile_launch_enable_flag == False){
		this -> enable_time_change_flag = False;
		pwm_init(this -> pin, 200000, 0);
	}
}

// и
void radar_tracking(struct DOG_RWR* this){
	if(this -> radar_tracking_enable_flag == True){
		// ʹʱı־λΪ Falseǰʱʱʹʱ,ñ־λΪTrueʾ޸Ĺ
		if(this -> enable_time_change_flag == False){
			this -> enable_time = *this -> time;
			this -> enable_time_change_flag = True;
		}
		uint32 time = fmod(*this -> time-this -> enable_time, 500);
		// 0-250ms,523Hz
		if(time >= 0 && time < 250){
			pwm_init(this -> pin, 523, PWM_DUTY_MAX / 2);
		}
		// 250-500ms,349Hz
		if(time >= 250 && time < 500){
			pwm_init(this -> pin, 349, PWM_DUTY_MAX / 2);
		}
	}
	if(this -> radar_scanning_enable_flag == False && this -> radar_tracking_enable_flag == False && this -> missile_launch_enable_flag == False){
		this -> enable_time_change_flag = False;
		pwm_init(this -> pin, 200000, 0);
	}
}

// е
void missile_launch(struct DOG_RWR* this){
	if(this -> missile_launch_enable_flag == True){
		// ʹʱı־λΪ Falseǰʱʱʹʱ,ñ־λΪTrueʾ޸Ĺ
		if(this -> enable_time_change_flag == False){
			this -> enable_time = *this -> time;
			this -> enable_time_change_flag = True;
		}
		uint32 time = fmod(*this -> time-this -> enable_time, 220);
		// 0-100ms,523Hz
		if(time >= 0 && time < 100){
			pwm_init(this -> pin, 523, PWM_DUTY_MAX / 2);
		}
		// 100-220ms,392Hz
		if(time >= 100 && time < 220){
			pwm_init(this -> pin, 392, PWM_DUTY_MAX / 2);
		}
	}
	if(this -> radar_scanning_enable_flag == False && this -> radar_tracking_enable_flag == False && this -> missile_launch_enable_flag == False){
		this -> enable_time_change_flag = False;
		pwm_init(this -> pin, 200000, 0);
	}
}		

// 캯
void rwr(struct DOG_RWR* this, pwm_channel_enum pin, uint32* time){
	/* ־λ */
	this -> radar_scanning_enable_flag = False;
	this -> radar_tracking_enable_flag = False;
	this -> missile_launch_enable_flag = False;
	this -> enable_time_change_flag = False;
	
	/* Ա */
	this -> enable_time = 0;
	this -> pin = pin;
	
	/* ļʱ */
	this -> time = time;
	
	/* Ա */
	this -> radar_scanning = radar_scanning;
	this -> radar_tracking = radar_tracking;
	this -> missile_launch = missile_launch;
}

// 
void _rwr(struct DOG_RWR* this){
	/* ־λ */
	this -> radar_scanning_enable_flag = False;
	this -> radar_tracking_enable_flag = False;
	this -> missile_launch_enable_flag = False;
}