学习笔记CGI & LED

学习笔记CGI & LED

    在上一篇帖子中给大家分享了一下 GoAhead服务器的移植,简单的介绍了下CGI,和Form的两种方法GET和POST。

    今天就再探一下CGI。通过网页来控制开发板上的4颗LED灯的亮灭状态

 新建一个cgi_led.html 文件放到开发板主目录的 web/ 文件夹下:


  1. <html>  
  2. <head>  
  3. <title> DEMO CGI LED--HelloWii </title>  
  4. </head>  
  5. <body bgcolor=#87CEEB>  
  6. <div align="center">  
  7.   <form method=get action="cgi-bin/led.cgi">  
  8.        <p>please input a number</p>  
  9.        <input  type="text" name="value" maxlength="1" size="1" >  
  10.        <input  type="submit" name="button" value="input">  
  11.    </form>  
  12.    <p>0: is open  LED1  </p>  
  13.    <p>1: is close LED1  </p>  
  14.    <p>2: is open  LED2  </p>  
  15.    <p>3: is close LED2  </p>  
  16.    <p>4: is open  LED3  </p>  
  17.    <p>5: is close LED3  </p>                                                      
  18.    <p>6: is open  LED4  </p>                                                      
  19.    <p>7: is close LED4  </p>                                                      
  20.    <p>8: is open  ALL LED </p>                                                    
  21.    <p>9: is close ALL LED</p>                                                     
  22.    <br \>                                                                         
  23.    <p>  HelloWii </p>                                                             
  24.    </div>                                                                         
  25. </body>                                                                           
  26. </html>                          

 

通过浏览器打开(http://192.168.1.20/cgi_led.html)的效果图如下(IP地址请更换为自己的开发板IP):


                                       图11-1:cgi_led.html


上面的信息已经很清楚的表述了各个数字的功能。


下面就是写CGI的程序了,具体编译上一篇CGI学习笔记中已经将了makefile和编写,下面直接贴led.c的代码,如下:


  1. #include <stdio.h>  
  2. #include <string.h>  
  3. #include <sys/types.h>  
  4. #include <sys/stat.h>  
  5. #include <fcntl.h>  
  6. #include <linux/input.h>  
  7. #define LED1         0  
  8. #define LED2         1  
  9. #define LED3         2  
  10. #define LED4         3  
  11.   
  12. #define LED_ON       0  
  13. #define LED_OFF      1    
  14. typedef struct  
  15. {  
  16.   char name[32];  
  17.   char value[32];  
  18. }entry;  
  19. char *ledc;  
  20. void getValue(char *value,char *line,char flag)  
  21. {    
  22.    int i=0,j=0;  
  23.    for(i=0;( (line[i]) && (line[i] != flag));i++)  
  24.        {value[i] = line[i];}  
  25.    value[i] = '\0';   
  26.    if(line[i])  ++i;  
  27.    while(line[j++] = line[i++]);       
  28. }  
  29. int main(int argc, char **argv)  
  30. {  
  31.     unsigned int count = 0;  
  32.     int fdled = -1;  
  33.     entry get;  
  34.     printf("Content-type : text/html\n\n");  
  35.     printf("<html>\n<head><title>LED CGI PROGRAM</title></head>\n");  
  36.     printf("<body>\n");     
  37.       
  38.     usleep(500*1000);  
  39.     fdled = open("/dev/led",O_RDWR);  
  40.   
  41.     if(fdled<0)  
  42.      {    
  43.        printf("Error:Can't open /dev/leds\n");  
  44.        
  45.        return -1;  
  46.      }   
  47.     ioctl(fdled,1,LED3);ioctl(fdled,0,LED4);  
  48.     ledc = (char *)getenv("QUERY_STRING");  
  49.     getValue(get.name, ledc,'=');  
  50.     getValue(get.value,ledc,'&');  
  51.     printf("<br><center>Your input value is <b> %c </b> </center>",get.value[0]);   
  52.     switch(get.value[0])  
  53.     {  
  54.       case '0':ioctl(fdled,LED_ON,LED1); break;  
  55.       case '1':ioctl(fdled,LED_OFF,LED1); break;  
  56.       case '2':ioctl(fdled,LED_ON,LED2); break;  
  57.       case '3':ioctl(fdled,LED_OFF,LED2); break;  
  58.       case '4':ioctl(fdled,LED_ON,LED3); break;  
  59.       case '5':ioctl(fdled,LED_OFF,LED3); break;  
  60.       case '6':ioctl(fdled,LED_ON,LED4); break;  
  61.       case '7':ioctl(fdled,LED_OFF,LED4); break;  
  62.       case '8':ioctl(fdled,LED_ON,LED1);  
  63.                ioctl(fdled,LED_ON,LED2);  
  64.                ioctl(fdled,LED_ON,LED3);  
  65.                ioctl(fdled,LED_ON,LED4);   
  66.                break;  
  67.       case '9':ioctl(fdled,LED_OFF,LED1);  
  68.                ioctl(fdled,LED_OFF,LED2);  
  69.                ioctl(fdled,LED_OFF,LED3);  
  70.                ioctl(fdled,LED_OFF,LED4);   
  71.                break;  
  72.      }  
  73.     close(fdled);  
  74.     printf("</body>\n<html>");  
  75.   
  76.    return 0;  
  77. }  

 

makefile如下:



  1. # General Makefile  
  2.   
  3. Exec := led.cgi  
  4. Obj := led.c  
  5. CC := arm-linux-gcc  
  6.   
  7. $(Exec) : $(Obj)  
  8.     $(CC) -o $@ $(Obj) $(LDLIBS$(LDLIBS-$(@)))  
  9.   
  10. clean:  
  11.     rm -vf $(Exec) *.elf *.o  

 

将生成的leg.cgi 可执行程序,放到开发板的 web/cgi-bin/ 文件夹下,运行程序,即可通过网页对LED灯的控制。。。


今天我就抛砖引玉,简单的讲一下我的小例子。。。期待网友可以用SIN210开发出更炫的功能,比如通过网页显示开发板的温度什么的。。。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值