PWM QT GUI in friendly arm mini2440 - qt

I am using a mini2440 board. I want to develop a gui for pwm that can increase and decrease the frequency of the signal. It has four buttons - start, stop, max and min - but pressing the max button makes no change in freq. By using the start and stop the gui program terminate but still pwm is on, I mean buzzer is on.
Here is the code
#include "hello.h"
#include <qlabel.h>
#include <qpushbutton.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <linux/fs.h>
#include <errno.h>
#include <string.h>
#include <qobject.h>
#include <fstream>
#include <linux/i2c-dev.h>
#include <string.h>
#include <stdio.h>
#include <linux/fs.h>
#include <termios.h> //Headers
#include <stdint.h>
#include <getopt.h>
#include <linux/types.h>
#include <linux/spi/spidev.h>
#include <time.h>
#include <string.h>
#include <sys/time.h>
#define PWM_IOCTL_SET_FREQ 1
#define PWM_IOCTL_STOP 0
int freq=1000;
int fd;
HelloForm::HelloForm( QWidget* parent, const char* name, WFlags fl)
:HelloBaseForm(parent, name, fl)
{
connect(PushButton33,SIGNAL(clicked()),this,SLOT(start()));
connect(PushButton30,SIGNAL(clicked()),this,SLOT(stop()));
connect(PushButton31,SIGNAL(clicked()),this,SLOT(plus()));
connect(PushButton32,SIGNAL(clicked()),this,SLOT(minus()));
}
HelloForm::~HelloForm()
{
}
// this is start button
void HelloForm::start()
{
int fd = open("/dev/pwm", 0);
if (fd < 0) {
status->setText("open pwm_buzzer device");
}
else {
status->setText("start");
}
ioctl(fd, PWM_IOCTL_SET_FREQ, freq);
pwm->setText(QString::number(freq));
//close(fd);
}
//stop button
void HelloForm::stop()
{
close(fd);
int fd=open("/dev/pwm",0); //File handler
ioctl(fd, PWM_IOCTL_STOP); // stop the ioctl
close(fd);
}
// increase the pwm
void HelloForm::plus()
{
if(freq<20000)
{
freq+=10;
ioctl(fd,PWM_IOCTL_SET_FREQ,freq); //freq set
pwm->setText(QString::number(freq));
}
}
void HelloForm::minus()
{
if(freq>11)
{
freq-=10;
ioctl(fd,PWM_IOCTL_SET_FREQ,freq);
pwm->setText(QString::number(freq));
}
}

Related

Facing some problem with global Variable in QT C++

I have a login page and a welcome page (which open if user can bypass the login). In my login page
there is a line edit for entering username. So, for holding the username I declared a global variable in
the login.h.
My login.h file:
#include <QSqlQuery>
#include <QGridLayout>
#include <QPushButton>
#include <QString>
#include <QLabel>
#include <QFrame>
#include <QSqlRecord>
#include <QFileDialog>
#include <QPixmap>
#include <QMessageBox>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class login : public Qlogin
{
Q_OBJECT
public:
login(QWidget *parent = nullptr);
~login();
private:
Ui::login *ui;
QString userDat; ///global variable
};
#endif // MAINWINDOW_H
I hold the username from the line edit in login.cpp like this.
My login.cpp file:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QSqlQueryModel>
#include <QSqlQuery>
#include <QGridLayout>
#include <QPushButton>
#include <QString>
#include <QLabel>
#include <QFrame>
#include <QSqlRecord>
#include <QFileDialog>
#include <QPixmap>
#include <QMessageBox>
login::login(QWidget *parent)
: Qlogin(parent)
, ui(new Ui::login)
{
ui->setupUi(this);
userDat = ui-> login_lineEdit ->text();
}
login::~login()
{
delete ui;
}
I want to use the global variable userDat in my welcome.cpp file like this.
ui-> welcome_lineEdit->setText(userDat);
I've included all required header file in welcome.h and welcome.cpp. But it still says:
userDat is not declared in this scope.
please help
Update from comments:
#include <QSqlQuery>
#include <QGridLayout>
#include <QPushButton>
#include <QString>
#include <QLabel>
#include <QFrame>
#include <QSqlRecord>
#include <QFileDialog>
#include <QPixmap>
#include <QMessageBox>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class UserData {
public:
static QString userName;
};
class login : public Qlogin
{
Q_OBJECT
public:
login(QWidget *parent = nullptr);
~login();
private:
Ui::login *ui;
};
#endif // MAINWINDOW_H
in login.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QSqlQueryModel>
#include <QSqlQuery>
#include <QGridLayout>
#include <QPushButton>
#include <QString>
#include <QLabel>
#include <QFrame>
#include <QSqlRecord>
#include <QFileDialog>
#include <QPixmap>
#include <QMessageBox>
QString UserData::userName = "empty";
login::login(QWidget *parent)
: Qlogin(parent)
, ui(new Ui::login)
{
ui->setupUi(this);
UserData::userName = ui-> login_lineEdit ->text();
}
login::~login()
{
delete ui;
}
and somethere in another place:
ui-> welcome_lineEdit->setText(UserData::userName);

Signal is emitting, connection returns true, slot not triggering

This is the second way I have rearranged this, and it is doing the same thing, so now I seek help. There are no errors being thrown, the slot just never does anything.
There are two other connections that are working between the same two cpps, and I decided to add this third one that the main window triggers, to the main window.
on_line_edit_returnPressed(), is printing hol_num and that is where it ends. ReadyHollander emits and HolPub never does anything. What am I doing wrong?
mainwindow cpp
#include "wheelscannerui.h"
#include "./ui_wheelscannerui.h"
gui_image_node *m_gui_image_node;
WheelScannerUI::WheelScannerUI(QWidget *parent)
: QMainWindow(parent),
ui(new Ui::WheelScannerUI)
{
ui->setupUi(this);
connect(m_gui_image_node, &gui_image_node::ReadyImage, this, &WheelScannerUI::updateWheelImage);
connect(m_gui_image_node, &gui_image_node::OpenHollander, this, &WheelScannerUI::Open_No_ID);
connect(this, &WheelScannerUI::ReadyHollander, m_gui_image_node, &gui_image_node::HolPub);
qDebug() << connect(this, &WheelScannerUI::ReadyHollander, m_gui_image_node, &gui_image_node::HolPub);
ui->lineEdit->setVisible(false);
QMainWindow::showFullScreen();
}
WheelScannerUI::~WheelScannerUI()
{
delete ui;
}
void WheelScannerUI::Open_No_ID(QString qsteve)
{
ui->lineEdit->setVisible(true);
}
void WheelScannerUI::on_lineEdit_returnPressed()
{
QString hol_num = ui->lineEdit->text();
Q_EMIT ReadyHollander(hol_num);
ui->lineEdit->setVisible(false);
ui->lineEdit->clear();
qDebug() << hol_num;
}
main windows .h
#ifndef WHEELSCANNERUI_H
#define WHEELSCANNERUI_H
#include <QMainWindow>
#include <QtSql>
#include <QSqlQuery>
#include <QSqlDatabase>
#include <QSqlQueryModel>
#include "gui_image_node.h"
#include <QPixmap>
#include <ros/ros.h>
#include <std_msgs/String.h>
#include <std_msgs/Int64.h>
#include <sensor_msgs/image_encodings.h>
#include <nodelet/nodelet.h>
#include <image_transport/image_transport.h>
#include <cv_bridge/cv_bridge.h>
#include <opencv2/highgui/highgui.hpp>
#include <pluginlib/class_list_macros.h>
#include <stdio.h>
#include <string.h>
#include <sstream>
#include <iostream>
extern gui_image_node *m_gui_image_node;
QT_BEGIN_NAMESPACE
namespace Ui { class WheelScannerUI; }
QT_END_NAMESPACE
class WheelScannerUI : public QMainWindow
{
Q_OBJECT
public:
WheelScannerUI(QWidget *parent = nullptr);
~WheelScannerUI();
WheelScannerUI *m_WheelScannerUI;
void connectionClose()
{
db.close();
db.removeDatabase(QSqlDatabase::defaultConnection);
}
bool connectionOpen()
{
db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("~/wheels.db");
if(!db.open())
{
qDebug()<<("Database failed to open");
return false;
}
else{
qDebug()<<("database connected");
return true;
}
}
public slots:
Q_SLOT void Open_No_ID(QString qsteve);
Q_SLOT void open_db_table(); //trigger with a ros callback?
signals:
Q_SIGNAL void ReadyHollander(QString);
private slots:
void on_lineEdit_returnPressed();
void on_Mode_Button_clicked();
private:
Ui::WheelScannerUI *ui;
QSqlDatabase db;
};
#endif // WHEELSCANNERUI_H
node cpp
#include "gui_image_node.h"
gui_image_node::gui_image_node()
{
}
bool gui_image_node::init(int argc, char** argv)
{
m_pThread = new QThread();
this->moveToThread(m_pThread);
connect(m_pThread, &QThread::started, this, &gui_image_node::run);
ros::init(argc, argv, "wheels_gui_image");
if ( ! ros::master::check() )
{
return false;
}
ros::start();
ros::Time::init();
ros::NodeHandle nh;
hollander_pub = nh.advertise<std_msgs::String>("/hollander_chat", 1);
hol_trigger = nh.subscribe("awaiting_hollander", 1, &gui_image_node::Hollander_Screen_trigger_callback, this);
m_pThread->start();
return true;
}
void gui_image_node::Hollander_Screen_trigger_callback(const std_msgs::String::ConstPtr& msg)
{
std::string steve = msg->data;
QString qsteve = QString::fromStdString(steve);
Q_EMIT OpenHollander(qsteve);
}
void gui_image_node::HolPub(QString hol_num)
{
qDebug() << "received number";
std::string hol_num_conv = hol_num.toUtf8().constData();
std_msgs::String msg;
msg.data = hol_num_conv;
hollander_pub.publish(msg);
}
nodes .h
#ifndef GUI_IMAGE_NODE_H
#define GUI_IMAGE_NODE_H
#include <ros/ros.h>
#include <nodelet/nodelet.h>
#include <image_transport/image_transport.h>
#include <cv_bridge/cv_bridge.h>
#include <opencv2/highgui/highgui.hpp>
#include <std_msgs/String.h>
#include <QMainWindow>
#include <QObject>
#include <QSharedDataPointer>
#include <QWidget>
#include <QThread>
#include <string>
#include <QMutex>
#include <QDebug>
#include <QDialog>
#include <stdio.h>
#include <string.h>
#include <sstream>
#include <iostream>
class gui_image_node : public QThread
{
Q_OBJECT
public:
gui_image_node();
~gui_image_node();
bool init(int argc, char** argv);
void Hollander_Screen_trigger_callback(const std_msgs::String::ConstPtr& msg);
protected:
public slots:
Q_SLOT void run();
Q_SLOT void HolPub(QString hol_num);
signals:
Q_SIGNAL void OpenHollander(QString);
private:
ros::Publisher hollander_pub;
ros::Subscriber hol_trigger;
QThread * m_pThread;
};
#endif // GUI_IMAGE_NODE_H
OK, I figured it out, connect(this, &WheelScannerUI::ReadyHollander, m_gui_image_node, &gui_image_node::HolPub, Qt::Directconnection);
Since this is being triggered from another thread, Directconnection allows the thread it is being emitted from to trigger the slot in a different thread.

QT load QLibrary user32

Im trying to load user32 in QT5 by doing:
#include <qlibrary.h>
#include <windows.h>
#include <lmcons.h>
#include <process.h>
#include <stdio.h>
#include <userenv.h>
#include <winuser.h>
QLibrary *lib = new QLibrary("user32");
QFunctionPointer p;
if(lib->load() && (p = lib->resolve("GetLastInputInfo"))) {
qDebug() << "Lib loaded";
} else {
qDebug() << "Lib could not load";
}
But keep getting output "Lib could not load"
Any ideas what I could be doing wrong?

Qt QOpenGLFunctions not declared in this scope

I'm creating a sort of drawable object class for mesh data and i'm getting this linker error. This is also on top of another class that handles the drawing of the meshes that was used in one of the Qt tutorials. I'm also using a QGLWidget to be the surface i'm drawing to. Here's what the header file looks for the first class.
#ifndef GLOBJECT_H
#define GLOBJECT_H
#include <QOpenGLFunctions>
#include <QOpenGLShaderProgram>
#include <QVector3D>
#include <QVector2D>
#include <QDebug>
#include <QFile>
struct VertexData
{
QVector3D position;
QVector2D texCoord;
QVector3D normal;
};
class GLObject
{
public:
GLObject();
VertexData *data;
GLushort *indices;
GLuint vboIds[2];
int faceCount, vertCount;
bool generateFromPLY(QString filename);
};
#endif // GLOBJECT_H
Here's the header for the other class.
#ifndef GEOMETRYENGINE_H
#define GEOMETRYENGINE_H
#include <QObject>
#include <QOpenGLFunctions>
#include <QOpenGLShaderProgram>
#include <QVector2D>
#include <QVector3D>
#include <QFile>
#include <QDebug>
#include <QVector>
#include <globject.h>
class GeometryEngine : public QObject, protected QOpenGLFunctions
{
Q_OBJECT
public:
GeometryEngine();
~GeometryEngine();
void init();
void drawGeometry(QOpenGLShaderProgram *program);
//void drawCubeGeometry(QOpenGLShaderProgram *program);
bool generateFromPly(QString filename);
QVector<GLObject> drawables;
int drawableId = 0;
};
#endif // GEOMETRYENGINE_H
G:\Dropbox\GLSLDemo\globject.cpp:60: error: 'glGenBuffers' was not declared in this scope
glGenBuffers(2, vboIds);
along with the same error for the other gl calls.
Initially I had all the code in GeometryEngine to begin with. I didn't have a scope issue then. initilizeOpenGLFunctions() is called in GeometryEngine's init() if that's relevant.
^
QT OpenGL is weird. I think the QOpenGLFunctions maintains a common context. Also i'm bad at C++ and used protected wrong.

Unable to determine reason for SIGPIPE

I have a Client Server where client issues file operations to Server. Program runs perfectly when first read/delete command is issued. But when I issue second command read/delete, it exits with exit code 141. I determine reason to be SIGPIPE.But unable to resolve it. Can someone help me on this
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <string.h>
#include <sys/wait.h>
#include <mqueue.h>
#include <sys/stat.h>
//#include <limits.h>
#include "Functions.h"
#define PIPE_BUF 50000
#define MAXMESGDATA (PIPE_BUF -2*sizeof(long))
#define MESGHDRSIZE (sizeof(Message_buf) -MAXMESGDATA)
#define MAX_SIZE 512
pid_t serverPid;
pid_t clientPid;
void Server(int readfd,int writefd)
{
Message_buf server_MessageBuf;
int operationStatus = 0;
char inputFileName[MAXMESGDATA];
char operationToBePerformed[MAXMESGDATA];
char messageOnPIPE[MAXMESGDATA];
ssize_t length;
if((length=mesg_recv(readfd,&server_MessageBuf))==0)
{
printf("\n End of file while reading pathname");
}
strcpy(messageOnPIPE,server_MessageBuf.messageText);
printf("\n Server side Message on PIPE:%s \n ",messageOnPIPE);
operationStatus=interpretCommand(messageOnPIPE,operationToBePerformed,inputFileName);
if(strcasecmp(operationToBePerformed,"read")==0)
{
readFile(writefd,inputFileName);
//printf("\n Read %s ",inputFileName);
}
if(strcasecmp(operationToBePerformed,"delete")==0)
{
deleteFile(writefd,inputFileName);
}
}
int main()
{
int pipe1[2],pipe2[2];
pipe(pipe1);
pipe(pipe2);
//signal(SIGPIPE, SIG_IGN);
pid_t pid;
pid=fork();
serverPid=pid;
if(pid==0)
{
/*Call Server*/
close(pipe1[1]);
close(pipe2[0]);
Server(pipe1[0], pipe2[1]);
}
else
{
close(pipe1[0]);
close(pipe2[1]);
Client(pipe2[0],pipe1[1]);
}
return 0;
}
Your server is not running in a loop. It receives one message and then closes the pipe, so the second write fails and a SIGPIPE is sent to the client.

Resources