1.設置LED測試程序的UI界面
複製上篇文章說做的Helloworld.ui,改名leds.ui並修改其caption爲My leds,同時刪除掉原來界面上所有的控件。
點擊check Box,在窗體上放置4個check Box控件,其name屬性爲m_ledx(x=1,2,3,4),caption爲LEDx(x=1,2,3,4)。保存後退出。
2.編寫代碼,還是需要3個文件:main.cpp, main_form.cpp, main_form.h
main.cpp
#include "main_form.h"
#include <qtopia/qpeapplication.h>
QTOPIA_ADD_APPLICATION("leds",TMainForm)
QTOPIA_MAIN
main_form.cpp
#include "main_form.h"
//#include <qlineedit.h>
#include <qcheckbox.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h>
TMainForm::TMainForm(QWidget * parent, const char * name, WFlags f)
: TMainFormBase(parent, name, f)
{
//::system("insmod /lib/modules/ok6410_leds.ko");
//因爲我的LED驅動是做成模塊,所以需要先加載模塊,
//但在此處加載後點擊QT界面卻沒法控制板上的LED亮滅,
//據說是system這個方法會導致阻塞??希望您能幫我解答!
//所以我把led模塊的加載放到了啓動led的腳本上了
m_fd = ::open("/dev/leds0", O_RDONLY);
if (m_fd < 0) {
m_fd = ::open("/dev/leds", O_RDONLY);
}
connect(m_led1, SIGNAL(clicked()), this, SLOT(checkBoxClicked()) );
connect(m_led2, SIGNAL(clicked()), this, SLOT(checkBoxClicked()) );
connect(m_led3, SIGNAL(clicked()), this, SLOT(checkBoxClicked()) );
connect(m_led4, SIGNAL(clicked()), this, SLOT(checkBoxClicked()) );
checkBoxClicked();
}
TMainForm::~TMainForm()
{
::close(m_fd);
::system("rmmod ok6410_leds");//這裏移除掉led模塊
}
void TMainForm::checkBoxClicked()
{
ioctl(m_fd, int(!m_led1->isChecked()), 0);
ioctl(m_fd, int(!m_led2->isChecked()), 1);
ioctl(m_fd, int(!m_led3->isChecked()), 2);
ioctl(m_fd, int(!m_led4->isChecked()), 3);
}
main_form.h
#if !defined (__MAIN_FORM_H__)
# define __MAIN_FORM_H__
#include "leds.h"
//#include <qsocketnotifier.h>
class TMainForm: public TMainFormBase {
Q_OBJECT
public:
TMainForm(QWidget * parent = 0, const char * name = 0, WFlags f = WType_TopLevel) ;
virtual ~TMainForm();
//public slots:
// void closeButtonClicked();
private slots:
void checkBoxClicked();
private:
int m_fd;
};
#endif
3.還需要一個leds.pro文件,內容如下:
CONFIG += qtopiaapp
CONFIG -= buildQuicklaunch
HEADERS = main_form.h
SOURCES = main_form.cpp main.cpp
INTERFACES = leds.ui
TARGET = leds
4.編寫交叉編譯的腳本buildarm.sh
#!/bin/bash source /home/my/qtopia2.2.0/arm-qtopia2.2.0/qtopia-free-2.2.0/setQpeEnv qmake -spec qws/linux-arm-g++ -o Makefile.target *.pro make -f Makefile.target clean make -f Makefile.target運行buildarm.sh生成leds的可執行文件,拷貝到開發板根目錄下/opt/Qtopia/bin目錄下
5.編寫啓動led測試程序的腳本,放在開發板根目錄下/bin/裏面
如下:
#!/bin/sh export TSLIB_ROOT=/usr/local/tslib export TSLIB_TSDEVICE=/dev/event0 export TSLIB_TSEVENTTYPE=H3600 export TSLIB_CONFFILE=/usr/local/tslib/etc/ts.conf export TSLIB_PLUGINDIR=/usr/local/tslib/lib/ts export TSLIB_CALIBFILE=/etc/pointercal export TSLIB_PLUGINDIR=$TSLIB_ROOT/lib/ts export TSLIB_CONSOLEDEVICE=none export TSLIB_FBDEVICE=/dev/fb0 export QWS_MOUSE_PROTO=Tslib:/dev/event0 #export QWS_MOUSE_PROTO=TPanel:/dev/input/event1 #export QWS_MOUSE_PROTO="Tslib:/dev/input/event1 MouseMan:/dev/input/mice" #export QWS_MOUSE_PROTO=MouseMan:/dev/input/mice export QWS_KEYBOARD="TTY:/dev/tty1" export KDEDIR=/opt/konqueror if [ -c /dev/event0 ]; then echo "touch..." export QWS_MOUSE_PROTO='TPanel:/dev/event0' if [ ! -e /etc/pointercal ]; then /bin/calibrate fi if [ -e /etc/pointercal -a ! -s /etc/pointercal ] ; then rm /mnt/disk/pointercal /bin/calibrate fi else echo "mouse..." export QWS_MOUSE_PROTO="MouseMan:/dev/mice" >/etc/pointercal fi export QTDIR=/opt/Qtopia export QPEDIR=/opt/Qtopia export PATH=$QTDIR/bin:$PATH export QT_PLUGIN_PATH=$QTDIR/plugins:$QTDIR/qt_plugins/ export QT_QWS_FONTDIR=$QPEDIR/lib/fonts/ export LD_LIBRARY_PATH=$QTDIR/plugins/qtopialmigrate/:$QTDIR/qt_plugins/imageformats/:$QTDIR/lib:/usr/local/tslib/lib:$LD_LIBRARY_PATH FB_SIZE=$(cat /sys/class/graphics/fb0/virtual_size) #export QWS_DISPLAY="LinuxFb:mmWidth76:mmHeight44:1" case "$FB_SIZE" in 800,480) export QWS_DISPLAY="LinuxFb:mmWidth91:mmHeight53:1" ;; 480,272) export QWS_DISPLAY="LinuxFb:mmWidth76:mmHeight44:1" ;; *) export QWS_DISPLAY="LinuxFb:mmWidth91:mmHeight53:1" ;; esac export HOME=/root/QtopiaHome if [ ! -e $HOME ]; then mkdir -p $HOME; fi if [ ! -e /usr/share ]; then mkdir /usr/share; fi if [ ! -e $HOME/Settings/Trolltech/qpe.conf ]; then mkdir -p $HOME/Settings/Trolltech/ cp $QPEDIR/etc/default/qpe.conf $HOME/Settings/Trolltech/qpe.conf -f fi export LANG="en_US" #temp if [ ! -e $HOME/Documents ]; then mkdir -p $HOME/Documents; fi # make link file for Documents of Qtopia #if [ ! -h $HOME/Documents/grassland.jpg -o ! -h $HOME/Documents/panda.mpg -o ! -h $HOME/Documents/青花瓷.mp3 ]; then # ln -s /root/QtopiaHome/Documents/grassland.jpg $HOME/Documents # ln -s /root/QtopiaHome/Documents/青花瓷.mp3 $HOME/Documents # ln -s /root/QtopiaHome/Documents/panda.mpg $HOME/Documents #fi # insmod /lib/modules/ok6410_leds.ko #注意這裏需要先加載led驅動模塊,因爲我編譯linux的時候並沒有把led驅動編譯到內核裏 exec $QPEDIR/bin/leds啓動開發板,進入系統後輸入# leds & 即可看到led測試界面,點擊上面的LED可以控制板上LED的亮滅。