// Polled SLCD example
//
// Assumes that the "host" is connected to the AUX port,
// and a PC is connected to the main port
// (This could be swapped by sending the *com1main command
// once to the unit to swap ports on power-on)
//
// This macro sets up a screen with two buttons.
// when either is pressed, a corresponding variable is set.
//
// The host polls the SLCD by sending "Esc,1" = 0x1b, 0x31
// When the unit is polled it responds with:
//  0x7d,0x51,0x00,0xff for button 1
//  0x7d,0x52,0x00,0xff for button 2
//
#define pollDemo 1 /*

// for debugging, clear interpreter state of previous code
.empty

// define variables that hold button state. initialized to zero by default
.int iB1pushed
.int iB2pushed

// define 20 byte array for incoming poll
.20 1 1 array inBuf     // equivalent to unsigned char inBuf[20];

// define array with 4 elements, 1 subscript, 1 byte for button response
.4 1 1 array butResp
// populate response with base values
.0x7d 0 butResp b!      // equivalent to butResp[0] = 0x7d;
.0x51 1 butResp b!
.0x00 2 butResp b!
.0xff 3 butResp b!

// dsplay two pushbuttons using default bitmap
z                            // clear screen
bd 1 75 100 1 "ONE" 10 15
bd 2 150 100 1 "TWO" 10 15

// assign actions to buttons
xdo 1 ".1 to iB1pushed"
xdo 2 ".1 to iB2pushed"

// define background task to handle the poll
// background task is state driven
.0 int= bgState         // initialize to zero
.: pollTask             // name of the task
.//
.  bgState 0 == if      // handle state 0 (idle)
.    areadn 1 >= if     // at least 1 char in auxRx buffer
.      1 0 inBuf aread  // get 1 character into first inbuf
.      drop             // drop top of stack (number of characters actually read)
.      0 inBuf b@       // get what we read
.      0x1b == if       // look for esc
.        1 to bgState   // new state
.        exit           // end of state
.      endif
.    endif
.  endif
.//
.  bgState 1 == if      // handle state 1 (got escape)
.    areadn 1 >= if     // at least 1 char in auxRx buffer
.      1 1 inBuf aread  // get 1 character into second inbuf
.      drop             // drop top of stack (number of characters actually read)
.      1 inBuf b@       // get what we read
.      0x31 == if       // look for my ID = '1'
.        2 to bgState   // new state
.        exit           // end of state
.      endif
.    endif
.  endif
.//
.  bgState 2 == if      	// handle state 2 (got poll)
.    1 iB1pushed == if   	// button 1 pushed?
.      0x51 1 butResp b!    // fill in button #1
.      4 0 butResp awrite   // write 4 bytes starting at "butResp[0]
.      0 to iB1pushed        // clear flag
.      0 to bgState         // reset state machine
.      exit                 // done
.    endif
.
.	 1 iB2pushed == if   	// button 2 pushed?
.      0x52 1 butResp b!    // fill in button #1
.      4 0 butResp awrite   // write 4 bytes starting at "butResp[0]
.      0 to iB2pushed        // clear flag
.      0 to bgState         // reset state machine
.      exit                 // done
.    endif
.
.    0x50 1 butResp b!      // neither button pushed, so send default response
.    4 0 butResp awrite     // write 4 bytes starting at "butResp[0]
.     0 to bgState           // reset state machine
.
.  endif
.; // end definition of pollTask

// run the background task
bgtask ".polltask"

#end */
