#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 "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 VOLT_STATE 1	//ѹդ̶
#define TIME_STATE 2	//դ̶
#define LEVEL_STATE 3	//δֱλ
#define OFFSET_STATE 4	//ˮƽλ
#define TRIG_STATE 5	//ѹ
#define BUFFER_SIZE 1024*4

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


volatile short Y1_Offset, Y2_Offset;

uint8_t *fb = tft_base | 0x80000000;

TOUCH_HANDLE hTouch;


void fb_init(uint8_t *framebuffer, int width, int height, int color_depth) {
	IOWR(MASTER_RD_CTRL_BASE, 0, 0);	//رʾ
	IOWR(MASTER_RD_CTRL_BASE, 1, framebuffer);	//ַָ
	IOWR(MASTER_RD_CTRL_BASE, 2, width*height*color_depth/8);	//ָС
	IOWR(MASTER_RD_CTRL_BASE, 0, 0x2);	//ʾ
}

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

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

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

	CH2_WAVE.Wave_Color = GREEN;
	CH2_WAVE.TIME_factor_NOW = 1;
	CH2_WAVE.VOLT_factor_NOW = 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 is_INC_key(int x_pos) {
	if ((x_pos > 385) && (x_pos < 415))
		return 1;
	else
		return 0;
}

int is_DEC_key(int x_pos) {
	if ((x_pos > 425) && (x_pos < 460))
		return 1;
	else
		return 0;
}

int Get_Key_Info(int y_pos) {
	if (is_Voltage_key(y_pos)) {
		return VOLT_STATE; //Voltage
	}

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

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

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

	else if (is_Trigger_key(y_pos)) {
		return TRIG_STATE; //Trigger
	} else
		return 0;
}


void update_button()
{
	switch(control_state)
	{
	case VOLT_STATE:
		{
			BUTTON_VOLTAGE.back_color = ~0x8C;
			draw_Rectangle_button(&BUTTON_VOLTAGE);
		}break;

	case TIME_STATE:
		{
			BUTTON_TIME.back_color = ~0x8C;
			draw_Rectangle_button(&BUTTON_TIME);
		}break;

	case LEVEL_STATE:
		{
			BUTTON_LEVEL.back_color = ~0x8C;
			draw_Rectangle_button(&BUTTON_LEVEL);
		}break;

	case OFFSET_STATE:
		{
			BUTTON_OFFSET.back_color = ~0x8C;
			draw_Rectangle_button(&BUTTON_OFFSET);
		}break;

	case TRIG_STATE:
		{
			BUTTON_TRIGGER.back_color = ~0x8C;
			draw_Rectangle_button(&BUTTON_TRIGGER);
		}break;
	default:break;
	}

	if(control_state_pre == control_state)
	{

	}else
	{
		switch(control_state_pre)
		{
		case VOLT_STATE:
			{
				BUTTON_VOLTAGE.back_color = 0x8C;
				draw_Rectangle_button(&BUTTON_VOLTAGE);
			}break;

		case TIME_STATE:
			{
				BUTTON_TIME.back_color = 0x8C;
				draw_Rectangle_button(&BUTTON_TIME);
			}break;

		case LEVEL_STATE:
			{
				BUTTON_LEVEL.back_color = 0x8C;
				draw_Rectangle_button(&BUTTON_LEVEL);
			}break;

		case OFFSET_STATE:
			{
				BUTTON_OFFSET.back_color = 0x8C;
				draw_Rectangle_button(&BUTTON_OFFSET);
			}break;

		case TRIG_STATE:
			{
				BUTTON_TRIGGER.back_color = 0x8C;
				draw_Rectangle_button(&BUTTON_TRIGGER);
			}break;

		default:break;
		}
	}
}

void touch_screen_app(void) {

	char tmp[30] = { 0 };
	int state;

	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 (y_pos > 50) {
			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_channel_key(y_pos)) //CH
						{
					if (channel_sel) {
						channel_sel = 0;
						BUTTON_CHANNEL.back_color = YELLOW;
						BUTTON_CHANNEL.text_color = ~YELLOW;
						BUTTON_CHANNEL.str = " CH0";
						draw_Rectangle_button(&BUTTON_CHANNEL);
					} else {
						channel_sel = 1;
						BUTTON_CHANNEL.back_color = GREEN;
						BUTTON_CHANNEL.text_color = ~GREEN;
						BUTTON_CHANNEL.str = " CH1";
						draw_Rectangle_button(&BUTTON_CHANNEL);
					}
				} else {
					state = Get_Key_Info(y_pos);
					if (state)
					{
						control_state_pre = control_state;
						control_state = state;
						update_button();
					}
				}
			}
		} else if ((x_pos > 380) && (x_pos < SCREEN_LENGTH)) {
			if (is_DEC_key(x_pos)) {
				switch (control_state) {
				case VOLT_STATE:
					if (!channel_sel) {
						if (CH1_WAVE.VOLT_factor_NOW == 1)
							CH1_WAVE.VOLT_factor_NOW = 1;
						else
							CH1_WAVE.VOLT_factor_NOW = CH1_WAVE.VOLT_factor_NOW >> 1;
					} else {
						if (CH2_WAVE.VOLT_factor_NOW == 1)
							CH2_WAVE.VOLT_factor_NOW = 1;
						else
							CH2_WAVE.VOLT_factor_NOW = CH2_WAVE.VOLT_factor_NOW >> 1;
					}
					break;

				case TIME_STATE:
					{
						if(sample_rate >= 64)
							sample_rate = 64;
						else if(sample_rate == 0)
							sample_rate = 1;
						else
							sample_rate = sample_rate << 1;

						IOWR(PIO_SFREQ_BASE, 0, sample_rate);	//ò

					}
					break;

				case LEVEL_STATE:
					if (!channel_sel) {
						if (CH1_WAVE.Y_Offset_NOW <= -128)
							CH1_WAVE.Y_Offset_NOW = -128;
						else
							CH1_WAVE.Y_Offset_NOW = CH1_WAVE.Y_Offset_NOW - 1;
					} else {
						if (CH2_WAVE.Y_Offset_NOW <= -128)
							CH2_WAVE.Y_Offset_NOW = -128;
						else
							CH2_WAVE.Y_Offset_NOW = CH2_WAVE.Y_Offset_NOW - 1;
					}
					break;

				default:
					break;

				}

			} else if (is_INC_key(x_pos)) {
				switch (control_state) {
				case VOLT_STATE:
					if (!channel_sel) {
						if (CH1_WAVE.VOLT_factor_NOW == 128)
							CH1_WAVE.VOLT_factor_NOW = 128;
						else
							CH1_WAVE.VOLT_factor_NOW = CH1_WAVE.VOLT_factor_NOW << 1;
					} else {
						if (CH2_WAVE.VOLT_factor_NOW == 128)
							CH2_WAVE.VOLT_factor_NOW = 128;
						else
							CH2_WAVE.VOLT_factor_NOW = CH2_WAVE.VOLT_factor_NOW << 1;
					}
					break;

				case TIME_STATE:
					{
						if(sample_rate <= 1)
							sample_rate = 0;
						else
							sample_rate = sample_rate >> 1;

						IOWR(PIO_SFREQ_BASE, 0, sample_rate);	//ò
					}

					break;

				case LEVEL_STATE:
					if (!channel_sel) {
						if (CH1_WAVE.Y_Offset_NOW == 128)
							CH1_WAVE.Y_Offset_NOW = 128;
						else
							CH1_WAVE.Y_Offset_NOW = CH1_WAVE.Y_Offset_NOW + 1;
					} else {
						if (CH2_WAVE.Y_Offset_NOW == 128)
							CH2_WAVE.Y_Offset_NOW = 128;
						else
							CH2_WAVE.Y_Offset_NOW = CH2_WAVE.Y_Offset_NOW + 1;
					}
					break;

				default:
					break;
				}
			}
		}
	}
}

void key_isr(void)
{
	int key_value;

	key_value = IORD_ALTERA_AVALON_PIO_EDGE_CAP(KEY_BOARD16_BASE);
	if(key_value & 0x10)
	{
		IOWR_ALTERA_AVALON_PIO_EDGE_CAP(KEY_BOARD16_BASE,0);
		key_value = IORD_ALTERA_AVALON_PIO_DATA(KEY_BOARD16_BASE) & 0xf;
		if(key_value == 0xa)
		{
			change_speed(1);
		}
		else
		{
			change_speed(0);
		}
	}

}




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

	int key_value = 0;

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

    alt_iic_isr_register(
    		KEY_BOARD16_IRQ_INTERRUPT_CONTROLLER_ID,
    		KEY_BOARD16_IRQ,
    		key_isr,
    		NULL,
    		NULL);

    IOWR_ALTERA_AVALON_PIO_IRQ_MASK(KEY_BOARD16_BASE, 0x10);


	display_device_init();
	dso_init();
	sample_init();
	wave_set_init();
	while (1) {
		CH1_WAVE.Y_Offset_PRE = CH1_WAVE.Y_Offset_NOW;
		CH2_WAVE.Y_Offset_PRE = CH2_WAVE.Y_Offset_NOW;
		CH1_WAVE.TIME_factor_PRE = CH1_WAVE.TIME_factor_NOW;
		CH2_WAVE.TIME_factor_PRE = CH2_WAVE.TIME_factor_NOW;
		CH1_WAVE.VOLT_factor_PRE = CH1_WAVE.VOLT_factor_NOW;
		CH2_WAVE.VOLT_factor_PRE = CH2_WAVE.VOLT_factor_NOW;
		touch_screen_app();
		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)/2;
				pd++;
				CH1_WAVE.current_data[i] = *pd;
				pd++;
			}
			draw_wave(&CH1_WAVE);
			draw_wave(&CH2_WAVE);
			usleep(1000);
		}
	}
}
