前言
上一篇博文 https://blog.csdn.net/wenhao_ir/article/details/145459006 中,我們是直接利用GPIO子系統控制了LED2的亮和滅,這篇博文中我們利用之前寫好的LED驅動程序在Qt的生成的界面中控制LED2的亮和滅。
之前已經在下面兩篇博文中實現了LED驅動程序:
https://blog.csdn.net/wenhao_ir/article/details/144973219
https://blog.csdn.net/wenhao_ir/article/details/145119224
本篇博文中我們就在Qt的代碼中利用已經寫好的LED驅動程序來控制LED2的亮和滅。由于第2個LED驅動程序要去修改設備樹文件,比較麻煩,所以我們就用上面第1篇博文中的LED驅動程序來實現本篇博文“利用之前已經開發好的LED驅動在Qt生成的界面中控制LED2的亮和滅”的目的。
代碼來源及修改說明
本文的代碼在上一篇博文 https://blog.csdn.net/wenhao_ir/article/details/145459006 的基礎上進行修改,其實只需要改動文件led.cpp中的代碼,即把函數led_init
和函數led_control
按驅動程序的使用方法進行修改就行了。
由于之前是認真仔細地學習了驅動程序的書寫,所以其實代碼的修改是非常簡單的,所以相關的代碼就不去進行說明了。
完整源代碼
文件led.h中的代碼
#ifndef LED_H
#define LED_Hvoid led_init(void);
void led_control(int on);#endif // LED_H
文件mainwindow.h中的代碼
#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACEclass MainWindow : public QMainWindow
{Q_OBJECTpublic:MainWindow(QWidget *parent = nullptr);~MainWindow();private slots:void on_pushButton_clicked();
private:Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
文件led.cpp中的代碼
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <QDebug>static int fd;void led_init(void)
{fd = open("/dev/imx6ull_led0", O_RDWR);if (fd < 0){qDebug()<<"open /dev/imx6ull_led0 failed";}
}void led_control(int on)
{char status;// 因為int類型是占4個字節,而我只向驅動空間寫入1個字節,正好char類型只占一個字節,所以這里要強制轉換一下// 當status為1時,驅動程序會在對應的GPIO口輸出低電平,此時燈亮,反之燈滅status = (char)on;write(fd, &status, 1);}
文件main.cpp中的代碼
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "led.h"
#include <QDebug>MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow)
{ui->setupUi(this);
}MainWindow::~MainWindow()
{delete ui;
}void MainWindow::on_pushButton_clicked()
{static int status = 1;if (status)qDebug()<<"LED clicked on";elseqDebug()<<"LED clicked off";/* 2. control LED */led_control(status);status = !status;
}
文件mainwindow.cpp中的代碼
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "led.h"
#include <QDebug>MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow)
{ui->setupUi(this);
}MainWindow::~MainWindow()
{delete ui;
}void MainWindow::on_pushButton_clicked()
{static int status = 1;if (status)qDebug()<<"LED clicked on";elseqDebug()<<"LED clicked off";/* 2. control LED */led_control(status);status = !status;
}
編譯工程生成可執行程序
為了確保是新生成的可執行程序,所以把之前在別的博文中生成的可執行程序刪掉~
然后編譯:
生成了新的ELF可執行文件:
放到NFS網絡文件目錄中,備用:
把驅動程序放到NFS目錄中備用
把博文 https://blog.csdn.net/wenhao_ir/article/details/144973219 中生成的驅動程序放到NFS目錄中備用
上板測試
打開串口→啟動開發板
打開串口終端→打開開發板
關掉開發板上自帶的QT的GUI
經實測,如果不關閉開發板自帶的QT的GUI,雖然你自己寫的Qt界面能加載出來,但是當你用手劃動屏幕時,開發板上自帶的QT的GUI有可能會彈出來。
參考博文 https://blog.csdn.net/wenhao_ir/article/details/144591685 用一次性有效的方法(即不是永久有效的方法)關掉自帶的QT的GUI界面。
這里用一次性的方法,即不是永久有效的方法:
執行下面這條命令:
/etc/init.d/S99myirhmi2 stop
執行完成后再用手去操作屏幕上的UI界面,UI界面就沒有任何反應了,說明QT的GUI界面被關掉了
加載驅動程序
掛載網絡文件系統:
mount -t nfs -o nolock,vers=3 192.168.5.11:/home/book/nfs_rootfs /mnt
加載驅動程序
insmod /mnt/qt_driver_led/led_driver.ko
設置Qt環境變量
export QT_QPA_GENERIC_PLUGINS=tslib:/dev/input/event1
export QT_QPA_PLATFORM=linuxfb:fb=/dev/fb0
export QT_QPA_FONTDIR=/usr/lib/fonts/
這三條命令的詳細解釋見 https://blog.csdn.net/wenhao_ir/article/details/145433648
運行編譯生成的Qt程序
注意:運行前請確保Qt運行的環境變量設置好了。
注意:運行前請確保Qt運行的環境變量設置好了。
/mnt/qt_driver_led/test_01
屏幕如下圖所示:
點擊LED按鈕1次:
終端運行結果如下:
此時LED2燈是亮著的。
再點LED按鈕1次:
終端運行結果如下:
此是LED2燈滅了。
這樣測試就成功了。
附編譯完成后完整的工程目錄
https://pan.baidu.com/s/1sOm3uA8q_TLcuGqPCJ2I2g?pwd=gt5u
注意:QtCreator的工程換位置后一定要更換一下Build directory的位置,因為在QtCreator中Build directory是一個絕對路徑,詳情見 https://blog.csdn.net/wenhao_ir/article/details/145458743