mini2440之--pwm程序

如图所示,图中用到了:PixmapLabel,TextLabel,LCDNumber,GroupBox,PushButton组件

pwm.h

#ifndef MYHELLOFORM_H
#define MYHELLOFORM_H
#include "pwm_base.h"
#include <qpixmap.h>
 
class MyPwmForm : public PwmBaseForm
{ 
	Q_OBJECT
	
	public:
	    	MyPwmForm( QWidget* parent = 0, const char* name = 0, WFlags fl = 0 );
	    	virtual ~MyPwmForm();
	    	//const QPixmap *eddy;
		int freq;
		//int fd;
 
	private slots:
	    	void buzzInc();
	    	void buzzDec();
		void buzzON();
	    	void buzzOFF();
};
 
#endif // MYHELLOFORM_H

pwm.cpp

#include "pwm.h"
#include <qlabel.h>
#include <qlcdnumber.h>
#include <qpushbutton.h>
#include <qbutton.h>
 
#include <qtopia/qpeapplication.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
 
 
#include <stdio.h>
#include <termios.h>
#include <unistd.h>
#include <stdlib.h>
 
 
#include <sys/ioctl.h>
 
static int fd;
#define PWM_IOCTL_SET_FREQ		1
#define PWM_IOCTL_STOP			2
 
MyPwmForm::MyPwmForm( QWidget* parent, const char* name, WFlags fl)
	    :PwmBaseForm(parent, name, fl)
{
	fd = -1;
	freq = 1000;
	LCDNumber1->setDecMode();
	LCDNumber1->display(freq);
    PushButton1->setEnabled( 0 );
    PushButton2->setEnabled( 0 );
    //PushButton1->show();
    //PushButton2->hide();
   
	connect(PushButton1,SIGNAL(clicked()),this,SLOT(buzzInc()));
	connect(PushButton2,SIGNAL(clicked()),this,SLOT(buzzDec()));
	connect(PushButton3,SIGNAL(clicked()),this,SLOT(buzzON()));
	connect(PushButton4,SIGNAL(clicked()),this,SLOT(buzzOFF()));
}
 
MyPwmForm::~MyPwmForm()
{
	buzzOFF();
}
 
static void close_buzzer(void)
{
	if (fd >= 0) {
		::ioctl(fd, PWM_IOCTL_STOP);
		::close(fd);
		fd = -1;
	}
}
static void set_buzzer_freq(int freq)
{
    // this IOCTL command is the key to set frequency
    int ret = ::ioctl(fd, PWM_IOCTL_SET_FREQ, freq);
    if(ret < 0) {
	::perror("set the frequency of the buzzer");
	::exit(1);
    }
}
 
void MyPwmForm::buzzON()
{
 
    fd = ::open("/dev/pwm", 0);
    if (fd < 0) {
	::perror("open pwm_buzzer device");
	::exit(1);
    }
    set_buzzer_freq(freq);
    LCDNumber2->display(fd);
    // any function exit call will stop the buzzer
    ::atexit(close_buzzer);
 
    PushButton1->setEnabled( 1 );
    PushButton2->setEnabled( 1 );
}
 
void MyPwmForm::buzzOFF()
{
    int ret = ::ioctl(fd, PWM_IOCTL_STOP);	//PWM_IOCTL_STOP= 2
    //int ret = ::ioctl(fd, 2);
    LCDNumber2->display(fd);
    if(ret < 0) {
	::perror("stop the buzzer");
	::exit(1);
    }
    ::close(fd);
 
    PushButton1->setEnabled( 0 );
    PushButton2->setEnabled( 0 );
    //::exit(0);	// exit the program directly
}
void MyPwmForm::buzzInc()
{
    if( freq < 20000 )
        freq += 10;
    LCDNumber1->display(freq);
    
    set_buzzer_freq(freq);
}
 
void MyPwmForm::buzzDec()
{
    if( freq > 11 )
        freq -= 10 ;
    LCDNumber1->display(freq);
    
    set_buzzer_freq(freq);
}
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值