📄 template.c
字号:
/* template.c */#include <stdio.h>#include <string.h>#include <stdlib.h>#include "cgivars.h"#include "htmllib.h"#include "io_lib.h"// should be set if CGI program is executed on a test-pc for testing purpose:// must be commented if build is for target-hardware!//#define TEST// for debug output://#define DEBUG// if changing device names is wished://#ifdef DEVICE_NAME_CHANGEABLE#define DEVICE_NAME_LEN 40/*TODO:------ make device names changeable (set appropriate debug-constant and change constant names in html-form to the given variables, make these variable global to template.c and io_lib.c. now a plausibility-check is necessary to catch invalid device names)- enable also hex-values in input-boxes for LED-values- plausibility-check for input values (forbid special characters etc.), maybe this is done very easily in javascript (execute javascript which does the plausibility-check and then submit the form if the values are ok)*/int template_page(char **postvars, char **getvars, int form_method) { int i; // lokale variablen für eingabefelder char mode[8] = "\0";#ifdef DEVICE_NAME_CHANGEABLE char leds_green__device[DEVICE_NAME_LEN] = LEDS_GREEN; char leds_red__device[DEVICE_NAME_LEN] = LEDS_RED; char seg7__device[DEVICE_NAME_LEN] = SEG7; char lcd__device[DEVICE_NAME_LEN] = LCD; char switches__device[DEVICE_NAME_LEN] = SWITCHES; char buttons__device[DEVICE_NAME_LEN] = BUTTONS;#endif char leds_green__value[STRING_LEN] = "0"; char leds_red__value[STRING_LEN] = "0"; char seg7__value[STRING_LEN] = "0"; char lcd__value[32] = "0"; char switches__value[STRING_LEN] = "0"; char buttons__value[STRING_LEN] = "0";#ifdef DEBUG // lists all variables which are coming from GET request for (i=0; getvars[i]; i+= 2) printf("<li>DEBUG: [%s] = [%s]\n", getvars[i], getvars[i+1]);#endif // if we got a POST request: copy all contents to local variables if(form_method == POST) {#ifdef TEST printf("TEST is set, so no I/O interaction is possible");#endif for (i=0; postvars[i]; i+= 2) { if(!strcmp(postvars[i],"leds_green__value")) strcpy(leds_green__value,postvars[i+1]); if(!strcmp(postvars[i],"leds_red__value")) strcpy(leds_red__value,postvars[i+1]); if(!strcmp(postvars[i],"seg7__value")) strcpy(seg7__value,postvars[i+1]); if(!strcmp(postvars[i],"lcd__value")) strcpy(lcd__value,postvars[i+1]); if(!strcmp(postvars[i],"mode")) strcpy(mode,postvars[i+1]); // if changing device names if wished:#ifdef DEVICE_NAME_CHANGEABLE if(!strcmp(postvars[i],"leds_green__device")) strcpy(leds_green__device,postvars[i+1]); if(!strcmp(postvars[i],"leds_red__device")) strcpy(leds_red__device,postvars[i]); if(!strcmp(postvars[i],"seg7__device")) strcpy(seg7__device,postvars[i]); if(!strcmp(postvars[i],"lcd__device")) strcpy(lcd__device,postvars[i]); if(!strcmp(postvars[i],"buttons__device")) strcpy(buttons__device,postvars[i]); if(!strcmp(postvars[i],"switches__device")) strcpy(switches__device,postvars[i]);#endif#ifdef DEBUG // lists all variables which are coming from POST request printf("<li>DEBUG: [%s] = [%s]\n", postvars[i], postvars[i+1]);#endif }#ifndef TEST // write values to hardware if(!strcmp(mode,"write")) { write_dev(leds_red, leds_red__value); write_dev(leds_green, leds_green__value); write_dev(seg7, seg7__value); if(i = write_lcd(lcd__value)) printf("ERROR: write_lcd error code %i\n",i); } // read values from hardware even in write mode if(!strcmp(mode,"read") || !strcmp(mode,"write")) { //ltoa(buttons__value,read_dev(buttons)); //ltoa(switches__value,read_dev(switches)); sprintf(buttons__value, "%d", read_dev(buttons)); sprintf(switches__value, "%ld", read_dev(switches)); }#endif } // layout table for device inputs printf("<fieldset>\n"); printf("<legend>Write</legend>\n"); printf("<FORM ACTION=\"%s\" METHOD=POST>\n", "/cgi-bin/cgi_demo"); printf("<table border=\"0\" width=\"1024\">\n"); printf("<tr>\n"); printf(" <td width=\"200\"></td>\n"); printf(" <td width=\"350\"><strong>Configuration of device files</strong></td>\n"); printf(" <td><strong>Writeable values</strong></td>\n"); printf("</tr>\n"); printf("</table>\n"); printf("<table border=\"0\" width=\"1024\">\n"); printf("<tr>\n"); printf(" <td width=\"200\">Green LEDs:</td>\n"); printf(" <td width=\"350\"><input name=\"leds_green__device\" type=\"text\" size=\"30\" maxlength=\"50\" value=\"%s\" readonly disabled></td>\n", LEDS_GREEN); printf(" <td><input name=\"leds_green__value\" type=\"text\" size=\"20\" maxlength=\"20\" value=\"%s\"> (max. 511)</td>\n", leds_green__value); printf("</tr>\n"); printf("<tr>\n"); printf(" <td>Red LEDs:</td>\n"); printf(" <td><input name=\"leds_red__device\" type=\"text\" size=\"30\" maxlength=\"50\" value=\"%s\" readonly disabled></td>\n", LEDS_RED); printf(" <td><input name=\"leds_red__value\" type=\"text\" size=\"20\" maxlength=\"20\" value=\"%s\"> (max. 262143)</td>\n", leds_red__value); printf("</tr>\n"); printf("<tr>\n"); printf(" <td>7-segment Display:</td>\n"); printf(" <td><input name=\"seg7__device\" type=\"text\" size=\"30\" maxlength=\"50\" value=\"%s\" readonly disabled></td>\n", SEG7); printf(" <td><input name=\"seg7__value\" type=\"text\" size=\"8\" maxlength=\"8\" value=\"%s\"> (max. 8 characters, hex values)</td>\n", seg7__value); printf("</tr>\n"); printf("<tr>\n"); printf(" <td>LC-Display:</td>\n"); printf(" <td><input name=\"lcd__device\" type=\"text\" size=\"30\" maxlength=\"50\" value=\"%s\" readonly disabled></td>\n", LCD); printf(" <td><input name=\"lcd__value\" type=\"text\" size=\"32\" maxlength=\"32\" value=\"%s\"> (max. 32 characters)</td>\n", lcd__value); printf("</tr>\n"); printf("</table>\n"); printf("<input type=\"hidden\" name=\"mode\" value=0>\n"); printf("<INPUT TYPE=\"submit\" VALUE=\"Submit\" onclick=\"this.form.mode.value='write'\"><INPUT TYPE=reset VALUE=\"Reset\">\n"); printf("</fieldset>\n"); // layout table for outputs printf("<fieldset>\n"); printf("<legend>Read</legend>\n"); printf("<table border=\"0\" width=\"1024\">\n"); printf("<tr>\n"); printf(" <td width=\"200\"></td>\n"); printf(" <td width=\"350\"><strong>Configuration of device files</strong></td>\n"); printf(" <td><strong>Readable values</strong></td>\n"); printf("</tr>\n"); printf("</table>\n"); printf("<table border=\"0\" width=\"1024\">\n"); printf("<tr>\n"); printf(" <td width=\"200\">Switches:</td>\n"); printf(" <td width=\"350\"><input name=\"switches__device\" type=\"text\" size=\"30\" maxlength=\"50\" value=\"%s\" readonly disabled></td>\n", SWITCHES); printf(" <td><input name=\"switches__value\" type=\"text\" size=\"10\" maxlength=\"10\" value=\"%s\" readonly disabled></td>\n", switches__value); printf("</tr>\n"); printf("<tr>\n"); printf(" <td>Buttons:</td>\n"); printf(" <td><input name=\"buttons__device\" type=\"text\" size=\"30\" maxlength=\"50\" value=\"%s\" readonly disabled></td>\n", BUTTONS); printf(" <td><input name=\"buttons__value\" type=\"text\" size=\"10\" maxlength=\"10\" value=\"%s\" readonly disabled></td>\n", buttons__value); printf("</tr>\n"); printf("</table>\n"); printf("<INPUT TYPE=submit VALUE=\"Refresh\" onclick=\"this.form.mode.value='read'\">\n"); printf("</FORM>\n"); printf("</fieldset>\n"); return 0; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -