



#define 		DIGITAL_OUT				2 
#define			OUT_LOW					0x00
#define			OUT_HI					0x01
#define			STRGLO_STRGHI			0x30
#define			SIO_20					20

DIM a, errCode



SUB GPIO_toggleN(byVal n as INTEGER)
	dim i
	
	for i = 1 to n 
		GpioWrite(SIO_20, OUT_LOW)
		GpioWrite(SIO_20, OUT_HI)
	next
	GpioWrite(SIO_20, OUT_LOW)					// at the end ensure line is left in low state
ENDSUB


FUNCTION HandlerTimer6()
	a = RandEx(10)								// generate random, smaller than or equal to 10 
	print "Will sleep: "; a; " seconds.\n"		// print out the number of second the app will sleep
	GPIO_toggleN(a)								// toggle GPIO 

	TimerStart(6, a * 1000, 0)					// restart the timer and return to sleep
ENDFUNC 1 


// the equivalent of main() function in a .c program starts here
	
	ONEVENT EVTMR6 CALL HandlerTimer6			// bind timer 6 event handler
	
	// configure the pin as output; start with pin set to low
	errCode = GpioSetFunc(SIO_20, DIGITAL_OUT, STRGLO_STRGHI | OUT_LOW)	
	
	TimerStart(6, 1000, 0)						// enable the timer to fire an event in 1000ms; timer is one shot
	
WAITEVENT										// back to sleep and wait next event


