-1

I have created small normal c++ code to test in raspberry pi which is working perfectly fine on the terminal.Now I want to write it down in qt, the problem here I am facing is I can able to move my functions inside the main function into another function. This is the code: rotary_encoder.cpp

#include <iostream>

#include <pigpio.h>

#include "rotary_encoder.hpp"

void re_decoder::_pulse(int gpio, int level, uint32_t tick)
{
  if (gpio == mygpioA) levA = level; else levB = level;

   if (gpio != lastGpio) /* debounce */
   {
     lastGpio = gpio;

     if ((gpio == mygpioA) && (level == 1))
     {
      if (levB) (mycallback)(1);
  }
  else if ((gpio == mygpioB) && (level == 1))
  {
     if (levA) (mycallback)(-1);
  }
 }
}

 void re_decoder::_pulseEx(int gpio, int level, uint32_t tick, void *user)
 {
  re_decoder *mySelf = (re_decoder *) user;
  mySelf->_pulse(gpio, level, tick); 
 }

re_decoder::re_decoder(int gpioA, int gpioB, re_decoderCB_t callback)
{
  mygpioA = gpioA;
  mygpioB = gpioB;

  mycallback = callback;

  levA=0;
  levB=0;

  lastGpio = -1;

  gpioSetMode(gpioA, PI_INPUT);
  gpioSetMode(gpioB, PI_INPUT);

  gpioSetPullUpDown(gpioA, PI_PUD_UP);
  gpioSetPullUpDown(gpioB, PI_PUD_UP);

  gpioSetAlertFuncEx(gpioA, _pulseEx, this);
  gpioSetAlertFuncEx(gpioB, _pulseEx, this);
 }

void re_decoder::re_cancel(void)
{
 gpioSetAlertFuncEx(mygpioA, 0, this);
 gpioSetAlertFuncEx(mygpioB, 0, this);
 }

rotary_encoder.hpp

#ifndef ROTARY_ENCODER_HPP
#define ROTARY_ENCODER_HPP

#include <stdint.h>

typedef void (*re_decoderCB_t)(int);

class re_decoder
{
   int mygpioA, mygpioB, levA, levB, lastGpio;
   re_decoderCB_t mycallback;
   void _pulse(int gpio, int level, uint32_t tick);
   static void _pulseEx(int gpio, int level, uint32_t tick, void *user);

   public:
   void re_cancel(void);
};

#endif

test_encoder.cpp

#include <pigpio.h>
#include "rotary_encoder.hpp"

void callback(int way)
{
   static int pos = 0;
   pos += way;
   std::cout << "pos=" << pos << std::endl;
}

int main(int argc, char *argv[])
{
   if (gpioInitialise() < 0) return 1;
   re_decoder dec(7, 8, callback);
   sleep(3000);
   dec.re_cancel();
   gpioTerminate();
}

I have created qt widget project and added rotary_encoder.cpp hpp file but I have a problem with a test_encoder.cpp file. Can anyone suggest something?

2
  • You have not told us what problem(s) you encountered. Got any error messages?
    – Oldfart
    Commented Jan 8, 2018 at 7:05
  • I cant add keep the functions in the main function in qt main function. So where should I add the functions in main()? There is no error but I have no idea how to do this.
    – Ashish
    Commented Jan 8, 2018 at 7:48

1 Answer 1

1

you can add test_encoder.cpp in mainwindow.cpp and should create a pointer of class in mainwindow.h like this:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <rotary_encoder.h>
#include <QMainWindow>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private:
    Ui::MainWindow *ui;
    rotary_decoder *rc;
  void callback(int way);

};

and use main.cpp as default:

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

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}

mainwindow.cpp:

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
     rc=new rotary_decoder();
     //your code
     //for call a function like:
     rc.re_cancel();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::callback(int way)
{
    static int pos = 0;
       pos += way;
       qDebug() << "pos=" << pos ;
}
2
  • where should i write this: if (gpioInitialise() < 0) return 1; re_decoder dec(7, 8, callback); sleep(3000); dec.re_cancel(); gpioTerminate();
    – Ashish
    Commented Jan 8, 2018 at 12:48
  • I got the error: 'rotary_encoder' does not name a type rotary_encoder *rc; ^
    – Ashish
    Commented Jan 9, 2018 at 5:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.