Chinaunix
标题:
读写24c08
[打印本页]
作者:
mclovein
时间:
2009-11-30 18:14
标题:
读写24c08
hello.h---------------------------
#include
#include
#include
#include
#include
#include
#include
#include
class myMainWindow : public QWidget
{
Q_OBJECT
public:
myMainWindow();
void write2eeprom(void);
void readFromEEprom();
public slots:
void bEvent(); //按钮事件
private:
bool flag; //启动/停止指示
QPushButton *b_on1,*bExit;
QMultiLineEdit *editTop;
QMultiLineEdit *editBtm;
QProgressBar *bar;
QLabel *label;
QVBoxLayout *vbox;
};
hello.cpp--------------------------
#include "hello.h"
myMainWindow::myMainWindow()
{
setMinimumSize(200,200);
// setMaximumSize(200,200);
label = new QLabel(this);
label->setMinimumSize(150,30);
label->setMaximumSize(240,200);
label->setFont(QFont("san",10,QFont::Black));
label->setText("press the top button to\n"
"read/write the text to the eeprom");
label->setAlignment(AlignLeft);
flag = 0; //0:表示当前是读操作
b_on1 = new QPushButton("read",this);
b_on1->setMinimumSize(30,20);
b_on1->setFont(QFont("Times",15,QFont::Bold));
bExit = new QPushButton("exit",this);
bExit->setMinimumSize(30,20);
bExit->setFont(QFont("Times",15,QFont::Bold));
editTop = new QMultiLineEdit(this);
editTop->setMinimumSize(200,60);
editTop->setText(QString("long long ago ...\n"
"a king die\n"
"and another king die\n"
"and the rest king die\n"));
editBtm = new QMultiLineEdit(this);
editBtm->setMinimumSize(200,60);
editBtm->setText(QString(""));
bar = new QProgressBar(1024,this);
bar->setMinimumSize(100,20);
QVBoxLayout *vbox = new QVBoxLayout(this); //没有这个this,布局管理器将不能工作
vbox->addWidget(b_on1);
vbox->addWidget(editTop);
vbox->addWidget(bar);
vbox->addWidget(editBtm);
vbox->addWidget(label);
vbox->addWidget(bExit);
connect(b_on1,SIGNAL(clicked()),this,SLOT(bEvent()));
connect(bExit,SIGNAL(clicked()),qApp,SLOT(quit()));
// setWindowTitle("ad convert"); //这个东东在qte2.2.0版本里没有
}
#include
#include
#include
#include
#include
#include
#include
#define I2C_RETRIES 0x0701
#define I2C_TIMEOUT 0x0702
#define I2C_SLAVE 0x0703
#define BUF_SIZE 1024
//要实现读出24c08的所有内容,并显示在multilineedit里面
void myMainWindow::readFromEEprom(void)
{
int fd;
unsigned char *buf;
buf = (unsigned char *)malloc(BUF_SIZE);
memset(buf,0x98,BUF_SIZE);
fd = open("/dev/i2c/0",O_RDWR);
if(fd >1 = 0x50,驱动里面会把最地位补全的(读或者写)
ioctl(fd,I2C_TIMEOUT,1);
ioctl(fd,I2C_RETRIES,1);
int block,i;
for(block=0;blocksetProgress(i+(block*256)+1);
}
}
editBtm->setText(QString((const char*)buf));
free(buf);
::close(fd);
}
void myMainWindow::write2eeprom()
{
int fd;
char *buf;
union data{
unsigned short addr;
char byte[2];
}my_data;
QString qbuf;
qbuf = editTop->text();
buf = (char *)malloc(BUF_SIZE);
memset(buf,0x98,BUF_SIZE);
::strcpy(buf,qbuf.ascii());
fd = open("/dev/i2c/0",O_RDWR);
if(fd >1 = 0x50,驱动里面会把最地位补全的(读或者写)
ioctl(fd,I2C_TIMEOUT,1);
ioctl(fd,I2C_RETRIES,1);
int block,i;
for(block=0;blocksetProgress(i+(block*256)+1);
}
}
free(buf);
::close(fd);
}
void myMainWindow::bEvent()
{
flag = (flag?false:true); //读写操作改变
if(flag){ //这里表示开始读操作
b_on1->setText(QString("write"));
readFromEEprom();
}else{ //这里是关闭pwm
b_on1->setText(QString("read"));
write2eeprom();
}
}
int main(int argc,char **argv)
{
QApplication a(argc,argv);
myMainWindow w;
a.setMainWidget(&w);
w.show();
a.exec();
}
helloworld.desktop---------------------
[Translation]
File=QtopiaApplications
Context=helloworld
Comment[Desktop Entry/Name]=Use soft hyphen (char U00AD) to indicate hyphenation
[Desktop Entry]
Comment[]=helloworld program
Exec=helloworld
Icon=helloworld
Type=Application
Name[]=helloworld
本文来自ChinaUnix博客,如果查看原文请点:
http://blog.chinaunix.net/u3/93291/showart_2107597.html
欢迎光临 Chinaunix (http://bbs.chinaunix.net/)
Powered by Discuz! X3.2