Advertisement
Mark2020H

Code for GUI using GTK Windows and BCM2835

Mar 1st, 2020
515
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.10 KB | None | 0 0
  1. /*   You can see this all up and running in void realms on face book at this address
  2.  *   https://www.facebook.com/groups/1400884323467285/
  3.  *   or at https://www.facebook.com/mark.harrington.142892
  4.  *   Alternatively the net-beans project can be downloaded from here at this address
  5.  *   https://github.com/markh2016/GTKBCMGUI.git
  6.  *   File:   main.c
  7.  *   Author: Mark David Harrington
  8.  *
  9.  *  Created on 23 February 2020, 15:53
  10.  *  Add this line to properties-> build -> complier -> additional properties below
  11.  * `pkg-config --cflags gtk+-3.0`
  12.  *  Add this line to properties -> build -> linker -> additional properties  below
  13.  *  pkg-config --libs gtk+-3.0`
  14.  *  Then click apply  then click OK
  15.  *  Now  your ready to compile GTK  windows software for Linux / Unix  
  16.  *  You also need to  include  the following to your settings compiler path  
  17.  * Project -> Properties -> Catagories -> C Compiler -> Include Directories
  18.    Copy and past list below into that field within netbeans
  19.  
  20.  *  /usr/include/gtk-3.0;/usr/include/at-spi2-atk;/usr/include/at-spi2-atk/2.0;/usr/include/dbus-1.0;
  21.     /usr/lib/arm-linux-gnueabihf/dbus-1.0/include;/usr/include/gio-unix-2.0;/usr/include/cairo;
  22.     /usr/include/pango-1.0;/usr/include/harfbuzz;/usr/include/fribidi;/usr/include/atk-1.0;/usr/include/pixman-1;
  23.     /usr/include/freetype2;/usr/include/libpng16;/usr/include/gdk-pixbuf-2.0;/usr/include/libmount;
  24.     /usr/include/blkid;/usr/include/uuid;/usr/include/glib-2.0;
  25.     /usr/lib/arm-linux-gnueabihf/glib-2.0/include;/usr/local/lib
  26.  */
  27.  
  28. /* Program source code below  from here */
  29.  
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <gtk/gtk.h>
  33. #include <bcm2835.h>
  34.  
  35. /*
  36.  * simple gtk application
  37.  *
  38.  */
  39.  
  40. #define PIN RPI_GPIO_P1_11
  41.  
  42.  
  43. /*global variable declaration*/
  44.  
  45.     GtkWidget *window; /* window */
  46.     GtkWidget *halign; /*Layout of child objects in window container */
  47.     GtkWidget *btn;    /*Button which we will send signals from */
  48.     int gv_count = 0 ;    
  49.     char *btofftext = "Switch Off" ;
  50.     char *btontext =  "Switch On " ;
  51.  
  52.  
  53. /* this is our event handler for button clicked */
  54. void button_clicked(GtkWidget *widget, gpointer data) {
  55.    
  56.    
  57.   if(gv_count > 1)  
  58.   {
  59.       gv_count = 0 ;
  60.   }
  61.    
  62.   g_print("clicked\n");
  63.  
  64.   if(gv_count == 0){
  65.   bcm2835_gpio_write(PIN, HIGH);
  66.   gtk_button_set_label(GTK_BUTTON(btn),btofftext);
  67.   }
  68.  
  69.  
  70.   if(gv_count==1 )
  71.   {
  72.     bcm2835_gpio_write(PIN, LOW);  
  73.      gtk_button_set_label(GTK_BUTTON(btn),btontext);
  74.   }
  75.  
  76.    gv_count+=1 ;
  77.  
  78. }
  79.  
  80. /* another callback */
  81. void destroy (GtkWidget *widget, gpointer *data)
  82. {    
  83.     bcm2835_close();
  84.     gtk_main_quit ();
  85. }
  86.  
  87.  
  88. int main(int argc, char** argv) {
  89.  
  90.    
  91.    
  92.     /* init hardware */
  93.    
  94.     if (!bcm2835_init())
  95.       return 1;
  96.      // Set the pin to be an output
  97.     bcm2835_gpio_fsel(PIN, BCM2835_GPIO_FSEL_OUTP);
  98.    
  99.    
  100.        
  101.     gtk_init(&argc, &argv);
  102.  
  103.     window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  104.     gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
  105.     gtk_window_set_default_size(GTK_WINDOW(window), 200, 100);
  106.     gtk_window_set_title(GTK_WINDOW(window), "RPI LIGHT Switch");
  107.     gtk_container_set_border_width(GTK_CONTAINER(window), 15);
  108.    
  109.    
  110.     /* Define container and add button pluc container */
  111.     halign = gtk_alignment_new(0, 0, 0, 0);
  112.     btn = gtk_button_new_with_label(btontext);
  113.     gtk_widget_set_size_request(btn, 70, 30);
  114.    
  115.     /*Now add objects */
  116.    
  117.     gtk_container_add(GTK_CONTAINER(halign), btn);
  118.     gtk_container_add(GTK_CONTAINER(window), halign);
  119.    
  120.    /* Connect signals */
  121.  
  122.       g_signal_connect(G_OBJECT(btn), "clicked",
  123.       G_CALLBACK(button_clicked), NULL);
  124.    
  125.    
  126.       g_signal_connect(window, "destroy",
  127.             G_CALLBACK (destroy), NULL);
  128.      
  129.      
  130.     /*show window with components */
  131.     gtk_widget_show_all(window);
  132.  
  133.     /* This is the message event loop which continuously monitors the  all events dispatched by
  134.      * buttons , any other widgets inside the windows  and window events
  135.      */
  136.    
  137.     gtk_main();  
  138.    
  139.     return (EXIT_SUCCESS);
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement