#include "AI8051U.h"
#include "stdio.h"
#include "type.h"
#include "intrins.h"
#include "led.h"
#include "buzzer.h"
#include "pwm_servos.h"
#include "robot_arm.h"
#include "global.h"
#include "serial_servos.h"

#include "AI8051U_Uart.h"
#include "pc_porting.h"
#include "LineFollowerLearn8CH.h"

linefollower_8ch linefollower;
#define sensor_IIC  1                                        //八路巡线传感器IIC通信
#define sensor_UART 2                                        //八路巡线传感器UART通信
#define linefollower_8ch_communication  sensor_IIC          //八路巡线传感器通信方式使能宏定义

char imformation_string[200];
char debug_bit =0x00;
char debug_value =0x00;
int num =0;

/* 定时 1000us */
#define TIME_us   1000

/* 计算定时器初值 */
#define TIMER0_INIT_VALUE (65536 - (TIME_us * (MAIN_Fosc / 1000000)) / 12)

#define ULTRASOUND_OFFSET 9.0f


//void Delay20ms(void)	//@40MHz
//{
//	unsigned long edata i;

//	_nop_();
//	_nop_();
//	i = 199998UL;
//	while (i) i--;
//}

void Delay200ms(void)	//@40MHz
{
	unsigned long edata i;

	_nop_();
	_nop_();
	i = 1999998UL;
	while (i) i--;
}

void Delay500ms(void)	//@40MHz
{
	unsigned long edata i;

	_nop_();
	_nop_();
	i = 4999998UL;
	while (i) i--;
}

void Delay1100ms(void)	//@40MHz
{
	unsigned long edata i;

	_nop_();
	_nop_();
	i = 10999998UL;
	while (i) i--;
}

uint32_t sys_count = 0;
uint32_t get_tick()
{
	return sys_count;
}


void main(void)
{
	EAXFR = 1; 		/* 允许访问扩展寄存器 */
	WTST = 0x00;	/* 设置外部数据总线速度为最快*/
	CKCON = 0x00;	/* 设置CPU扩展RAM数据总线等待时钟数为0 */
	
	P0M0 = 0x00;
	P1M0 = 0x00;
	P2M0 = 0x00;
	P3M0 = 0x00;
	P4M0 = 0x00;
	P5M0 = 0x00;
	
	P0M1 = 0x00;
	P1M1 = 0x00;
	P2M1 = 0x00;
	P3M1 = 0x00;
	P4M1 = 0x00;
	P5M1 = 0x00;

	TMOD |= 0x01;                   // 定时器0为16位定时器
	TH0 = TIMER0_INIT_VALUE / 256;
	TL0 = TIMER0_INIT_VALUE % 256;
	ET0 = 1;                        // 定时器0使能
	TR0 = 1;                        // 启动定时器0
	ES = 1;                         /* 使能串口1中断 */
	ES2 = 1;                        /* 使能串口2中断 */
	ES4 = 1;                        /* 使能串口4中断 */
	EA = 1; 		                    /* 打开总中断 */
	
	leds_init();
	uart1_init(115200);
	uart4_init(115200);
		
#if (linefollower_8ch_communication == sensor_IIC)
	LineFollowerLearn8CH_IIC_init(); 
	
#elif (linefollower_8ch_communication == sensor_UART)
	LineFollowerLearn8CH_UART_init(&linefollower , manual_mode);  //初始化传感器对象用于UART通信相关的成员，指定工作模式

/*
	//用户也可以使用Transmit_SensorState_auto_mode等其他模式，初始化后传感器会持续发来数据，下面以该模式为例演示初始化和数据获取和打印
	LineFollowerLearn8CH_UART_init(&linefollower , Transmit_SensorState_auto_mode);
	
*/
	
#endif

	led_flash(1, 500, 500, 2);
	uart1_Sendstring("Welcome to use Hiwonder's sensor!\r\n");
	
	while(1)
	{	
#if (linefollower_8ch_communication == sensor_IIC)		
		detect_state_all_channel_IIC(&linefollower);
		get_AnalogVoltage_all_channel_IIC(&linefollower);
		get_ThresholdValue_all_channel_IIC(&linefollower);
		
#elif (linefollower_8ch_communication == sensor_UART)
		
		Change_TransmitData_In_Mode_manual(Transmit_SensorState);
		while(! detect_state_all_channel_UART(&linefollower));

		Change_TransmitData_In_Mode_manual(Transmit_AnalogValue);		
		while(! get_AnalogVoltage_all_channel_UART(&linefollower));
		
		Change_TransmitData_In_Mode_manual(Transmit_ThresholdValue);
		while(! get_ThresholdValue_all_channel_UART(&linefollower));		

/*
	//用户也可以使用Transmit_SensorState_auto_mode等其他模式，初始化后传感器会持续发来数据，下面以该模式为例演示初始化和数据获取和打印
	while(! detect_state_all_channel_UART(&linefollower));
	
*/
		
#endif
	
		
		sprintf(imformation_string,\
						"ch1:%hhu,ch2:%hhu,ch3:%hhu,ch4:%hhu,ch5:%hhu,ch6:%hhu,ch7:%hhu,ch8:%hhu\r\n",\
						linefollower.state_all_ch[0],linefollower.state_all_ch[1],linefollower.state_all_ch[2],linefollower.state_all_ch[3],\
						linefollower.state_all_ch[4],linefollower.state_all_ch[5],linefollower.state_all_ch[6],linefollower.state_all_ch[7]);
		uart1_Sendstring(imformation_string);
		Delay500ms();
		
		sprintf(imformation_string,\
						"analog1:%hu,analog2:%hu,analog3:%hu,analog4:%hu,analog5:%hu,analog6:%hu,analog7:%hu,analog8:%hu\r\n",\
						linefollower.AnalogVoltage_all_ch[0],linefollower.AnalogVoltage_all_ch[1],linefollower.AnalogVoltage_all_ch[2],\
						linefollower.AnalogVoltage_all_ch[3],linefollower.AnalogVoltage_all_ch[4],linefollower.AnalogVoltage_all_ch[5],\
						linefollower.AnalogVoltage_all_ch[6],linefollower.AnalogVoltage_all_ch[7]);
		uart1_Sendstring(imformation_string);
		Delay500ms();

		sprintf(imformation_string,\
						"Threshold1:%hu,Threshold2:%hu,Threshold3:%hu,Threshold4:%hu,Threshold5:%hu,Threshold6:%hu,Threshold7:%hu,Threshold8:%hu\r\n",\
						linefollower.ThresholdVoltage_all_ch[0],linefollower.ThresholdVoltage_all_ch[1],linefollower.ThresholdVoltage_all_ch[2],\
						linefollower.ThresholdVoltage_all_ch[3],linefollower.ThresholdVoltage_all_ch[4],linefollower.ThresholdVoltage_all_ch[5],\
						linefollower.ThresholdVoltage_all_ch[6],linefollower.ThresholdVoltage_all_ch[7]);
		uart1_Sendstring(imformation_string);	
		Delay500ms();	
		
/*
	//用户也可以使用Transmit_SensorState_auto_mode等其他模式，初始化后传感器会持续发来数据，下面以该模式为例演示初始化和数据获取和打印
	sprintf(imformation_string,\
					"ch1:%hhu,ch2:%hhu,ch3:%hhu,ch4:%hhu,ch5:%hhu,ch6:%hhu,ch7:%hhu,ch8:%hhu\r\n",\
					linefollower.state_all_ch[0],linefollower.state_all_ch[1],linefollower.state_all_ch[2],linefollower.state_all_ch[3],\
					linefollower.state_all_ch[4],linefollower.state_all_ch[5],linefollower.state_all_ch[6],linefollower.state_all_ch[7]);
	uart1_Sendstring(imformation_string);
	Delay500ms();
	
*/
		
	}
}

void timer0_isr(void) interrupt 1
{
	// 重新装载TH0和TL0的值
	TH0 = TIMER0_INIT_VALUE / 256;
	TL0 = TIMER0_INIT_VALUE % 256;
	
	sys_count++;
	if(sys_count % 20 == 0)
	{
		leds_handler();
//		buzzer_handler();
#if (SERVO_TYPE == 1)
//		pwm_servos_handler();
#endif
	}
}