/**********************************************************
	SLCD Demo program for the RCM3720 kit

 	!!!!! NOTE !!!!!
   WHILE RUNNING FROM THE DYNAMIC C ENVIRONMENT, YOU MUST RUN
	THIS PROGRAM IN "NO POLLING" MODE TO AVOID MISSING RECEIVE
	CHARACTERS AT 115,200 BAUD (DEFAULT) WHILE RUNNING THE
	CONTINUOUSLY UPDATING CHART DEMO
 	!!!!! NOTE !!!!!

**********************************************************/

//#define DEBUG

/*
   This demo assumes the following bitmaps are loaded into the
   SLCD in the order shown below. Bitmaps 01 thru 20 are reserved
   for the SLCD demo macros. This program uses bitmaps 21 through
   40. The text file for loading these bitmaps via the BMPload
   program is slcd_demo.lst

01_button.bmp
02_button_click.bmp
03_check_box.bmp
04_check_box_click.bmp
05_Hitachi_Logos.BMP
06_Hitachi_PanelInfo.BMP
07_SLCDInfo.BMP
08_Contact_Blank.BMP
09_logo_bounce.bmp
10_button_up.BMP
11_button_dn.BMP
12_input_box.bmp
13_null.bmp
14_null.bmp
15_null.bmp
16_null.bmp
17_null.bmp
18_null.bmp
19_null.bmp
20_null.bmp
21_MainScrn.bmp
22_round_button.bmp
23_round_button_dn.bmp
24_check_box.bmp
25_check_box_click.bmp
26_LEDOFF.bmp
27_LEDON.bmp
28_board_sw_off.BMP
29_board_sw_on.BMP
30_big_button.BMP
31_big_button_dn.BMP
32_Bezel.bmp
33_volts_on.bmp
34_volts_off.bmp
35_mA_on.bmp
36_mA_off.bmp
37_button_on.bmp
38_button_off.bmp
39_button_up.bmp
40_button_dn.bmp

*/

// these defines are available for the bitmaps this demo uses
#define 	BMP_MainScrn			 21
#define 	BMP_round_button		 22
#define 	BMP_round_button_dn	 23
#define 	BMP_check_box			 24
#define	BMP_check_box_click	 25
#define	BMP_LEDOFF				 26
#define	BMP_LEDON             27
#define	BMP_board_sw_off      28
#define	BMP_board_sw_on       29
#define	BMP_big_button        30
#define	BMP_big_button_dn     31
#define	BMP_Bezel             32
#define	BMP_volts_on          33
#define	BMP_volts_off         34
#define	BMP_mA_on             35
#define	BMP_mA_off            36
#define	BMP_button_on         37
#define	BMP_button_off        38
#define	BMP_button_up         39
#define	BMP_button_dn         40

#class auto

#define DS1 6		//port F bit 6
#define DS2 7		//port F bit 7
#define S1  4		//switch, port F bit 4
#define S2  7		//switch, port B bit 7

#define DINBUFSIZE  255
#define DOUTBUFSIZE 255

///////
// change serial baud rate here
///////
#ifndef _232BAUD
#define _232BAUD 115200
#endif


//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//  Set MAX_CMDS to the maximum number of commands outstanding
//  to the SLCD at any time.
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
#ifdef DEBUG
#define MAX_CMDS 1
#else
#define MAX_CMDS 4
#endif

typedef unsigned char uchar;
typedef unsigned int uint;

struct button {
	uchar index;
   uchar state;
};
// only one button active at a time, so this can be global
struct button btn;

void slcd( char *s );
uchar anySLCDrx(struct button *btn);
void sRxInit(void);
void protoboardDemo(void);
void pbLedOn(int led);
void pbLedOff(int led);
void slcdDemo1(void);
void slcdDemo2(void);
void bmp(uint bitmap, uint x, uint y);
void pauseMs(int delay);
void moreDemos(void);
void levelGraph(void);
void drawLevelBar( uchar index, int x, int y );

// count of commands outstanding
static unsigned char cmdCount;

main()
{
   auto int testing;

	brdInit();				//initialize board for this demo

   BitWrPortI(PEDR, &PEDRShadow, 0, 5);	//set low to enable rs232 device

 	serDopen(_232BAUD);
	serDwrFlush();
	serDrdFlush();
   sRxInit();
   cmdCount = 0;

   // clear any junk that may be in the SLCD's input buffer
   serDputc('\r');	// send <return>
   pauseMs(100);  	// wait for any returns
   serDrdFlush(); 	// flush

   // verify we are connected...
   testing = 1;
   while( testing )
   {
   	slcd("");			// send <return>
      pauseMs(10);		// wait for response
   	anySLCDrx(&btn); 	// process returns
      if( cmdCount == 0 )
      {
      	// got a prompt
      	testing = 0;
      }
      else
      	cmdCount = 0;
   }

   // calibration screen
   slcd("s 0 1");	// set color
   slcd("z");     // clear screen
   slcd("f24");   // set font
   // send the text - use a backslash " to embed a quote inside a string. This is a 'C' convention
   slcd("t \"Press S2 while power applied\nto Calibrate Touch\" 25 50");
   pauseMs(1000);

   // check for touch calibration request - S2 pushed on power-on
   if( !BitRdPortI(PBDR, S2) )
   {
   	pauseMs(20);  // debounce
      if( !BitRdPortI(PBDR, S2) )
      {
      	slcd("z");
         slcd("t \"Touch calibration requested\n(S2 pushed at power-on)\" 25 50");
   		pauseMs(500);	  // let user see message
         slcd("tc");
   		anySLCDrx(&btn); // process accumulated returns
      }
   }

   while (1)
	{
      //
      // Logo splash screen
      //
      slcd("s 0 1");
      slcd("z");
      slcd("o 0 0");
   	slcd("xi 21 0 0");
   	pauseMs(2000);  // let user see message

      slcd("z");
      slcd("f24");
      slcd("o 20 14");
      slcd("s 0 1");
      slcd("bd 1 0 0 1 \"Push\" 12 20 30 31");
      slcd("S 900 FFF");	// dark red
      slcd("t \"Prototype Board I/O\" 94 17");
      slcd("o 20 89");
      slcd("s 0 1");
      slcd("bd 2 0 0 1 \"Push\" 12 20 30 31");
      slcd("S 090 FFF");	// dark green
      slcd("t \"Panel Meter Demo\" 94 17");
      slcd("o 20 165");
      slcd("s 0 1");
      slcd("bd 3 0 0 1 \"Push\" 12 20 30 31");
      slcd("S 009 FFF");	// dark blue
      slcd("t \"More Demos...\" 94 17");

      // wait for a button
		while( !anySLCDrx(&btn) );

      switch( btn.index )
      {
      	case 1:
         	protoboardDemo();
            break;
         case 2:
         	slcdDemo1();
         	break;
         case 3:
            moreDemos();
	        	break;
         default:
         	break;
      }
	}
}

void moreDemos(void)
{
   slcd("z");
   slcd("f24");
   slcd("o 20 14");
   slcd("s 0 1");
   slcd("bd 1 0 0 1 \"Push\" 12 20 30 31");
   slcd("S 900 FFF");   // dark red
   slcd("t \"Keyboard Demo\" 94 17");
   slcd("o 20 89");
   slcd("s 0 1");
   slcd("bd 2 0 0 1 \"Push\" 12 20 30 31");
   slcd("S 090 FFF");   // dark green
   slcd("t \"Level / Graph Demo\" 94 17");

   // wait for a button
   while( !anySLCDrx(&btn) );

   switch( btn.index )
   {
      case 1:
         slcdDemo2();
         break;

      case 2:
         levelGraph();
         break;

      default:
         break;
   }
}

void drawLevelBar( uchar index, int x, int y )
{
	char buf[100];

   sprintf(buf,"o %d %d",x,y);
   slcd(buf);
   sprintf(buf,"ld %d 0 0 20 80 0 0 1 888 100 F00 65 FF0 50 0F0",index);
   slcd(buf);
	slcd("p 2");
	slcd("l 21 0 21 81");
	slcd("l 0 81 21 81");
	sprintf(buf,"lv %d 0", index);
   slcd(buf);
}

void levelGraph(void)
{
	char buf[100];
   float f;
   int levelVal[5];
   int chartVal[3];
   int i, j;


   // draw 5 levelbars
   slcd("s 0 1");
   slcd("z");
   slcd("f 24BC");
   slcd("t \"Data visualization charts\" 28 0");
   drawLevelBar(0, 43, 40);
 	drawLevelBar(1, 96, 40);
 	drawLevelBar(2, 149, 40);
 	drawLevelBar(3, 202, 40);
 	drawLevelBar(4, 255, 40);

   // draw long chart
   slcd("o 10 140");
   slcd("cd 0 0 0 301 49 1 3 1 100 008 2 F00 2 0F0 2 FFF");
	slcd("p 2");
	slcd("l 302 0 302 50");
	slcd("l 0 50 302 50");
	slcd("o 0 0");

   // next button
   slcd("f13B");
   slcd("bd 1 257 202 1 \"Next\" 16 11 22 23");

   while(1)
   {
      // while waiting for button press, generate data and update display
      while( !anySLCDrx(&btn) )
      {
         // this costate does the data aquisition or in our case, simulation
         costate
         {
         	// generate data for 5 levelbars
         	for(i=0;i<5;i++)
            {
            	f = rand();
               levelVal[i] = (int)( 10+ f * 90);
            }

            // generate data for chart
           	f = rand();
            chartVal[0] = (int)(4 + f * 25);
           	f = rand();
            chartVal[1] = (int)(35 + f * 25);
           	f = rand();
            chartVal[2] = (int)(70 + f * 25);

            // use a delay to reduce update rate
            waitfor(DelayMs(100));
         }

         // this costate displays the levelbars with its own update rate
         costate
         {
         	for(i=0;i<5;i++)
            {
               // update level bars
               sprintf(buf,"lv %d %d",i,levelVal[i]);
               slcd(buf);
            }

            waitfor(DelayMs(250));
         }

         // this costate displays the chart with its own update rate
         costate
         {
				// update chart
            sprintf(buf,"cv 0 %d %d %d",chartVal[0], chartVal[1], chartVal[2]);
            slcd(buf);

            waitfor(DelayMs(100));
         }
      }

	   switch( btn.index )
	   {
	      case 1: 	// Next
            return;
	         break;

         default:
	         printf("Internal error - invalid button #%d\n",btn.index);
	         break;
	   }
   }
}


#define MAX_CHARS 20
void slcdDemo2(void)
{
   int i;
   char cmd[80];
   char input[MAX_CHARS+1];
   uchar inputIndex;
   char c;

   // where to put incoming characters
   inputIndex = 0;
   // clear input so there is a valid end of string everywhere
   for(i=0;i<MAX_CHARS+1;i++)
   	input[i] = 0;

   slcd("s 0 1");
	slcd("z");
   slcd("o 0 0");
   slcd("f24");
   slcd("t \"Keyboard Demo\" 70 10");
   // draw alpha keyboard
   // first row
   slcd("bd 49   0 112 1 \"1\" 8 5 39 40");
	slcd("bd 50  32 112 1 \"2\" 8 5 39 40");
	slcd("bd 51  64 112 1 \"3\" 8 5 39 40");
	slcd("bd 52  96 112 1 \"4\" 8 5 39 40");
	slcd("bd 53 128 112 1 \"5\" 8 5 39 40");
	slcd("bd 54 160 112 1 \"6\" 8 5 39 40");
	slcd("bd 55 192 112 1 \"7\" 8 5 39 40");
	slcd("bd 56 224 112 1 \"8\" 8 5 39 40");
	slcd("bd 57 256 112 1 \"9\" 8 5 39 40");
	slcd("bd 48 288 112 1 \"0\" 8 5 39 40");
   // second row
   slcd("bd 81   0 144 1 \"Q\" 8 5 39 40");
   slcd("bd 87  32 144 1 \"W\" 6 5 39 40");
   slcd("bd 69  64 144 1 \"E\" 9 5 39 40");
   slcd("bd 82  96 144 1 \"R\" 8 5 39 40");
   slcd("bd 84 128 144 1 \"T\" 9 5 39 40");
   slcd("bd 89 160 144 1 \"Y\" 9 5 39 40");
   slcd("bd 85 192 144 1 \"U\" 8 5 39 40");
   slcd("bd 73 224 144 1 \"I\" 11 5 39 40");
   slcd("bd 79 256 144 1 \"O\" 8 5 39 40");
   slcd("bd 80 288 144 1 \"P\" 9 5 39 40");
	// third row
   slcd("bd 65   0 176 1 \"A\" 9 5 39 40");
   slcd("bd 83  32 176 1 \"S\" 8 5 39 40");
   slcd("bd 68  64 176 1 \"D\" 9 5 39 40");
   slcd("bd 70  96 176 1 \"F\" 9 5 39 40");
   slcd("bd 71 128 176 1 \"G\" 8 5 39 40");
   slcd("bd 72 160 176 1 \"H\" 8 5 39 40");
   slcd("bd 74 192 176 1 \"J\" 9 5 39 40");
   slcd("bd 75 224 176 1 \"K\" 9 5 39 40");
   slcd("bd 76 256 176 1 \"L\" 9 5 39 40");
	slcd("f 16B");
   slcd("bd 32 288 176 1 \"sp\" 5 5 39 40");
   slcd("f 24");
   // fourth row
   slcd("bd 90   0 208 1 \"Z\" 9 5 39 40");
   slcd("bd 88  32 208 1 \"X\" 9 5 39 40");
   slcd("bd 67  64 208 1 \"C\" 8 5 39 40");
   slcd("bd 86  96 208 1 \"V\" 9 5 39 40");
   slcd("bd 66 128 208 1 \"B\" 8 5 39 40");
   slcd("bd 78 160 208 1 \"N\" 8 5 39 40");
   slcd("bd 77 192 208 1 \"M\" 8 5 39 40");
   slcd("bd 44 224 208 1 \",\" 10 3 39 40");
   slcd("bd 45 256 208 1 \"-\" 10 5 39 40");
	// rubout == backspace
	slcd("f 16B");
	slcd("bd 8  288 208 1 \"rub\" 4 9 39 40");

   // next button
   slcd("f13B");
   slcd("bd 3 257 0 1 \"Next\" 16 11 22 23");

   // font for keystroke string
   slcd("f 12x24");
   // underscore for cusrsor
   slcd("t \"_\" 60 75");

   while(1)
   {
      // wait for button press
      while( !anySLCDrx(&btn) )
			;

	   switch( btn.index )
	   {
	      case 8: 	// RUBOUT (destructive backspace)
            if( inputIndex >= 1 )
               input[--inputIndex] = 0;
	         break;

         case 3:	// NEXT

         	return;

         default:	// alpha-numeric keystroke
            input[inputIndex++] = btn.index;
            if( inputIndex == MAX_CHARS )
            	inputIndex--;
	         break;
	   }
      // blank the line for rubout
      slcd("sc 50 75");
      for(i=0;i<MAX_CHARS;i++)
         slcd("t \" \"");

      // display stored string
      slcd("sc 0 0");
      sprintf(cmd,"t \"%s\" 50 75",input);
		slcd(cmd);
      // display underscore as "cursor"
      slcd("t \"_\"");

   }

}
void slcdDemo1(void)
{
   char cmd[80];
   float f;
   enum {
   	m_volts,
      m_mAmps,
   } meterState;
   float volts, mAmps;
   char input;

   // background slightly off white helps buttons stand out
   slcd("S 000 EEE");
	slcd("z");
   slcd("o 0 0");
   bmp(BMP_Bezel, 29, 16);
   // volts selected
   meterState = m_volts;
   slcd("bd 1 258 24 21 \"\" \"\" 0 0 0 0 34 33");
   // amps deselected
   slcd("bd 2 258 72 20 \"\" \"\" 0 0 0 0 36 35");
   // input select
   slcd("f24");
   slcd("t \"Input:\" 30 138");
   // input 1 selected
   input = 1;
   slcd("bd 11 107 129 21 \"1\" \"1\" 9 5 9 5 39 40");
   // input 2, 3 deselected
   slcd("bd 12 151 129 20 \"2\" \"2\" 9 5 9 5 39 40");
   slcd("bd 13 195 129 20 \"3\" \"3\" 9 5 9 5 39 40");
   // next button
   slcd("f13B");
   slcd("bd 3 257 202 1 \"Next\" 16 11 22 23");

   while(1)
   {
      // while waiting for button press, aquire data and update meter
      while( !anySLCDrx(&btn) )
      {
         // this costate does the data aquisition
         costate
         {
               //
               // measure and filter voltage here...
               //
     				if( meterState == m_volts )
               {
	               f = rand();
                  switch(input)
                  {
                  	case 1:
                  		//fake 24V +/- 3V
	               		volts = 24 + (f*6)-3;
                        break;

                     case 2:
                     	// fake 12V +/- 1V
                        volts = 12 + (f*2)-1;
                     	break;

                     case 3:
                     default:
                     	// fake 0
                        volts = 0;
                        break;
                  }
	               // use a delay to reduce update rate
	               waitfor(DelayMs(250));
               }
               else
               if( meterState == m_mAmps )
               {
	               f = rand();
                  switch(input)
                  {
                  	case 1:
                     	// fake 500mA +/- 100
	               		mAmps = 500 + (f*200)-100;
                        break;

                     case 2:
                     	// fake 150mA +/- 50;
                        mAmps = 150 + (f*100) -50;
                        break;

                     case 3:
                     default:
                     	mAmps = 0;
                        break;
                  }
	               // use a delay to reduce update rate
	               waitfor(DelayMs(250));
               }
         }

         // this costate displays the value with its own update rate
         costate
         {
     				if( meterState == m_volts )
               {
   					slcd("f32x64");
   					slcd("S F00 000");
               	sprintf(cmd,"t \"%5.2f\" 54 43",volts);
                  slcd(cmd);
	               // use a delay to set update rate
	               waitfor(DelayMs(400));
               }
               else
               if( meterState == m_mAmps )
               {
   					slcd("f32x64");
   					slcd("S F00 000");
               	sprintf(cmd,"t \"%5.1f\" 54 43",mAmps);
                  slcd(cmd);
	               // use a delay to set update rate
	               waitfor(DelayMs(500));
               }

         }
      }

	   switch( btn.index )
	   {
	      case 1: 	// VOLTS
	         if(btn.state == 1) 	// selected
            {
            	// set amps off
               slcd("ssb 2 0");
               meterState = m_volts;
            }
	         else
            {
            	// set amps on
               slcd("ssb 2 1");
               meterState = m_mAmps;
            }
	         break;

	      case 2:	// AMPS
	         if(btn.state == 1)  // selected
            {
            	// set volts off
               slcd("ssb 1 0");
               meterState = m_mAmps;
            }
	         else
            {
            	// set volts on
               slcd("ssb 1 1");
               meterState = m_volts;
            }
	         break;

         case 3:	// NEXT
         	return;

         case 11:	// input 1
	         if(btn.state == 1) 	// selected
            {
               input = 1;
	            // turn others off
	            slcd("ssb 12 0");
	            slcd("ssb 13 0");
            }
            else
            	input = 0;
            break;

         case 12:	// input 2
	         if(btn.state == 1) 	// selected
            {
               input = 2;
	            // turn others off
	            slcd("ssb 11 0");
	            slcd("ssb 13 0");
            }
            else
            	input = 0;
            break;

         case 13:	// input 3
	         if(btn.state == 1) 	// selected
            {
               input = 3;
	            // turn others off
	            slcd("ssb 11 0");
	            slcd("ssb 12 0");
            }
            else
            	input = 0;
            break;


         default:
	         printf("Internal error - invalid button #%d\n",btn.index);
	         break;
	   }
   }

}

void bmp(uint bitmap, uint x, uint y)
{
	char buf[20];
 	sprintf(buf,"xi %d %d %d",bitmap,x,y);
   slcd(buf);
}

void protoboardDemo(void)
{
   struct button btn;
   uchar s1, s2;
   uchar s1Image, s2Image;

   // switches are "off"
   s1 = s2 = 0;
   // images are off
   s1Image = s2Image = 0;

   slcd("z");
   slcd("s 0 1");
   slcd("f18BC");
   slcd("t \"Push to activate board LEDs:\" 47 19");
   slcd("bd 2 70 35 2 \"\" \"\" 0 0 0 0 26 27");
   slcd("f24B");
   slcd("t \"DS2\" 79 90");
   slcd("t \"DS1\" 183 90");
   slcd("f18BC");
   slcd("bd 1 174 35 2 \"\" \"\" 0 0 0 0 26 27");
   slcd("t \"Push board switches to change:\" 45 135");
   slcd("xi 28 46 166");
   slcd("xi 28 171 166");
   slcd("f24B");
   slcd("t \"S2\" 75 207");
   slcd("t \"S1\" 201 207");
   slcd("f13B");
   slcd("bd 3 257 202 1 \"Next\" 16 11 22 23");

   while(1)
   {
      // while no button press, check board switches
      while( !anySLCDrx(&btn) )
      {
      	// this code is modeled on the TOGGLESWITCH.C sample
	      costate
	      {
            // check for high == "off" state
            if (BitRdPortI(PFDR, S1))
	         {
             	s1 = 0;							// set to off
               abort;                     // get out
            }
            waitfor(DelayMs(10));         //switch press detected if got to here
	         if (BitRdPortI(PFDR, S1))     //debounce - see if it still low
	            abort;
				// have a valid S1 press, so set switch state
            s1 = 1;
	         abort;
	      }

	      costate
	      {
            // check for high == "off" state
            if (BitRdPortI(PBDR, S2))
	         {
             	s2 = 0;							// set to off
               abort;                     // get out
            }
            waitfor(DelayMs(10));         //switch press detected if got to here
	         if (BitRdPortI(PBDR, S2))     //debounce - see if it still low
	            abort;
				// have a valid S1 press, so set switch state
            s2 = 1;
	         abort;
	      }

	      costate
	      {  // set image
	         if(s2 != s2Image)
	         {
	            s2Image = s2;
					if( s2Image == 0 )
   					slcd("xi 28 46 166");
               else
               	slcd("xi 29 46 166");
	         }

	         if(s1 != s1Image)
	         {
	            s1Image = s1;
					if( s1Image == 0 )
   					slcd("xi 28 171 166");
               else
               	slcd("xi 29 171 166");
	         }
	      }
      } // end while no button press from SLCD

	   switch( btn.index )
	   {
	      case 1:
	         if(btn.state == 1)
	            pbLedOn(DS1);
	         else
	            pbLedOff(DS1);
	         break;

	      case 2:
	         if(btn.state == 1)
	            pbLedOn(DS2);
	         else
	            pbLedOff(DS2);
	         break;

	      case 3:
	         return;

	      default:
	         printf("Internal error - invalid button #%d\n",btn.index);
	         break;
	   }
   }
}

#ifdef DEBUG
	static char lastCommand[120];
#endif

void slcd( char *s )
{
#ifdef DEBUG
   strcpy(lastCommand,s);
#endif
	if(*s) serDputs(s);
   serDputc('\r');
   cmdCount++;
   // want no more than MAX_CMDS commands outstanding
   while( cmdCount >= MAX_CMDS )
   {
      // process SLCD prompts; ignore button presses
      anySLCDrx(&btn);
   }
}

#define RXBUF_SIZE 129
static char rxBuf[RXBUF_SIZE];
static char *rxHead;
static char *rxTail;
static uchar rxCount;
static uchar rxGotline;

// putChar - assumes there is room in the buffer...
void sPutChar(unsigned char c)
{
	*rxHead++ = (char) c;
    rxCount++;
	if( rxHead == rxBuf+RXBUF_SIZE )
	{
		rxHead = rxBuf;
 	}
}

// getChar - assumes there is something to get...
uchar sGetChar(void)
{
	unsigned char c;
   c = *rxTail++;
	rxCount --;
	if( rxTail == rxBuf+RXBUF_SIZE )
    {
		rxTail = rxBuf;
    }
	return( c );
}


// getResponse - assumes string with a <return> is in the buffer
void getResponse(char *s, int len)
{
	uchar c;

   c = 0;
	while( rxCount && len && c != '\r' )
   {
   	c = sGetChar();
		*s++ = c;
      len--;
   }
   if( c != '\r' )
   {
	   printf("internal error #2");
   }
   // null terminate the string
   if(len)
   {
   	*s = 0;
   }
   else
   {
	   printf("internal error #3");
   }
}

// peekChar - assumes there is something to get...
uchar peekChar(void)
{
	return( *rxTail );
}

void sRxInit(void)
{
      int i;
      // for debug
      for(i=0;i<RXBUF_SIZE;i++) rxBuf[i]=0;

      rxHead = rxTail = rxBuf;
      rxCount = 0;
      rxGotline = 0;
      cmdCount = 0;
}

//====================================================
// checkSLCDrx()
// This checks the SLCD rx for a response.
// return is the number of the button press received
// return -1 menas no response
//====================================================
#define BUF_LEN 80
uchar anySLCDrx(struct button *btn)
{
   int i;
   uchar c;
   uchar buf[BUF_LEN];

   //
   // get any rx'd characters
   //
   while( -1 != (i= serDgetc()))
   {
   	sPutChar((char)i);
      if( i == '\r' )
      {
      	rxGotline++;
      }
   }

   //
   // process responses (strings followed by a return)
   //
   while( rxGotline )
   {
   	// get the response
      getResponse( buf, BUF_LEN );
      rxGotline--;

      // see what to do with it
      switch( buf[0] )
      {
         // command prompt
         case '>':
            if( buf[1] == '\r' )   // end of string
            {
               if( cmdCount )
               {
               	cmdCount--;
               }
               else
               {
                  printf("command count mismatch - extra prompt\n");
               }
            }
            else
            {
               printf("> prompt without following <return>\n");
            }
				break;

         // momentary button or hotspot press
         case 'x':
            i = atoi(&buf[1]);
            if( i > 255 )
            {
            	printf("Bad rx string \"%s\"\n",buf);
               return(0);
            }
            btn->index = i;
            return(1);
            break;

         // latching button press
         case 's':
            i = atoi(&buf[1]);
            if( i > 255 )
            {
            	printf("Bad rx string \"%s\"",buf);
               return(0);
            }
            btn->index = i/10;
            btn->state = i%10;
            return(1);
            break;

         case '!':
         	printf("Command sent to SLCD generated a syntax error\n");
#ifdef DEBUG
				printf("Last Command was\n%s\n",lastCommand);
#endif
            if( cmdCount )	cmdCount--;
            return(0);
            break;

         case '^':
         	printf("SLCD input buffer oevrflow\n");
            // on overflow, all commands are flushed
            if( cmdCount )	cmdCount = 0;
            return(0);
            break;

         case '?':
         	printf("SLCD input framing error\n");
            return(0);
            break;

         default:
         	printf("Unrecognized SLCD response: 1st char 0x%x, string = \"%s\"\n",buf[0],buf);
#ifdef DEBUG
				printf("Last Command was\n%s\n",lastCommand);
#endif
            return(0);
            break;

      }
   }
   // this hits if there are only command prompts...
   return(0);
}


///////////////////////////////////////////////////////////
// DS1 led on protoboard is controlled by port D bit 6
// DS2 led on protoboard is controlled by port D bit 7
// turns on when port bit is set to 0
///////////////////////////////////////////////////////////
void pbLedOn(int led)
{
	BitWrPortI(PFDR, &PFDRShadow, 0, led);
}
void pbLedOff(int led)
{
	BitWrPortI(PFDR, &PFDRShadow, 1, led);
}

// delay function - hack when it rolls over
void pauseMs(int delay)
{
	unsigned long t, t2;
   char wait;

   wait = 1;
   t = MS_TIMER;
   while( wait )
   {
      if( MS_TIMER >= t )
      {
      	if( MS_TIMER > t+delay )
         	wait = 0;
      }
      else
      {
      	if( MS_TIMER > delay )
         	wait = 0;
      }
   }
}

