Qt + DS18B20 - jammy
来源:百度文库 编辑:神马文学网 时间:2024/04/29 19:07:41
Qt + DS18B20
TQ2440 2009-10-30 21:24:55 阅读180 评论2 字号:大中小
大四了,大学也快结束了~~打算以ARM9为控制核心 + Qt图形界面做个毕业设计。至于具体是做个什么东西,现在还没有头绪。唉~~~只怪学艺不精。
在此之前没有学过C++,而Qt是以C++为设计基础,所以不得不硬着头皮看看C++的书籍。通过Qt设计一个显示温度的界面,可以将之前学习的底层驱动和应用程序联系上,同时也对Qt设计过程有个了解。
首先,按照开发板的资料,建立好Qt的开发环境。不同的开发板可能提供的资料也有所差别。这里就不详细记录了~
开发板上的Qtopia的版本:1.7.0 ,Linux内核版本:2.6.13 。用Qt designer设计好界面,如下图:
部件名称
数量
Form
1
GroupBox
2
LCDNumber
1
PixmapLbabel
3
PushButton
2
接着将相应的部件连接好信号/槽(signal\slot) :
// signals and slots connections
connect( cmdTemp, SIGNAL( clicked() ), this, SLOT( get_temp() ) );
connect( cmdCls, SIGNAL( clicked() ), this, SLOT( close() ) );
connect( timer, SIGNAL( timeout() ), this, SLOT( lcd_display() ) );
l 按钮(cmdTemp)与获取温度函数get_temp() 建立连接
l 按钮(cmdCls)与关闭窗口函数close() 建立连接
l 定时器(timer)与显示函数lcd_display() 建立连接
终端输入progen –o temperature.pro temperature.ui ,生成temperature的工程文件。
将temperature.ui图形文件转换为代码文件,运行脚本:
#!/bin/sh
$QTDIR/bin/uic -o temperature.h temperature.ui
$QTDIR/bin/uic -o temperature.cpp -impl temperature.h temperature.ui
$QTDIR/bin/moc temperature.h -o moc_temperature.cpp
终端输入$TMAKEPATH 和 tmake –o Makefile temperature.pro 生成Makefile文件。对Makefile文件进行相应的修改:
#############################################################################
# Makefile for building temperature
# Generated by tmake at 02:14, 2009/09/28
# Project: temperature
# Template: app
#############################################################################
####### Compiler, tools and options
CC = arm-linux-gcc
CXX = arm-linux-g++
CFLAGS = -pipe -Wall -W -O2 -DNO_DEBUG
CXXFLAGS= -pipe -DQT_QWS_IPAQ -DQWS -fno-exceptions -fno-rtti -Wall -W -O2 -DNO_DEBUG
INCPATH = -I$(QTDIR)/include -I$(QPEDIR)/include
LINK = arm-linux-g++
LFLAGS =
LIBS = $(SUBLIBS) -L$(QPEDIR)/lib -L$(QTDIR)/lib -L/opt/EmbedSky/crosstools_3.4.5_softfloat/gcc-3.4.5-glibc-2.3.6/arm-linux/ -lqpe -lqtopia -lqte
MOC = $(QTDIR)/bin/moc
UIC = $(QTDIR)/bin/uic
TAR = tar -cf
GZIP = gzip -9f
####### Files
HEADERS = temperature.h
SOURCES = main.cpp \
temperature.cpp
OBJECTS = main.o \
temperature.o
INTERFACES = temperature.ui
UICDECLS = temperature.h
UICIMPLS = temperature.cpp
SRCMOC = moc_temperature.cpp
OBJMOC = moc_temperature.o
DIST =
TARGET = $(QPEDIR)/bin/temperature
DESKTOP = $(QPEDIR)/apps/EmbedSky/temperature.desktop
ICON = $(QPEDIR)/pics/temperature.png
INTERFACE_DECL_PATH = .
####### Implicit rules
.SUFFIXES: .cpp .cxx .cc .C .c
.cpp.o:
$(CXX) -c $(CXXFLAGS) $(INCPATH) -o $@ $<
.cxx.o:
$(CXX) -c $(CXXFLAGS) $(INCPATH) -o $@ $<
.cc.o:
$(CXX) -c $(CXXFLAGS) $(INCPATH) -o $@ $<
.C.o:
$(CXX) -c $(CXXFLAGS) $(INCPATH) -o $@ $<
.c.o:
$(CC) -c $(CFLAGS) $(INCPATH) -o $@ $<
####### Build rules
all: $(TARGET)
cp -f temperature.desktop $(DESKTOP)
cp -f temperature.png $(ICON)
$(TARGET): $(UICDECLS) $(OBJECTS) $(OBJMOC)
$(LINK) $(LFLAGS) -o $(TARGET) $(OBJECTS) $(OBJMOC) $(LIBS)
moc: $(SRCMOC)
tmake: Makefile
Makefile: temperature.pro
tmake temperature.pro -o Makefile
dist:
$(TAR) temperature.tar temperature.pro $(SOURCES) $(HEADERS) $(INTERFACES) $(DIST)
$(GZIP) temperature.tar
clean:
-rm -f $(OBJECTS) $(OBJMOC) $(DESKTOP) $(ICON) $(TARGET) $(TARGET)
-rm -f *~ core
####### Sub-libraries
###### Combined headers
####### Compile
main.o: main.cpp \
temperature.h \
/opt/EmbedSky/Qte/touch_qtopia/qtopia/include/qtopia/qpeapplication.h \
/opt/EmbedSky/Qte/touch_qtopia/qtopia/include/qtopia/qpeglobal.h \
/opt/EmbedSky/Qte/touch_qtopia/qtopia/include/qtopia/qlibrary.h \
/opt/EmbedSky/Qte/touch_qtopia/qtopia/include/qtopia/qcom.h \
/opt/EmbedSky/Qte/touch_qtopia/qtopia/include/qtopia/quuid.h \
/opt/EmbedSky/Qte/touch_qtopia/qtopia/include/qtopia/qtopiawinexport.h \
/opt/EmbedSky/Qte/touch_qtopia/qtopia/include/qtopia/timestring.h \
/opt/EmbedSky/Qte/touch_qtopia/qtopia/include/qtopia/applicationinterface.h
temperature.o: temperature.cpp \
temperature.h \
temperature.ui
temperature.h: temperature.ui
$(UIC) temperature.ui -o $(INTERFACE_DECL_PATH)/temperature.h
temperature.cpp: temperature.ui
$(UIC) temperature.ui -i temperature.h -o temperature.cpp
temperature.o: temperature.cpp \
temperature.h \
temperature.ui
moc_temperature.o: moc_temperature.cpp \
temperature.h
moc_temperature.o: moc_temperature.cpp \
temperature.h
moc_temperature.cpp: temperature.h
$(MOC) temperature.h -o moc_temperature.cpp
moc_temperature.cpp: temperature.h
$(MOC) temperature.h -o moc_temperature.cpp
对temperature.cpp代码文件加入控制程序:
/****************************************************************************
** Form implementation generated from reading ui file 'temperature.ui'
**
** Created: Mon Sep 28 11:09:16 2009
** by: The User Interface Compiler (uic)
**
** WARNING! All changes made in this file will be lost!
****************************************************************************/
#include "temperature.h"
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
static int fd = -1;
static const char* const image0_da
"59 60 549 2",
"Qt c None",
"gF c #000000",
//………..
};//图片编译后的代码,很长,没有全部列出
/*
* Constructs a temperature_form which is a child of 'parent', with the
* name 'name' and widget flags set to 'f'
*/
temperature_form::temperature_form( QWidget* parent, const char* name, WFlags fl )
: QWidget( parent, name, fl )
{
QPixmap image0( ( const char** ) image0_da
QPixmap image1( ( const char** ) image1_da
QPixmap image2( ( const char** ) image2_da
QPixmap image3( ( const char** ) image3_da
if ( !name )
setName( "temperature_form" );
resize( 320, 240 );
setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)5, (QSizePolicy::SizeType)5, sizePolicy().hasHeightForWidth() ) );
setMinimumSize( QSize( 320, 240 ) );
setMaximumSize( QSize( 320, 240 ) );
setBaseSize( QSize( 320, 240 ) );
setCaption( tr( "Temperature" ) );
setIcon( image0 );
GroupBox1 = new QGroupBox( this, "GroupBox1" );
GroupBox1->setGeometry( QRect( 90, 10, 140, 60 ) );
GroupBox1->setMaximumSize( QSize( 320, 240 ) );
GroupBox1->setTitle( tr( "Current Temperture" ) );
LCDNumber1 = new QLCDNumber( GroupBox1, "LCDNumber1" );
LCDNumber1->setGeometry( QRect( 20, 20, 100, 30 ) );
LCDNumber1->setNumDigits( 5 );
LCDNumber1->setMode( QLCDNumber::DEC );
PixmapLabel2 = new QLabel( this, "PixmapLabel2" );
PixmapLabel2->setGeometry( QRect( 10, 10, 60, 60 ) );
PixmapLabel2->setPixmap( image1 );
PixmapLabel2->setScaledContents( TRUE );
PixmapLabel1 = new QLabel( this, "PixmapLabel1" );
PixmapLabel1->setGeometry( QRect( 250, 10, 60, 60 ) );
PixmapLabel1->setPixmap( image2 );
PixmapLabel1->setScaledContents( TRUE );
GroupBox2 = new QGroupBox( this, "GroupBox2" );
GroupBox2->setGeometry( QRect( 10, 80, 301, 90 ) );
GroupBox2->setTitle( tr( "Infomation" ) );
TextLabel1 = new QLabel( GroupBox2, "TextLabel1" );
TextLabel1->setGeometry( QRect( 10, 20, 180, 20 ) );
QFont TextLabel1_font( TextLabel1->font() );
TextLabel1_font.setFamily( "adobe-helvetica" );
TextLabel1_font.setPointSize( 14 );
TextLabel1_font.setBold( TRUE );
TextLabel1->setFont( TextLabel1_font );
TextLabel1->setText( tr( "by:Jammy-lee" ) );
TextLabel2 = new QLabel( GroupBox2, "TextLabel2" );
TextLabel2->setGeometry( QRect( 10, 40, 180, 21 ) );
QFont TextLabel2_font( TextLabel2->font() );
TextLabel2_font.setFamily( "adobe-helvetica" );
TextLabel2_font.setPointSize( 14 );
TextLabel2_font.setBold( TRUE );
TextLabel2->setFont( TextLabel2_font );
TextLabel2->setText( tr( "QQ:34376050" ) );
PixmapLabel3 = new QLabel( GroupBox2, "PixmapLabel3" );
PixmapLabel3->setGeometry( QRect( 210, 10, 50, 50 ) );
PixmapLabel3->setPixmap( image3 );
PixmapLabel3->setScaledContents( TRUE );
TextLabel3 = new QLabel( GroupBox2, "TextLabel3" );
TextLabel3->setGeometry( QRect( 10, 60, 281, 20 ) );
QFont TextLabel3_font( TextLabel3->font() );
TextLabel3_font.setFamily( "adobe-helvetica" );
TextLabel3_font.setPointSize( 14 );
TextLabel3_font.setBold( TRUE );
TextLabel3_font.setUnderline( TRUE );
TextLabel3->setFont( TextLabel3_font );
TextLabel3->setText( tr( "Blog:http:\\\\blog.163.com/jammy_lee" ) );
cmdTemp = new QPushButton( this, "cmdTemp" );
cmdTemp->setGeometry( QRect( 70, 170, 71, 30 ) );
cmdTemp->setText( tr( "Get 'C" ) );
cmdCls = new QPushButton( this, "cmdCls" );
cmdCls->setGeometry( QRect( 190, 170, 70, 30 ) );
cmdCls->setText( tr( "Close" ) );
timer = new QTimer(this);
// signals and slots connections
connect( cmdTemp, SIGNAL( clicked() ), this, SLOT( get_temp() ) );
connect( cmdCls, SIGNAL( clicked() ), this, SLOT( close() ) );
connect( timer, SIGNAL( timeout() ), this, SLOT( lcd_display() ) );
timer -> start(1000); //设置定时器
fd = open("/dev/ds18b20", 0); //打开ds18b20设备文件,
//该设备文件是使用之前的设备驱动时建立好的。
if(fd < 0)
{
perror("Can't open /dev/ds18b20 \n");
exit(1);
}
printf("open ds18b20 success \n");
}
/*
* Destroys the object and frees any allocated resources
*/
temperature_form::~temperature_form()
{
// no need to delete child widgets, Qt does it all for us
}
/*
* Main event handler. Reimplemented to handle application
* font changes
*/
bool temperature_form::event( QEvent* ev )
{
bool ret = QWidget::event( ev );
if ( ev->type() == QEvent::ApplicationFontChange ) {
QFont TextLabel1_font( TextLabel1->font() );
TextLabel1_font.setFamily( "adobe-helvetica" );
TextLabel1_font.setPointSize( 14 );
TextLabel1_font.setBold( TRUE );
TextLabel1->setFont( TextLabel1_font );
QFont TextLabel2_font( TextLabel2->font() );
TextLabel2_font.setFamily( "adobe-helvetica" );
TextLabel2_font.setPointSize( 14 );
TextLabel2_font.setBold( TRUE );
TextLabel2->setFont( TextLabel2_font );
QFont TextLabel3_font( TextLabel3->font() );
TextLabel3_font.setFamily( "adobe-helvetica" );
TextLabel3_font.setPointSize( 14 );
TextLabel3_font.setBold( TRUE );
TextLabel3_font.setUnderline( TRUE );
TextLabel3->setFont( TextLabel3_font );
}
return ret;
}
void temperature_form::lcd_display() //定时器每隔一段时间显示温度
{
int tmp = 0;
read(fd, &tmp , sizeof(int));
LCDNumber1->display(tmp);
}
void temperature_form::get_temp() //点击按钮后显示五次温度
{
char count = 5;
int tmp = 0;
while(count--)
{
read(fd, &tmp , sizeof(int));
printf("the currently temperature is %d \n",tmp);
LCDNumber1->display(tmp);
sleep(1);
}
}
编译成功后,制作桌面启动器temperature.desktop:
[Desktop Entry]
Version=1.0
Encoding=UTF-8
Name=temperature_jammy
comment=Jammy_lee temperature
Exec=temperature
Icon=temperature
Type=Application
Name[zh_CN]=temperature_jammy
Name[zh_TW]=temperature_jammy
最后,需要注意的是Makefile中的这段代码:
TARGET = $(QPEDIR)/bin/temperature
DESKTOP = $(QPEDIR)/apps/EmbedSky/temperature.desktop
ICON = $(QPEDIR)/pics/temperature.png
意思是编译后的程序、桌面启动器和桌面的图标所在位置,我的PC上的
$(QPEDIR) = /opt/EmbeddedSky/Qte/touch_qtopia/qtopia/
因此,需要在该目录下将编译后的程序、桌面启动器和桌面的图标分别复制到开发板相应的文件夹里,重启后就能看见效果。