⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 devicecontroller.c

📁 embedded_ethernet_complete_code_rabbit
💻 C
字号:
// Device Controller demo.
// The Rabbit hosts a Web page that displays two LEDs and two control buttons.
// Clicking a button toggles the corresponding LED on the module and redirects
// the client's browser to an updated page.
// On the Rabbit 3200's prototyping board, the LEDs are DS1 and DS2, controlled
// by Port G, pins 6 and 7.

// Select a network configuration from \lib\tcpip\tcp_config.lib
// in the Dynamic C distribution.
#define TCPCONFIG 1

// Reduce the TCP socket buffer size to allow more connections.
// The default is 4096.
#define TCP_BUF_SIZE 2048

// Set the number of HTTP servers.
// Provide one per page or image for improved performance.
#define HTTP_MAXSERVERS 4

// The IP address clients will use to access the Web server.
// _PRIMARY_STATIC_IP is defined in tcp_config.lib.
#define REDIRECTHOST _PRIMARY_STATIC_IP

// In the cgi_redirectto function, REDIRECTTO specifies the Web page clients
// are redirected to after clicking a button on the Web page.
// For this application, clients are redirected to the home page, which
// is updated with the current states of the LEDs.
#define REDIRECTTO 		"http://" REDIRECTHOST "/index.shtml"

// All C functions not declared as root go to extended memory.
#memmap xmem

// The dcrtcp library supports IP and TCP; the http library supports HTTP functions.
#use "dcrtcp.lib"
#use "http.lib"

// ximport loads a file from the development PC into the Rabbit's flash memory.
// The symbol (index_html, etc.) is a macro that gives the file's length and
// physical address in the Rabbit's memory.

// YOU MUST CHANGE THESE PATHS to match the locations of the files in your system.
// OK to use forward or back slashes as separators.
#ximport "c:\embedded_ethernet_code\rabbit\devicecontroller\index.shtml"    index_html
#ximport "c:\embedded_ethernet_code/rabbit\devicecontroller\ledon.gif"      ledon_gif
#ximport "c:\embedded_ethernet_code/rabbit\devicecontroller\ledoff.gif"     ledoff_gif
#ximport "c:\embedded_ethernet_code/rabbit\devicecontroller\button.gif"     button_gif

// The HttpType structure names the handler to use with specific file extensions.
// The first item in the structure is the handler used when a request
// doesn't name a page or other resource.
// A request for a resource with the .shtml extension uses shtml_handler, which
// handles requests for HTML pages that include SSI directives.
// Requests with .html, .cgi, or .gif extensions use the default handler, which
// serves the resource unchanged.
const HttpType http_types[] =
{
   { ".shtml", "text/html", shtml_handler},
   { ".html", "text/html", NULL},
   { ".cgi", "", NULL},
   { ".gif", "image/gif", NULL}
};

// The file names of the images for lit and unlit LEDs:
const char ledon_image[] = "ledon.gif";
const char ledoff_image[] = "ledoff.gif";

// led1 and led2 each contain a file name for an image
// that matches an LED's state.
char led1_image[15];
char led2_image[15];

// led1state and led2state hold either "on" or "off".
char led1_state[4];
char led2_state[4];

// For each LED, read the state of the port bit.
// Then toggle the bit to turn the LED on or off,
// store the appropriate file name in led1_image or led2_image,
// and store "on" or "off" as appropriate in led1_state and led2_state.
// These functions are called when a user clicks a button on the Web page.
int led1toggle(HttpState* state) {

	if (BitRdPortI(PGDR, 6) == 0) {

		// When the bit is 0, the LED is on, so switch it off.
		BitWrPortI(PGDR, &PGDRShadow, 1, 6);
	   strcpy(led1_image,ledoff_image);
	   strcpy(led1_state, "off");
	   }
	else {

		// When the bit is 1, the LED is off, so switch it on.
		BitWrPortI(PGDR, &PGDRShadow, 0, 6);
		strcpy(led1_image,ledon_image);
	   strcpy(led1_state, "on");
		}

	// Return an HTTP response that advises the client to request an updated Web page.
   cgi_redirectto(state,REDIRECTTO);
   return 0;
} // end led1toggle

int led2toggle(HttpState* state) {
	if (BitRdPortI(PGDR, 7) == 0) {

		// When the bit is 0, the LED is on, so switch it off.
		BitWrPortI(PGDR, &PGDRShadow, 1, 7);
	   strcpy(led2_image,ledoff_image);
  	   strcpy(led2_state, "off");
	   }
	else {

		// When the bit is 1, the LED is off, so switch it on.
		BitWrPortI(PGDR, &PGDRShadow, 0, 7);
		strcpy(led2_image,ledon_image);
	   strcpy(led2_state, "on");
		}

	// Return an HTTP response that advises the client to request an updated Web page.
   cgi_redirectto(state,REDIRECTTO);
   return 0;
} // end led2toggle

// The http_flashspec structure contains information about the files, variables,
// and structures the Web server can access.

// On receiving a request for the file index.shtml or a request with no file
// name specified, the Web server serves the page that the index_html macro
// points to.

// This Web server can also serve three image files and can access four variables
// that contain file names or text that indicates an LED's state.

// On receiving a request for /led1toggle.cgi or /led2toggle.cgi, the Web server
// executes the function led1toggle or led2toggle.
const HttpSpec http_flashspec[] =
{
   { HTTPSPEC_FILE,  "/",              index_html,    NULL, 0, NULL, NULL},
   { HTTPSPEC_FILE,  "/index.shtml",   index_html,    NULL, 0, NULL, NULL},

   { HTTPSPEC_FILE,  "/ledon.gif",  ledon_gif,     NULL, 0, NULL, NULL},
   { HTTPSPEC_FILE,  "/ledoff.gif", ledoff_gif,    NULL, 0, NULL, NULL},
   { HTTPSPEC_FILE,  "/button.gif",    button_gif,    NULL, 0, NULL, NULL},

   { HTTPSPEC_VARIABLE, "led1_image",  0,    led1_image, PTR16,   "%s", NULL},
   { HTTPSPEC_VARIABLE, "led2_image",  0,    led2_image, PTR16,   "%s", NULL},

   { HTTPSPEC_VARIABLE, "led1_state",  0,    led1_state, PTR16,   "%s", NULL},
   { HTTPSPEC_VARIABLE, "led2_state",  0,    led2_state, PTR16,   "%s", NULL},

   { HTTPSPEC_FUNCTION, "/led1toggle.cgi",   0, led1toggle, 0, NULL, NULL},
   { HTTPSPEC_FUNCTION, "/led2toggle.cgi",   0, led2toggle, 0, NULL, NULL},
};

main() {

	// Initialize variables and process HTTP requests.
 	// On the RCM3200 Port G bits 6 and 7 control the LEDs on the prototyping board.
	// Setting bits 6 and 7 in PGDDR configures the bits as outputs.
	WrPortI(PGDDR, NULL, 0xC0);

	// Read each bit and set the appropriate image and text to display on
   // the Web page.
	if (BitRdPortI(PGDR, 6) == 0) {
	   strcpy(led1_image,ledon_image);
  	   strcpy(led1_state, "on");
  	   }
	else {
		strcpy(led1_image,ledoff_image);
	   strcpy(led1_state, "off");
	   }

	if (BitRdPortI(PGDR, 7) == 0) {
	   strcpy(led2_image,ledon_image);
  	   strcpy(led2_state, "on");
  	   }
	else {
		strcpy(led2_image,ledoff_image);
	   strcpy(led2_state, "off");
	   }

	// Initialize the TCP/IP stack and Web server.
   sock_init();
   http_init();

   // For improved performance, reserve port 80 for the Web server.
   tcp_reserveport(80);

   while (1) {

   	// An endless loop handles HTTP requests.
      http_handler();

      // Can place code to perform other tasks here.
   }
} // end main


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -