Chinaunix首页 | 论坛 | 博客
  • 博客访问: 130475
  • 博文数量: 30
  • 博客积分: 972
  • 博客等级: 中士
  • 技术积分: 332
  • 用 户 组: 普通用户
  • 注册时间: 2012-02-08 10:04
文章分类

全部博文(30)

文章存档

2012年(30)

分类: LINUX

2012-07-13 18:55:06

/************************HTML文件*********************/

点击(此处)折叠或打开

  1. <html>
  2. <head>
  3.     <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  4. <title>LED测试</title>
  5.   <script language="JavaScript">
  6.     function loadLEDStatus() {
  7.         var xmlhttp = null;
  8.         if(window.XMLHttpRequest)
  9.             xmlhttp = new XMLHttpRequest();
  10.         else if(window.ActiveXObject)
  11.             xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  12.         xmlhttp.open("GET", "/cgi-bin/getled.cgi", false);
  13.         xmlhttp.setRequestHeader("If-Modified-Since", "0");
  14.         xmlhttp.onreadystatechange=function() {
  15.             if(xmlhttp.readyState != 4 || xmlhttp.status != 200)
  16.                 return;
  17.             document.getElementById("myDiv").innerHTML = xmlhttp.responseText;
  18.             var value = new Array();
  19.             value = xmlhttp.responseText.split(",");
  20.             //document.write(value);
  21.             var i;
  22.             for(i = 0; i < 3; i ) {
  23.                 var img = document.getElementById("led" i);
  24.                 if(value[i] == "1") {
  25.                     img.src = "led_on.jpg";
  26.                 } else {
  27.                     img.src = "led_off.jpg";
  28.                 }
  29.             }
  30.         }
  31.         xmlhttp.send();
  32.     }
  33.     function switchLED(led,on_off) {
  34.         var url="/cgi-bin/setled.cgi?" led;
  35.         if(on_off) {
  36.             url = url "=1";
  37.         } else{
  38.             url = url "=0";
  39.         }
  40.         var xmlhttp = null;
  41.         if(window.XMLHttpRequest)
  42.             xmlhttp = new XMLHttpRequest();
  43.         else if(window.ActiveXObject)
  44.             xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  45.             
  46.         xmlhttp.open("GET", url, false);
  47.         xmlhttp.setRequestHeader("If-Modified-Since", "0");

  48.         //xmlhttp.onreadystatechange = function() {
  49.         //     if(xmlhttp.readyState != 4 || xmlhttp.status != 200)
  50.         //        return;
  51.         //    document.getElementById("myDiv").innerHTML = xmlhttp.responseText;
  52.         //}
  53.         xmlhttp.send();
  54.         loadLEDStatus();
  55.     }
  56. </script>
  57. </head>
  58. <body onLoad="loadLEDStatus();">

  59.     <div id="myDiv">
  60.     <h2>Let AJAX change this text</h2>
  61.     </div>
  62.     
  63.     <form>
  64.     <table align="center" summary="">
  65.     <tr>
  66.     <td colspan="3" align="center"><h1>LED 测试</h1></td>
  67.     </tr>
  68.     <tr>
  69.     <td align="center">
  70.         <img src="led_off.jpg" id="led0" width=30 height=30 alt="led1"/>
  71.     </td>
  72.     <td align="center"><img src="led_off.jpg" id="led1" width=30 height=30 alt="led2"/></td>
  73.     <td align="center"><img src="led_off.jpg" id="led2" width=30 height=30 alt="led3"/></td>
  74.     </tr>
  75.     <tr>
  76.     <td align="center"><input type="button" id="led0_btn" value="关闭" onClick="switchLED('led0',0);"/></td>
  77.     <td align="center"><input type="button" id="led1_btn" value="关闭" onClick="switchLED('led1',0);"/></td>
  78.     <td align="center"><input type="button" id="led2_btn" value="关闭" onClick="switchLED('led2',0);"/></td>
  79.     </tr>
  80.     <tr>
  81.     <td align="center"><input type="button" id="led0_btn" value="打开" onClick="switchLED('led0',1);"/></td>
  82.     <td align="center"><input type="button" id="led1_btn" value="打开" onClick="switchLED('led1',1);"/></td>
  83.     <td align="center"><input type="button" id="led2_btn" value="打开" onClick="switchLED('led2',1);"/></td>
  84.     </tr>
  85.     </table>
  86.     </form>
  87. </body>
  88. </html>

/***********************CGI文件(setled.c)*****************************/

点击(此处)折叠或打开

  1. #include<stdio.h>
  2. #include<unistd.h>
  3. #include <stdlib.h>
  4. #include<sys/types.h>
  5. #include<string.h>
  6. #include<sys/stat.h>
  7. #include<sys/ioctl.h>
  8. #include<fcntl.h>
  9. #include"gpio.h"

  10. int deal_with(int num,int value,int led)
  11. {
  12.     if(value == 1)
  13.      led |= 0x01 << num;
  14.      if(value == 0)
  15.      led &= ~(0x01 << num);
  16.      return led;
  17. }

  18. int main(int argc,char *argv[])
  19. {
  20.     int led_fd;
  21.     char *data;
  22.     char led;
  23.     int num=0,value=0;
  24.     
  25.     data = getenv("QUERY_STRING");    
  26.     printf("%s\r\n\r\n","Content-Type:text/html");

  27.     led_fd = open("/dev/gpgdriver",O_RDWR);
  28.     ioctl(led_fd,GPIO_CFG_PIN_OUT,GPIO_PIN_AREA(0,2));
  29.     
  30.     sscanf(data,"led%d=%d",&num,&value);
  31.     read(led_fd,&led,1);
  32.     led=deal_with(num,value,led);
  33.     write(led_fd,&led,1);
  34.     
  35.     //printf("%s
    "
    ,data);
  36.     close(led_fd);
  37.     return 0;
  38. }
/**************************CGI(getled.c)*******************************/

点击(此处)折叠或打开

  1. #include<stdio.h>
  2. #include<unistd.h>
  3. #include<sys/types.h>
  4. #include<sys/stat.h>
  5. #include<fcntl.h>
  6. #include <sys/ioctl.h>
  7. #include"gpio.h"

  8. int main()
  9. {
  10.     int led_fd;
  11.     char value=0;
  12.     char led0, led1, led2;
  13.     led_fd = open("/dev/gpgdriver",O_RDWR);
  14.     ioctl(led_fd,GPIO_CFG_PIN_OUT,GPIO_PIN_AREA(0,2));
  15.     
  16.     read(led_fd,&value,1);
  17.     printf("%s\r\n\r\n","Content-Type:text/html");
  18.     //printf("1111
    "
    );
  19. /*
  20.     if (value & 0x01)
  21.     {
  22.         led0 = '1';
  23.     }
  24.     else
  25.     {
  26.         led0 = '0';
  27.     }
  28.     if (value & 0x02)
  29.     {
  30.         led1 = '1';
  31.     }
  32.     else
  33.     {
  34.         led1 = '0';
  35.     }
  36.     if (value & 0x04)
  37.     {
  38.         led2 = '1';
  39.     }
  40.     else
  41.     {
  42.         led2 = '0';
  43.     }
  44.     printf("%c,%c,%c,\n",led0,led1,led2);
  45. */

  46.     value=value & 0x07;
  47.     
  48.     if(value == 0x00)
  49.         printf("%c,%c,%c,",'0','0','0');
  50.     else if(value == 0x01)
  51.         printf("%c,%c,%c,",'1','0','0');
  52.     else if(value == 0x02)
  53.         printf("%c,%c,%c",'0','1','0');
  54.     else if(value == 0x03)
  55.         printf("%c,%c,%c,",'1','1','0');
  56.     else if(value == 0x04)
  57.         printf("%c,%c,%c",'0','0','1');
  58.     else if(value == 0x05)
  59.         printf("%c,%c,%c,",'1','0','1');
  60.     else if(value == 0x06)
  61.         printf("%c,%c,%c",'0','1','1');
  62.     else if(value == 0x07)
  63.         printf("%c,%c,%c,",'1','1','1');
  64.     printf("%d
    "
    ,value);

  65.     close(led_fd);
  66.     return 0;
  67. }


阅读(3190) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~