html控制LED,网页控制LED (写通道形式)

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

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;

}

%%%%%%%%%%%%%%%ledtest.c%%%%%%%%%%%%%%%%%%%%%%%

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值