分类: 嵌入式
2014-01-11 11:28:45
首先编写 led.h 文件
#ifndef LED_H
#define LED_H
#define LED_IOC_MAGIC 'k'
#define LEDON _IOW(LED_IOC_MAGIC,0,int)
#define LEDOFF _IOW(LED_IOC_MAGIC,1,int)
void roomled1on(int led_number);
void roomled1off(int led_number);
#endif // LED_H
然后编写led.c 文件
#include
#include
#include
#include
#include
#include
#include
#include "led.h"
void roomled1on(int led_number)
{
int fd;
fd = open("/dev/myled", O_RDWR);
if(fd < 0)
{
perror("open device /dev/myled");
exit(1);
}
ioctl(fd,LEDON,led_number);
close(fd);
}
void roomled1off(int led_number)
{
int fd;
fd = open("/dev/myled", O_RDWR);
if(fd < 0)
{
perror("open device /dev/myled");
exit(1);
}
ioctl(fd,LEDOFF,led_number);
close(fd);
}
然后定义QT里面的一个槽文件
#ifndef MAINFORM_H
#define MAINFORM_H
#include "ui_form.h"
class MainForm :public QWidget
{
Q_OBJECT
public :
MainForm(QWidget *parent =0);
public:
Ui::Form ui;
public slots:
void on_RoomLedBtn_clicked();
void on_HallLedBtn_clicked()();
};
#endif // MAINFORM_H
最后具体对槽实现,并且连接槽于信号
#include "mainform.h"
#include "led.c"
static int i = 1;
static int j = 1;
MainForm::MainForm(QWidget *parent)
:QWidget(parent)
{
ui.setupUi(this);
QObject::connect(ui.pushButton_Led,SIGNAL(clicked()),this,SLOT(on_RoomLedBtn_clicked()));
QObject::connect(ui.pushButton_Led_2,SIGNAL(clicked()),this,SLOT(on_HallLedBtn_clicked()));
}
void LedCtrlDlg::on_RoomLedBtn_clicked()
{
i++;
if (i%2==1)
{
roomled1on(1);
printf("roomled1 has been on");
}
else
{
roomled1off(1);
printf("roomled1 has been off");
}
}
void LedCtrlDlg::on_HallLedBtn_clicked()
{
j++;
if (j%2==1)
{
roomled1on(2);
printf("Hallled1 has been on");
}
else
{
roomled1off(2);
printf("Hallled1 has been off");
}
}
这样程序就写完了,然后直接在开发板上面运行即可