2015-06-16 29 views
-2

我正在使用q24工具mini2440。 我爲i2c adc做了一個gui,它有starti2c和stopi2c。 所有的東西都運轉良好:它在starti2c按鈕按下時讀取信號的值,但我希望更新此值。我知道可以使用Qtimer,但我該怎麼做? 下面是代碼:更新Qtimer的ac值


# include <termio.h> 
# include <time.h> 
# include <string.h> 
# include <sys/time.h> 

HelloForm::HelloForm(QWidget* parent, const char* name, WFlags fl): 

HelloBaseForm(parent, name, fl) 

{ 

connect(PushButton1,SIGNAL(clicked()),this,SLOT(starti2c())); 
connect(PushButton2,SIGNAL(clicked()),this,SLOT(stopi2c())); 

} 

HelloForm::~HelloForm() 

{ 

} 

//*********************Code for getting i2c**************************// 
    char HelloForm::geti2c() 
{ 

    char buf[100];        
    char buff[100];  
    char valuee; 

    int m1; 

    char con_buff[10]; 

    int fd=open("/dev/i2c/0",O_RDWR); 

    if (fd<0) 
    { 

    Message->setText(" NOT ABLE TO OPEN THE DRIVER "); 

    } 

    else 

    { 

    Message->setText(" I2C IS WORKING "); 

} 

    int io,wbyte,rbyte,i; 

    //********i2cdetect and read************ 

    buf[0]=0x48;  

    buf[1]=0x00; 

    buf[2]=0x91; 

    io=ioctl(fd,I2C_SLAVE,0x48); 

    if(io<0) 

{ 

Message->setText(" "); 

Message->setText("error ioctl"); 

} 

else 

{ 

wbyte=write(fd,buf,3); 
        // write all three control word to arm 
usleep(1*1000); 

} 

if(wbyte!=3) 

{ 
Message->setText("error write"); 

Message->setText(QString::number(wbyte)); 

rbyte=read(fd,buff,10);  

//ADC->setText(buff); 

sscanf(buff,"%c",&valuee);    

m1=int(valuee);  

return(m1); 

} 


void HelloForm::starti2c() 

{ 


while(1) 

{ 

float adc_val=0; 

adc_val=geti2c(); 

adc_val=(adc_val*5)/255.00; 

usleep(1*1000); 

ADC->setText(QString::number(adc_val)); 
    } 
    } 

//***********stop********// 
void HelloForm::stopi2c() 
{       

    ADC->setText(" "); 
Message->setText("Stopped"); 
}   

+0

任何人都可以請告訴我...它的迫切我使用trolltech qt 2.2.0 –

+1

我真的不明白一個問題。但是'QTimer'有它的超時信號用於更新。還有Qt 2.2?這是** 15 **歲。你應該考慮使用更新的版本 – Bowdzone

+0

'我希望這個值是更新'=>哪一個? @Bowdzone他可能指的是Qt Creator 2.2,這是有點年輕 –

回答

0

希望這將讓你開始 - 它創建了一個定時器,每隔次1000毫秒。定時器的超時信號連接到與PushButton1連接的相同插槽 - starti2c。

QTimer *timer = new QTimer(this); 
connect(timer, SIGNAL(timeout()), this, SLOT(starti2c())); 
timer->start(1000); 

該代碼應放置在您有2個連接語句的位置之下。

+0

是否需要添加任何標題... –

+0

您需要#include

+0

HelloForm :: HelloForm(QWidget * parent,const char * name, WFlags fl): HelloBaseForm(parent,name,fl) connect(PushButton1,SIGNAL(clicked()),this,SLOT(starti2c())); connect(PushButton2,SIGNAL(clicked()),this,SLOT(stopi2c())); QTimer * timer = new QTimer(this); connect(timer,SIGNAL(timeout()),this,SLOT(starti2c())); timer-> start(1000); } –