/*
	SHOWIN.C

	Example of the graphical capabilities of the PK2400.

	This program displays a graph of the state of eight digital
	inputs versus time. It forms an eight channel oscilloscope.

	Ground any input then observe the lcd screen when you remove
	the ground.
*/

#use eziopk24.lib
#use wintek.lib
#use glcd.lib
#use vdriver.lib

void readState(char *stateTable) {
	auto int i;

	for (i = 0; i < 9; ++i) {
		stateTable[i] = eioBrdDI(i);
	}
}

#define STEP_WIDTH 4
#define WAVE_AMP 4
#define WAVE_HEIGHT 6
#define BM_RIGHT 13
#define BM_LEFT BM_RIGHT+STEP_WIDTH*25

main() {
	auto struct _fontInfo fi;	//	declare struct to hold font information
	auto char diState[2][9];
	auto int i;
	auto int flag;
	static char localBm[128*8];
	
	VdInit();
	_eioBrdInit(0);
	glInit();
	glFontInit(		//	initialize the font info structure needed later
		&fi,			//	pointer to the font info structure to fill
		6,				//	horizontal size (in pixels) of each character
		8,				//	vertical size (in pixels) of each character
		32,			//	beginning code of printable characters
		127,			//	ending code of printable characters
		engFont6x8R	//	address of font bitmap in root memory
		);
	glSetBrushType(GL_SET);
	readState(diState[0]);
	for (i = 0; i < 9; ++i)	{
		glPrintf((i&1)*6,i*WAVE_HEIGHT,&fi,"%d",i);
		glPlotLine(BM_RIGHT,i*WAVE_HEIGHT+(diState[0][i]?0:WAVE_AMP),
			BM_LEFT,i*WAVE_HEIGHT+(diState[0][i]?0:WAVE_AMP));
		glPrintf(116+(i&1)*6,i*WAVE_HEIGHT,&fi,"%d",i);
	}
	flag = 0;
	while (1) {
		wtHScroll(BM_RIGHT+STEP_WIDTH,BM_LEFT-BM_RIGHT-STEP_WIDTH+1,-STEP_WIDTH);
		readState(diState[!flag]);
		for (i = 0; i < 9; ++i)	{
			glSetBrushType(GL_CLEAR);
			glPlotLine(BM_LEFT-STEP_WIDTH,i*WAVE_HEIGHT+(diState[flag][i]?0:WAVE_AMP),
							BM_LEFT,i*WAVE_HEIGHT+(diState[flag][i]?0:WAVE_AMP));
			glSetBrushType(GL_SET);
			glPlotLine(BM_LEFT-STEP_WIDTH,i*WAVE_HEIGHT+(diState[!flag][i]?0:WAVE_AMP),
							BM_LEFT,i*WAVE_HEIGHT+(diState[!flag][i]?0:WAVE_AMP));
			if (diState[flag][i] != diState[!flag][i]) {
				glPlotLine(BM_LEFT-STEP_WIDTH,i*WAVE_HEIGHT+(diState[flag][i]?0:WAVE_AMP),
								BM_LEFT-STEP_WIDTH,i*WAVE_HEIGHT+(diState[!flag][i]?0:WAVE_AMP));
			}
		}
		flag = !flag;
	}
}