jackbella 发表于 2013-10-25 15:57:35

无线模块NRF24L01驱动QT应用程序,这是qt应用程序,从此本科不再玩嵌入式了

good.cpp
#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;
int fd1;

void open_wire()
{
fd=open("/dev/nrf24l01",0);
if(fd <0)
{
                perror("open device kern_time error");
                exit(1);
}

}


void close_wire()
{
close(fd);

}



void open_time0()
{
fd1=open("/dev/kern_time",0);
if (fd1 < 0)
{
                perror("open device kern_time error");
                exit(1);
}
ioctl(fd1,1,1);
}




void close_time0()
{
close(fd1);
}


QProcess *pro_1=new QProcess;
Maindialog::Maindialog(QWidget *parent):QDialog(parent)
{
//定时器部分
qtime=new QTimer(this);

//标签部分
label_madplay=new QLabel(tr("MP3 player"));
label_sensor=new QLabel(tr("Sensor"));
label_temperature=new QLabel(tr("Temperature   "));
label_humidity=new QLabel(tr("Humidity   "));
label_temperature_danwei=new QLabel(tr("`C"));
label_humidity_danwei=new QLabel(tr("%"));
label_wire=new QLabel(tr("Wireless Systems"));
label_led2=new QLabel(tr("Lamp"));
label_led3=new QLabel(tr("Curtain"));
label_led4=new QLabel(tr("Electricity"));
label_temperature_display=new QLabel(tr(" "));
label_humidity_display=new QLabel(tr(" "));

//按键部分
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(true);
btn_closetime0=new QPushButton(tr("close"));
btn_closetime0->setEnabled(true);
btn_wireless_open=new QPushButton(tr("open"));
btn_wireless_open->setEnabled(true);
btn_wireless_close=new QPushButton(tr("close"));
btn_wireless_close->setEnabled(true);
btn_light_on=new QPushButton(tr("Lamp On"));
btn_light_on->setEnabled(true);
btn_light_off=new QPushButton(tr("Lamp Off"));
btn_light_off->setEnabled(true);
btn_curtain_on=new QPushButton(tr("Curtain On"));
btn_curtain_on->setEnabled(true);
btn_curtain_off=new QPushButton(tr("Curtain Off"));
btn_curtain_off->setEnabled(true);
btn_electricity_on=new QPushButton(tr("Electricity On"));
btn_electricity_on->setEnabled(true);
btn_electricity_off=new QPushButton(tr("Electricity Off"));
btn_electricity_off->setEnabled(true);




//槽函数
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(btn_closetime0,SIGNAL(clicked()),this,SLOT(closetime0()));
connect(btn_wireless_open,SIGNAL(clicked()),this,SLOT(wireless_on()));
connect(btn_wireless_close,SIGNAL(clicked()),this,SLOT(wireless_off()));
connect(btn_light_on,SIGNAL(clicked()),this,SLOT(light_on()));
connect(btn_light_off,SIGNAL(clicked()),this,SLOT(light_off()));
connect(btn_curtain_on,SIGNAL(clicked()),this,SLOT(curtain_on()));
connect(btn_curtain_off,SIGNAL(clicked()),this,SLOT(curtain_off()));
connect(btn_electricity_on,SIGNAL(clicked()),this,SLOT(electricity_on()));
connect(btn_electricity_off,SIGNAL(clicked()),this,SLOT(electricity_off()));
connect(qtime,SIGNAL(timeout()),this,SLOT(flash()));


//
QHBoxLayout *layout_madplay=new QHBoxLayout;
layout_madplay->addWidget(label_madplay);
layout_madplay->addWidget(btn_madplay);
layout_madplay->addWidget(btn_madplay_off);

QHBoxLayout *layout_sensor=new QHBoxLayout;
layout_sensor->addWidget(label_sensor);
layout_sensor->addWidget(btn_time0);
layout_sensor->addWidget(btn_closetime0);

QHBoxLayout *layout_temperature=new QHBoxLayout;
layout_temperature->addWidget(label_temperature);
layout_temperature->addWidget(label_temperature_display);
layout_temperature->addWidget(label_temperature_danwei);
layout_temperature->addWidget(label_humidity);
layout_temperature->addWidget(label_humidity_display);
layout_temperature->addWidget(label_humidity_danwei);

QHBoxLayout *layout_wire=new QHBoxLayout;
layout_wire->addWidget(label_wire);
layout_wire->addWidget(btn_wireless_open);
layout_wire->addWidget(btn_wireless_close);




QHBoxLayout *layout_led2=new QHBoxLayout;
layout_led2->addWidget(label_led2);
layout_led2->addWidget(btn_light_on);
layout_led2->addWidget(btn_light_off);

QHBoxLayout *layout_led3=new QHBoxLayout;
layout_led3->addWidget(label_led3);
layout_led3->addWidget(btn_curtain_on);
layout_led3->addWidget(btn_curtain_off);

QHBoxLayout *layout_led4=new QHBoxLayout;
layout_led4->addWidget(label_led4);
layout_led4->addWidget(btn_electricity_on);
layout_led4->addWidget(btn_electricity_off);

QGridLayout *layout_main=new QGridLayout;
layout_main->addLayout(layout_madplay,0,0,1,1);
layout_main->addLayout(layout_sensor,1,0,1,1);
layout_main->addLayout(layout_temperature,2,0,1,1);
layout_main->addLayout(layout_wire,3,0,1,1);
layout_main->addLayout(layout_led2,4,0,1,1);
layout_main->addLayout(layout_led3,5,0,1,1);
layout_main->addLayout(layout_led4,6,0,1,1);
layout_main->setContentsMargins(5,5,5,5);//设置布局内部四边的空隙
layout_main->setMargin(2);//它的含义是设置边框大小。所谓边框,应该有4边,这里是部件边缘相对父部件的间距这个是重要的!!即使是这样,字体大了还是不行的
layout_main->setAlignment(Qt::AlignHCenter);
layout_main->setAlignment(Qt::AlignVCenter);
//layout_main->setColumnStretch(10,1);//设置列宽度
layout_main->setSpacing(2); //设置间距

setLayout(layout_main);

setWindowTitle(tr("APP for Jack Sun"));
setFixedHeight(sizeHint().height());
}


void flash_ioctl()
{
/*
unsigned int i;
//i=ioctl(fd1,1,2);
i=10;
label_display->setText(QString::number(i));
*/
}

void Maindialog::madplay()
{

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

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

void Maindialog::time0()
{
open_time0();//打开文件
qtime->start(500);//每两秒刷新一次
btn_time0->setEnabled(false);
/*
QProcess *pro_8=new QProcess;
pro_8->start("/opt/time0_run");
*/
}

void Maindialog::closetime0()
{

btn_time0->setEnabled(true);
qtime->stop();
close_time0();
label_temperature_display->setText(QString::number(0));
label_humidity_display->setText(QString::number(0));

}
void Maindialog::flash()
{
unsigned int temp_sensor;
float t=0;
float h=0;
read(fd1,temp_sensor,sizeof(temp_sensor));
t=(float)temp_sensor/10;
h=(float)temp_sensor/10;
label_temperature_display->setText(QString::number(t));
label_humidity_display->setText(QString::number(h));
}



void Maindialog::wireless_on()
{
open_wire();
btn_wireless_open->setEnabled(false);
}
void Maindialog::wireless_off()
{
btn_wireless_open->setEnabled(true);
close_wire();

}
void Maindialog::light_on()
{
btn_light_on->setEnabled(false);
close_wire();//先关掉内核定时器 防止内核出乱
open_wire();
ioctl(fd,1,1);
}
void Maindialog::light_off()
{
btn_light_on->setEnabled(true);
close_wire();//先关掉内核定时器 防止内核出乱
open_wire();
ioctl(fd,2,1);

}
void Maindialog::curtain_on()
{
btn_curtain_on->setEnabled(false);
close_wire();//先关掉内核定时器 防止内核出乱
open_wire();
ioctl(fd,3,1);
}
void Maindialog::curtain_off()
{
btn_curtain_on->setEnabled(true);
close_wire();//先关掉内核定时器 防止内核出乱
open_wire();
ioctl(fd,4,1);
}
void Maindialog::electricity_on()
{
btn_electricity_on->setEnabled(false);
close_wire();//先关掉内核定时器 防止内核出乱
open_wire();
ioctl(fd,5,1);
}
void Maindialog::electricity_off()
{
btn_electricity_on->setEnabled(true);
close_wire();//先关掉内核定时器 防止内核出乱
open_wire();
ioctl(fd,6,1);
}


good.h

#ifndef GOOD_H_
#define GOOD_H_

#include <QDialog>

class QLabel;
class QPushButton;
class QCheckBox;

class Maindialog:public QDialog
{
Q_OBJECT
public:
Maindialog(QWidget *parent=0);



QLabel *label_temperature_display;
QLabel *label_humidity_display;


private slots:
void madplay();
void madplay_off();
void time0();
void closetime0();
void flash();
void wireless_on();
void wireless_off();
void light_on();
void light_off();
void curtain_on();
void curtain_off();
void electricity_on();
void electricity_off();


private:
QLabel *label_madplay;
QLabel *label_sensor;
QLabel *label_temperature;
QLabel *label_humidity;
QLabel *label_temperature_danwei;
QLabel *label_humidity_danwei;
QLabel *label_wire;
QLabel *label_led2;
QLabel *label_led3;
QLabel *label_led4;


QPushButton *btn_madplay;
QPushButton *btn_madplay_off;
QPushButton *btn_time0;
QPushButton *btn_closetime0;
QPushButton *btn_wireless_open;
QPushButton *btn_wireless_close;
QPushButton *btn_light_on;
QPushButton *btn_light_off;
QPushButton *btn_curtain_on;
QPushButton *btn_curtain_off;
QPushButton *btn_electricity_on;
QPushButton *btn_electricity_off;

QTimer *qtime;

};
#endif


goomain.cpp

#include <QApplication>
#include "good.h"

int main(int argc,char *argv[])
{
QApplication app(argc,argv);
Maindialog *dialog=new Maindialog;
QFont font= app .font();
font.setPointSize(8);
app.setFont(font);
dialog->showMaximized();
return app.exec();
}
页: [1]
查看完整版本: 无线模块NRF24L01驱动QT应用程序,这是qt应用程序,从此本科不再玩嵌入式了