刚开始自己学习MINI2440开发板,按照手册装了qtopia2.20平台,电脑用的是LINUX的。在网上看见人家写的一个小的测试程序,自己就想着改一下,用应用程序控制控制开发板的LED灯和蜂鸣器。这个小程序,希望能给像我一样刚学qt的朋友一些小的借鉴,大虾的话就无视小弟把。
由于是借鉴网上的一个朋友的程序,所以有些函数命名没有改动。。。。。
贴出代码:
counter.h
#ifndef COUNTER_H
#define COUNTER_H
#include <qlabel.h>
#include <qwidget.h>
#include <qpushbutton.h>
#include <qlayout.h>
#include <unistd.h>
#include <qevent.h>
class Counter : public QWidget
{
Q_OBJECT
public:
Counter(QWidget *parent=0 , const char *name=0, WFlags f=0);
~Counter();
QPushButton *AddButton;
QPushButton *SubButton;
QPushButton *CloButton;
QPushButton *AutoButton;
QPushButton *ManButton;
QPushButton *ADdButton;
QPushButton *SUbButton;
public slots:
void IncCounter();
void DecCounter();
void CloCounter();
void Man_Led_On();
void Time_Add();
void Time_Sub();
private:
int counter;
int i,j,k;
QVBoxLayout *counterVBoxLayout;
QHBoxLayout *counterHBoxLayout;
QLabel *CounterLabel;
QLabel *CreatorLabel;
QLabel *CloseLabel;
QLabel *TimeLabel;
};
#endif
counter.cpp
#include "counter.h"
#include <stdlib.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/resource.h>
#include <unistd.h>
#include <fcntl.h>
#include <qlabel.h>
Counter::Counter(QWidget *parent, const char *name, WFlags f) : QWidget(parent, name, f)
{
counter = 0;
AddButton = new QPushButton("Beep_On", this);
SubButton = new QPushButton("LED Auto On", this);
CloButton = new QPushButton("LED_Man_Off", this);
AutoButton = new QPushButton("Auto",this);
ManButton = new QPushButton("LED_Man_On",this);
CounterLabel = new QLabel(tr("0"), this);
ADdButton = new QPushButton("Add",this);
SUbButton = new QPushButton("Sub",this);
CounterLabel->setAlignment(Qt::AlignCenter);
CreatorLabel = new QLabel(tr("--By LiuChao"), this);
CreatorLabel->setAlignment(Qt::AlignRight);
counterVBoxLayout = new QVBoxLayout(this);
counterHBoxLayout = new QHBoxLayout(this);
counterVBoxLayout->addWidget(AddButton);
counterVBoxLayout->addWidget(SubButton);
counterHBoxLayout->addWidget(AutoButton);
counterHBoxLayout->addWidget(ManButton);
counterVBoxLayout->addWidget(CloButton);
counterVBoxLayout->addLayout(counterHBoxLayout);
counterVBoxLayout->addWidget(CloButton);
counterVBoxLayout->addWidget(ADdButton);
counterVBoxLayout->addWidget(SUbButton);
counterVBoxLayout->addWidget(CounterLabel);
counterVBoxLayout->addWidget(CreatorLabel);
counterVBoxLayout->addWidget(CloseLabel);
QObject::connect(AddButton, SIGNAL(clicked()), this, SLOT(IncCounter()));
QObject::connect(SubButton, SIGNAL(clicked()), this, SLOT(DecCounter()));
QObject::connect(CloButton, SIGNAL(clicked()), this, SLOT(CloCounter()));
QObject::connect(ADdButton, SIGNAL(clicked()), this, SLOT(Time_Add()));
QObject::connect(SUbButton, SIGNAL(clicked()), this, SLOT(Time_Sub()));
QObject::connect(AutoButton, SIGNAL(clicked()), this, SLOT(DecCounter()));
QObject::connect(ManButton, SIGNAL(clicked()), this, SLOT(Man_Led_On()));
}
Counter::~Counter()
{
}
void Counter ::Time_Add()
{
CounterLabel->setNum(++counter);
}
void Counter ::Time_Sub()
{
if(counter<=1)
{
counter=2;
}
CounterLabel->setNum(--counter);
}
void Counter ::IncCounter()
{
int fd = open("/dev/pwm",O_RDWR);
if(fd==-1)
{
CounterLabel->setNum(--counter);
}
ioctl(fd,1,1000);
}
void Counter ::Man_Led_On()
{
int k;
system("/etc/rc.d/init.d/leds stop");
int fd = open("/dev/leds",O_RDWR);
if(-1==fd)
{
CounterLabel->setNum(++counter);
}
else
{
for(k=0;k<4;k++)
{
ioctl(fd,0,k);
}
ioctl(fd,1,1);
}
}
void Counter ::DecCounter()
{
int i;
i=counter;
int k;
system("/etc/rc.d/init.d/leds stop");
int fd = open("/dev/leds",O_RDWR);
if(-1==fd)
{
CounterLabel->setNum(++counter);
}
else
{
for(k=0;k<4;k++)
{
ioctl(fd,0,k);
}
ioctl(fd,1,1);
sleep(i);
ioctl(fd,0,1);
}