#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"
#include "touchpad.h"

#define tft_base (SDRAM_FB_BASE + SDRAM_FB_BASE / 2) // Where we want our frame buffers
#define  WIDTH 800
#define  HEIGHT 480
#define  NUM_FRAME 1


#define BUFFER_SIZE 1024*4

unsigned char run_state = 1; //״̬



volatile short Y1_Offset, Y2_Offset;

volatile uint8_t *fb = tft_base | 0x80000000;



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, WIDTH, HEIGHT, 8);
	LCD_Init();
}

void dso_init() {
	LCD_Fill(0, 0, 799, 479,0);//䲨ʾɫ
	draw_wave_area();
}

void wave_set_init() {
	CH1_WAVE.Wave_Color = YELLOW;
	CH1_WAVE.TIME_factor_NOW = 1;
	CH1_WAVE.VOLT_factor = 1;
	CH1_WAVE.Y_Offset = 0;


	CH2_WAVE.Wave_Color = GREEN;
	CH2_WAVE.TIME_factor_NOW = 1;
	CH2_WAVE.VOLT_factor = 1;
	CH2_WAVE.Y_Offset = -30;
}


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

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

	int16_t Vmin = 0;
	int16_t Vmax = 255;
	int16_t Vmid = 128;
	uint16_t Vpp;
	volatile int j=0;

	volatile uint8_t *pd;

	int buffer_flag = 0;

	char a[20];

	display_device_init();
	dso_init();
	sample_init();
	wave_set_init();
	touchpad_init();
	while (1) {

		CH1_WAVE.Y_Offset_PRE = CH1_WAVE.Y_Offset_NOW;
		CH1_WAVE.Y_Offset_NOW = CH1_WAVE.Y_Offset;
		CH2_WAVE.Y_Offset_PRE = CH2_WAVE.Y_Offset_NOW;
		CH2_WAVE.Y_Offset_NOW = CH2_WAVE.Y_Offset;

		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;
		CH1_WAVE.VOLT_factor_NOW = CH1_WAVE.VOLT_factor;
		CH2_WAVE.VOLT_factor_PRE = CH2_WAVE.VOLT_factor_NOW;
		CH2_WAVE.VOLT_factor_NOW = CH2_WAVE.VOLT_factor;
		POINT_COLOR = GREEN;
		BACK_COLOR = BLUE;


		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()){
				if(trig_mode == AUTO)
				{
					Vmid = IORD(PIO_VMID_BASE, 0);
					set_trig_level(Vmid/2);
				}
			}

			pd = (DATA_MEM_BASE + 32) | 0x80000000;

			for (i = 0; i < 512; i++) {
				CH1_WAVE.current_data[i] = (*pd)/2;
				pd++;
//				CH2_WAVE.current_data[i] = *pd;
				CH2_WAVE.current_data[i] = CH1_WAVE.current_data[i];
				pd++;
			}

			draw_wave(&CH1_WAVE);

			if(j >= 10)
			{
				update_freq();
				update_voltage();
				j=0;
			}
			else
			{
				j++;
			}
			usleep(1000);
		}
	}
}
