Qt-creator problem: error: 'gpio26' was not declared in this scope



  • Hello,
    I found some code on web and want to use it in my app.
    But I can figure out where i my mistake.
    I hope someone could have a look for that https://dl.dropboxusercontent.com/u/98706767/LEDControl.zip

    My host is Ubuntu 12.04
    qt 4 and latest qt creator.
    target i.mx6

    Joerg



  • It obviously cant know what gpio26 is. That pointer is only available in your main function



  • How can I make it usable in slot of my buttons ?



  • Put it into your LEDcontrol.h

    ledcontrol.h
    @#ifndef LEDCONTROLL_H
    #define LEDCONTROLL_H

    #include <QMainWindow>
    #include "GPIOClass.h"

    namespace Ui {
    class LedControll;
    }

    class LedControll : public QMainWindow
    {
    Q_OBJECT

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

    private slots:
    void on_Button_LED_on_clicked();

    void on_Button_LED_off_clicked();
    

    private:
    Ui::LedControll ui;
    GPIOClass
    gpio26;

    };

    #endif // LEDCONTROLL_H
    @

    ledcontroll.cpp

    @#include "ledcontroll.h"
    #include "ui_ledcontroll.h"
    #include "GPIOClass.h"
    #include <iostream>
    #include <unistd.h>
    #include <errno.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <signal.h>
    using namespace std;

    LedControll::LedControll(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::LedControll)

    {

    gpio26 = new GPIOClass("26"); //create new GPIO object to be attached to  GPIO4
    
    
    gpio26->export_gpio(); //export GPIO26
    
    
    gpio26->setdir_gpio("out"); //GPIO26 set to output
    
    ui->setupUi(this);
    

    }

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

    void LedControll::on_Button_LED_on_clicked()
    {
    gpio26->setval_gpio("1"); // turn LED ON
    }

    void LedControll::on_Button_LED_off_clicked()
    {
    gpio26->setval_gpio("0");
    }
    @

    main.cpp

    @#include "ledcontroll.h"
    #include <QApplication>

    int main(int argc, char *argv[])
    {

    QApplication a(argc, argv);
    LedControll w;
    w.show();
    
    
    
    
    return a.exec&#40;&#41;;
    

    }
    @



  • Thank You very much.
    Works fine now !



  • One more question.
    The program needs to be root to access gpio. I think thats not the best way. How to change that?


  • Lifetime Qt Champion

    Hi,

    Depending on how the device presents itself, one possibility would be to change the group of the gpio device and add your user to that group



  • I try that but did not realy works.
    I build a group 'hwuser'
    add the user to the group

    change the group with

    chgrp -R hwuser /sys/class

    with ls -l could see that it is done.

    but after reboot the group is root again.


  • Lifetime Qt Champion

    I thought you were accessing the GPIO through a /dev device or does the sys fs entry point to a device ?

    Anyway, sys (like /dev in general) is generated dynamically so that method won't work.

    If you are indeed accessing a device that can be found in /dev, you could try to setup an udev rule that sets the group of that device.



  • That is more Linux question than Qt specific. If your target Linux uses udev for device handling then you need to specify rules which states you want "hwuser" group for gpio. Example setting udev-rules for gpio in raspberry pi: http://www.raspberrypi.org/phpBB3/viewtopic.php?f=29&t=9667



  • Ok thanks to all.
    I figured out now.

    Thats works for me:
    sudo nano /etc/udev/rules.d/99-gpio.rules
    paste:
    SUBSYSTEM==”gpio”, ACTION==”add”, PROGRAM=”/bin/sh -c ‘chown -R [user]:[group] /sys%p’”
    PROGRAM=”/bin/sh -c ‘chown -R [user]:[group] /sys/class’”

    save and reboot


  • Lifetime Qt Champion

    That might be a little too radical not to say that it's a security breach. Just modify the file you use not the entire tree



  • ok I will test that.

    thx



  • it is mysterious,
    after reboot export gpio works fine but set direction fails.

    the same again on root works fine.


  • Lifetime Qt Champion

    Are you accessing several files ?



  • not sure what You mean.

    thats the init of the gpio

    gpio26 = new GPIOClass("26"); //create new GPIO object

    gpio26->export_gpio(); //export GPIO26
    
    
    gpio26->setdir_gpio("out"); //GPIO26 set to output
    

    the export works, I can see it after start program

    i have made a button to set direction... thats works to

    i have made button for export unexport and set direction.

    thats works fine too


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.