這個範例是星期六上模組D【嵌入式系統專案實作】,學員當場提出功能,由我現場實作完成的,其功能是在 ARM-PXA270上執行一QT UI程式,透過鍵盤輸入0或1的一串字串,這個0或1的字串可以控制IO子版的 LED 閃爍。閃爍方式的原則是 1 亮 0 不亮。
控制 LED 閃爍是透過 驅動程式 LED.ko( 這一支程式 在 模組C Step by Step講授嵌入式開發板Linux驅動程式實務班 已經完成,這裡只是拿來使用) 。首先先建立一個driver的NODE: mknod /dev/LED1 c 99 0,再來將 LED.ko 動態加入 kernel: insmod ./LED1.ko,接著執行我寫完的 QT UI程式: ./P2 -qws。畫面如下圖:
執行完 QT 的 UI程式,在ARM-PXA270上出現如下簡單的畫面,請在 兩個可以輸入的 QLineedit上輸入00001111 及 11110000,接著按下 OK 鍵。
接著在 IO 子板上的LED 就會依照你輸入的 0 1 作閃爍動作,畫面如下(PS:因為LCD背光問題,所以照片上的QT畫面整個反白太亮了):
這是LED另一畫面(PS:因為LCD背光問題,所以照片上的QT畫面整個反白太亮了)。
這個小範例的程式碼如下:
p.h 程式檔案
#include "ui_p.h"
class myWidget:public QWidget, public
Ui::Form
{
Q_OBJECT
public:
myWidget(QWidget * parent=0);
};
/*
QWidget::QWidget ( QWidget * parent
= 0, Qt::WindowFlags f = 0 )
*/
main.cpp 程式檔案
#include "myQTHead.h"
#include "p.h"
#include
<QInputDialog>
myWidget::myWidget(QWidget *
parent)
{
setupUi( this );
}
void
myWidget::pushButton_Clicked()
{
int fd, n;
static char
*ShowStr1, *ShowStr2;
ShowStr1 = (char *)(MALLOC( 9
));
STRCPY( ShowStr1, this->lineEdit->text().toAscii().data()
);
ShowStr2 = (char *)(::malloc( 9 ));
STRCPY(
ShowStr2, this->lineEdit_2->text().toAscii().data() );
fd = OPEN( QString( "/dev/LED" ).toAscii().constData(), O_RDWR );
for( n=0; n<=10; n++)
{
WRITE( fd,
ShowStr1 ,9 );
SLEEP( 1 );
WRITE(
fd, ShowStr2 ,9 );
SLEEP( 1 );
}
CLOSE( fd ); // OK
FREE(ShowStr1);
FREE(ShowStr2);
}
int main(int argc, char
*argv[])
{
QApplication app(argc,
argv);
myWidget *form = new
myWidget;
form->show();
return
app.exec();
}
/*
QListWidgetItem * QListWidget::currentItem ()
const
void QListWidget::removeItemWidget ( QListWidgetItem * item )
int
row ( const QListWidgetItem * item ) const
QListWidgetItem * takeItem ( int
row )
QString QInputDialog::getText ( QWidget * parent, const QString &
title, const QString & label, QLineEdit::EchoMode mode = QLineEdit::Normal,
const QString & text = QString(), bool * ok = 0, Qt::WindowFlags f = 0
) [static]
*/
ui_p.h 程式檔案
/********************************************************************************
**
Form generated from reading ui file 'p.ui'
**
** Created: Sat Jan 17
03:45:12 2009
** by: Qt User Interface Compiler
version 4.4.3
**
** WARNING! All changes made in this file will be lost
when recompiling ui
file!
********************************************************************************/
#ifndef UI_P_H
#define UI_P_H
#include <QtCore/QVariant>
#include
<QtGui/QAction>
#include <QtGui/QApplication>
#include
<QtGui/QButtonGroup>
#include <QtGui/QLabel>
#include
<QtGui/QLineEdit>
#include <QtGui/QPushButton>
#include
<QtGui/QWidget>
QT_BEGIN_NAMESPACE
class Ui_Form
{
public:
QPushButton
*pushButton;
QLineEdit *lineEdit;
QLineEdit *lineEdit_2;
QLabel *label;
void setupUi(QWidget *Form)
{
if
(Form->objectName().isEmpty())
Form->setObjectName(QString::fromUtf8("Form"));
Form->resize(239, 118);
pushButton = new
QPushButton(Form);
pushButton->setObjectName(QString::fromUtf8("pushButton"));
pushButton->setGeometry(QRect(155, 50, 75, 23));
lineEdit = new QLineEdit(Form);
lineEdit->setObjectName(QString::fromUtf8("lineEdit"));
lineEdit->setGeometry(QRect(20, 50, 113, 20));
lineEdit_2 = new QLineEdit(Form);
lineEdit_2->setObjectName(QString::fromUtf8("lineEdit_2"));
lineEdit_2->setGeometry(QRect(20, 75, 113, 20));
label
= new QLabel(Form);
label->setObjectName(QString::fromUtf8("label"));
label->setGeometry(QRect(25, 25, 36, 16));
retranslateUi(Form);
QMetaObject::connectSlotsByName(Form);
} // setupUi
void retranslateUi(QWidget *Form)
{
Form->setWindowTitle(QApplication::translate("Form",
"\351\200\232\350\250\212\351\214\204", 0,
QApplication::UnicodeUTF8));
pushButton->setText(QApplication::translate("Form", "Ok", 0,
QApplication::UnicodeUTF8));
label->setText(QApplication::translate("Form", "LED", 0,
QApplication::UnicodeUTF8));
Q_UNUSED(Form);
} // retranslateUi
};
namespace Ui {
class Form: public Ui_Form {};
} //
namespace Ui
QT_END_NAMESPACE
#endif // UI_P_H
沒有留言:
張貼留言