更新 Qtimer 的 ac 值
Updating ac value for Qtimer
我正在为 mini2440 使用 qt 工具。
我为具有 starti2c 和 stopi2c 的 i2c adc 制作了一个 gui。
一切都运行良好:按下 starti2c 按钮时它读取信号值,但我希望更新该值。我知道可以使用Qtimer,但是我该怎么做呢?
这是代码:
# 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");
}
希望这能让您入门 - 它创建了一个每 1000 毫秒超时一次的计时器。计时器的超时信号连接到您的 PushButton1 连接到的同一个插槽 - starti2c。
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(starti2c()));
timer->start(1000);
该代码应放在您有 2 个连接语句的下方。
我正在为 mini2440 使用 qt 工具。 我为具有 starti2c 和 stopi2c 的 i2c adc 制作了一个 gui。 一切都运行良好:按下 starti2c 按钮时它读取信号值,但我希望更新该值。我知道可以使用Qtimer,但是我该怎么做呢? 这是代码:
# 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");
}
希望这能让您入门 - 它创建了一个每 1000 毫秒超时一次的计时器。计时器的超时信号连接到您的 PushButton1 连接到的同一个插槽 - starti2c。
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(starti2c()));
timer->start(1000);
该代码应放在您有 2 个连接语句的下方。