📄 realtime_webpage.c
字号:
// The Rabbit serves a Web page that displays how long the current application
// has been running.
// The application uses the server-side-include (SSI) #echo directive to insert
// the current values of time variables on the page.
// This file and other embedded Ethernet and Internet code and resources are
// available from www.Lvr.com.
// Select a network configuration from \lib\tcpip\tcp_config.lib
// in the Dynamic C distribution.
#define TCPCONFIG 1
// 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) is a macro that gives the file's length and
// physical address in the Rabbit's memory.
// YOU MUST CHANGE THE PATH to match the location of the file in your system.
// OK to use forward or back slashes as separators.
#ximport "c:\embedded_ethernet_code\rabbit\realtimewebpage\index.shtml" index_html
// 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.
// This structure has just one handler, which handles HTML pages that include
// SSI directives.
const HttpType http_types[] = {
{ ".shtml", "text/html", shtml_handler},
};
// The time variables to display on the Web page.
unsigned long days;
unsigned long seconds;
unsigned long hours;
unsigned long minutes;
// The HttpSpec 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.
// The Web server can also access four variables that contain time information.
const HttpSpec http_flashspec[] =
{
{ HTTPSPEC_FILE, "/", index_html, NULL, 0, NULL, NULL},
{ HTTPSPEC_FILE, "/index.shtml", index_html, NULL, 0, NULL, NULL},
{ HTTPSPEC_VARIABLE, "days", 0, &days, INT32, "%d", NULL},
{ HTTPSPEC_VARIABLE, "hours", 0, &hours, INT32, "%d", NULL},
{ HTTPSPEC_VARIABLE, "minutes", 0, &minutes, INT32, "%d", NULL},
{ HTTPSPEC_VARIABLE, "seconds", 0, &seconds, INT32, "%d", NULL},
};
main() {
// Update the time variables periodically and wait for and respond to
// requests for the Web page.
unsigned long start_time;
unsigned long total_seconds;
// 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);
// Get an initial seconds count for measuring elapsed time.
start_time = SEC_TIMER;
// An infinite while loop upates the time variables once per second
// and calls http_handler to process network requests. Code to perform
// other tasks can be placed in the loop as well.
while (1) {
// http_handler must be called periodically to process HTTP requests.
http_handler();
costate {
// Wait DelaySec seconds between updates.
waitfor(DelaySec(1));
// Update the uptime value.
// Get the elapsed time in seconds since the program began running.
total_seconds = SEC_TIMER - start_time;
// For the number of days, divide by the number of seconds per day.
days = total_seconds /86400;
// For the number of hours excluding full days, divide the total hours
// by the number of hours per day and use the remainder.
hours = (total_seconds / 3600) % 24;
// For the number of minutes excluding full hours, divide the total minutes
// by the number of minutes per hour and use the remainder.
minutes = (total_seconds / 60) % 60;
// For the number of seconds excluding full minutes, divide the elapsed time
// by the number of seconds per minute and use the remainder.
seconds = total_seconds % 60;
} // end costate
// Can place code to perform other tasks here.
} // end while (1)
} // end main()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -