#include "stdio.h"
#include "stdlib.h"
#include "stdint.h"
#include "unistd.h"
#include "io.h"
#include "sys/alt_alarm.h"
#include "alt_types.h"
#include "TFT_API.h"
#include "touch_spi.h"
#include "DSO.h"
#include "adc128s022.h"
#include "sample.h"
#include "altera_avalon_pio_regs.h"

#define tft_base (SDRAM_BASE + SDRAM_SPAN / 2) // Where we want our frame buffers
#define  WIDTH 480
#define  HEIGHT 272
#define  NUM_FRAME 1

#define LEVEL_STATE 0	//δֱλ
#define OFFSET_STATE 1	//ˮƽλ
#define TIME_STATE 2	//դ̶
#define VOLT_STATE 3	//ѹդ̶
#define TRIG_STATE 3	//ѹ

#define BUFFER_SIZE 1024*4

unsigned char run_state = 1; //״̬
unsigned char control_state = LEVEL_STATE; //ϵͳ״̬
unsigned char channel_sel = 0; //ͨѡ

volatile short Y1_Offset, Y2_Offset;

uint8_t *fb = tft_base  | 0x80000000;

TOUCH_HANDLE hTouch;

void tft_black_light(int light) {
	if (light < 10) {
		IOWR(PWM_TFT_BL_BASE, 2, 0);
	} else if (light <= 100) {
		IOWR(PWM_TFT_BL_BASE, 0, 500);
		IOWR(PWM_TFT_BL_BASE, 1, light*5);
		IOWR(PWM_TFT_BL_BASE, 2, 1);
	}
}

void fb_init(uint8_t *framebuffer, int width, int height, int color_depth) {
	IOWR_ALTERA_AVALON_PIO_DATA(EN_AMM_RD_BASE, 0);
	IOWR_ALTERA_AVALON_PIO_DATA(AMM_LENGTH_BASE, width*height*color_depth/8);
	IOWR_ALTERA_AVALON_PIO_DATA(AMM_BASE_BASE, framebuffer);
	IOWR_ALTERA_AVALON_PIO_DATA(EN_AMM_RD_BASE, 1);
}

void display_device_init() {
	fb_init(fb, 480, 272, 8);
	tft_black_light(99);
	LCD_Init();
}

void dso_init() {
	LCD_Clear(BLUE);
	draw_wave_area();
}

void wave_set_init()
{
	CH1_WAVE.Wave_Color = YELLOW;
	CH1_WAVE.TIME_factor = 1;
	CH1_WAVE.VOLT_factor = 1;
	CH1_WAVE.Y_Offset_NOW = 30;
	CH1_WAVE.Y_Offset_PRE = 0;

	CH2_WAVE.Wave_Color = GREEN;
	CH2_WAVE.TIME_factor = 1;
	CH2_WAVE.VOLT_factor = 1;
	CH2_WAVE.Y_Offset_NOW = -30;
	CH2_WAVE.Y_Offset_PRE = 0;
}

int is_run_stop_key(int y_pos)
{
	if((y_pos > 220) && (y_pos < 240))
		return 1;
	else
		return 0;
}

int is_level_key(int y_pos)
{
	if((y_pos > 143) && (y_pos < 163))
		return 1;
	else
		return 0;
}

int is_channel_key(int y_pos)
{
	if((y_pos > 62) && (y_pos < 82))
		return 1;
	else
		return 0;
}

int is_Yoffset_key(int y_pos)
{
	if((y_pos > 115) && (y_pos < 135))
		return 1;
	else
		return 0;
}

int is_Time_key(int y_pos)
{
	if((y_pos > 169) && (y_pos < 189))
		return 1;
	else
		return 0;
}

int is_Voltage_key(int y_pos)
{
	if((y_pos > 197) && (y_pos < 217))
		return 1;
	else
		return 0;
}

int is_Trigger_key(int y_pos)
{
	if((y_pos > 88) && (y_pos < 108))
		return 1;
	else
		return 0;
}


int Get_Key_Info(int y_pos)
{
	if(is_run_stop_key(y_pos))	//RUN/STOP
	{
		if(run_state)
		{
			run_state = 0;
			BUTTON_RUN.back_color = RED;
			BUTTON_RUN.text_color = YELLOW;
			BUTTON_RUN.str = " STOP";
			draw_Rectangle_button(&BUTTON_RUN);
		}
		else
		{
			run_state = 1;
			BUTTON_RUN.back_color = GREEN;
			BUTTON_RUN.text_color = RED;
			BUTTON_RUN.str = "  RUN";
			draw_Rectangle_button(&BUTTON_RUN);
		}
	}

	else if(is_Voltage_key(y_pos))
	{
		control_state = VOLT_STATE;	//Voltage
	}

	else if(is_Time_key(y_pos))
	{
		control_state = TIME_STATE;	//TIME
	}

	else if(is_level_key(y_pos))	//LEVEL
	{
		control_state = LEVEL_STATE;
	}

	else if(is_Yoffset_key(y_pos))	//OFFSET
	{
		control_state = OFFSET_STATE;
	}

	else if(is_Trigger_key(y_pos))
	{
		control_state = TRIG_STATE;	//Trigger
	}

	else if(is_channel_key(y_pos))	//CH
	{
		if(channel_sel)
		{
			channel_sel = 0;
			BUTTON_CHANNEL.text_color = YELLOW;
			BUTTON_CHANNEL.str = "CH0";
			draw_Rectangle_button(&BUTTON_CHANNEL);
		}
		else
		{
			channel_sel = 1;
			BUTTON_CHANNEL.text_color = GREEN;
			BUTTON_CHANNEL.str = "CH1";
			draw_Rectangle_button(&BUTTON_CHANNEL);
			run_state = 1;
		}
	}
}


void touch_screen_app(void)
{

	char tmp[30]={0};

	int x_pos,y_pos;

	if(Touch_GetXY(hTouch, &x_pos, &y_pos))
	{
		//Mѹ
		sprintf(tmp,"x:%d y:%d",x_pos,y_pos);
		textbox7.str = tmp;
		draw_Rectangle_textbox(&textbox7);

		if((x_pos > WAVE_AREA_XR) && (x_pos < SCREEN_LENGTH))
		{



		}
	}
}



int main(int argc, char **argv) {

	int i;

	volatile int8_t wave1[WAVE_SIZE];
	volatile int8_t wave2[WAVE_SIZE];

	volatile int8_t wave3[WAVE_SIZE];
	volatile int8_t wave4[WAVE_SIZE];

	int8_t *pd;

	int buffer_flag = 0;


	hTouch = Touch_Init(TOUCH_SPI_0_BASE, TOUCH_PEN_BASE,
			TOUCH_PEN_IRQ);
	if (!hTouch) {
		LCD_ShowString(200, 200, 16, "error");
	}

	display_device_init();
	dso_init();
	sample_init();
	wave_set_init();
	adc_init();
	while (1) {
		touch_screen_app();
		//updata_measurement();
		if (run_state) {
			if (buffer_flag) {

				CH1_WAVE.current_data = wave1;
				CH2_WAVE.current_data = wave2;

				CH1_WAVE.pre_data = wave3;
				CH2_WAVE.pre_data = wave4;

				buffer_flag = 0;
			} else {
				CH1_WAVE.current_data = wave3;
				CH2_WAVE.current_data = wave4;

				CH1_WAVE.pre_data = wave1;
				CH2_WAVE.pre_data = wave2;

				buffer_flag = 1;
			}
			start_sample();	//һβ
			usleep(1);
			while(!get_sample_state());	//ȴ

			pd =	(DATA_MEM_BASE + 32) | 0x80000000;;
			for (i = 0; i < 512;i++) {
				CH2_WAVE.current_data[i] = *pd;
				pd++;
				CH1_WAVE.current_data[i] = *pd;
				pd++;
			}
			CH1_WAVE.Y_Offset_PRE = CH1_WAVE.Y_Offset_NOW;
			CH2_WAVE.Y_Offset_PRE = CH2_WAVE.Y_Offset_NOW;

			draw_wave(&CH1_WAVE);
			draw_wave(&CH2_WAVE);
			usleep(10000);
		}
	}
}
