Plotting waveform graphics code

Draw waveform display effect
Insert picture description here
Insert picture description here

head File:

#ifndef  MY_WAVE_H
#define  MY_WAVE_H

#include "DIALOG.h"
#include "sys.h"
#include "my_GUI.h"

//图形结构体
typedef struct _Graph
{
    
    
	int oX;
	int oY;
	int datMax;		//最大值
	int datMin;		//最小值
	int post;		//当前位置
	RECT_INFO rect; //窗口尺寸
	GUI_COLOR color;	//前景颜色值
	GUI_COLOR bk_color;	//背颜色值
}GRAPH_INFO;

void GUI_Graph_Init(GRAPH_INFO *gp,RECT_INFO *rect,int max,int min,GUI_COLOR fc,GUI_COLOR bc);
void GUI_Graph_Clear(GRAPH_INFO *gp);	//清除
void GUI_Graph_AddData(GRAPH_INFO *gp,int dat); //写入一个数据

#endif

C file


#include "my_GUIwave.h"

/*---------------------------------------------------------------------------
@Function   :GUI_Graph_Init
@Description:初始化
@Input      :gp:要操作的图形数据指针
			 rect:图形界面尺寸
			 max:图形显示最大值的Y
			 min:图形显示最小值的Y
			 fc:前景色
			 bc:背景色
@Retrun     :无
@Others     :
----------------------------------------------------------------------------*/
void GUI_Graph_Init(GRAPH_INFO *gp,RECT_INFO *rect,int max,int min,GUI_COLOR fc,GUI_COLOR bc)
{
    
    
	gp->rect.x = rect->x;
	gp->rect.y = rect->y;
	gp->rect.xSize = rect->xSize;
	gp->rect.ySize = rect->ySize;

	gp->color = fc;
	gp->bk_color = bc;
	gp->datMax = max;
	gp->datMin = min;

	gp->oX = gp->rect.x;
	gp->oY =gp->rect.y + gp->rect.ySize;
	gp->post = 0;
}


/*---------------------------------------------------------------------------
@Function   :GUI_Graph_Clear
@Description:清除
@Input      :
@Retrun     :无
@Others     :
----------------------------------------------------------------------------*/
void GUI_Graph_Clear(GRAPH_INFO *gp)
{
    
    
	GUI_RECT mRect;

	myCoverRect(&mRect,&gp->rect);
	GUI_SetColor(gp->bk_color);
	GUI_FillRect(mRect.x0,mRect.y0,mRect.x1,mRect.y1); //画背景

	//复位起始位置
	gp->post = 0;
	gp->oX = gp->rect.x;
	gp->oY = gp->rect.y+gp->rect.ySize;
}

/*---------------------------------------------------------------------------
@Function   :GUI_Graph_AddData
@Description:清除
@Input      :dat 是放大了10倍的值,即传进来的数是小数转为整数的
@Retrun     :无
@Others     :
----------------------------------------------------------------------------*/
void GUI_Graph_AddData(GRAPH_INFO *gp,int dat)
{
    
    
	int dpX,dpY;
	int ran;
	int dat_Int;	//整数
	int dat_Dec; //小数
	int x1;

	GUI_RECT mRect;

	myCoverRect(&mRect,&gp->rect);

	//计算当前数据的X点
	if(++gp->post > gp->rect.xSize)
	{
    
    
		gp->post = 0; //防止超过
		gp->oX = gp->rect.x; //从头开始画
	}
	dpX = gp->rect.x + gp->post;
	
	 //分离出整数和小数
	dat_Int = dat / 10; 
	dat_Dec = dat % 10;

	//限制数据范围,防止超出显示
	if (dat_Int > gp->datMax)
	{
    
    
		dat_Int = gp->datMax;
		dat_Dec = 0;		
	}
	else if (dat_Int < gp->datMin)
	{
    
    
		dat_Int = gp->datMin;
		dat_Dec = 0;
	}

	//计算当前数据的Y点
	ran = (gp->datMax - gp->datMin)*10 / gp->rect.ySize; //计算ySize个像素点中每个点表示的数值
	dpY = (dat_Int - gp->datMin)*10 / ran + dat_Dec / ran; //计算出Y的高度
	
	dpY = gp->rect.y + gp->rect.ySize -dpY; //高度转换成Y坐标

	if (dpY < gp->rect.y) dpY = gp->rect.y; //限制不能超过显示区域

	if (gp->post <= 1)gp->oY = dpY;//从头开始

	//计算擦除的位置	
	x1 = dpX + 4; //擦除4个像素点
	if (x1 > (mRect.x1))x1 = mRect.x1;

	GUI_SetPenSize(1);
	GUI_SetColor(gp->bk_color);
	GUI_FillRect(dpX,mRect.y0,x1,mRect.y1);//擦除

	GUI_SetColor(gp->color);
	GUI_DrawLine(gp->oX,gp->oY,dpX,dpY);	//绘制线

	//存储坐标
	gp->oX = dpX;
	gp->oY = dpY;
}

//-------------END OF FILE--------------

usage:


GRAPH_INFO graph_tmp,graph_speed,graph_pressure; //定义3个波形图

/*---------------------------------------------------------------------------
@Function   :Graph_Init
@Description:图表初始化
@Input      :
@Retrun     :无
@Others     :
----------------------------------------------------------------------------*/
void Graph_Init(void)
{
    
    
	RECT_INFO rect;

	//温度
	rect.x = 7;
	rect.y = 169;
	rect.xSize = 148;
	rect.ySize = 60;
	GUI_Graph_Init(&graph_tmp,&rect,40,20,UI_COLOR_TMP,GUI_BLACK);

	//流速
	rect.x = 7;
	rect.y = 89;
	rect.xSize = 148;
	rect.ySize = 60;
	GUI_Graph_Init(&graph_speed,&rect,30,0,UI_COLOR_SPEED,GUI_BLACK);

	//压力
	rect.x = 7;
	rect.y = 9;
	rect.xSize = 148;
	rect.ySize = 60;
	GUI_Graph_Init(&graph_pressure,&rect,390,0,UI_COLOR_PRES1,GUI_BLACK);
}

	//画图形
	GUI_Graph_AddData(&graph_tmp,FlowSensor_GetTemperature());
	GUI_Graph_AddData(&graph_speed,FlowSensor_GetSpeed());
	GUI_Graph_AddData(&graph_pressure,MPXV5004DP_GetPressure());

Guess you like

Origin blog.csdn.net/Lennon8_8/article/details/108586478