#include "stdarg.h"
#include "stdlib.h"
#include <stdint.h>
#include "unistd.h"
#include "TFT_API.h"
#include "DSO.h"

#define SCREEN_LENGTH 800	//Ļؿ
#define SCREEN_HIGHT 480	//Ļظ߶

#define WAVE_AREA_XL 0	//ʾΧ߽
#define WAVE_AREA_WIDTH (SCREEN_LENGTH - 50)	//ʾ
#define WAVE_AREA_XR (WAVE_AREA_XL + WAVE_AREA_WIDTH)	//ʾΧұ߽
#define WAVE_AREA_XM ((WAVE_AREA_XL + WAVE_AREA_XR)/2)	//ʾX

#define WAVE_AREA_YD 40		//ʾΧ±߽
#define WAVE_AREA_HIGHT (SCREEN_HIGHT - WAVE_AREA_YD - 16)	//ʾ߶
#define WAVE_AREA_YU (WAVE_AREA_YD + WAVE_AREA_HIGHT) 	//ʾΧϱ߽


#define WAVE_AREA_YM ((WAVE_AREA_YU+1 + WAVE_AREA_YD)/2)	//ʾY

#define Y_GIRD_NUM	8	//ֱդ
#define Y_GIRD_SIZE  ((WAVE_AREA_YU - WAVE_AREA_YD)/Y_GIRD_NUM)	//ÿդ߶ռ

#define X_GIRD_NUM	10	//ˮƽդ
#define X_GIRD_SIZE  ((WAVE_AREA_XR - WAVE_AREA_XL)/X_GIRD_NUM)	//ÿդռ

#define WAVE_SIZE 1024	//δ洢ռС

#define WAVE_BACK_COLOR BLACK	//αɫ
#define GIRD_COLOR	GRAY		//դɫ
#define CURSOR_A_COLOR	WHITE	//Aɫ
#define CURSOR_B_COLOR	GREEN	//Bɫ


uint8_t sample_rate=100000000;	//β
int wave_point_num = 700;	//ÿʾʱҪʾĵʺʱ

buttontypedef button0;	//
buttontypedef button1;	//
buttontypedef button2;	//
buttontypedef button3;	//
buttontypedef button4;	//
buttontypedef button5;	//

buttontypedef button6;	//
buttontypedef button7;	//

buttontypedef BUTTON_RUN;	//	ɫrunɫstop
buttontypedef BUTTON_VOLTAGE;	//ѹ֮󣬿޸դĵѹС
buttontypedef BUTTON_TIME;	//ʱ״̬ͨ޸Ĳʣ޸ÿդʱ
buttontypedef BUTTON_LEVEL;	//ֱƫƵεĴֱλ
buttontypedef BUTTON_OFFSET;	//ˮƽλѡ
buttontypedef BUTTON_TRIGGER;	//ѹ
buttontypedef BUTTON_CHANNEL;	//ͨѡ

buttontypedef BUTTON_INC;	//	Ӽ
buttontypedef BUTTON_DEC;	//  С

textboxtypedef textbox0;	//
textboxtypedef textbox1;	//
textboxtypedef textbox2;	//
textboxtypedef textbox3;	//
textboxtypedef textbox4;	//
textboxtypedef textbox5;	//
textboxtypedef textbox6;	//
textboxtypedef textbox7;	//
textboxtypedef textbox8;	//
textboxtypedef textbox9;	//

volatile wavetypedef CH1_WAVE;
volatile wavetypedef CH2_WAVE;

void draw_wave(wavetypedef *CH_WAVE)
{
	volatile int x_i=1,d_i=0;
	volatile int x1,y1,x2,y2;

	volatile uint8_t *pp;
	volatile uint8_t *pn;

	pp = CH_WAVE->pre_data;
	pn = CH_WAVE->current_data;

	do{
		//ϴβεһ
		x1 = x_i + WAVE_AREA_XL;
//		x2 = x_i+CH_WAVE->TIME_factor_PRE + WAVE_AREA_XL;
		x2 = x_i+ 1 + WAVE_AREA_XL;
		y1 = (pp[d_i])*CH_WAVE->VOLT_factor_PRE + CH_WAVE->Y_Offset_PRE + WAVE_AREA_YM;
		y2 = (pp[d_i+1])*CH_WAVE->VOLT_factor_PRE + CH_WAVE->Y_Offset_PRE + WAVE_AREA_YM;
		POINT_COLOR = WAVE_BACK_COLOR;
		LCD_DrawWaveLine(x1, y1,x2, y2);

		//²εһ
		y1 = (pn[d_i])*CH_WAVE->VOLT_factor_NOW + CH_WAVE->Y_Offset_NOW + WAVE_AREA_YM;
		y2 = (pn[d_i+1])*CH_WAVE->VOLT_factor_NOW + CH_WAVE->Y_Offset_NOW + WAVE_AREA_YM;
		POINT_COLOR = CH_WAVE->Wave_Color;
		LCD_DrawWaveLine(x1, y1,x2, y2);
//		x_i = x_i + CH_WAVE->TIME_factor_NOW;
		x_i ++;
		d_i = d_i + 1;
	}while(((x_i+CH_WAVE->TIME_factor_NOW) <= WAVE_AREA_WIDTH) && (d_i < WAVE_SIZE - 1));
}



void draw_wave_area()
{
	LCD_Fill(WAVE_AREA_XL, WAVE_AREA_YD, WAVE_AREA_XR, WAVE_AREA_YU,WAVE_BACK_COLOR);//䲨ʾɫ
	draw_gui();
}

//ֱǰť
void draw_Rectangle_button(buttontypedef *button)
{
	POINT_COLOR = ~button->back_color;
	LCD_DrawRectangle(button->pos_x, button->pos_y, button->pos_x + button->width,button->pos_y + button->hight);
	LCD_Fill(button->pos_x + 1, button->pos_y + 1, button->pos_x + button->width - 1,button->pos_y + button->hight - 1,button->back_color);
	POINT_COLOR = button->text_color;
	BACK_COLOR = button->back_color;
	LCD_ShowString(button->pos_x + 5, button->pos_y + (button->hight - button->char_size)/2,12,  button->str);
}

//Բı
void draw_circle_textbox(textboxtypedef *textbox)
{
	int i;
	POINT_COLOR = textbox->back_color;
	for(i=textbox->hight/2 - 1;i>0;i--)
	{
		Draw_Circle(textbox->pos_x + textbox->hight/2, textbox->pos_y + textbox->hight/2,i);
	}
	for(i=textbox->hight/2 - 1;i>0;i--)
	{
		Draw_Circle(textbox->pos_x + textbox->width - textbox->hight/2, textbox->pos_y + textbox->hight/2,i);
	}
	LCD_Fill(textbox->pos_x + textbox->hight/2, textbox->pos_y, textbox->pos_x + textbox->width - textbox->hight/2, textbox->pos_y + textbox->hight,textbox->back_color);
	POINT_COLOR = textbox->text_color;
	BACK_COLOR = textbox->back_color;
	LCD_ShowString(textbox->pos_x + textbox->hight/2, textbox->pos_y + (textbox->hight - textbox->char_size)/2, 12, textbox->str);
}

void updata_textbox(textboxtypedef *textbox)
{
	POINT_COLOR = textbox->text_color;
	BACK_COLOR = textbox->back_color;
	LCD_ShowString(textbox->pos_x + 4, textbox->pos_y + (textbox->hight - textbox->char_size)/2,12,  textbox->str);
}

//Բı
void draw_Rectangle_textbox(textboxtypedef *textbox)
{
	int i;
	POINT_COLOR = textbox->back_color;
	LCD_Fill(textbox->pos_x, textbox->pos_y, textbox->pos_x + textbox->width, textbox->pos_y + textbox->hight,textbox->back_color);
	POINT_COLOR = WHITE;
	LCD_DrawRectangle(textbox->pos_x, textbox->pos_y, textbox->pos_x + textbox->width, textbox->pos_y + textbox->hight);
	POINT_COLOR = textbox->text_color;
	BACK_COLOR = textbox->back_color;
	LCD_ShowString(textbox->pos_x + 4, textbox->pos_y + (textbox->hight - textbox->char_size)/2,12,  textbox->str);
}

#define PRESSED 0	//
#define RELEASED 1	//ͷ

void creat_buttons(void)
{
	//RUN 
	BUTTON_RUN.pos_x = WAVE_AREA_XR + 2;
	BUTTON_RUN.pos_y = 230;
	BUTTON_RUN.hight = 25;
	BUTTON_RUN.width = SCREEN_LENGTH - WAVE_AREA_XR - 8;
	BUTTON_RUN.back_color = GREEN;
	BUTTON_RUN.text_color = RED;
	BUTTON_RUN.button_state = RELEASED;
	BUTTON_RUN.char_size = 16;
	BUTTON_RUN.str = "RUN";
	draw_Rectangle_button(&BUTTON_RUN);


	//Voltate 
	BUTTON_VOLTAGE.pos_x = WAVE_AREA_XR + 2;
	BUTTON_VOLTAGE.pos_y = 200;
	BUTTON_VOLTAGE.hight = 25;
	BUTTON_VOLTAGE.width = SCREEN_LENGTH - WAVE_AREA_XR - 8;
	BUTTON_VOLTAGE.back_color = 0x8C;
	BUTTON_VOLTAGE.text_color = YELLOW;
	BUTTON_VOLTAGE.button_state = RELEASED;
	BUTTON_VOLTAGE.char_size = 16;
	BUTTON_VOLTAGE.str = "  V";
	draw_Rectangle_button(&BUTTON_VOLTAGE);

	//Time 
	BUTTON_TIME.pos_x = WAVE_AREA_XR + 2;
	BUTTON_TIME.pos_y = 170;
	BUTTON_TIME.hight = 25;
	BUTTON_TIME.width = SCREEN_LENGTH - WAVE_AREA_XR - 8;
	BUTTON_TIME.back_color = 0x8C;
	BUTTON_TIME.text_color = YELLOW;
	BUTTON_TIME.button_state = RELEASED;
	BUTTON_TIME.char_size = 16;
	BUTTON_TIME.str = "Time";
	draw_Rectangle_button(&BUTTON_TIME);
//
	//LEVEL 
	BUTTON_LEVEL.pos_x = WAVE_AREA_XR + 2;
	BUTTON_LEVEL.pos_y = 140;
	BUTTON_LEVEL.hight = 25;
	BUTTON_LEVEL.width = SCREEN_LENGTH - WAVE_AREA_XR - 8;
	BUTTON_LEVEL.back_color = 0x8C;
	BUTTON_LEVEL.text_color = YELLOW;
	BUTTON_LEVEL.button_state = RELEASED;
	BUTTON_LEVEL.char_size = 16;
	BUTTON_LEVEL.str = "LEVEL";
	draw_Rectangle_button(&BUTTON_LEVEL);
//
	//OFFSET 
	BUTTON_OFFSET.pos_x = WAVE_AREA_XR + 2;
	BUTTON_OFFSET.pos_y = 110;
	BUTTON_OFFSET.hight = 25;
	BUTTON_OFFSET.width = SCREEN_LENGTH - WAVE_AREA_XR - 8;
	BUTTON_OFFSET.back_color = 0x8C;
	BUTTON_OFFSET.text_color = YELLOW;
	BUTTON_OFFSET.button_state = RELEASED;
	BUTTON_OFFSET.char_size = 16;
	BUTTON_OFFSET.str = "OFFSET";
	draw_Rectangle_button(&BUTTON_OFFSET);
//
	//TRIGGER 
	BUTTON_TRIGGER.pos_x = WAVE_AREA_XR + 2;
	BUTTON_TRIGGER.pos_y = 80;
	BUTTON_TRIGGER.hight = 25;
	BUTTON_TRIGGER.width = SCREEN_LENGTH - WAVE_AREA_XR - 8;
	BUTTON_TRIGGER.back_color = 0x8C;
	BUTTON_TRIGGER.text_color = YELLOW;
	BUTTON_TRIGGER.button_state = RELEASED;
	BUTTON_TRIGGER.char_size = 16;
	BUTTON_TRIGGER.str = "TRIGER";
	draw_Rectangle_button(&BUTTON_TRIGGER);
//
	//Channel 
	BUTTON_CHANNEL.pos_x = WAVE_AREA_XR + 2;
	BUTTON_CHANNEL.pos_y = 50;
	BUTTON_CHANNEL.hight = 25;
	BUTTON_CHANNEL.width = SCREEN_LENGTH - WAVE_AREA_XR - 8;
	BUTTON_CHANNEL.back_color = 0x8C;
	BUTTON_CHANNEL.text_color = YELLOW;
	BUTTON_CHANNEL.button_state = RELEASED;
	BUTTON_CHANNEL.char_size = 16;
	BUTTON_CHANNEL.str = "CH0";
	draw_Rectangle_button(&BUTTON_CHANNEL);

	//INC 
	BUTTON_INC.pos_x = WAVE_AREA_XR + 2 - (SCREEN_LENGTH - WAVE_AREA_XR - 5) - 2;
	BUTTON_INC.pos_y = 0;
	BUTTON_INC.hight = 25;
	BUTTON_INC.width = SCREEN_LENGTH - WAVE_AREA_XR - 8;
	BUTTON_INC.back_color = 0x8C;
	BUTTON_INC.text_color = YELLOW;
	BUTTON_INC.button_state = RELEASED;
	BUTTON_INC.char_size = 16;
	BUTTON_INC.str = " INC";
	draw_Rectangle_button(&BUTTON_INC);

	//DEC 
	BUTTON_DEC.pos_x = WAVE_AREA_XR + 2;
	BUTTON_DEC.pos_y = 0;
	BUTTON_DEC.hight = 25;
	BUTTON_DEC.width = SCREEN_LENGTH - WAVE_AREA_XR - 8;
	BUTTON_DEC.back_color = 0x8C;
	BUTTON_DEC.text_color = YELLOW;
	BUTTON_DEC.button_state = RELEASED;
	BUTTON_DEC.char_size = 16;
	BUTTON_DEC.str = " DEC";
	draw_Rectangle_button(&BUTTON_DEC);
}

void draw_gui()
{
	BACK_COLOR = BLUE;
	POINT_COLOR = GREEN;

	LCD_Fill(0, 430, 799, 479,BLUE);
	LCD_ShowString(40,430,24,"xiaomeige");
	LCD_ShowString(140,430,24,"www.corecourse.cn");

//
	LCD_ShowString(340,430,24,"CH1:");
	LCD_ShowString(378,430,24,"89.6mV");


//	LCD_Fill(0, 256, 479, 271,BLUE);
//	LCD_ShowString(40,256,16,"xiaomeige");
//	LCD_ShowString(140,256,16,"www.corecourse.cn");
//
////
//	LCD_Fill(300, 256, 380, 271,BLUE);
//	LCD_ShowString(340,256,16,"CH1:");
//	LCD_ShowString(378,256,16,"89.6mV");
//
//	LCD_Fill(400, 256, 380, 271,BLACK);
//	LCD_ShowString(420,256,16,"16:43");

	BACK_COLOR = RED;
	creat_buttons();

	//ͨ1ѹդ
	textbox0.pos_x = 0;
	textbox0.pos_y = 0;
	textbox0.hight = 16;
	textbox0.width = 80;
	textbox0.back_color = BLACK;
	textbox0.text_color = YELLOW;
	textbox0.char_size = 12;
	textbox0.str = "CH1:750mV";
	draw_Rectangle_textbox(&textbox0);

	//ͨ2ѹդ
	textbox1.pos_x = 0;
	textbox1.pos_y = 16;
	textbox1.hight = 16;
	textbox1.width = 80;
	textbox1.back_color = BLACK;
	textbox1.text_color = GRAY;
	textbox1.char_size = 12;
	textbox1.str = "CH2:100mV";
	draw_Rectangle_textbox(&textbox1);
//
	//V(1)
	textbox2.pos_x = 80;
	textbox2.pos_y = 0;
	textbox2.hight = 16;
	textbox2.width = 120;
	textbox2.back_color = BLACK;
	textbox2.text_color = YELLOW;
	textbox2.char_size = 12;
	textbox2.str = "V(1)=40.0mV";
	draw_Rectangle_textbox(&textbox2);
//
	//V(2)
	textbox3.pos_x = 80;
	textbox3.pos_y = 16;
	textbox3.hight = 16;
	textbox3.width = 120;
	textbox3.back_color = BLACK;
	textbox3.text_color = GRAY;
	textbox3.char_size = 12;
	textbox3.str = "V(2)=60.0mV";
	draw_Rectangle_textbox(&textbox3);
//
	//Freq(1)
	textbox4.pos_x = 200;
	textbox4.pos_y = 16;
	textbox4.hight = 16;
	textbox4.width = 100;
	textbox4.back_color = BLACK;
	textbox4.text_color = GRAY;
	textbox4.char_size = 12;
	textbox4.str = "Freq(2)=32.768K";
	draw_Rectangle_textbox(&textbox4);

	//Freq(2)
	textbox5.pos_x = 200;
	textbox5.pos_y = 0;
	textbox5.hight = 16;
	textbox5.width = 100;
	textbox5.back_color = BLACK;
	textbox5.text_color = WHITE;
	textbox5.char_size = 12;
	textbox5.str = "Freq(1)=50.000M";
	draw_Rectangle_textbox(&textbox5);
//
	//ʱTime
	textbox6.pos_x = 300;
	textbox6.pos_y = 16;
	textbox6.hight = 16;
	textbox6.width = 80;
	textbox6.back_color = BLACK;
	textbox6.text_color = GREEN;
	textbox6.char_size = 12;
	textbox6.str = "Time:100KHz";
	draw_Rectangle_textbox(&textbox6);

	//ʱƫT
	textbox7.pos_x = 300;
	textbox7.pos_y = 0;
	textbox7.hight = 16;
	textbox7.width = 80;
	textbox7.back_color = BLACK;
	textbox7.text_color = RED;
	textbox7.char_size = 12;
	textbox7.str = "x:0 y:0";
	draw_Rectangle_textbox(&textbox7);
}



