|
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来处理啊。 |
|