Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* You can see this all up and running in void realms on face book at this address
- * https://www.facebook.com/groups/1400884323467285/
- * or at https://www.facebook.com/mark.harrington.142892
- * Alternatively the net-beans project can be downloaded from here at this address
- * https://github.com/markh2016/GTKBCMGUI.git
- * File: main.c
- * Author: Mark David Harrington
- *
- * Created on 23 February 2020, 15:53
- * Add this line to properties-> build -> complier -> additional properties below
- * `pkg-config --cflags gtk+-3.0`
- * Add this line to properties -> build -> linker -> additional properties below
- * pkg-config --libs gtk+-3.0`
- * Then click apply then click OK
- * Now your ready to compile GTK windows software for Linux / Unix
- * You also need to include the following to your settings compiler path
- * Project -> Properties -> Catagories -> C Compiler -> Include Directories
- Copy and past list below into that field within netbeans
- * /usr/include/gtk-3.0;/usr/include/at-spi2-atk;/usr/include/at-spi2-atk/2.0;/usr/include/dbus-1.0;
- /usr/lib/arm-linux-gnueabihf/dbus-1.0/include;/usr/include/gio-unix-2.0;/usr/include/cairo;
- /usr/include/pango-1.0;/usr/include/harfbuzz;/usr/include/fribidi;/usr/include/atk-1.0;/usr/include/pixman-1;
- /usr/include/freetype2;/usr/include/libpng16;/usr/include/gdk-pixbuf-2.0;/usr/include/libmount;
- /usr/include/blkid;/usr/include/uuid;/usr/include/glib-2.0;
- /usr/lib/arm-linux-gnueabihf/glib-2.0/include;/usr/local/lib
- */
- /* Program source code below from here */
- #include <stdio.h>
- #include <stdlib.h>
- #include <gtk/gtk.h>
- #include <bcm2835.h>
- /*
- * simple gtk application
- *
- */
- #define PIN RPI_GPIO_P1_11
- /*global variable declaration*/
- GtkWidget *window; /* window */
- GtkWidget *halign; /*Layout of child objects in window container */
- GtkWidget *btn; /*Button which we will send signals from */
- int gv_count = 0 ;
- char *btofftext = "Switch Off" ;
- char *btontext = "Switch On " ;
- /* this is our event handler for button clicked */
- void button_clicked(GtkWidget *widget, gpointer data) {
- if(gv_count > 1)
- {
- gv_count = 0 ;
- }
- g_print("clicked\n");
- if(gv_count == 0){
- bcm2835_gpio_write(PIN, HIGH);
- gtk_button_set_label(GTK_BUTTON(btn),btofftext);
- }
- if(gv_count==1 )
- {
- bcm2835_gpio_write(PIN, LOW);
- gtk_button_set_label(GTK_BUTTON(btn),btontext);
- }
- gv_count+=1 ;
- }
- /* another callback */
- void destroy (GtkWidget *widget, gpointer *data)
- {
- bcm2835_close();
- gtk_main_quit ();
- }
- int main(int argc, char** argv) {
- /* init hardware */
- if (!bcm2835_init())
- return 1;
- // Set the pin to be an output
- bcm2835_gpio_fsel(PIN, BCM2835_GPIO_FSEL_OUTP);
- gtk_init(&argc, &argv);
- window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
- gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
- gtk_window_set_default_size(GTK_WINDOW(window), 200, 100);
- gtk_window_set_title(GTK_WINDOW(window), "RPI LIGHT Switch");
- gtk_container_set_border_width(GTK_CONTAINER(window), 15);
- /* Define container and add button pluc container */
- halign = gtk_alignment_new(0, 0, 0, 0);
- btn = gtk_button_new_with_label(btontext);
- gtk_widget_set_size_request(btn, 70, 30);
- /*Now add objects */
- gtk_container_add(GTK_CONTAINER(halign), btn);
- gtk_container_add(GTK_CONTAINER(window), halign);
- /* Connect signals */
- g_signal_connect(G_OBJECT(btn), "clicked",
- G_CALLBACK(button_clicked), NULL);
- g_signal_connect(window, "destroy",
- G_CALLBACK (destroy), NULL);
- /*show window with components */
- gtk_widget_show_all(window);
- /* This is the message event loop which continuously monitors the all events dispatched by
- * buttons , any other widgets inside the windows and window events
- */
- gtk_main();
- return (EXIT_SUCCESS);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement