/************************HTML文件*********************/
- <html>
- <head>
- <meta http-equiv="content-type" content="text/html; charset=utf-8" />
- <title>LED测试</title>
- <script language="JavaScript">
- function loadLEDStatus() {
- var xmlhttp = null;
- if(window.XMLHttpRequest)
- xmlhttp = new XMLHttpRequest();
- else if(window.ActiveXObject)
- xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
- xmlhttp.open("GET", "/cgi-bin/getled.cgi", false);
- xmlhttp.setRequestHeader("If-Modified-Since", "0");
- xmlhttp.onreadystatechange=function() {
- if(xmlhttp.readyState != 4 || xmlhttp.status != 200)
- return;
- document.getElementById("myDiv").innerHTML = xmlhttp.responseText;
- var value = new Array();
- value = xmlhttp.responseText.split(",");
- //document.write(value);
- var i;
- for(i = 0; i < 3; i ) {
- var img = document.getElementById("led" i);
- if(value[i] == "1") {
- img.src = "led_on.jpg";
- } else {
- img.src = "led_off.jpg";
- }
- }
- }
- xmlhttp.send();
- }
- function switchLED(led,on_off) {
- var url="/cgi-bin/setled.cgi?" led;
- if(on_off) {
- url = url "=1";
- } else{
- url = url "=0";
- }
- var xmlhttp = null;
- if(window.XMLHttpRequest)
- xmlhttp = new XMLHttpRequest();
- else if(window.ActiveXObject)
- xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
-
- xmlhttp.open("GET", url, false);
- xmlhttp.setRequestHeader("If-Modified-Since", "0");
- //xmlhttp.onreadystatechange = function() {
- // if(xmlhttp.readyState != 4 || xmlhttp.status != 200)
- // return;
- // document.getElementById("myDiv").innerHTML = xmlhttp.responseText;
- //}
- xmlhttp.send();
- loadLEDStatus();
- }
- </script>
- </head>
- <body onLoad="loadLEDStatus();">
- <div id="myDiv">
- <h2>Let AJAX change this text</h2>
- </div>
-
- <form>
- <table align="center" summary="">
- <tr>
- <td colspan="3" align="center"><h1>LED 测试</h1></td>
- </tr>
- <tr>
- <td align="center">
- <img src="led_off.jpg" id="led0" width=30 height=30 alt="led1"/>
- </td>
- <td align="center"><img src="led_off.jpg" id="led1" width=30 height=30 alt="led2"/></td>
- <td align="center"><img src="led_off.jpg" id="led2" width=30 height=30 alt="led3"/></td>
- </tr>
- <tr>
- <td align="center"><input type="button" id="led0_btn" value="关闭" onClick="switchLED('led0',0);"/></td>
- <td align="center"><input type="button" id="led1_btn" value="关闭" onClick="switchLED('led1',0);"/></td>
- <td align="center"><input type="button" id="led2_btn" value="关闭" onClick="switchLED('led2',0);"/></td>
- </tr>
- <tr>
- <td align="center"><input type="button" id="led0_btn" value="打开" onClick="switchLED('led0',1);"/></td>
- <td align="center"><input type="button" id="led1_btn" value="打开" onClick="switchLED('led1',1);"/></td>
- <td align="center"><input type="button" id="led2_btn" value="打开" onClick="switchLED('led2',1);"/></td>
- </tr>
- </table>
- </form>
- </body>
- </html>
/***********************CGI文件(setled.c)*****************************/
- #include<stdio.h>
- #include<unistd.h>
- #include <stdlib.h>
- #include<sys/types.h>
- #include<string.h>
- #include<sys/stat.h>
- #include<sys/ioctl.h>
- #include<fcntl.h>
- #include"gpio.h"
- int deal_with(int num,int value,int led)
- {
- if(value == 1)
- led |= 0x01 << num;
- if(value == 0)
- led &= ~(0x01 << num);
- return led;
- }
- int main(int argc,char *argv[])
- {
- int led_fd;
- char *data;
- char led;
- int num=0,value=0;
-
- data = getenv("QUERY_STRING");
- printf("%s\r\n\r\n","Content-Type:text/html");
- led_fd = open("/dev/gpgdriver",O_RDWR);
- ioctl(led_fd,GPIO_CFG_PIN_OUT,GPIO_PIN_AREA(0,2));
-
- sscanf(data,"led%d=%d",&num,&value);
- read(led_fd,&led,1);
- led=deal_with(num,value,led);
- write(led_fd,&led,1);
-
- //printf("%s
",data);
- close(led_fd);
- return 0;
- }
/**************************CGI(getled.c)*******************************/
- #include<stdio.h>
- #include<unistd.h>
- #include<sys/types.h>
- #include<sys/stat.h>
- #include<fcntl.h>
- #include <sys/ioctl.h>
- #include"gpio.h"
- int main()
- {
- int led_fd;
- char value=0;
- char led0, led1, led2;
- led_fd = open("/dev/gpgdriver",O_RDWR);
- ioctl(led_fd,GPIO_CFG_PIN_OUT,GPIO_PIN_AREA(0,2));
-
- read(led_fd,&value,1);
- printf("%s\r\n\r\n","Content-Type:text/html");
- //printf("1111
");
- /*
- if (value & 0x01)
- {
- led0 = '1';
- }
- else
- {
- led0 = '0';
- }
- if (value & 0x02)
- {
- led1 = '1';
- }
- else
- {
- led1 = '0';
- }
- if (value & 0x04)
- {
- led2 = '1';
- }
- else
- {
- led2 = '0';
- }
- printf("%c,%c,%c,\n",led0,led1,led2);
- */
- value=value & 0x07;
-
- if(value == 0x00)
- printf("%c,%c,%c,",'0','0','0');
- else if(value == 0x01)
- printf("%c,%c,%c,",'1','0','0');
- else if(value == 0x02)
- printf("%c,%c,%c",'0','1','0');
- else if(value == 0x03)
- printf("%c,%c,%c,",'1','1','0');
- else if(value == 0x04)
- printf("%c,%c,%c",'0','0','1');
- else if(value == 0x05)
- printf("%c,%c,%c,",'1','0','1');
- else if(value == 0x06)
- printf("%c,%c,%c",'0','1','1');
- else if(value == 0x07)
- printf("%c,%c,%c,",'1','1','1');
- printf("%d
",value);
- close(led_fd);
- return 0;
- }
阅读(3184) | 评论(0) | 转发(0) |