Chinaunix首页 | 论坛 | 博客
  • 博客访问: 408522
  • 博文数量: 62
  • 博客积分: 1483
  • 博客等级: 上尉
  • 技术积分: 779
  • 用 户 组: 普通用户
  • 注册时间: 2009-02-24 12:25
文章分类

全部博文(62)

文章存档

2012年(2)

2011年(6)

2010年(6)

2009年(48)

我的朋友

分类: LINUX

2009-11-30 18:14:39

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 < 0){printf("open\n");exit(0);}

    ioctl(fd,I2C_SLAVE,0x50); //0xa0>>1 = 0x50,驱动里面会把最地位补全的(读或者写)
    ioctl(fd,I2C_TIMEOUT,1);
    ioctl(fd,I2C_RETRIES,1);

    int block,i;
    for(block=0;block<4;block++){
        ioctl(fd,I2C_SLAVE,0x50 | block);
        printf("\nread block:%d\n",block);
        for(i=0;i<256;i++){
            unsigned addr;
            addr = (unsigned char)i;
            write(fd,&addr,1);
            read(fd,&buf[(block<<8)+i],1);
            bar->setProgress(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 < 0){printf("open\n");exit(0);}

    ioctl(fd,I2C_SLAVE,0x50); //0xa0>>1 = 0x50,驱动里面会把最地位补全的(读或者写)
    ioctl(fd,I2C_TIMEOUT,1);
    ioctl(fd,I2C_RETRIES,1);

    int block,i;

    for(block=0;block<4;block++){
        ioctl(fd,I2C_SLAVE,0x50 | block);
        printf("\nwrite block:%d\n",block);
        for(i=0;i<256;i++){
            my_data.addr = (unsigned short)i;
            my_data.byte[1] = buf[(block<<8)+i];
            write(fd,&my_data.addr,2);
            usleep(10000); //怕写的时间不够,应该没问题
            bar->setProgress(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

阅读(3010) | 评论(0) | 转发(0) |
0

上一篇:qt的按键代码

下一篇:qt 看门狗

给主人留下些什么吧!~~