📄 main.c
字号:
#include <avr/io.h>#include <avr/interrupt.h>#include <stdlib.h>#include <stdio.h>#include <string.h>#include <avr/pgmspace.h>#include "ip_arp_udp_tcp.h"#include "enc28j60.h"#include "timeout.h"#include "avr_compat.h"#include "net.h"// Please modify the following two lines.// Attention! MAC and IP have to be unique in your local area network.// You can not have the same numbers in two devices. If a conflict appears// change it!static uint8_t mymac[6] = {0x54, 0x55, 0x58, 0x10, 0x00, 0x24};static uint8_t myip[4] = {10, 38, 243, 240};#define MY_IP_STR "10.38.243.240"// Your IP.uint8_t yip[4];// Base url (you can put a DNS name instead of an IP address if you have// a DNS server (baseurl must end in "/").static char baseurl[] = "http://10.38.243.240/";// Listen port for tcp/www (max range 1-254).static uint16_t mywwwport = 80;#define BUFFER_SIZE 800unsigned char buf[BUFFER_SIZE + 1];uint8_t page = 1;uint8_t num = 0;uint16_t milisec = 0;uint8_t secunde = 0, minute = 0, ore = 0;int8_t analyse_get_url(char *str){ uint8_t i = 0; while (*str && i < 10 && *str > ',' && *str < '{') { if (*str == '/') { page = 1; ++str; break; } else if (*str == '?') { num = 0; page = 1; ++str; break; } ++i; ++str; } while (*str) { if (*str == 'c') { ++str; page = 0; } else if (*str == 'f' || *str == 's') { page = 0; break; } else if (*str == 't') { page = 2; break; } else if (*str == 'l') { page = 3; break; } else if (*str == 'b') { page = 4; break; } else if (*str == 'h') { page = 5; break; } else if (*str == 'd') { page = 6; break; } else { break; } num |= (1 << ((*str-0x30)-1)); str += 4; } return num;}uint16_t print_webpage(uint8_t *buf, uint8_t on_off){ char buffer[15]; uint16_t plen; plen = fill_tcp_data_p(buf, 0, PSTR("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n")); if (page == 0) { plen = fill_tcp_data_p(buf, plen, PSTR("<HTML><BODY bgcolor=LightCyan>\n")); plen = fill_tcp_data_p(buf, plen, PSTR("<FORM action=\"http://"MY_IP_STR"\" method=\"get\">")); plen = fill_tcp_data_p(buf, plen, PSTR("<input type=\"checkbox\" name=\"c1\" value=\"1\"")); if (on_off & 0x01) plen = fill_tcp_data_p(buf, plen, PSTR(" checked")); plen = fill_tcp_data_p(buf, plen, PSTR(">LED0<br>")); plen = fill_tcp_data_p(buf, plen, PSTR("<input type=\"checkbox\" name=\"c2\" value=\"1\"")); if (on_off & 0x02) plen = fill_tcp_data_p(buf, plen, PSTR(" checked")); plen = fill_tcp_data_p(buf, plen, PSTR(">LED1<br>")); plen = fill_tcp_data_p(buf, plen, PSTR("<input type=\"checkbox\" name=\"c3\" value=\"1\"")); if (on_off & 0x04) plen = fill_tcp_data_p(buf, plen, PSTR(" checked")); plen = fill_tcp_data_p(buf, plen, PSTR(">LED2<br>")); plen = fill_tcp_data_p(buf, plen, PSTR("<input type=\"checkbox\" name=\"c4\" value=\"1\"")); if (on_off & 0x08) plen = fill_tcp_data_p(buf, plen, PSTR(" checked")); plen = fill_tcp_data_p(buf, plen, PSTR(">LED3<br>")); plen = fill_tcp_data_p(buf, plen, PSTR("<input type=\"checkbox\" name=\"c5\" value=\"1\"")); if (on_off & 0x10) plen = fill_tcp_data_p(buf, plen, PSTR(" checked")); plen = fill_tcp_data_p(buf, plen, PSTR(">LED4<br>")); plen = fill_tcp_data_p(buf, plen, PSTR("<input type=\"checkbox\" name=\"c6\" value=\"1\"")); if (on_off & 0x20) plen = fill_tcp_data_p(buf, plen, PSTR(" checked")); plen = fill_tcp_data_p(buf, plen, PSTR(">LED5<br>")); plen = fill_tcp_data_p(buf, plen, PSTR("<input type=\"checkbox\" name=\"c7\" value=\"1\"")); if (on_off & 0x40) plen = fill_tcp_data_p(buf, plen, PSTR(" checked")); plen = fill_tcp_data_p(buf, plen, PSTR(">LED6<br>")); plen = fill_tcp_data_p(buf, plen, PSTR("<input type=\"checkbox\" name=\"c8\" value=\"1\"")); if (on_off & 0x80) plen = fill_tcp_data_p(buf, plen, PSTR(" checked")); plen = fill_tcp_data_p(buf, plen, PSTR(">LED7<br>")); plen = fill_tcp_data_p(buf, plen, PSTR("<br><INPUT type=\"submit\" name=\"submit\" value=\"OK\">")); plen = fill_tcp_data_p(buf, plen, PSTR("</FORM>\n</BODY>\n</HTML>\n")); } else if (page == 2) { plen = fill_tcp_data_p(buf, plen, PSTR("<html><head><META HTTP-EQUIV=REFRESH CONTENT=10></head>" "<body bgcolor=aqua style=\"text-align: center\">Time up<br>")); sprintf(buffer, "%02d:%02d:%02d", ore, minute, secunde); plen = fill_tcp_data(buf, plen, buffer); plen = fill_tcp_data_p(buf, plen, PSTR("</body></html>")); } else if (page == 3) { plen = fill_tcp_data_p(buf, plen, PSTR("<html><body><font color=navy><span style=\"font-family: Arial\">Menu<br><br><hr>")); plen = fill_tcp_data_p(buf, plen, PSTR("<a href =\"http://"MY_IP_STR"/sec/h.htm\" target =\"content\">Home</a><br><hr>")); plen = fill_tcp_data_p(buf, plen, PSTR("<a href =\"http://"MY_IP_STR"/sec/f.htm\" target =\"content\">Leds</a><br><hr>")); plen = fill_tcp_data_p(buf, plen, PSTR("<a href =\"http://"MY_IP_STR"/sec/d.htm\" target =\"content\">Time</a><hr>")); plen = fill_tcp_data_p(buf, plen, PSTR("</font></body></html>")); } else if (page == 4) { plen = fill_tcp_data_p(buf, plen, PSTR("<html><body bgcolor=navy style=\"text-align: right\">")); plen = fill_tcp_data_p(buf, plen, PSTR("<font color=white size=20><span style=\"font-family: Century Gothic\">webServer1.0</span></font>")); plen = fill_tcp_data_p(buf, plen, PSTR("</body></html>")); } else if (page == 5) { plen = fill_tcp_data_p(buf, plen, PSTR("<html><body bgcolor=LightCyan style=\"text-align: center\">")); plen = fill_tcp_data_p(buf, plen, PSTR("<font color=navy size=12><span style=\"font-family: Arial\"><br><br>Server is up and running.<br>Server IP: ")); sprintf(buffer, "%d.%d.%d.%d", myip[0], myip[1], myip[2], myip[3]); plen = fill_tcp_data(buf, plen, buffer); plen = fill_tcp_data_p(buf, plen, PSTR("<br>Your IP: ")); sprintf(buffer, "%d.%d.%d.%d", yip[0], yip[1], yip[2], yip[3]); plen = fill_tcp_data(buf, plen, buffer); plen = fill_tcp_data_p(buf, plen, PSTR("</span></font></body></html>")); } else if (page == 6) { plen = fill_tcp_data_p(buf, plen, PSTR("<HTML><script language=\"JavaScript\"> var clockID = 0;")); plen = fill_tcp_data_p(buf, plen, PSTR("function UpdateClock(){if(clockID){clearTimeout(clockID);clockID = 0;} var tDate = new Date();")); plen = fill_tcp_data_p(buf, plen, PSTR(" document.theClock.theTime.value = \"\"+ tDate.getHours()+\":\"+tDate.getMinutes()+\":\"+tDate.getSeconds();")); plen = fill_tcp_data_p(buf, plen, PSTR(" clockID = setTimeout(\"UpdateClock()\", 1000);}")); plen = fill_tcp_data_p(buf, plen, PSTR("function StartClock() {clockID = setTimeout(\"UpdateClock()\", 500);}")); plen = fill_tcp_data_p(buf, plen, PSTR("function KillClock() {if(clockID){clearTimeout(clockID);clockID = 0;}}")); plen = fill_tcp_data_p(buf, plen, PSTR("</script><body onload=\"StartClock()\" onunload=\"KillClock()\" bgcolor=LightCyan><center><form name=\"theClock\">"));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -