查看: 827|回复: 0

SIN210学习笔记__CGI & LED

[复制链接]
  • TA的每日心情
    慵懒
    2016-10-17 12:07
  • 签到天数: 306 天

    连续签到: 1 天

    [LV.8]以坛为家I

    发表于 2015-3-4 00:18:23 | 显示全部楼层 |阅读模式
    分享到:
    在上两篇帖子中给大家分享了一下 GoAhead服务器的移植和简单的介绍了下CGI以及Form的两种方法GET和POST
    今天就再探一下CGI。通过网页来控制开发板上的4颗LED灯的亮灭状态。
    新建一个cgi_led.html 文件放到开发板主目录的 web/ 文件夹下:
    <html>  <head>  <title> DEMO CGI LED--HelloWii </title>  </head>  <body bgcolor=#87CEEB>  <div align="center">    <form method=get action="cgi-bin/led.cgi">         <p>please input a number</p>         <input  type="text" name="value" maxlength="1" size="1" >         <input  type="submit" name="button" value="input">     </form>     <p>0: is open  LED1  </p>     <p>1: is close LED1  </p>     <p>2: is open  LED2  </p>     <p>3: is close LED2  </p>     <p>4: is open  LED3  </p>     <p>5: is close LED3  </p>                                                         <p>6: is open  LED4  </p>                                                         <p>7: is close LED4  </p>                                                         <p>8: is open  ALL LED </p>                                                       <p>9: is close ALL LED</p>                                                        <br \>                                                                            <p>  HelloWii </p>                                                                </div>                                                                         </body>                                                                           </html>         通过浏览器打开(https://192.168.1.20/cgi_led.html)的效果图如下(IP地址请更换为自己的开发板IP):

      图11-1:cgi_led.html
    上面的信息表述了各个数字的功能。
    下面就是写CGI的程序了,具体编译上一篇CGI学习笔记中已经将了makefile和编写,下面直接贴led.c的代码,如下:
    #include <stdio.h>  #include <string.h>  #include <sys/types.h>  #include <sys/stat.h>  #include <fcntl.h>  #include <linux/input.h>  #define LED1         0  #define LED2         1  #define LED3         2  #define LED4         3    #define LED_ON       0  #define LED_OFF      1    typedef struct  {    char name[32];    char value[32];  }entry;  char *ledc;  void getValue(char *value,char *line,char flag)  {       int i=0,j=0;     for(i=0;( (line) && (line != flag));i++)         {value = line;}     value = '\0';      if(line)  ++i;     while(line[j++] = line[i++]);       }  int main(int argc, char **argv)  {      unsigned int count = 0;      int fdled = -1;      entry get;      printf("Content-type : text/html\n\n");      printf("<html>\n<head><title>LED CGI PROGRAM</title></head>\n");      printf("<body>\n");               usleep(500*1000);      fdled = open("/dev/led",O_RDWR);        if(fdled<0)       {           printf("Error:Can't open /dev/leds\n");                return -1;       }       ioctl(fdled,1,LED3);ioctl(fdled,0,LED4);      ledc = (char *)getenv("QUERY_STRING");      getValue(get.name, ledc,'=');      getValue(get.value,ledc,'&');      printf("<br><center>Your input value is <b> %c </b> </center>",get.value[0]);       switch(get.value[0])      {        case '0':ioctl(fdled,LED_ON,LED1); break;        case '1':ioctl(fdled,LED_OFF,LED1); break;        case '2':ioctl(fdled,LED_ON,LED2); break;        case '3':ioctl(fdled,LED_OFF,LED2); break;        case '4':ioctl(fdled,LED_ON,LED3); break;        case '5':ioctl(fdled,LED_OFF,LED3); break;        case '6':ioctl(fdled,LED_ON,LED4); break;        case '7':ioctl(fdled,LED_OFF,LED4); break;        case '8':ioctl(fdled,LED_ON,LED1);                 ioctl(fdled,LED_ON,LED2);                 ioctl(fdled,LED_ON,LED3);                 ioctl(fdled,LED_ON,LED4);                  break;        case '9':ioctl(fdled,LED_OFF,LED1);                 ioctl(fdled,LED_OFF,LED2);                 ioctl(fdled,LED_OFF,LED3);                 ioctl(fdled,LED_OFF,LED4);                  break;       }      close(fdled);      printf("</body>\n<html>");       return 0;  }  Makefile如下:
    # General Makefile    Exec := led.cgi  Obj := led.c  CC := arm-linux-gcc    $(Exec) : $(Obj)      $(CC) -o $@ $(Obj) $(LDLIBS$(LDLIBS-$(@)))    clean:      rm -vf $(Exec) *.elf *.o  将生成的leg.cgi 可执行程序,放到开发板的 web/cgi-bin/ 文件夹下,运行程序,即可通过网页对LED灯的控制。。。
    今天我就抛砖引玉,简单的讲一下一个控制LED的小例子。。。
    最后非常感谢 SINLINX 公司提供的sin210开发板
    回复

    使用道具 举报

    您需要登录后才可以回帖 注册/登录

    本版积分规则

    关闭

    站长推荐上一条 /2 下一条

    手机版|小黑屋|与非网

    GMT+8, 2024-4-26 05:07 , Processed in 0.113472 second(s), 17 queries , MemCache On.

    ICP经营许可证 苏B2-20140176  苏ICP备14012660号-2   苏州灵动帧格网络科技有限公司 版权所有.

    苏公网安备 32059002001037号

    Powered by Discuz! X3.4

    Copyright © 2001-2024, Tencent Cloud.