jz2440:QT控制LED灯点亮熄灭(11)

1.LED灯的驱动:

首先要准备好在驱动文件,通过insmod led.ko来加载模块,然后在QT的代码里面配合调用open,write,read函数来点亮,关闭LED灯。这一步,做完,应该可以通过测试程序点亮关闭LED。

具体内容可参照:jz2440点亮LED

2.QT代码:

实现的功能:

  1. LED按键进入右图,Back键进入LED按键界面。
  2. LEDx open 与 LEDx close可以打开关闭3个LED灯。

 代码在这个位置:https://github.com/zion6135/QT/tree/main/4.LED/QT

LED页面:

这是LED按键的主要函数:比较关键的就是connect()函数:

4个参数是这样的,按键对象pushButtonLed 发送按下的信号SIGNAL(clicked())给this(也就是LED这个页面),然后执行回调函数ButtonToLed();

总之:点下LED按键就会执行ButtonToLed()函数。

#include "mainwindow.h"
#include <QList>
#include "Class/LedWindow.h"



MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    this->setGeometry(0, 0, 472, 300);  // set windows size
    pushButtonLed = new QPushButton("LED", this); //set button name
    pushButtonLed->setGeometry(50, 50, 100, 100); //set button size
    // button push callback exec ButtonToLed()
    connect(pushButtonLed, SIGNAL(clicked()), this, SLOT(ButtonToLed()));
}

void MainWindow::ButtonToLed() {
    LedWindow* mLed = new LedWindow();
    mLed->show();
    connect(mLed, SIGNAL(signaltomain()), this, SLOT(ButtonToMain()));

    this->hide();
    qDebug("To Led");
}

void MainWindow::ButtonToMain() {
    this->show();
    qDebug("To main");
}


MainWindow::~MainWindow()
{

}


6个按键的页面:

1.6个按键没有啥复杂的,主要是返回按键发送自定义的信号,然后需要在回调函数隐藏按键页面,显示LED页面。

#include "mainwindow.h"
#include <QList>
#include "Class/LedWindow.h"
#define upxxxxxxx 0

bool LedWindow::openLedDrv() {
#if upxxxxxxx
    fd = open("/dev/xyz", O_RDWR);
    if (fd < 0) {
        qDebug("open dev failed!!!");
        return false;
    }
#endif
    return true;
}

LedWindow::LedWindow(QWidget *parent)
    : QMainWindow(parent)
{
    if (true == openLedDrv()) {
        this->setGeometry(0, 0, 472, 300);

        pushButtonBack = new QPushButton("Back", this);
        pushButtonBack->setObjectName("btnBack");
        pushButtonBack ->setGeometry(10, 10, 60, 30);
        connect(pushButtonBack, SIGNAL(clicked()), this, SLOT(ButtonLedclicked()));

        pushButton[0] = new QPushButton("LED1 open", this);
        pushButton[0]->setObjectName("btn1");
        pushButton[0] ->setGeometry(50, 50, 100, 100);
        connect(pushButton[0], SIGNAL(clicked()), this, SLOT(ButtonLedclicked()));

        pushButton[1] = new QPushButton("LED2 open", this);
        pushButton[1]->setObjectName("btn2");
        pushButton[1] ->setGeometry(150, 50, 100, 100);
        connect(pushButton[1], SIGNAL(clicked()), this, SLOT(ButtonLedclicked()));

        pushButton[2] = new QPushButton("LED3 open", this);
        pushButton[2]->setObjectName("btn3");
        pushButton[2] ->setGeometry(250, 50, 100, 100);
        connect(pushButton[2], SIGNAL(clicked()), this, SLOT(ButtonLedclicked()));

        pushButton[3] = new QPushButton("LED4 close", this);
        pushButton[3]->setObjectName("btn4");
        pushButton[3] ->setGeometry(50, 150, 100, 100);
        connect(pushButton[3], SIGNAL(clicked()), this, SLOT(ButtonLedclicked()));

        pushButton[4] = new QPushButton("LED5 close", this);
        pushButton[4]->setObjectName("btn5");
        pushButton[4] ->setGeometry(150, 150, 100, 100);
        connect(pushButton[4], SIGNAL(clicked()), this, SLOT(ButtonLedclicked()));

        pushButton[5] = new QPushButton("LED6 close", this);
        pushButton[5]->setObjectName("btn6");
        pushButton[5] ->setGeometry(250, 150, 100, 100);
        connect(pushButton[5], SIGNAL(clicked()), this, SLOT(ButtonLedclicked()));
    } else {
        qDebug("open failed");
    }
}

void LedWindow::ButtonLedclicked() {

    QObject* obj = sender();
//    obj->dumpObjectInfo();
    int val = -1;
   if (obj->objectName() == "btn1") {
       qDebug("1");
#if upxxxxxxx
       val = 1;
       ::write(fd, &val, 4);
#endif
   } else if (obj->objectName() == "btn2") {
       qDebug("2");
#if upxxxxxxx
       val = 2;
       ::write(fd, &val, 4);
#endif
   } else if (obj->objectName() == "btn3") {
       qDebug("3");
#if upxxxxxxx
       val = 3;
       ::write(fd, &val, 4);
#endif
   } else if (obj->objectName() == "btn4") {
       qDebug("4");
#if upxxxxxxx
       val = 4;
       ::write(fd, &val, 4);
#endif
   } else if (obj->objectName() == "btn5") {
       qDebug("5");
#if upxxxxxxx
       val = 5;
       ::write(fd, &val, 4);
#endif
   } else if (obj->objectName() == "btn6") {
       qDebug("6");
#if upxxxxxxx
       val = 6;
       ::write(fd, &val, 4);
#endif
   } else if (obj->objectName() == "btnBack") {
       qDebug("7");
       emit signaltomain();
       this->close();
   }

}


 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值