Hey guys,
I have a gui that outputs a sine wave and square wave with user input frequency and amplitude. My code to produce both the square and sine wave is tested and works successfully.
My problem is: I have the sine wave working. I used threads to start and eliminate(from click of pushButtons) the generation of the sine wave. I am trying to do the same for the square wave, but when I click the pushButton, it generates the sine wave again.
I am not sure where I am going wrong. Any help would be appreciated. Code snippets below:
wave.cpp:
ui(new Ui::wave)
{
ui->setupUi(this);
setup();
thread = new MyThread();
thread2 = new MyThread();
}
wave::~wave()
{
delete ui;
delete thread;
delete thread2;
}
void wave::on_pushButton_clicked()
{
//Generate
freq = ui->frequency->text().toDouble();
ampl = ui->amplitude->text().toDouble();
thread->start();
ui->pushButton->setEnabled(false);
ui->pushButton->setStyleSheet("background-color: gray");
ui->pushButton_2->setStyleSheet("background-color: rgb(255, 192, 192);"
"color: red;");
ui->pushButton_2->setEnabled(true);
}
void wave::on_pushButton_2_clicked()
{
//Terminate
thread->terminate();
ui->pushButton_2->setEnabled(false);
ui->pushButton_2->setStyleSheet("background-color: gray");
ui->pushButton->setStyleSheet("background-color: rgb(192, 255, 208);"
"color: green;");
ui->pushButton->setEnabled(true);
}
void wave::on_pushButton_3_clicked()
{
freq = ui->frequency->text().toDouble();
ampl = ui->amplitude->text().toDouble();
thread2->start();
ui->pushButton_3->setEnabled(false);
ui->pushButton_3->setStyleSheet("background-color: gray");
ui->pushButton_4->setStyleSheet("background-color: rgb(255, 192, 192);"
"color: red;");
ui->pushButton_4->setEnabled(true);
}
void wave::on_pushButton_4_clicked()
{
thread2->terminate();
ui->pushButton_4->setEnabled(false);
ui->pushButton_4->setStyleSheet("background-color: gray");
ui->pushButton_3->setStyleSheet("background-color: rgb(192, 255, 208);"
"color: green;");
ui->pushButton_3->setEnabled(true);
}
wave::wave(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::wave)
{
ui->setupUi(this);
setup();
thread = new MyThread();
thread2 = new MyThread();
}
wave::~wave()
{
delete ui;
delete thread;
delete thread2;
}
void wave::on_pushButton_clicked()
{
//Generate
freq = ui->frequency->text().toDouble();
ampl = ui->amplitude->text().toDouble();
thread->start();
ui->pushButton->setEnabled(false);
ui->pushButton->setStyleSheet("background-color: gray");
ui->pushButton_2->setStyleSheet("background-color: rgb(255, 192, 192);"
"color: red;");
ui->pushButton_2->setEnabled(true);
}
void wave::on_pushButton_2_clicked()
{
//Terminate
thread->terminate();
ui->pushButton_2->setEnabled(false);
ui->pushButton_2->setStyleSheet("background-color: gray");
ui->pushButton->setStyleSheet("background-color: rgb(192, 255, 208);"
"color: green;");
ui->pushButton->setEnabled(true);
}
void wave::on_pushButton_3_clicked()
{
freq = ui->frequency->text().toDouble();
ampl = ui->amplitude->text().toDouble();
thread2->start();
ui->pushButton_3->setEnabled(false);
ui->pushButton_3->setStyleSheet("background-color: gray");
ui->pushButton_4->setStyleSheet("background-color: rgb(255, 192, 192);"
"color: red;");
ui->pushButton_4->setEnabled(true);
}
void wave::on_pushButton_4_clicked()
{
thread2->terminate();
ui->pushButton_4->setEnabled(false);
ui->pushButton_4->setStyleSheet("background-color: gray");
ui->pushButton_3->setStyleSheet("background-color: rgb(192, 255, 208);"
"color: green;");
ui->pushButton_3->setEnabled(true);
}
To copy to clipboard, switch view to plain text mode
wave.h:
#ifndef WAVE_H
#define WAVE_H
#include "ui_wave.h"
#include <alsa/asoundlib.h>
#include <QMainWindow>
#include <QObject>
#include "mythread.h"
namespace Ui {
class wave;
}
{
Q_OBJECT
MyThread *thread;
MyThread *thread2;
public:
explicit wave
(QWidget *parent
= 0);
~wave();
private slots:
void on_pushButton_clicked();
void on_pushButton_2_clicked();
void on_pushButton_3_clicked();
void on_pushButton_4_clicked();
private:
Ui::wave *ui;
};
void onWriteLoopSine();
void onWriteLoopSquare();
#endif // WAVE_H
#ifndef WAVE_H
#define WAVE_H
#include "ui_wave.h"
#include <alsa/asoundlib.h>
#include <QMainWindow>
#include <QObject>
#include "mythread.h"
namespace Ui {
class wave;
}
class wave : public QMainWindow
{
Q_OBJECT
MyThread *thread;
MyThread *thread2;
public:
explicit wave(QWidget *parent = 0);
~wave();
private slots:
void on_pushButton_clicked();
void on_pushButton_2_clicked();
void on_pushButton_3_clicked();
void on_pushButton_4_clicked();
private:
Ui::wave *ui;
};
void onWriteLoopSine();
void onWriteLoopSquare();
#endif // WAVE_H
To copy to clipboard, switch view to plain text mode
mythread.h:
#ifndef MYTHREAD_H
#define MYTHREAD_H
#include <QThread>
{
Q_OBJECT
protected:
void run();
public:
explicit MyThread
(QObject *parent
= 0);
};
#endif // MYTHREAD_H
#ifndef MYTHREAD_H
#define MYTHREAD_H
#include <QThread>
class MyThread : public QThread
{
Q_OBJECT
protected:
void run();
public:
explicit MyThread(QObject *parent = 0);
};
#endif // MYTHREAD_H
To copy to clipboard, switch view to plain text mode
mythread.cpp:
#include "wave.h"
#include "mythread.h"
MyThread
::MyThread(QObject *parent
) :{
}
void MyThread::run()
{
onWriteLoopSine();
onWriteLoopSquare();
}
#include "wave.h"
#include "mythread.h"
MyThread::MyThread(QObject *parent) :
QThread(parent)
{
}
void MyThread::run()
{
onWriteLoopSine();
onWriteLoopSquare();
}
To copy to clipboard, switch view to plain text mode
Bookmarks