wangshenbo 发表于 2013-2-1 01:19:44

QT程序:Led与按键显示

下面是我自己做的一个程序,把LED与按键结合到一起了,就是在出厂的LEd灯测试程序中在加了一个显示键值的框,但是有个问题就是键盘按键要时实检测,这导致了点击Led的按钮不会响应,这个问题苦恼我很久了,哪位大神指导下!!谢谢!

/****************************************************************************
** Form implementation generated from reading ui file 'Led_Key.ui'
**
** Created: Fri Feb 1 00:08:08 2013
**      by:The User Interface Compiler (uic)
**
** WARNING! All changes made in this file will be lost!
****************************************************************************/
#include "Led_Key.h"

#include <qbuttongroup.h>
#include <qcheckbox.h>
#include <qlabel.h>
#include <qlineedit.h>
#include <qpushbutton.h>
#include <qlayout.h>
#include <qvariant.h>
#include <qtooltip.h>
#include <qwhatsthis.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/select.h>
#include <sys/time.h>
#include <errno.h>

static int fd,fd1;
static char led1_s = 0, led2_s = 0, led3_s = 0, led4_s = 0;
char key_value_temp;
/*
*Constructs a Led_Key which is a child of 'parent', with the
*name 'name' and widget flags set to 'f'
*/
Led_Key::Led_Key( QWidget* parent,const char* name, WFlags fl )
    : QWidget( parent, name, fl )
{
    if ( !name )
        setName( "Led_Key" );
    resize( 402, 274 );
    setCaption( tr( "TQ2440 LED&KEY Test" ) );

    close = new QPushButton( this, "close" );
    close->setGeometry( QRect( 200, 130, 60, 20 ) );
    close->setText( tr( "Close" ) );

    led_g = new QButtonGroup( this, "led_g" );
    led_g->setGeometry( QRect( 10, 0, 250, 130 ) );
    led_g->setTitle( tr( "LED Test" ) );

    led4 = new QCheckBox( led_g, "led4" );
    led4->setGeometry( QRect( 11, 96, 78, 19 ) );
    led4->setText( tr( "LED4" ) );

    led3 = new QCheckBox( led_g, "led3" );
    led3->setGeometry( QRect( 11, 71, 78, 19 ) );
    led3->setText( tr( "LED3" ) );

    key_value = new QLabel( led_g, "key_value" );
    key_value->setGeometry( QRect( 100, 80, 70, 31 ) );
    key_value->setText( tr( "Key Value:" ) );

    LineEdit1 = new QLineEdit( led_g, "LineEdit1" );
    LineEdit1->setGeometry( QRect( 170, 81, 61, 23 ) );

    alloff = new QPushButton( led_g, "alloff" );
    alloff->setGeometry( QRect( 100, 50, 60, 20 ) );
    alloff->setText( tr( "All Off" ) );

    allon = new QPushButton( led_g, "allon" );
    allon->setGeometry( QRect( 100, 20, 60, 20 ) );
    allon->setText( tr( "All On" ) );

    led1 = new QCheckBox( led_g, "led1" );
    led1->setGeometry( QRect( 11, 21, 78, 19 ) );
    led1->setText( tr( "LED1" ) );

    led2 = new QCheckBox( led_g, "led2" );
    led2->setGeometry( QRect( 11, 46, 78, 19 ) );
    led2->setText( tr( "LED2" ) );


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

    // signals and slots connections
    connect( led1, SIGNAL( stateChanged(int) ), this, SLOT( led1_p() ) );
    connect( led2, SIGNAL( stateChanged(int) ), this, SLOT( led2_p() ) );
    connect( led3, SIGNAL( stateChanged(int) ), this, SLOT( led3_p() ) );
    connect( led4, SIGNAL( stateChanged(int) ), this, SLOT( led4_p() ) );
    connect( allon, SIGNAL( clicked() ), this, SLOT( all_on() ) );
    connect( alloff, SIGNAL( clicked() ), this, SLOT( all_off() ) );
    connect( close, SIGNAL( clicked() ), this, SLOT( close() ) );

        fd = open("/dev/TQ2440_leds", O_RDWR);
        if (fd < 0)
        {
                perror("open device EmbedSky-leds");
                SLOT( close() );
        }
        for (int i = 0 ; i < 4; i ++)
        {
                ioctl(fd, 0, i);
        }
        fd1=open("/dev/TQ2440_buttons",0);
        if(fd1<0)
        {
                perror("can't open device buttons");
                SLOT( close() );
        }
}

/*
*Destroys the object and frees any allocated resources
*/
Led_Key::~Led_Key()
{
    // no need to delete child widgets, Qt does it all for us
}

void Led_Key::getkey()
{       
        timer->stop();
        fd_set rds;
        int ret,i;

        FD_ZERO(&rds);
        FD_SET(fd1,&rds);

        ret=select(fd1 + 1,&rds,NULL,NULL,NULL);
        if(ret<0)
        {
                perror("select");
                exit(1);               
        }
        if(ret==0)
        {
                printf("Timeout.\n");
        }
        else if(FD_ISSET(fd1,&rds))
        {
                int ret =read(fd1,key_value_temp,sizeof(key_value_temp));
                if(ret != sizeof(key_value_temp))
                {
                        if(errno != EAGAIN)
                                perror("read buttons\n");
                }
                else
                {
                        for(i=0;i<4;i++)
                                if( key_value_temp != 0 )
                                {
                                        char temp;
                                        temp=key_value_temp+0x30;
                                        temp='\0';
                                        LineEdit1->setText(tr(temp));                               
                                }
                }
        }
        timer->start(1000);
}

void Led_Key::all_off()
{
        for (int i = 0 ; i < 4; i ++)
        {
                ioctl(fd, 0, i);
        }
        led1->setChecked( 0);
        led2->setChecked( 0);
        led3->setChecked( 0);
        led4->setChecked( 0);
//    qWarning( "ledplayer::all_off(): Not implemented yet!" );
}

void Led_Key::all_on()
{
        for (int i = 0 ; i < 4; i ++)
        {
                ioctl(fd, 1, i);
        }
        led1->setChecked( 1);
        led2->setChecked( 1);
        led3->setChecked( 1);
        led4->setChecked( 1);
//    qWarning( "ledplayer::all_on(): Not implemented yet!" );
}

void Led_Key::led1_p()
{
        led1_s = ~led1_s;
//        printf("led1_s = %d\n", led1_s);
        if ( led1_s == 0)
                ioctl(fd, 0, 0);
        else
                ioctl(fd, 1, 0);
//    qWarning( "ledplayer::led1_p(): Not implemented yet!" );
}

void Led_Key::led2_p()
{
        led2_s = ~led2_s;
//        printf("led2_s = %d\n", led2_s);
        if ( led2_s == 0)
                ioctl(fd, 0, 1);
        else
                ioctl(fd, 1, 1);
//    qWarning( "ledplayer::led2_p(): Not implemented yet!" );
}

void Led_Key::led3_p()
{
        led3_s = ~led3_s;
//        printf("led3_s = %d\n", led3_s);
        if ( led3_s == 0)
                ioctl(fd, 0, 2);
        else
                ioctl(fd, 1, 2);
//    qWarning( "ledplayer::led3_p(): Not implemented yet!" );
}

void Led_Key::led4_p()
{
        led4_s = ~led4_s;
//        printf("led4_s = %d\n", led4_s);
        if ( led4_s == 0)
                ioctl(fd, 0, 3);
        else
                ioctl(fd, 1, 3);
//    qWarning( "ledplayer::led4_p(): Not implemented yet!" );
}
页: [1]
查看完整版本: QT程序:Led与按键显示