⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 graphic.c

📁 Very very small GUI. Usefull for small system, without OS or small OS. Event driven, support user m
💻 C
字号:
#include <rtx166t.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <math.h>

#include "GUI.h"
#include "MainMenu.h"

#include "pwin.h"
#include "guidebug.h"

//int MaxY, MinY;
#define GRAPH_ST_MIN			10
#define GRAPH_ST_MAX			80
#define ZOOM_Y_ENABLED			1

extern Pw_Bitmap bmGamma3;
TResult * res_ptr; 

void SendToHost(char * str, INT16U t) ;
void ReadBlock(char xhuge * , unsigned long , unsigned int);

void SetZoomY(TGraphic * graph) {
int i;
unsigned int Np;

#ifdef ZOOM_Y_ENABLED
	float MaxX, MaxY, MinX, MinY;
	MaxX = 0; MaxY = 0;
	MinX = 100000; MinY = MinX;
	if (graph->PointCount >	RESULT_MAX_COUNT)  Np = RESULT_MAX_COUNT;
	else Np = graph->PointCount;

	for (i = 0; i < Np; i++) {
/*		if (res_ptr[i].LifeTimeM >= 10)*/ {
			if (res_ptr[i].SurfTensC > MaxY) MaxY = res_ptr[i].SurfTensC;
			if ((res_ptr[i].SurfTensC > 0) && (res_ptr[i].SurfTensC < MinY)) MinY = res_ptr[i].SurfTensC;
		}
	}		
	if (Np > 0) {
		graph->MaxY = ceil(MaxY) + 1;
		graph->MinY = floor(MinY) - 1;
		if ((graph->MaxY - graph->MinY)< 2) {
			graph->MaxY++;
			graph->MinY--;
		}
	}
	else {
		graph->MaxY = GRAPH_ST_MAX; 
		graph->MinY = GRAPH_ST_MIN;
	}
#else	
		i = i;
		graph->MaxY = GRAPH_ST_MAX; 
		graph->MinY = GRAPH_ST_MIN;
#endif
//sprintf(s, "\2%s\tMax/Min\t%f\t%f\3\0", cDEBUG_MSG, graph->MaxY, graph->MinY);    
//SendToHost (s, 0);
}

void DrawAxis(Pw_GC * gc) {
Pw_Window * win;
int i, j, w, h, dx, dy, x, y;
long lj, cnt;
float tj, t0, t1;
float hh, mm, ss;
char axistext[36];
char seczn;
TGraphic * graph;
#ifdef ZOOM_Y_ENABLED
int CellY;
#endif
  	win = Pw_GCGetWindow(gc);
   	Pw_WindowGetSize(win, &w, &h);
	x = 3;
	dy =  GRAPH_H /7;//(MaxY - MinY);
	Pw_GCSetFont(gc, slkscrb_pwf); 			 	   	
	Pw_GCSetColor(gc, pw_white_pixel);
	graph = (TGraphic *)(win->cdata);
	// Y axis
#ifdef ZOOM_Y_ENABLED
	CellY = (int)(graph->MaxY - graph->MinY);
	if (CellY < 10) {
		dy =  GRAPH_H /CellY;
		j = (int)graph->MinY; 
		for (i = 0; i < (CellY); i++) {
			sprintf(axistext, "%2d\0", j);
			y = h - 20- dy * i;
			if (i < 7) Pw_DrawString(gc, x, y, axistext);				
			if (i != 0) Pw_DrawLine(gc, 17, y-3, GRAPH_X0, y-3);
			j++;
		}
	}
	else {
		CellY = (int)((ceil(CellY/10.0)));
		j = (int)graph->MinY; 
		for (i = 0; i < 10; i++) {
			sprintf(axistext, "%2d\0", j);
			y = h - 20- dy * i;
			if (i < 7) Pw_DrawString(gc, x, y, axistext);				
			if (i != 0) Pw_DrawLine(gc, 17, y-3, GRAPH_X0, y-3);
			j += CellY;
		}
	}
#else
	for (i = 0; i < 9; i++) {
		j = (i+1) * 10;
		sprintf(axistext, "%2d\0", j);
		y = h - 20- dy * i;
		if (i < 7) Pw_DrawString(gc, x, y, axistext);				
		if (i != 0) Pw_DrawLine(gc, 17, y-3, GRAPH_X0, y-3);
	}
#endif
	lj = 1;
	y = h - 10;
	dx = GRAPH_W / 4;
	// X axis
	if (graph->GraphicType == LOGARIPHM) {
		TimeAxisTitul = (char *) "Life Time, sec\0";
		for (i = 1; i < 6 ; i++) {
			lj *=  10; 
			tj = (float)lj/1000.0;
			if (lj == 10)
				sprintf(axistext, "%4.2f\0", tj);
			else
				if (lj == 100)
					sprintf(axistext, "%3.1f\0", tj);
				else
					sprintf(axistext, "%0.0f\0", tj);
			x = GRAPH_X0 + dx*(i-1) ;
			if (i != 5) Pw_DrawString(gc, x - Pw_FontGetStringWidth(gc->font, axistext)/2, y, axistext);				
			else 
				Pw_DrawString(gc, x-14, y, axistext);				
			if (i == 4) { 
				Pw_DrawString(gc, (GRAPH_X0+GRAPH_W-Pw_FontGetStringWidth(gc->font,TimeAxisTitul))/2, y+Pw_GCGetFontHeight(gc), TimeAxisTitul);
			}
			Pw_DrawLine(gc, x, y-8, x, y-5);
		}  
	}
	else {
		TimeAxisTitul = (char *) "Time, s\0";
		i = 0;
		seczn = '"';
		t0 = res_ptr[0].fReserved;
		cnt = graph->PointCount;
//		if (cnt > RESULT_MAX_COUNT) cnt = RESULT_MAX_COUNT;
		while (TRUE) {
			if (((i % 25) == 0) && (i < 120)) {
				t1 = res_ptr[i].fReserved - t0;
				hh = floor(t1 / 3600);
				mm = floor((t1 - hh*3600)/60);
				ss = floor(t1  - hh* 3600 - mm*60);

				if (hh > 0)
					sprintf(axistext, "%0.0f:%0.0f'%0.0f%c\0", hh, mm, ss, seczn);			
				else
					if (mm > 0)
						sprintf(axistext, "%0.0f'%0.0f%c\0", mm, ss, seczn);
					else
						sprintf(axistext, "%0.0f%c\0", ss, seczn);
				x = GRAPH_X0 + 2*i + 1;				
				Pw_DrawString(gc, x - Pw_FontGetStringWidth(gc->font, axistext)/2, y, axistext);				
				Pw_DrawLine(gc, x, y-8, x, y-5);
			}
			i++;
			if (i >= graph->ScrollPage) break;
			if ((i + graph->ScrollPos) >= cnt)break;
		}
		Pw_DrawString(gc, (GRAPH_X0+GRAPH_W-Pw_FontGetStringWidth(gc->font,TimeAxisTitul))/2, y+Pw_GCGetFontHeight(gc), TimeAxisTitul);
	}
	Pw_DrawBitmap(gc, 4, 10, &bmGamma3);
	Pw_DrawLine(gc, GRAPH_X0, GRAPH_Y0, GRAPH_X0, GRAPH_Y0+GRAPH_H);	 
	Pw_DrawLine(gc, GRAPH_X0, GRAPH_Y0, GRAPH_X0+GRAPH_W, GRAPH_Y0);	 
	Pw_DrawLine(gc, GRAPH_X0+GRAPH_W, GRAPH_Y0,GRAPH_X0+GRAPH_W, GRAPH_Y0+GRAPH_H);	
	Pw_DrawLine(gc, GRAPH_X0, GRAPH_Y0+GRAPH_H, GRAPH_X0+GRAPH_W, GRAPH_Y0+GRAPH_H); // v r
}

//-----------------------------------------------------------------------------
void DrawPointsLinear(Pw_GC * gc, TResult * res_ptr, int cnt) {
Pw_Window * win;
TGraphic * graph;
int i, j, w, h, /*xt,*/ x, y, dx;
float my;
	if (cnt == 0) return;

  	win = Pw_GCGetWindow(gc);
   	Pw_WindowGetSize(win, &w, &h);
	Pw_GCSetColor(gc, pw_white_pixel);
	graph = (TGraphic *)(win->cdata);
	dx = GRAPH_W / 5 ;
//SetZoomY(graph);
	y = h - 10;	
	i = 0; j = 0;
	my = GRAPH_H /(graph->MaxY - graph->MinY);
	i = 0;
	while (TRUE) {
		x = GRAPH_X0 + 2*i + 1;
		y = (graph->MaxY - res_ptr[i].SurfTensC) * my + GRAPH_Y0;
		if (i != 0) Pw_FillRect(gc, x-1, y-1, 3, 3);
		else Pw_FillRect(gc, x, y-1, 2, 3);
		i++;
		if (i >= graph->ScrollPage) break;
		if ((i + graph->ScrollPos) >= cnt)break;
	}
}

void DrawPointsLogariph(Pw_GC * gc, TResult * res, int cnt) {
Pw_Window * win;
int i, w, h, x, y;
float mx, my, aLT;
TGraphic * graph;
  	win = Pw_GCGetWindow(gc);
   	Pw_WindowGetSize(win, &w, &h);
	Pw_GCSetColor(gc, pw_white_pixel);
	graph = (TGraphic *)(win->cdata);
	mx = GRAPH_W / 4;//log10(100000);
	my = GRAPH_H /(graph->MaxY - graph->MinY);
	for (i = 0; i < cnt; i++) {
		aLT = (res[i].LifeTimeM);
		if (aLT < 10) aLT = 10;				// !!!
		if (aLT >= 10) {
			aLT = log10(aLT) - 1;
			x = (int)(aLT * mx)+ GRAPH_X0;
			y = (int)((graph->MaxY - res[i].SurfTensC) * my + GRAPH_Y0);
			Pw_FillRect(gc, x-1, y-1, 3, 3);
		}
	}
}

void DrawStandartModes(Pw_GC * gc) {
TGraphic * graph;
Pw_Window * win;
  	win = Pw_GCGetWindow(gc);
	graph = (TGraphic *)(win->cdata);
	if (graph->GraphicType == LOGARIPHM)
    	DrawPointsLogariph(gc, Res, graph->PointCount);
	else {
    	DrawPointsLinear(gc, Res, graph->PointCount);
	}
}

//-----------------------------------------------------------------------------
extern int Selected;

void GraphPaint(Pw_GC * gc) {
Pw_Window * win;
int i, j, w, h;
TGraphic * graph;
unsigned long adrOffs;
  	win = Pw_GCGetWindow(gc);
   	Pw_WindowGetSize(win, &w, &h);
	graph = (TGraphic *)(win->cdata);
	j = (int) (graph->ScrollPos / RESULT_MAX_COUNT);
	graph->ScrollPos = j * RESULT_MAX_COUNT;
	j = 0;
	if ((graph->GraphicType == LINEAR) && (graph->PointCount > RESULT_MAX_COUNT)) {
		if (!ExperimentInProgress) {
  			for (i = graph->ScrollPos; i < (graph->ScrollPos + RESULT_MAX_COUNT); i++) {
				adrOffs = Fat[Selected].Adr+(i)*sizeof(TResult);
				ReadBlock((char xhuge *) &Res[j], adrOffs, sizeof(TResult));
				j++; 
				if (i >= graph->PointCount - 1) break;
			}
		}
	}
	Pw_GCSetColor(gc, pw_black_pixel);
	Pw_FillRect(gc, 0, 0, w, h); 
	Pw_GCSetColor(gc, pw_white_pixel);
	Pw_DrawRect(gc, 1, 1, w-1/*-10*/, h-1); 
	if (GraphTitul[0] != 0) {
		Pw_GCSetFont(gc, slkscrb_pwf); 			 	   	
		Pw_DrawString(gc, (GRAPH_X0+GRAPH_W-Pw_FontGetStringWidth(gc->font,GraphTitul))/2, Pw_GCGetFontHeight(gc)+2, GraphTitul);
	}
	SetZoomY(graph);
	DrawAxis(gc);
	if (graph->PointDrawMetod != NULL)
		graph->PointDrawMetod(gc);
	else
// default metod, for new experiment
		DrawStandartModes(gc);		
}

int GraphEventHandler(Pw_Event * ev) {
//Pw_Event   event;
Pw_UserEvent * UserEvent;
Pw_Window * win ;
TGraphic * graph;
Pw_uInt key;

  win = Pw_EventGetWindow(ev); 
  graph = (TGraphic *)(win->cdata);
  switch (Pw_EventGetType(ev)) {
	case Pw_KeyPressEventType:
		if (ExperimentInProgress) return FALSE; 
		key = ev->key.keycode;
		switch (key) {
/*			case DOWN_KEY :
				if (graph->ScrollPos < (graph->ScrollPage-1)) { 						 	
					if ((graph->Selected < (graph->ItemCount-1))) {
							graph->Selected++;
							Pw_WindowRepaint(win);	
					}
				}
				else
					if (((graph->ScrollPos + graph->ScrollPage-1) < (graph->ItemCount-1))) {
				 			graph->Selected++;
							graph->ScrollPos++;
							Pw_WindowRepaint(win);	
					}
				return TRUE;
			case UP_KEY :
						if (listbox->Selected > listbox->ScrollPos) {
						 	listbox->Selected--;
							Pw_WindowRepaint(win);	
						}
						else
							if (listbox->Selected > 0) {
						 		listbox->Selected--;
								listbox->ScrollPos--;
								Pw_WindowRepaint(win);	
							}
				return TRUE;  */
		}					 
		break;
	case Pw_UserEventType :
		UserEvent = (Pw_UserEvent *) ev;
		switch (UserEvent->MsgData.MsgType) {
			case BPA_DATA_LT_POINT:
				graph->PointCount++;// = *((int *)UserEvent->MsgData.UserInfo.Data);
				res_ptr = Res;
				Pw_WindowRepaint(win);
				return FALSE;
			case BPA_DATA_M2_POINT:
				graph->PointCount++;
				res_ptr = Res;
				Pw_WindowRepaint(win);
				return FALSE;
			case BPA_DATA_M2_POINT_SCROLL:
				graph->PointCount =*((unsigned int *) UserEvent->MsgData.UserInfo.Ptr);
				res_ptr = Res;//(TResult *) UserEvent->MsgData.UserInfo.Ptr;
				Pw_WindowRepaint(win);
				return FALSE;
		}
		return FALSE;
  }
  return FALSE;
}

TGraphic * CreateGraphic(Pw_Window * parent, TGraphic * graph, TGraphicType type, Pw_RepaintCb point_draw_metod, int cnt, int x, int y, int w, int h) {
	graph->PointCount = cnt;
	res_ptr = Res;	  			// preventive assign
	Pw_WindowCreate(parent, x, y, w, h, &(graph->Win));
    graph->GraphicType = type;
	(graph->Win).cdata = graph;
	graph->ZoomX = 1; graph->ZoomY = 1;
	graph->MaxY = GRAPH_ST_MAX; 
	graph->MinY = GRAPH_ST_MIN;
	graph->ScrollPos = 0;
	graph->ScrollPage = GRAPH_W/2;
	Pw_WindowSetRepaintCallback(&(graph->Win), GraphPaint);
	Pw_WindowSetEventCallback(&(graph->Win), GraphEventHandler);
	graph->PointDrawMetod = point_draw_metod;
	Pw_WindowMap(&(graph->Win));
	return graph;	
} 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -