PDA

View Full Version : Executing a C file from Qt



vivek74155
19th May 2016, 12:33
Hello everyone

I am using Qt 5.3.2 and have also installed Qtcreator 3.2.1 in my Raspberry pi 2 running Linux.

I have a C code which continously toggles the GPIO pin no. 17. Here the code is
//it is saved as blink..c

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <errno.h>
#include <stdint.h>

static volatile uint32_t *gpio;

int main(int argc, char **argv)
{
int fd ;

//Obtain handle to physical memory
if ((fd = open ("/dev/mem", O_RDWR | O_SYNC) ) < 0) {
printf("Unable to open /dev/mem: %s\n", strerror(errno));
return -1;
}


//map a page of memory to gpio at offset 0x3F200000 which is where GPIO goodnessstarts
gpio = (uint32_t *)mmap(0, getpagesize(), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0x3F200000);

if ((int32_t)gpio < 0){
printf("Mmap failed: %s\n", strerror(errno));
return -1;
}

//set gpio17 as an output

//set the value through a little bit twiddling where we only modify the bits 21-23 in the register
*(gpio + 1) = (*(gpio + 1) & ~(7 << 21)) | (1 << 21);

//toggle gpio17 every second
while(1){
//set the pin high
//increment the pointer to 0x3F20001C
*(gpio + 7) = 1 << 17;

//sleep
// sleep(1);

//set the pin to low
//increment the pointer to 0x3F200028
*(gpio + 10) = 1 << 17;

//sleep(1);
}
}



this code is saved as blink.c

And I can compile it through following code in terminal (not in Qt)
gcc blink.c

And it is executing through following code in terminal (not in Qt)
sudo ./a.out

The above compilation and execution I am doing is without Qt.

I want to execute the same code When I push the button created in QtCreator. I know how to create button as I had gonr through this linkhttp://http://www2.cs.uic.edu/~i340/qt/QT_Designer_Tutorial/

How should I move futher. I just want my file to execute when I press button.

A detailed help will be very useful.

Thanks

anda_skoa
19th May 2016, 18:26
Executing an external process is a matter of using QProcess.

Cheers,
_

ChrisW67
19th May 2016, 20:39
Or you can embed your C code in your Qt C++ code and not have a separate process to toggle a couple of bits.

d_stranz
20th May 2016, 00:54
I just want my file to execute when I press button.

Just to be clear - your file will not be the thing executing. It will be the code from that file that you compile and link into something executable.

Or, as ChrisW67 suggests, if you want this code to execute from within a Qt program, then you put it into a slot that will be called when your button is clicked. But be careful - your while loop is an infinite loop, and will cause your Qt program to hang as it blinks the light every second.

Better to use a QTimer that gets started when you click the button. Set the timer to fire every second, and in the slot you connect to the timeout() signal, execute the code inside the while loop once, then set the timer to fire again. You can add another button ("Stop") that stops the timer (and the blinking) through another slot.

Signals, slots, and timers are covered very well in the Qt tutorials, so I don't think you need to be spoon-fed code. Go read.