#include <gtk/gtk.h>
#include <string.h>
#include <wiringPi.h>
#include <stdlib.h>
#include <stdio.h>
GtkWidget *window;
GtkWidget *button_01;
GtkWidget *button_02;
GtkWidget *label0;
GtkWidget *label1;
GtkWidget *label2;
#define TRIG 22
#define ECHO 25
char r_string[30];
void setup()
{
wiringPiSetupGpio();
pinMode(TRIG, OUTPUT);
pinMode(ECHO, INPUT);
//TRIG pin must start LOW
digitalWrite(TRIG, LOW);
delay(30);
}
int getCM()
{
//Send trig pulse
digitalWrite(TRIG, HIGH);
delayMicroseconds(20);
digitalWrite(TRIG, LOW);
//Wait for echo start
while(digitalRead(ECHO) == LOW);
//Wait for echo end
long startTime = micros();
while(digitalRead(ECHO) == HIGH);
long travelTime = micros() - startTime;
//Get distance in cm
int distance = travelTime / 58;
return distance;
}
static void print_hello (GtkWidget *widget, gpointer data)
{
for (int mac1=0;mac1<10;mac1++);
gtk_label_set_text(GTK_LABEL(label0),(const gchar*)r_string);
sprintf(r_string,"Distance: %dcm\n");
}
static void activate (GtkApplication *app, gpointer user_data)
{
window = gtk_application_window_new (app);;
button_01 = gtk_button_new_with_label("This is button 01");
g_signal_connect (button_01, "clicked", G_CALLBACK (print_hello), NULL);
button_02 = gtk_button_new_with_label("This is button 02");
g_signal_connect (button_02, "clicked", G_CALLBACK (print_hello), NULL);
label0 = gtk_label_new("hello 0");
label1 = gtk_label_new("hello 1");
label2 = gtk_label_new("hello 2");
GtkWidget *main_box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 25);
GtkCssProvider *cssProvider = gtk_css_provider_new();
gtk_css_provider_load_from_path(cssProvider, "theme.css", NULL);
gtk_style_context_add_provider_for_screen(gdk_screen_get_default(),
GTK_STYLE_PROVIDER(cssProvider),
GTK_STYLE_PROVIDER_PRIORITY_USER);
gtk_box_set_homogeneous (GTK_BOX (main_box), TRUE);
gtk_container_add (GTK_CONTAINER (window), main_box);
gtk_container_add (GTK_CONTAINER (main_box), button_01);
gtk_container_add (GTK_CONTAINER (main_box), label0);
gtk_container_add (GTK_CONTAINER (main_box), label1);
gtk_container_add (GTK_CONTAINER (main_box), label2);
gtk_container_add (GTK_CONTAINER (main_box), button_02);
g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
gtk_widget_show_all (window);
}
int main (int argc, char **argv)
{
GtkApplication *app;
int status;
app = gtk_application_new ("org.gtk.example", G_APPLICATION_FLAGS_NONE);
g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
status = g_application_run (G_APPLICATION (app), argc, argv);
g_object_unref (app);
while (1) {printf("MESAFE: %dcm\n", getCM()); delay(1000);}
return status;
}