📄 maindemo.c
字号:
while(BusyUART());
WriteUART('.');
itoa(IPVal->v[2], IPDigit);
putsUART(IPDigit);
#ifdef USE_LCD
for(i=0; i < strlen(IPDigit); i++)
{
LCDText[LCDPos++] = IPDigit[i];
}
LCDText[LCDPos++] = '.';
#endif
while(BusyUART());
WriteUART('.');
itoa(IPVal->v[3], IPDigit);
putsUART(IPDigit);
#ifdef USE_LCD
for(i=0; i < strlen(IPDigit); i++)
{
LCDText[LCDPos++] = IPDigit[i];
}
while(LCDPos < 32)
{
LCDText[LCDPos++] = ' ';
}
LCDUpdate();
#endif
while(BusyUART());
}
static char AN0String[8];
//static char AN1String[8] = "";
static void ProcessIO(void)
{
#ifdef __C30__
// Note: floats and sprintf uses a lot of program memory/CPU cycles, so it's commented out
// float Temperature;
//
// // Convert temperature result into ASCII string
// Temperature = ((float)(ADC1BUF0)*(3.3/1024.)-0.500)*100.;
// sprintf(AN1String, "%3.1f癈", Temperature);
// Convert potentiometer result into ASCII string
itoa((unsigned)ADC1BUF0, AN0String);
#else
// AN0 should already be set up as an analog input
ADCON0bits.GO = 1;
// Wait until A/D conversion is done
while(ADCON0bits.GO);
// AD converter errata work around (ex: PIC18F87J10 A2)
#if !defined(__18F452)
PRODL = ADCON2;
ADCON2bits.ADCS0 = 1;
ADCON2bits.ADCS1 = 1;
ADCON2 = PRODL;
#endif
// Convert 10-bit value into ASCII string
itoa(*((WORD*)(&ADRESL)), AN0String);
#endif
}
// CGI Command Codes
#define CGI_CMD_DIGOUT (0)
#define CGI_CMD_LCDOUT (1)
#define CGI_CMD_RECONFIG (2)
// CGI Variable codes. - There could be 00h-FFh variables.
// NOTE: When specifying variables in your dynamic pages (.cgi),
// use the hexadecimal numbering scheme and always zero pad it
// to be exactly two characters. Eg: "%04", "%2C"; not "%4" or "%02C"
#define VAR_LED0 (0x00)
#define VAR_LED1 (0x01)
#define VAR_LED2 (0x10)
#define VAR_LED3 (0x11)
#define VAR_LED4 (0x12)
#define VAR_LED5 (0x13)
#define VAR_LED6 (0x14)
#define VAR_LED7 (0x15)
#define VAR_ANAIN_AN0 (0x02)
#define VAR_ANAIN_AN1 (0x03)
#define VAR_DIGIN0 (0x04) // Button0 on Explorer16
#define VAR_DIGIN1 (0x0D) // Button1 on Explorer16
#define VAR_DIGIN2 (0x0E) // Button2 on Explorer16
#define VAR_DIGIN3 (0x0F) // Button3 on Explorer16
#define VAR_STACK_VERSION (0x16)
#define VAR_STACK_DATE (0x17)
#define VAR_STROUT_LCD (0x05)
#define VAR_MAC_ADDRESS (0x06)
#define VAR_SERIAL_NUMBER (0x07)
#define VAR_IP_ADDRESS (0x08)
#define VAR_SUBNET_MASK (0x09)
#define VAR_GATEWAY_ADDRESS (0x0A)
#define VAR_DHCP (0x0B) // Use this variable when the web page is updating us
#define VAR_DHCP_TRUE (0x0B) // Use this variable when we are generating the web page
#define VAR_DHCP_FALSE (0x0C) // Use this variable when we are generating the web page
// CGI Command codes (CGI_CMD_DIGOUT).
// Should be a one digit numerical value
#define CMD_LED1 (0x0)
#define CMD_LED2 (0x1)
/*********************************************************************
* Function: void HTTPExecCmd(BYTE** argv, BYTE argc)
*
* PreCondition: None
*
* Input: argv - List of arguments
* argc - Argument count.
*
* Output: None
*
* Side Effects: None
*
* Overview: This function is a "callback" from HTTPServer
* task. Whenever a remote node performs
* interactive task on page that was served,
* HTTPServer calls this functions with action
* arguments info.
* Main application should interpret this argument
* and act accordingly.
*
* Following is the format of argv:
* If HTTP action was : thank.htm?name=Joe&age=25
* argv[0] => thank.htm
* argv[1] => name
* argv[2] => Joe
* argv[3] => age
* argv[4] => 25
*
* Use argv[0] as a command identifier and rests
* of the items as command arguments.
*
* Note: THIS IS AN EXAMPLE CALLBACK.
********************************************************************/
#if defined(STACK_USE_HTTP_SERVER)
ROM char COMMANDS_OK_PAGE[] = "INDEX.CGI";
ROM char CONFIG_UPDATE_PAGE[] = "CONFIG.CGI";
ROM char CMD_UNKNOWN_PAGE[] = "INDEX.CGI";
// Copy string with NULL termination.
#define COMMANDS_OK_PAGE_LEN (sizeof(COMMANDS_OK_PAGE))
#define CONFIG_UPDATE_PAGE_LEN (sizeof(CONFIG_UPDATE_PAGE))
#define CMD_UNKNOWN_PAGE_LEN (sizeof(CMD_UNKNOWN_PAGE))
void HTTPExecCmd(BYTE** argv, BYTE argc)
{
BYTE command;
BYTE var;
#ifdef ENABLE_REMOTE_CONFIG
BYTE CurrentArg;
WORD_VAL TmpWord;
#endif
/*
* Design your pages such that they contain command code
* as a one character numerical value.
* Being a one character numerical value greatly simplifies
* the job.
*/
command = argv[0][0] - '0';
/*
* Find out the cgi file name and interpret parameters
* accordingly
*/
switch(command)
{
case CGI_CMD_DIGOUT: // ACTION=0
/*
* Identify the parameters.
* Compare it in upper case format.
*/
var = argv[1][0] - '0';
switch(var)
{
case CMD_LED1: // NAME=0
// Toggle LED.
LED1_IO ^= 1;
break;
case CMD_LED2: // NAME=1
// Toggle LED.
LED2_IO ^= 1;
break;
}
memcpypgm2ram((void*)argv[0], (ROM void*)COMMANDS_OK_PAGE, COMMANDS_OK_PAGE_LEN);
break;
#if defined(USE_LCD)
case CGI_CMD_LCDOUT: // ACTION=1
if(argc > 2) // Text provided in argv[2]
{
// Write 32 received characters or less to LCDText
if(strlen(argv[2]) < 32)
{
memset(LCDText, ' ', 32);
strcpy(LCDText, argv[2]);
}
else
{
memcpy(LCDText, (void*)argv[2], 32);
}
// Write LCDText to the LCD
LCDUpdate();
}
else // No text provided
{
LCDErase();
}
memcpypgm2ram((void*)argv[0], (ROM void*)COMMANDS_OK_PAGE, COMMANDS_OK_PAGE_LEN);
break;
#endif
#if ENABLE_REMOTE_CONFIG
// Possibly useful code for remotely reconfiguring the board through
// HTTP
case CGI_CMD_RECONFIG: // ACTION=2
// Loop through all variables that we've been given
CurrentArg = 1;
while(argc > CurrentArg)
{
// Get the variable identifier (HTML "name"), and
// increment to the variable's value
TmpWord.byte.MSB = argv[CurrentArg][0];
TmpWord.byte.LSB = argv[CurrentArg++][1];
var = hexatob(TmpWord);
// Make sure the variable's value exists
if(CurrentArg >= argc)
break;
// Take action with this variable/value
switch(var)
{
case VAR_SERIAL_NUMBER:
AppConfig.SerialNumber.Val = atoi(argv[CurrentArg]);
AppConfig.MyMACAddr.v[4] = AppConfig.SerialNumber.byte.MSB;
AppConfig.MyMACAddr.v[5] = AppConfig.SerialNumber.byte.LSB;
break;
case VAR_IP_ADDRESS:
case VAR_SUBNET_MASK:
case VAR_GATEWAY_ADDRESS:
{
DWORD TmpAddr;
// Convert the returned value to the 4 octect
// binary representation
if(!StringToIPAddress(argv[CurrentArg], (IP_ADDR*)&TmpAddr))
break;
// Reconfigure the App to use the new values
if(var == VAR_IP_ADDRESS)
{
// Cause the IP address to be rebroadcast
// through Announce.c or the RS232 port since
// we now have a new IP address
if(TmpAddr != *(DWORD*)&AppConfig.MyIPAddr)
DHCPBindCount++;
// Set the new address
memcpy((void*)&AppConfig.MyIPAddr, (void*)&TmpAddr, sizeof(AppConfig.MyIPAddr));
}
else if(var == VAR_SUBNET_MASK)
memcpy((void*)&AppConfig.MyMask, (void*)&TmpAddr, sizeof(AppConfig.MyMask));
else if(var == VAR_SUBNET_MASK)
memcpy((void*)&AppConfig.MyGateway, (void*)&TmpAddr, sizeof(AppConfig.MyGateway));
}
break;
case VAR_DHCP:
if(AppConfig.Flags.bIsDHCPEnabled)
{
if(!(argv[CurrentArg][0]-'0'))
{
AppConfig.Flags.bIsDHCPEnabled = FALSE;
}
}
else
{
if(argv[CurrentArg][0]-'0')
{
AppConfig.MyIPAddr.Val = 0x00000000ul;
AppConfig.Flags.bIsDHCPEnabled = TRUE;
AppConfig.Flags.bInConfigMode = TRUE;
DHCPReset();
}
}
break;
}
// Advance to the next variable (if present)
CurrentArg++;
}
// Save any changes to non-volatile memory
SaveAppConfig();
// Return the same CONFIG.CGI file as a result.
memcpypgm2ram((void*)argv[0],
(ROM void*)CONFIG_UPDATE_PAGE, CONFIG_UPDATE_PAGE_LEN);
break;
#endif
default:
memcpypgm2ram((void*)argv[0], (ROM void*)COMMANDS_OK_PAGE, COMMANDS_OK_PAGE_LEN);
break;
}
}
#endif
/*********************************************************************
* Function: WORD HTTPGetVar(BYTE var, WORD ref, BYTE* val)
*
* PreCondition: None
*
* Input: var - Variable Identifier
* ref - Current callback reference with
* respect to 'var' variable.
* val - Buffer for value storage.
*
* Output: Variable reference as required by application.
*
* Side Effects: None
*
* Overview: This is a callback function from HTTPServer() to
* main application.
* Whenever a variable substitution is required
* on any html pages, HTTPServer calls this function
* 8-bit variable identifier, variable reference,
* which indicates whether this is a first call or
* not. Application should return one character
* at a time as a variable value.
*
* Note: Since this function only allows one character
* to be returned at a time as part of variable
* value, HTTPServer() calls this function
* multiple times until main application indicates
* that there is no more value left for this
* variable.
* On begining, HTTPGetVar() is called with
* ref = HTTP_START_OF_VAR to indicate that
* this is a first call. Application should
* use this reference to start the variable value
* extraction and return updated reference. If
* there is no more values left for this variable
* application should send HTTP_END_OF_VAR. If
* there are any bytes to send, application should
* return other than HTTP_START_OF_VAR and
* HTTP_END_OF_VAR reference.
*
* THIS IS AN EXAMPLE CALLBACK.
* MODIFY THIS AS PER YOUR REQUIREMENTS.
********************************************************************/
#if defined(STACK_USE_HTTP_SERVER)
WORD HTTPGetVar(BYTE var, WORD ref, BYTE* val)
{
// Temporary variables designated for storage of a whole return
// result to simplify logic needed since one byte must be returned
// at a time.
static BYTE VarString[20];
#if ENABLE_REMOTE_CONFIG
static BYTE VarStringLen;
BYTE *VarStringPtr;
BYTE i;
BYTE *DataSource;
#endif
// Identify variable
switch(var)
{
case VAR_LED0:
*val = LED0_IO ? '1':'0';
break;
case VAR_LED1:
*val = LED1_IO ? '1':'0';
break;
case VAR_LED2:
*val = LED2_IO ? '1':'0';
break;
case VAR_LED3:
*val = LED3_IO ? '1':'0';
break;
case VAR_LED4:
*val = LED4_IO ? '1':'0';
break;
case VAR_LED5:
*val = LED5_IO ? '1':'0';
break;
case VAR_LED6:
*val = LED6_IO ? '1':'0';
break;
case VAR_LED7:
*val = LED7_IO ? '1':'0';
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -