天嵌 ARM开发社区

 找回密码
 注册
查看: 5846|回复: 6

GPIO按键与QT界面

[复制链接]
rx_leo 发表于 2010-7-21 22:44:44 | 显示全部楼层 |阅读模式
我想在qtopia的主界面中就能捕获到键盘消息,从而能够利用键盘实现关机功能,不知道应该修改qtopia的哪一个程序文件。
于是利用里面的qtopia程序ledtest做测试,我想让按键k1¬4也能达到程序界面操作一样的效果。
但是不知道,怎样修改ledplay.cpp实现我的功能啊,我看ledplay.cpp里面实现的就是几个SLOT函数,在哪儿添加我的按键读出与处理函数啊?
亚瑟王 发表于 2010-7-24 21:24:00 | 显示全部楼层
在LED测试程序中添加一个读取键盘的函数,然后可以解决了。
 楼主| rx_leo 发表于 2010-7-26 12:26:09 | 显示全部楼层
2# 亚瑟王


我现在不是用键盘,是GPIO按键嘛,所以不知道这个按键怎么触发QT,你说在QT中加个函数,但是什么时候执行这个函数,由什么触发呢,我想了好久,在网上也找了好久,一种是使用自定义键盘(不知道怎么改)
还有用Qtimer:
ledplayer::ledplayer( QWidget* parent,  const char* name, WFlags fl )
    : QWidget( parent, name, fl )
{
    QPixmap image0( ( const char** ) image0_data );
    if ( !name )
        setName( "ledplayer" );
    resize( 406, 274 );
    setCaption( tr( "EmbedSky LED Test" ) );

    logo = new QLabel( this, "logo" );
    logo->setGeometry( QRect( 210, 10, 28, 98 ) );
    logo->setPixmap( image0 );
    logo->setScaledContents( TRUE );

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

    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" ) );

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

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

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

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

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

    // signals and slots connections
    connect( led1, SIGNAL( stateChanged(int) ), this, SLOT( c() ) );
    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() ) );
    connect( this, SIGNAL( mysignal1() ), SLOT( led1_p() ) );
    connect( this, SIGNAL( mysignal2() ), SLOT( led2_p() ) );
    connect( this, SIGNAL( mysignal3() ), SLOT( led3_p() ) );
    connect( this, SIGNAL( mysignal4() ), SLOT( led4_p() ) );
    readpress = new QTimer(this);
    connect(readpress,SIGNAL(timeout()),this,SLOT(getkey()));
    readpress->start(2*1000);


        system("/etc/rc.d/init.d/leds stop");
        fd1 = open("/dev/GPIO-Control", O_RDWR);
        fd2 =open("/dev/IRQ-Test", 0);
        if (fd1 < 0)
        {
                perror("open device GPIO-Control");
                SLOT( close() );
        }
        if (fd2 < 0)
        {
                perror("open device IRQ-Test");
        }

        for (int i = 0 ; i < 4; i ++)
        {
                ioctl(fd1, 0, i);
        }
}

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

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

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

void ledplayer::led1_p()
{
        readpress->stop();       
        leds_n[0] = ~leds_n[0];
//        printf("led1_s = %d\n", led1_s);
        if ( leds_n[0] == 0)
                ioctl(fd1, 0, 0);
        else
                ioctl(fd1, 1, 0);
//    qWarning( "ledplayer::led1_p(): Not implemented yet!" );
        readpress->start(1000);
}

void ledplayer::led2_p()
{
        readpress->stop();       
        leds_n[1] = ~leds_n[1];
//        printf("led2_s = %d\n", led2_s);
        if ( leds_n[1] == 0)
                ioctl(fd1, 0, 1);
        else
                ioctl(fd1, 1, 1);
//    qWarning( "ledplayer::led2_p(): Not implemented yet!" );
        readpress->start(1000);
}

void ledplayer::led3_p()
{
        readpress->stop();       
        leds_n[2] = ~leds_n[2];
//        printf("led3_s = %d\n", led3_s);
        if ( leds_n[2] == 0)
                ioctl(fd1, 0, 2);
        else
                ioctl(fd1, 1, 2);
//    qWarning( "ledplayer::led3_p(): Not implemented yet!" );
        readpress->start(1000);
}

void ledplayer::led4_p()
{
        readpress->stop();       
        leds_n[3] = ~leds_n[3];
//        printf("led4_s = %d\n", led4_s);
        if ( leds_n[3] == 0)
                ioctl(fd1, 0, 3);
        else
                ioctl(fd1, 1, 3);
//    qWarning( "ledplayer::led4_p(): Not implemented yet!" );
        readpress->start(1000);
}


int ret;
char key_value_temp[4];

void ledplayer::getkey()
{
        readpress->stop();
        /*开始读取键盘驱动发出的数据,注意key_value和键盘驱动中定义为一致的类型*/
        ret = read(fd2, key_value_temp, sizeof(key_value_temp));
        if (ret != sizeof(key_value_temp))
        {
                perror("read buttons:");
                goto out;
        }
       
        if(key_value[0] != key_value_temp[0])
        {
                //ledplayer->led1_p();
                //emit(mysignal1());
                ioctl(fd1, 0, 0);
                goto out;
        }
        if(key_value[1] != key_value_temp[1])
        {
                //ledplayer->led2_p();
                //emit(mysignal2());
                ioctl(fd1, 0, 1);
                goto out;
        }
        if(key_value[2] != key_value_temp[2])
        {
                //ledplayer->led3_p();
                //emit(mysignal3());
                ioctl(fd1, 0, 2);
                goto out;
        }
        if(key_value[3] != key_value_temp[3])
        {
                //ledplayer->led4_p();
                //emit(mysignal4());
                ioctl(fd1, 0, 3);
                goto out;
        }
         
        out:        readpress->start(1000);
}


您可以看到,蓝色都是我添加的代码,包括一个您说的读键盘函数,我用Qtimer不断触发,但是不管我把触发时间怎么改,程序总是有问题,要么是触摸屏对LED控制无效,要等按键来确认似的;要么是按键不管用。很纠结啊。很想知道,怎么把一个按钮直接当中断似的传到QT来处理啊。
亚瑟王 发表于 2010-8-2 11:18:30 | 显示全部楼层
闹不明白了,键盘和按键有啥区别,反正都是使用read函数读取驱动中对应的read函数里面的数据。
亚瑟王 发表于 2010-8-2 11:19:19 | 显示全部楼层
闹不明白了,键盘和按键有啥区别,反正都是使用read函数读取驱动中对应的read函数里面的数据。
 楼主| rx_leo 发表于 2010-8-4 10:01:40 | 显示全部楼层
5# 亚瑟王

键盘嘛,可以通过QEvent这个类中的函数来通知QT做处理按键啊之类的
我就想把自己的GPIO的几个按键当键盘的TAB Enter等键用,因为做的东西用不着其他键,就需要实现TAB Enter的功能   不知道在哪、怎么修改啊QT~
求亚瑟王帮忙啊~
亚瑟王 发表于 2010-8-4 22:53:47 | 显示全部楼层
方法是一样的,只要你把你获取的对应按键的键值提供给Qt程序就可以了。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

关闭

i.MX8系列ARM cortex A53 M4 工控板上一条 /1 下一条

Archiver|手机版|小黑屋|天嵌 嵌入式开发社区 ( 粤ICP备11094220号-2 )

GMT+8, 2024-6-26 21:19 , Processed in 1.078125 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表