Hardware Interface
The teach pendant has some buttons and rotary knobs. The following example demonstrates how to obtain their states:
Create a Qt window application
mainwindow.h
cpp
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QSocketNotifier>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void onDataAvailable();
void getKeyValue();
void getEnableValue();
private:
Ui::MainWindow *ui;
int switchfd = -1;
QSocketNotifier *switch_notifier;
int buttonfd = -1;
QSocketNotifier *button_notifier;
int enablefd = -1;// Enable
QSocketNotifier *enable_notifier;
};
#endif // MAINWINDOW_Hmainwindow.cpp
cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <unistd.h>
#include <linux/input.h>
#include <QFile>
#include <QDebug>
#include <QTime>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
// Open rotary knob device file
switchfd = ::open("/dev/buttons", O_RDONLY | O_NONBLOCK);
if (switchfd < 0) {
qCritical() << "Failed to open device:" << strerror(errno);
QCoreApplication::exit(1);
return;
}
switch_notifier = new QSocketNotifier(switchfd, QSocketNotifier::Read, this);
connect(switch_notifier, SIGNAL(activated(int)), this, SLOT(onDataAvailable()));
// Open button device file
buttonfd = ::open("/dev/input/event0", O_RDWR);
if (buttonfd < 0) {
qCritical() << "Failed to open device:" << strerror(errno);
QCoreApplication::exit(1);
return;
}
button_notifier = new QSocketNotifier(buttonfd,QSocketNotifier::Read, this);
connect(button_notifier, SIGNAL(activated(int)), this, SLOT(getKeyValue()));
// Open enable device file
enablefd = ::open("/dev/buttonstop", O_RDWR);
if (enablefd < 0) {
qCritical() << "Failed to open device:" << strerror(errno);
QCoreApplication::exit(1);
return;
}
enable_notifier = new QSocketNotifier(enablefd,QSocketNotifier::Read, this);
connect(enable_notifier, SIGNAL(activated(int)), this, SLOT(getEnableValue()));
}
MainWindow::~MainWindow()
{
delete ui;
if (switchfd >= 0) ::close(switchfd);
if (buttonfd >= 0) ::close(buttonfd);
if (enablefd >= 0) ::close(enablefd);
}
void MainWindow::onDataAvailable()
{
char switchbuttons[8]={'0','0','0','0','0','0','0','0'};
ssize_t bytesRead;
// Read device data
while ((bytesRead = ::read(switchfd, switchbuttons, sizeof(switchbuttons))) > 0) {
qDebug() << "Data received:" << QByteArray(switchbuttons, bytesRead);
QString CurMode = "";
QString teachMode="00001000";// Teach mode
QString runMode="00000100";// Run mode
QString remoteMode="00010000";// Remote mode
for(unsigned int i=0;i<sizeof(switchbuttons);i++)
{
CurMode.append(QString(QChar(switchbuttons[i])));
}
qDebug() << CurMode;
if(CurMode == teachMode)
{
qDebug() << "Teach mode";
}
else if(CurMode == runMode)
{
qDebug() << "Run mode";
}
else if(CurMode == remoteMode)
{
qDebug() << "Remote mode";
}
else
{
// Up 00100000
// Down 01000000
if(CurMode == QString("00100000"))
{
// Scroll up
qDebug() << "Side scroll wheel up";
}
else if(CurMode == QString("01000000"))
{
// Scroll down
qDebug() << "Side scroll wheel down";
}
}
}
if (bytesRead < 0 && errno != EAGAIN) {
qWarning() << "Read error:" << strerror(errno);
}
}
void MainWindow::getKeyValue()
{
struct input_event keyBuf;
unsigned int bytesRead = ::read(buttonfd,&keyBuf, sizeof(keyBuf));
if(bytesRead < sizeof(keyBuf))
{
return;
}
if(keyBuf.type==EV_KEY)
{
if(keyBuf.value==2)
{
return;
}
qDebug()<<"keyBuf.code = "<<keyBuf.code<<"keyBuf.value = "<<keyBuf.value<<"time = "<<QTime::currentTime().toString("hh:mm:ss.zzz")<<endl;
}
}
void MainWindow::getEnableValue()
{
char enablebuttons[8]={'1','1','1','1','1','1','1','1'};
if(::read(enablefd,enablebuttons,sizeof(enablebuttons))!=sizeof(enablebuttons))
{
return;
}
QString CurEnable = "";
for(unsigned int i=0;i<sizeof(enablebuttons);i++)
{
CurEnable.append(QString(QChar(enablebuttons[i])));
}
if(CurEnable == "11000000" ||CurEnable == "10000000" ||CurEnable == "01000000")
{
// Power on
qDebug() << "Power on";
}
else if(CurEnable == "00000000")
{
// Power off
qDebug() << "Power off";
}
}