#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 "data.h"
#include "DSO.h"
#include "logo.h"
#include "adc128s022.h"
#include "altera_avalon_pio_regs.h"

#define  WIDTH 480
#define  HEIGHT 272
#define  NUM_FRAME 1

#define SCREEN_LENGTH 480
#define SCREEN_HIGHT 272
#define CHANNEL	8
#define WAVE_XSTART	80
#define WAVE_XlENGTH (WAVE_XEND - WAVE_XSTART)
#define WAVE_XEND (SCREEN_LENGTH - 1)
#define WAVE_YSTART	30
#define WAVE_YEND (SCREEN_HIGHT - 22)
#define WAVE_YlENGTH (WAVE_YEND - WAVE_YSTART)
#define CHANNEL_HIGHT (WAVE_YEND -  WAVE_YSTART)/8

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

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 sample_init(void)
{
	IOWR(PIO_SFREQ_BASE, 0, 0x1);
	IOWR(PIO_TRIG_SET_BASE, 0, 2047 | 0x8000);
}

int main(int argc, char **argv) {
	unsigned int d = 0;

	int read_buffer[BUFFER_SIZE] = { 0 };
	int retval;
	long int screensize = 0;
	int i;

	unsigned char t;

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

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

	volatile uint16_t datmp[WAVE_SIZE];

	volatile uint8_t *pre_data1 = NULL;
	volatile uint8_t *pre_data2 = NULL;

	volatile int8_t *current_data1;
	volatile int8_t *current_data2;

	int8_t *pd;

	int buffer_flag = 0;

	////Initial Touch panel
	TOUCH_HANDLE hTouch;
	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();

	Y1_Offset = 30;
	Y2_Offset = -70;
	adc_init();

	IOWR(MASTER_WR_CTRL_BASE, 1, DATA_MEM_BASE);
	IOWR(MASTER_WR_CTRL_BASE, 2, 1024*2);

	while (1) {
		if(Y2_Offset >= 40)
			Y2_Offset = -70;
		else
			Y2_Offset ++;
		//touch_screen_app();


		if (run_state) {
			CH1_Y_Offset_PRE = CH1_Y_Offset;
			CH1_Y_Offset = Y1_Offset;
			CH2_Y_Offset_PRE = CH2_Y_Offset;
			CH2_Y_Offset = Y2_Offset;
			//updata_measurement();

			if (buffer_flag) {
				current_data1 = wave1;
				current_data2 = wave2;
				pre_data1 = wave3;
				pre_data2 = wave4;
				buffer_flag = 0;
			} else {
				current_data1 = wave3;
				current_data2 = wave4;
				pre_data1 = wave1;
				pre_data2 = wave2;
				buffer_flag = 1;
			}
			IOWR(PIO_EN_BASE, 0, 1);
			IOWR(PIO_EN_BASE, 0, 0);
			usleep(1);
			while(!(IORD(PIO_STATE_BASE, 0)));

//			for(i=0;i<1024;i++)
//			{
//				current_data1[i] = adc_get_result(0) >> 5;
//				current_data2[i] = adc_get_result(0) >> 5;
//			}

			pd = DATA_MEM_BASE;
			for (i = 0; i < 512;i++) {
				current_data2[i] = *pd;
				pd++;
				current_data1[i] = *pd;
				pd++;
			}
			draw_wave2(pre_data1,current_data1);
			draw_wave1(pre_data2,current_data2);
			usleep(20000);
		}
	}
}
