1. 程式人生 > 其它 >Qt:蜂鳴器

Qt:蜂鳴器

技術標籤:C++qtqt

Qt:蜂鳴器原始碼(注:根據C改寫的)
需用到Tiny4412ADK 開發板

beep.h

#ifndef BEEP_H
#define BEEP_H

#define PWM_IOCTL_SET_FREQ 1
#define PWM_IOCTL_STOP 0
#define ESC_KEY 0x1b

#include <QMainWindow>
#include <stdio.h>
#include <termios.h>
#include <unistd.h>
#include <stdlib.h>
#include<fcntl.h>
#include<sys/ioctl.h>


namespace Ui {
class Beep;
}

class Beep : public QMainWindow
{
    Q_OBJECT
    
public:
    explicit Beep(QWidget *parent = 0);
    ~Beep();
    int fd;
    int freq;

private slots:
    void on_pushButton_clicked();

    void on_pushButton_2_clicked();

private:
    Ui::Beep *ui;
};

#endif // BEEP_H

beep.cpp

#include "beep.h"
#include "ui_beep.h"

Beep::Beep(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::Beep)
{
    ui->setupUi(this);
    system("/usr/bin/killall qpe qss quicklauncher");
    system("/bin/gpsshareqt4");
}

Beep::~Beep()
{
    delete ui;
    system("/bin/qtopia");
}


void Beep::on_pushButton_clicked() *//open按鈕*
{
    fd=-1;
    fd = open("/dev/pwm", 0);
    if (fd < 0)
    {
        perror("open pwm_buzzer device");
        exit(1);
    }
    // any function exit call will stop the buzzer
    // 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 Beep::on_pushButton_2_clicked()  *//pause按鈕*
{
    int ret = ioctl(fd, PWM_IOCTL_STOP);
    if(ret < 0)
    {
        perror("stop the buzzer");
    }
     ::close(fd);
}

main.cpp

#include <QApplication>
#include "beep.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Beep w;
    w.show();
    return a.exec();
}

ui介面圖