嵌入式爱好者

查看: 3377|回复: 1

自编QT应用程序,控制led灯的亮灭,qt应用程序直接读写设备驱动文件

[复制链接]

0

主题

0

帖子

3

积分

扫一扫,手机访问本帖
发表于 2013-10-14 11:11:29 | 显示全部楼层 |阅读模式



#include <QtGui>
#include <QtCore>
#include <sys/ioctl.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <fcntl.h>
#include <stdio.h>
#include "good.h"

#define LED_OPEN 0
#define LED_CLOSE 1

int fd;
void led2_on()
{
fd = open("/dev/leds", 0);
if (fd < 0)
{
                perror("open device leds error");
                exit(1);
}
ioctl(fd,LED_OPEN,1);//led2 close
        close(fd);
}
void led2_off()
{
fd = open("/dev/leds", 0);
if (fd < 0)
{
                perror("open device leds error");
                exit(1);
}
ioctl(fd,LED_CLOSE,1);//led2 close
        close(fd);
}
void led3_on()
{
fd = open("/dev/leds", 0);
if (fd < 0)
{
                perror("open device leds error");
                exit(1);
}
ioctl(fd,LED_OPEN,2);//led3 open
        close(fd);
        }

void led3_off()
{
        fd = open("/dev/leds", 0);
if (fd < 0)
{
perror("open device leds error");
exit(1);
}
ioctl(fd,LED_CLOSE,2);//led3 close
close(fd);
}
void led4_on()
{
fd = open("/dev/leds", 0);
if (fd < 0)
{
                perror("open device leds error");
                exit(1);
}
ioctl(fd,LED_OPEN,3);//led4 open
        close(fd);
}
void led4_off()
{
fd = open("/dev/leds", 0);
if (fd < 0)
{
                perror("open device leds error");
                exit(1);
}
ioctl(fd,LED_CLOSE,3);//led4 close
        close(fd);
}


QProcess *pro_1=new QProcess;
Maindialog::Maindialog(QWidget *parent):QDialog(parent)
{
label_madplay=new QLabel(tr("Madplay:"));
label_time0=new QLabel(tr("Time0:"));
label_led2=new QLabel(tr("LED2:"));
label_led3=new QLabel(tr("LED3:"));
label_led4=new QLabel(tr("LED4:"));

btn_madplay=new QPushButton(tr("open"));
btn_madplay->setEnabled(true);
btn_madplay_off=new QPushButton(tr("close"));
btn_madplay_off->setEnabled(true);
btn_time0=new QPushButton(tr("open"));
btn_time0->setEnabled(false);
check_led2=new QCheckBox(tr("led2_status"));
check_led2->setEnabled(true);
check_led3=new QCheckBox(tr("led3_status"));
check_led3->setEnabled(true);
check_led4=new QCheckBox(tr("led4_status"));
check_led4->setEnabled(true);

//slots
connect(btn_madplay,SIGNAL(clicked()),this,SLOT(madplay()));
connect(btn_madplay_off,SIGNAL(clicked()),this,SLOT(madplay_off()));
connect(btn_time0,SIGNAL(clicked()),this,SLOT(time0()));
connect(check_led2,SIGNAL(clicked()),this,SLOT(led2()));
connect(check_led3,SIGNAL(clicked()),this,SLOT(led3()));
connect(check_led4,SIGNAL(clicked()),this,SLOT(led4()));
//
QHBoxLayout *layout_madplay=new QHBoxLayout;
layout_madplay->addWidget(label_madplay);
layout_madplay->addWidget(btn_madplay);
layout_madplay->addWidget(btn_madplay_off);

QHBoxLayout *layout_time0=new QHBoxLayout;
layout_time0->addWidget(label_time0);
layout_time0->addWidget(btn_time0);
QHBoxLayout *layout_led2=new QHBoxLayout;
layout_led2->addWidget(label_led2);
layout_led2->addWidget(check_led2);
QHBoxLayout *layout_led3=new QHBoxLayout;
layout_led3->addWidget(label_led3);
layout_led3->addWidget(check_led3);
QHBoxLayout *layout_led4=new QHBoxLayout;
layout_led4->addWidget(label_led4);
layout_led4->addWidget(check_led4);

QVBoxLayout *layout_main=new QVBoxLayout;
layout_main->addLayout(layout_madplay);
layout_main->addLayout(layout_time0);
layout_main->addLayout(layout_led2);
layout_main->addLayout(layout_led3);
layout_main->addLayout(layout_led4);

setLayout(layout_main);

setWindowTitle(tr("API"));
setFixedHeight(sizeHint().height());
}

void Maindialog::madplay()
{

pro_1->start("/opt/madplay_run");//直接运行脚本
}

void Maindialog::madplay_off()
{
pro_1->write(QByteArray("q"));
//pro_1->terminate();
pro_1->kill();
}

void Maindialog::time0()
{
QProcess *pro_8=new QProcess;
pro_8->start("/opt/time0_run");

}

void  Maindialog::led2()
{
if(check_led2->isChecked())//选中了
{
led2_on();
/*
QProcess *pro_2=new QProcess;
pro_2->start("/opt/led2_on");
*/
}
else
{
led2_off();
/*
QProcess *pro_3=new QProcess;
pro_3->start("/opt/led2_off");
*/
}
}

void  Maindialog::led3()
{


if(check_led3->isChecked())//选中了
{
led3_on();

/*
QProcess *pro_4=new QProcess;
pro_4->start("/opt/led3_on");
*/
}
else
{
led3_off();
/*
QProcess *pro_5=new QProcess;
pro_5->start("/opt/led3_off");
*/
}


}
void  Maindialog::led4()
{
if(check_led4->isChecked())//选中了
{
led4_on();
/*
QProcess *pro_6=new QProcess;
pro_6->start("opt/led4_on");
*/
}
else
{
led4_off();
/*
QProcess *pro_7=new QProcess;
pro_7->start("/opt/led4_off");
*/
}


}












回复

使用道具 举报

0

主题

0

帖子

3

积分

 楼主| 发表于 2013-10-14 11:20:38 | 显示全部楼层
还有问题没有解决,不知道怎样关掉用QProcess打开的进程,对操作系统不是很了解,上边的void Maindialog::madplay()
{

pro_1->start("/opt/madplay_run");//直接运行脚本
}
这个打开了音乐播放器,但是不管怎样都关不掉音乐播放器
点评回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋| 飞凌嵌入式 ( 冀ICP备12004394号-1 )

GMT+8, 2024-12-22 15:16

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表