移植boa到TQ2440网页LED实现

硬件平台:TQ2440

内核版本:linux2.6.34.1

gcc : Thread model: posix   gcc version 4.4.3 (Sourcery G++ Lite 2009q1-203) EABI

文件系统:yaffs2,busybox1.13.1

1.1 leds.html的编写

1.2 index.html的编写

1.3 stytle.css的编写

1.4 。。。

二、CGI代码

该代码是以boa移植2上的CGI为基础的,区别在于添加了led_disp函数

该函数是写实现的功能在于读/dev/usr_led函数,这是一个设备文件,他的驱动的编写比较常见,可以直接移植,写可以自己编写,

2.1 关于led CGI的编写

 

//tq2440.c

//arm-linux-gcc -march=armv4t tq2440.c -o tq2440.cgi

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>

int led_disp(int argn,char *argv[]);

 

/
int main(int argn,char * argv[])

{

  printf("Content-Type: text/html\r\n\r\n");

  
   printf("<html>\n");

    printf("<head><title>TQ2440 Test </title></head>\n");


    printf("<body><pre>\n");


 

      while(argn-->0) 

      {

       printf("arg[%d]=%s<br \>",argn,argv[argn]);


        }//注意<br \>,这个是网页上的换行,因为C语言中\n是不能在网页上起到换行作用的
      led_disp(argn,argv);
  printf("</body>\n");


   printf("</html>\n");


     return 0;

     }
int led_disp(int argc, char *argv[])
{
    int fd;
    char file_path[30];
    memset(file_path,30,0);
       int led=argv[1][0]-'0';
     sprintf(file_path,"/dev/led%d",led);
     
    
    
    
    fd = open(file_path, O_NONBLOCK|O_RDWR);
     
    if(fd < 0)
    {
        printf("Open %s Device Faild!\n",file_path);
        exit(1);
    }
    else
     {
     printf("open %s ok\n",file_path) ;
     }

    
    
        write(fd,argv[2],1);       
 printf("write %c to %s using fd=%d\n",argv[2][0],file_path,fd);
 sleep(3);


    close(fd);

    return 0;
}

所需文件:leds.html--------LED的网页文件

                testleds.cgi--------作用就是一个shell文件,将网页提交的内容写到通道中去
    led-result.template------提交按钮点击后出现的网页的内容由其提供,作用就是返回到leds.html
    ledwebqidong文件放在 /etc/rc.d/init.d/下---------shell文件,与rcS中有关命令相结合
    ledtest.c------------应用文件,通过读取管道信息,管理LED的亮灭。生成ledweb针对开发板的可执行文件。将ledweb放在开发板的/sbin下。
上面完成以后(注意权限),只需更改/etc/init.d/rcs文件,添加一句:
/etc/rc.d/init.d/ledwebqidong start
即可。
在浏览器栏中输入:
http://192.168.1.6/ledweb/leds.html就操作控制led。
1. leds.html

%%%%%%%%%%%%%%%leds.html%%%%%%%%%%%%%%%%%%%%%%%
<head>
<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>LED灯测试</title>
<!--
body {
background-color: #FFFFFF;
);
}
-->
</style></head>
 
<body>
 
<h1 align="left">LED测试</h1>
<form method="get" action="testleds.cgi" name="LED-TEST">
  <div align="left">
<table border="0" width="100%">     
<tr>
<td width="80px">
        <b> LED灯:</b>
</td>
<td width="80px">
LED1<input type="checkbox" name="cb_led" value="led1" />
</td>
<td width="80px">
LED2<input type="checkbox" name="cb_led" value="led2" />
</td>
<td width="80px">
LED3<input type="checkbox" name="cb_led" value="led3" />
</td>
<td width="80px">
LED4<input type="checkbox" name="cb_led" value="led4" />
</td>
<td></td>
</tr>
 
<!--...-->  
<tr>
<td width="80px">
       <input type="submit" value="确定" name="submit">
</td>
<td></td>
</tr>
 
</table>
</div>
</form>
</body>
 
</html>
%%%%%%%%%%%%%%%leds.html %%%%%%%%%%%%%%%%%%%%%%%

2, testleds.cgi

%%%%%%%%%%%%%%%testleds.cgi%%%%%%%%%%%%%%%%%%%%%%%
#!/bin/sh

LED1_ON=0
LED2_ON=0
LED3_ON=0
LED4_ON=0
SPEED=1

case $QUERY_STRING in
    *cb_led=led1*)
        LED1_ON=1
        ;;
esac
 
case $QUERY_STRING in
    *cb_led=led2*)
        LED2_ON=1
        ;;
esac
 
case $QUERY_STRING in
    *cb_led=led3*)
        LED3_ON=1
        ;;
esac
 
case $QUERY_STRING in
    *cb_led=led4*)
        LED4_ON=1
        ;;
esac
       
 
/bin/echo $LED1_ON $LED2_ON  $LED3_ON $LED4_ON > /tmp/led-control
 
/bin/echo "Content-type: text/html; charset=gb2312"
/bin/echo
#/bin/echo $QUERY_STRING "STRING"

/bin/cat led-result.template
exit 0
%%%%%%%%%%%%%%%testleds.cgi%%%%%%%%%%%%%%%%%%%%%%%

3, led-result.template

%%%%%%%%%%%%%%%led-result.template%%%%%%%%%%%%%%%%%%%%%%%
<html>
<head>
<title>LED测试结果</title>
<style type="text/css">
<!--
body {
background-color: #666666;
}
-->
</style>
</head>
<body>
<p>LED设置已经提交</p>
<p><a href="leds.html">返回上一页</a></p>
</body>
</html>
%%%%%%%%%%%%%%%led-result.template%%%%%%%%%%%%%%%%%%%%%%%

4, ledwebqidong

%%%%%%%%%%%%%%%ledwebqidong%%%%%%%%%%%%%%%%%%%%%%%

#!/bin/sh

base=/sbin/ledweb

# See how we were called.
case "$1" in
  start)
$base &
        ;;
  stop)
pid=`/bin/pidof $base`
if [ -n "$pid" ]; then
kill -9 $pid
fi
        ;;
esac

exit 0

%%%%%%%%%%%%%%%ledwebqidong%%%%%%%%%%%%%%%%%%%%%%%

5, ledtest.c

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/select.h>
#include <sys/time.h>
#include<string.h>
static int led_fd;
static int leds[4] = {0};

static void lightleds(void)
{
int i;
for(i=0;i<4;i++)
{
ioctl(led_fd, leds[i], i);
}
}

int main(void)
{
int led_control_pipe;
int null_writer_fd; // for read endpoint not blocking when control process exit
led_fd = open("/dev/EmbedSky-leds", 0);
if (led_fd < 0) {
perror("open device leds");
exit(1);
}
unlink("/tmp/led-control");
mkfifo("/tmp/led-control", 0666);

led_control_pipe = open("/tmp/led-control", O_RDONLY | O_NONBLOCK);
if (led_control_pipe < 0) {
perror("open control pipe for read");
exit(1);
}
null_writer_fd = open("/tmp/led-control", O_WRONLY | O_NONBLOCK);
if (null_writer_fd < 0) {
perror("open control pipe for write");
exit(1);
}

for (;;) {
fd_set rds;
int ret;
struct timeval step;
step.tv_sec  = 0;
step.tv_usec =0.125*1000000L;
FD_ZERO(&rds);
FD_SET(led_control_pipe, &rds);
ret = select(led_control_pipe + 1, &rds, NULL, NULL, &step);
if (ret < 0) {
perror("select");
exit(1);
}
if (ret == 0) {
lightleds();
} 
else if (FD_ISSET(led_control_pipe, &rds)) {
static char buffer[200];
for (;;) {
char c;
int len = strlen(buffer);
if (len >= sizeof buffer - 1) {
memset(buffer, 0, sizeof buffer);
break;
}
if (read(led_control_pipe, &c, 1) != 1) {
break;
}
if (c == '\r') {
continue;
}
if (c == '\n') {
int tmp_leds[4];
if (sscanf(buffer,"%d%d%d%d", &tmp_leds[0], &tmp_leds[1],&tmp_leds[2],&tmp_leds[3]) == 4) {
leds[0] = tmp_leds[0];
 leds[1] = tmp_leds[1];
 leds[2] = tmp_leds[2];
 leds[3] = tmp_leds[3];
}
int j;
for(j=0;j<4;j++)
{
if(leds[j])
fprintf(stderr,"led%d is on\n",j+1);
else
fprintf(stderr, "led%d is off\n",j+1);
}
memset(buffer, 0, sizeof buffer);
break;
}
buffer[len] = c;
}
}
}

close(led_fd);
return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值