Posts: 31
Joined: Wed Jan 20, 2016 9:26 am

GUI related question

Mon May 09, 2016 4:39 am

Hello all,

I am using raspbian and I have a program written in C to get access of GPIO (Raspberry Pi 2). This program simple toggles a GPIO pin. I want to make a GUI application which should consist a simple button. So that when I press the button, my program should execute then.

Here is my code

Code: Select all

    #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
            //increment the pointer to 0x3F200004
            //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
                    //set the pin high
                    //increment the pointer to 0x3F20001C
                    *(gpio + 7) = 1 << 17;

                   // sleep(1);

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


To accomplish this task what are the apllications available?

me: A beginner in the PI world

Posts: 14297
Joined: Tue Jul 17, 2012 3:02 pm

Re: GUI related question

Mon May 09, 2016 8:53 am

Use the qt GUI tool kit. It has a great development environment qtcreator. Drag and drop GUI design.

$ apt-get install qtcreator
Memory in C++ is a leaky abstraction .

Posts: 35
Joined: Sat Mar 07, 2015 9:54 pm

Re: GUI related question

Tue May 10, 2016 11:35 pm

+1 for Qt and QtCreator - I love it!

The only negative is that it can be a bit of a beast. Not that it's bad to use for simple programs, but maybe in this case a simpler option would be a good thing -- especially if the OP wants to stick to C instead of C++. In that case Gtk+ might be worth looking at. Not that I recommend Gtk+ over Qt in the general case, but for a single button in a C program it might make more sense. Qt is wonderful but it really does take control of your project.

Posts: 22
Joined: Thu Jan 05, 2012 12:19 pm

Re: GUI related question

Fri May 13, 2016 5:54 am


Return to “C/C++”