/**********************************************************************
    var.c
    Z-World, 2001
   
    This program is intended to be run on a target processor.  It updates
    certain variable values to a DeviceMate, which are then reflected in
    a web page located on the DeviceMate.  Hence, these variables allow a
    simple means of having dynamic content in web pages.
   
    This program can be run in conjunction with any of the devmate_*.c
    DeviceMate programs and with var.c.
**********************************************************************/

/*
 * This macro defines the maximum number of variables that the
 * target processor will support.
 */
#define DEVMATE_VAR_MAXVARS 10

/* Request use of var services */
#define USE_TC_VAR

/*
 * Uncomment this line if this program will be loaded over ethernet. This will
 * force the DevMate communication to use serial port A, re-using the programming
 * link that was used to download the program.
 */
//#define USE_TC_LOADER

#ifdef CC_VER
	/* Dynamic C */
	#memmap xmem
	#use "tc_conf.lib"
#else
	/* Non-Rabbit */
	#include "tc_conf.h"
	#include <stdio.h>	
#endif

int main(void)
{
	/*
	 * These variables will help demonstrate the DeviceMate variable
	 * functionality.
	 */
	long starttime;
	long oldseconds;
	long seconds;
	char strvar[20];
	float floatvar;

	/*
	 * These calls are necessary to initialize target communications
	 * beween the target processor and the DeviceMate.
	 */
	devmate_init();

	/*
	 * Record the time at which the program started.
	 */
	starttime = SEC_TIMER;

	/*
	 * Initialize the variables.
	 */
	oldseconds = 0;
	seconds = 0;
	strcpy(strvar, "Good bunny.");
	floatvar = 42.42;

	/*
	 * Create the variables.
	 *
	 * The first parameter is the name for the variable.  This name must
	 * match the name given in the HTML page that references the variable.
	 *
	 * The second parameter is the address at which a variable's value
	 * can be found.  Note that the variables are updated to the DeviceMate
	 * from the given address.
	 *
	 * The third parameter is the type of the variable.
	 *
	 * The fourth parameter is the printf-style specifier that will be
	 * used to display the variable in a web page.
	 *
	 * The fifth parameter is only valid for variables of type PTR16
	 * (often used for strings).  It gives the maximum length of the data
	 * for that variable.  That is, the given variable must always be under
	 * this maximum length.
	 *
	 * The sixth parameter indicates which servers on the DeviceMate for
	 * which the variable should be used.  This will usually be
	 * VAR_SERVER_HTTP.
	 */
	devmate_var_add("seconds", &seconds, VAR_INT32, "%ld", 0, VAR_SERVER_HTTP);
	devmate_var_add("string", strvar, VAR_STRING, "%s", 20, VAR_SERVER_HTTP);
	devmate_var_add("float", &floatvar, VAR_FLOAT32, "%f", 0, VAR_SERVER_HTTP);
	for (;;) {
		/*
		 * Drive the target communications (including the DeviceMate variable
		 * support).
		 */
		devmate_tick();

		/*
		 * Update the variable only if 10 seconds have passed.
		 */
		seconds = SEC_TIMER - starttime;
		if (((seconds % 10) == 0) && (seconds > oldseconds)) {
			oldseconds = seconds;
			/*
			 * Update the variable to the DeviceMate.  Variables are not
			 * automatically updated.  This allows you to choose when the
			 * variables' values should be pushed to the DeviceMate.
			 */
			devmate_var_update("seconds");
			/*
			 * Although the other variables are not changing, you may wish
			 * to periodically update them, as well.  If the DeviceMate
			 * loses power, and the target processor does not, then the
			 * DeviceMate will forget the variables you have defined.  By
			 * updating them again, the DeviceMate will learn of these
			 * variables after any power failures.
			 */
			devmate_var_update("string");
			devmate_var_update("float");
			
			floatvar += 3.14159;
			if (strvar[0] == 'G')
				strcpy(strvar, "Bad bunny.");
			else
				strcpy(strvar, "Good bunny.");
		}
	}
}
