Advertisement
Mark2020H

Simple Raspberry pi IOT GTK example for touch screens

Apr 27th, 2020
653
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.26 KB | None | 0 0
  1. /*
  2.  * File:   main.c
  3.  * Author: Mark David Harrington
  4.  * code to illustrate how to use the bcm2835 with GTK 3  and Raspberry pi for IOT
  5.  * applications  across ssh with X11 forwarding
  6.  *
  7.  *  Created on 23 February 2020, 15:53
  8.  *  Add this line to properties-> build -> complier -> additional properties below
  9.     `pkg-config --cflags gtk+-3.0`
  10.  *  Add this line to properties -> build -> linker -> additional properties  below
  11.     `pkg-config --libs gtk+-3.0`
  12.  *  Then click apply  then click OK
  13.  *  Now  your ready to compile GTK  windows software for Linux / Unix
  14.  ******************************************************************************************  
  15.  *  Full code and image at https://github.com/markh2016/GTKSingleRPILightSwitch.git ***** *
  16.  * ****************************************************************************************
  17.  */
  18.  
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <gtk/gtk.h>
  22. #include <bcm2835.h>
  23.  
  24. /*
  25.  * simple gtk application
  26.  * MD Harrington
  27.  */
  28.  
  29. #define PIN RPI_GPIO_P1_11
  30.  
  31.  
  32. /*global variable declaration*/
  33.  
  34.     GtkWidget *window; /* window */
  35.     GtkWidget *btn;    /*Button which we will send signals from */
  36.     GtkWidget *offimage ;
  37.     GtkWidget *onimage  ;
  38.     int gv_count = 0 ;  
  39.  
  40.  
  41. /* this is our event handler for button clicked */
  42. void on_btn1_clicked(GtkWidget *widget, gpointer data) {
  43.    
  44.  
  45.  
  46.  
  47.   if(gv_count > 1)  
  48.   {
  49.       gv_count = 0 ;
  50.   }
  51.    
  52.  
  53.  
  54.   if(gv_count == 0){
  55.   bcm2835_gpio_write(PIN, HIGH);
  56.   onimage = gtk_image_new_from_file("lightsOn.png") ;
  57.   gtk_button_set_image (GTK_BUTTON (btn),onimage) ;
  58.   }
  59.  
  60.  
  61.   if(gv_count==1 )
  62.   {
  63.     bcm2835_gpio_write(PIN, LOW);  
  64.     offimage = gtk_image_new_from_file("lightsoff.png") ;
  65.     gtk_button_set_image (GTK_BUTTON (btn),offimage);
  66.   }
  67.  
  68.    gv_count+=1 ;
  69.  
  70. }
  71.  
  72. /* another callback */
  73. void on_main_window_destroy(GtkWidget *widget, gpointer *data)
  74. {   bcm2835_gpio_write(PIN, LOW);
  75.     bcm2835_close();
  76.     gtk_main_quit ();
  77. }
  78.  
  79.  
  80. int main(int argc, char** argv) {
  81.  
  82.     GtkBuilder  *builder;
  83.    
  84.     /* init hardware */
  85.    
  86.     if (!bcm2835_init())
  87.       return 1;
  88.      // Set the pin to be an output
  89.     bcm2835_gpio_fsel(PIN, BCM2835_GPIO_FSEL_OUTP);
  90.    
  91.     // pass main method params to  gtkinit
  92.        
  93.     gtk_init(&argc, &argv);
  94.  
  95.     // builder instance  and return pointer address
  96.    
  97.      builder = gtk_builder_new();
  98.     // grab the galde exml file snd read this in
  99.      gtk_builder_add_from_file (builder, "switch.glade", NULL);
  100.      
  101.     // return the window object
  102.    
  103.     window = GTK_WIDGET(gtk_builder_get_object(builder, "main_window"));
  104.  
  105.      // now read the signals slots that we created in the glade file
  106.      
  107.     gtk_builder_connect_signals(builder, NULL);
  108.  
  109.     gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
  110.    
  111.  
  112.     // get pointers to the buttons
  113.     btn = GTK_WIDGET(gtk_builder_get_object(builder, "btn1"));
  114.          
  115.      
  116.     /*show window with components */
  117.     gtk_widget_show_all(window);
  118.  
  119.     /* This is the message event loop which continously monitors the  all events dispatched by
  120.      * buttons , any other widgets inside the windows  and window events
  121.      */
  122.    
  123.     gtk_main();  
  124.    
  125.     return (EXIT_SUCCESS);
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement