#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 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;

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 > 145) && (y_pos < 160))
		return 1;
	else
		return 0;
}

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

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


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))
		{
			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_level_key(y_pos))	//LEVEL
			{
				control_state = LEVEL_STATE;
			}
			else if(is_channel_key(y_pos))	//CH
			{
				if(channel_sel)
				{
					channel_sel = 0;
					BUTTON_CHANNEL.str = "CH0";
					draw_Rectangle_button(&BUTTON_CHANNEL);
				}
				else
				{
					channel_sel = 1;
					BUTTON_CHANNEL.str = "CH1";
					draw_Rectangle_button(&BUTTON_CHANNEL);
					run_state = 1;
				}
			}
		}
		else if(is_Yoffset_key(y_pos))
		{
			if(!channel_sel)
				Y1_Offset = y_pos - WAVE_AREA_YM;
			else
				Y2_Offset = y_pos - WAVE_AREA_YM;

//			if(control_state == LEVEL_STATE)
//			{
//				POINT_COLOR = 0x00;
//				draw_x_line(WAVE_AREA_XL + 1, trig_level, WAVE_AREA_WIDTH - 2,POINT_COLOR);
//
//				POINT_COLOR = 0xffffff;
//				trig_level = y_pos;
//				draw_x_line(WAVE_AREA_XL + 1, trig_level, WAVE_AREA_WIDTH - 2,POINT_COLOR);
//			}
		}
	}
}



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;
			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);
		}
	}
}
