00001
00010 #include "h1.h"
00011
00012 GtkBuilder *builderG;
00013
00014
00015 void signal_2( int);
00016 void gtk_clear_exit( int);
00017 void delete_event_main(GtkWidget *widget, gpointer user_data);
00018 void delete_event_about(GtkWidget *widget, gpointer user_data);
00019 void delete_event_error(GtkWidget *widget, gpointer user_data);
00020
00021
00022
00023 unsigned char *SHM_config, *SHM_data;
00024 int shm_id1, shm_id2;
00025 char usb_device[64]= "ttyUSB0\0";
00026 char lb_content_n_sensors[16], lb_content_n_ultrasounds[16], lb_content_n_infrared[16];
00027 char lb_content_index1[16], lb_content_index2[16], lb_content_units[16];
00028
00029
00035 int gtk(void)
00036 {
00037 printf("\t\t\t gtk started!\n");
00038
00039
00040 memset(&SHM_data, 0, SHM_SIZE );
00041 memset(&SHM_config, 0, SHM_SIZE );
00042
00043
00044 signal(SIGUSR2,signal_2);
00045 signal(SIGINT, gtk_clear_exit);
00046 signal(SIGTERM, gtk_clear_exit);
00047
00048
00049 shm_id1 = GetSharedMem("gtk.c");
00050 if(shm_id1 == -1) return -1;
00051
00052 SHM_config = shmat(shm_id1, (void *) 0, 0);
00053 if(SHM_config == (unsigned char *) (-1)) {perror("shmat");exit(1);}
00054
00055
00056
00057 shm_id2 = GetSharedMem("rs232.c");
00058 if(shm_id2 == -1) return -1;
00059
00060 SHM_data = shmat(shm_id2, (void *) 0, 0);
00061 if(SHM_data == (unsigned char * ) (-1)) {perror("shmat");exit(1);}
00062
00063
00064 gtk_init(NULL, NULL);
00065
00066
00067 builderG = gtk_builder_new ();
00068 gtk_builder_add_from_file (builderG, "v1.glade", NULL);
00069
00070
00071 gtk_builder_connect_signals (builderG, NULL);
00072
00073
00074
00075 GtkWidget * main_window = GTK_WIDGET(gtk_builder_get_object (builderG, "main_window"));
00076 GtkWidget * about_window = GTK_WIDGET(gtk_builder_get_object (builderG, "about_window"));
00077 GtkWidget * error_window = GTK_WIDGET(gtk_builder_get_object (builderG, "error_window"));
00078
00079 if(main_window) {g_signal_connect(G_OBJECT(main_window), "delete_event", G_CALLBACK(delete_event_main), NULL);}
00080 if(about_window) {g_signal_connect(G_OBJECT(about_window), "delete_event", G_CALLBACK(delete_event_about), NULL);}
00081 if(error_window) {g_signal_connect(G_OBJECT(error_window), "delete_event", G_CALLBACK(delete_event_error), NULL);}
00082
00083
00084 GtkWidget * bt_connect = GTK_WIDGET(gtk_builder_get_object (builderG, "menu1_bt1"));
00085 gtk_widget_set_sensitive ((GtkWidget*) bt_connect, FALSE);
00086
00087
00088
00089 gtk_main();
00090
00091
00092
00093 if(shmdt(SHM_data) == -1){perror("shmdt"); exit(1);}
00094
00095 if(shm_id2 > 0) shmctl(shm_id2, IPC_RMID, NULL);
00096
00097
00098 if(shmdt(SHM_config) == -1){perror("shmdt"); exit(1);}
00099
00100 if(shm_id1 > 0) shmctl(shm_id1, IPC_RMID, NULL);
00101
00102 return 0;
00103 }
00104
00105
00106
00112 void signal_2( int x)
00113 {
00114
00115 char error_message[64];
00116
00117
00118 if (SHM_config[0]=='x')
00119 {
00120 sprintf(error_message, "Unable to open the device %s.", usb_device);
00121
00122
00123 GtkWidget * lb_error = GTK_WIDGET(gtk_builder_get_object (builderG, "lb_error"));
00124 gtk_label_set_label ((GtkLabel *)lb_error, error_message);
00125
00126 GtkWidget *error_window = GTK_WIDGET(gtk_builder_get_object (builderG, "error_window"));
00127 gtk_widget_show (error_window);
00128
00129 return;
00130 }
00131
00132
00133 else if (SHM_config[0]=='z')
00134 {
00135
00136
00137 GtkWidget * bt_connect = GTK_WIDGET(gtk_builder_get_object (builderG, "bt_connect"));
00138 gtk_widget_set_sensitive ((GtkWidget*) bt_connect, FALSE);
00139
00140 GtkWidget * bt_continuous = GTK_WIDGET(gtk_builder_get_object (builderG, "bt_continuous"));
00141 gtk_widget_set_sensitive ((GtkWidget*) bt_continuous, TRUE);
00142
00143 GtkWidget *bt_single_reading = GTK_WIDGET(gtk_builder_get_object (builderG, "bt_single_reading"));
00144 gtk_widget_set_sensitive ((GtkWidget*) bt_single_reading, TRUE);
00145
00146 GtkWidget *bt_stop = GTK_WIDGET(gtk_builder_get_object (builderG, "bt_stop"));
00147 gtk_widget_set_sensitive ((GtkWidget*) bt_stop, TRUE);
00148
00149
00150
00151
00152 GtkWidget * bt_usb0 = GTK_WIDGET(gtk_builder_get_object (builderG, "menu1_bt1"));
00153 gtk_widget_set_sensitive ((GtkWidget*) bt_usb0, FALSE);
00154
00155 GtkWidget * bt_usb1 = GTK_WIDGET(gtk_builder_get_object (builderG, "menu1_bt2"));
00156 gtk_widget_set_sensitive ((GtkWidget*) bt_usb1, FALSE);
00157
00158 GtkWidget *bt_usb2 = GTK_WIDGET(gtk_builder_get_object (builderG, "menu1_bt3"));
00159 gtk_widget_set_sensitive ((GtkWidget*) bt_usb2, FALSE);
00160
00161
00162
00163
00164 GtkWidget * lb_info1 = GTK_WIDGET(gtk_builder_get_object (builderG, "lb_info_1"));
00165 gtk_label_set_label ((GtkLabel *)lb_info1, "Connected\t");
00166
00167
00168 return;
00169 }
00170
00171
00172
00173
00174 int i;
00175 char index1[24];
00176 char index2[24];
00177 char index3[24];
00178 char hscale_name[24];
00179 static char *imageFile[2] = { "images/ok.png", "images/danger.png"};
00180
00181
00182 sprintf(lb_content_n_sensors, "%d\t", SHM_data[0]);
00183 sprintf(lb_content_n_ultrasounds, "%d\t", SHM_data[1]);
00184 sprintf(lb_content_n_infrared, "%d\t", SHM_data[2]);
00185 if (SHM_data[3]== 0) strcpy(lb_content_units, " in\t");
00186 else if (SHM_data[3]== 1) strcpy(lb_content_units, "cm\t");
00187
00188
00189 if (SHM_data[0]>max_sensors) return;
00190
00191
00192 GtkWidget * lb_n_sensors = GTK_WIDGET(gtk_builder_get_object (builderG, "lb_n_sensors"));
00193 gtk_label_set_label ((GtkLabel *)lb_n_sensors, lb_content_n_sensors);
00194
00195 GtkWidget * lb_n_ultrasounds = GTK_WIDGET(gtk_builder_get_object (builderG, "lb_n_ultrasounds"));
00196 gtk_label_set_label ((GtkLabel *)lb_n_ultrasounds, lb_content_n_ultrasounds);
00197
00198 GtkWidget * lb_n_infrared = GTK_WIDGET(gtk_builder_get_object (builderG, "lb_n_infrared"));
00199 gtk_label_set_label ((GtkLabel *)lb_n_infrared, lb_content_n_infrared);
00200
00201 GtkWidget * lb_units = GTK_WIDGET(gtk_builder_get_object (builderG, "lb_units"));
00202 gtk_label_set_label ((GtkLabel *)lb_units, lb_content_units);
00203
00204
00205 for (i=0; i<SHM_data[0]; i++)
00206 {
00207
00208 sprintf(index1,"lb_sensor_id%d",i+1);
00209 sprintf(index2,"ad_dist_s%d",i+1);
00210 sprintf(index3,"img_sensor%d",i+1);
00211 sprintf(hscale_name,"hscale%d",i+1);
00212
00213
00214 sprintf(lb_content_index1,"Sensor%d",i+1);
00215
00216
00217 GtkWidget * lb_index1 = GTK_WIDGET(gtk_builder_get_object (builderG, index1));
00218 gtk_label_set_label ((GtkLabel *)lb_index1, lb_content_index1);
00219
00220
00221 GtkWidget * hscale = GTK_WIDGET(gtk_builder_get_object (builderG, hscale_name));
00222 gtk_widget_show( hscale);
00223
00224 GtkAdjustment * ad_index2 = GTK_ADJUSTMENT(gtk_builder_get_object (builderG, index2));
00225 gtk_adjustment_set_value ((GtkAdjustment *) ad_index2, SHM_data[4+i]);
00226
00227
00228 GtkWidget * img_index3 = GTK_WIDGET(gtk_builder_get_object (builderG, index3));
00229 if (SHM_data[4+i] > distance_limit)
00230 gtk_image_set_from_file ((GtkImage *)img_index3,imageFile[0]);
00231 else
00232 gtk_image_set_from_file ((GtkImage *)img_index3,imageFile[1]);
00233 }
00234 }
00235
00236
00242 void gtk_clear_exit(int xx)
00243 {
00244
00245 if(shmdt(SHM_config) == -1){perror("shmdt"); exit(1);}
00246
00247 if(shm_id1 > 0) shmctl(shm_id1, IPC_RMID, NULL);
00248
00249
00250 if(shmdt(SHM_data) == -1){perror("shmdt"); exit(1);}
00251
00252 if(shm_id2 > 0) shmctl(shm_id2, IPC_RMID, NULL);
00253
00254 exit(0);
00255 }
00256
00257
00264 void delete_event_main(GtkWidget *widget, gpointer user_data)
00265 {
00266 gtk_main_quit();
00267 }
00268
00269
00276 void delete_event_about(GtkWidget *widget, gpointer user_data)
00277 {
00278 GtkWidget * about_window = GTK_WIDGET(gtk_builder_get_object (builderG, "about_window"));
00279 gtk_widget_hide (about_window);
00280 }
00281
00282
00289 void delete_event_error(GtkWidget *widget, gpointer user_data)
00290 {
00291 GtkWidget * error_window = GTK_WIDGET(gtk_builder_get_object (builderG, "error_window"));
00292 gtk_widget_hide (error_window);
00293 }
00294
00295
00296
00297
00304 void connect_handler(GtkWidget *widget, gpointer user_data)
00305 {
00306
00307 sprintf(SHM_config, "1\0");
00308 kill(pid, SIGUSR1);
00309 }
00310
00311
00318 void continuous_handler(GtkWidget *widget, gpointer user_data)
00319 {
00320 GtkWidget *bt_continuous = GTK_WIDGET(gtk_builder_get_object (builderG, "bt_continuous"));
00321
00322
00323 if (gtk_toggle_button_get_active ((GtkToggleButton *) bt_continuous))
00324 {
00325
00326 GtkWidget *bt_single_reading = GTK_WIDGET(gtk_builder_get_object (builderG, "bt_single_reading"));
00327 gtk_toggle_button_set_active ((GtkToggleButton *) bt_single_reading, FALSE);
00328
00329 GtkWidget *bt_stop = GTK_WIDGET(gtk_builder_get_object (builderG, "bt_stop"));
00330 gtk_toggle_button_set_active ((GtkToggleButton *) bt_stop, FALSE);
00331
00332
00333 GtkWidget * lb_info2 = GTK_WIDGET(gtk_builder_get_object (builderG, "lb_info_2"));
00334 gtk_label_set_label ((GtkLabel *)lb_info2, "Continouos mode");
00335
00336
00337
00338 sprintf(SHM_config, "3\0");
00339 kill(pid, SIGUSR1);
00340 }
00341 }
00342
00343
00350 void single_reading_handler(GtkWidget *widget, gpointer user_data)
00351 {
00352
00353 GtkWidget *bt_single_reading = GTK_WIDGET(gtk_builder_get_object (builderG, "bt_single_reading"));
00354
00355
00356 if (gtk_toggle_button_get_active ((GtkToggleButton *) bt_single_reading))
00357 {
00358
00359 GtkWidget *bt_continuous = GTK_WIDGET(gtk_builder_get_object (builderG, "bt_continuous"));
00360 gtk_toggle_button_set_active ((GtkToggleButton *) bt_continuous, FALSE);
00361
00362 GtkWidget *bt_stop = GTK_WIDGET(gtk_builder_get_object (builderG, "bt_stop"));
00363 gtk_toggle_button_set_active ((GtkToggleButton *) bt_stop, FALSE);
00364
00365
00366 GtkWidget *bt_get_reading = GTK_WIDGET(gtk_builder_get_object (builderG, "bt_get_reading"));
00367 gtk_widget_set_sensitive ((GtkWidget*) bt_get_reading, TRUE);
00368
00369
00370 GtkWidget * lb_info2 = GTK_WIDGET(gtk_builder_get_object (builderG, "lb_info_2"));
00371 gtk_label_set_label ((GtkLabel *)lb_info2, "Single reading mode");
00372
00373
00374
00375 sprintf(SHM_config, "4\0");
00376 kill(pid, SIGUSR1);
00377 return;
00378 }
00379
00380
00381 GtkWidget *bt_get_reading = GTK_WIDGET(gtk_builder_get_object (builderG, "bt_get_reading"));
00382 gtk_widget_set_sensitive ((GtkWidget*) bt_get_reading, FALSE);
00383 }
00384
00385
00392 void stop_handler(GtkWidget *widget, gpointer user_data)
00393 {
00394 GtkWidget *bt_stop = GTK_WIDGET(gtk_builder_get_object (builderG, "bt_stop"));
00395
00396
00397 if (gtk_toggle_button_get_active ((GtkToggleButton *) bt_stop))
00398 {
00399
00400 GtkWidget *bt_continuous = GTK_WIDGET(gtk_builder_get_object (builderG, "bt_continuous"));
00401 gtk_toggle_button_set_active ((GtkToggleButton *) bt_continuous, FALSE);
00402
00403 GtkWidget *bt_single_reading = GTK_WIDGET(gtk_builder_get_object (builderG, "bt_single_reading"));
00404 gtk_toggle_button_set_active ((GtkToggleButton *) bt_single_reading, FALSE);
00405
00406
00407 GtkWidget * lb_info2 = GTK_WIDGET(gtk_builder_get_object (builderG, "lb_info_2"));
00408 gtk_label_set_label ((GtkLabel *)lb_info2, "Stop mode");
00409
00410
00411 sprintf(SHM_config, "5\0");
00412 kill(pid, SIGUSR1);
00413 }
00414 }
00415
00416
00424 void get_reading_handler(GtkWidget *widget, gpointer user_data)
00425 {
00426
00427 sprintf(SHM_config, "6\0");
00428 kill(pid, SIGUSR1);
00429 }
00430
00431
00438 void about_handler(GtkWidget *widget, gpointer user_data)
00439 {
00440 GtkWidget *about_window = GTK_WIDGET(gtk_builder_get_object (builderG, "about_window"));
00441 gtk_widget_show (about_window);
00442 }
00443
00444
00451 void close_about_handler(GtkWidget *widget, gpointer user_data)
00452 {
00453 GtkWidget * about_window = GTK_WIDGET(gtk_builder_get_object (builderG, "about_window"));
00454 gtk_widget_hide (about_window);
00455 }
00456
00457
00464 void usb0_handler(GtkWidget *widget, gpointer user_data)
00465 {
00466
00467 GtkWidget * bt_usb0 = GTK_WIDGET(gtk_builder_get_object (builderG, "menu1_bt1"));
00468 gtk_widget_set_sensitive ((GtkWidget*) bt_usb0, FALSE);
00469
00470 GtkWidget * bt_usb1 = GTK_WIDGET(gtk_builder_get_object (builderG, "menu1_bt2"));
00471 gtk_widget_set_sensitive ((GtkWidget*) bt_usb1, TRUE);
00472
00473 GtkWidget *bt_usb2 = GTK_WIDGET(gtk_builder_get_object (builderG, "menu1_bt3"));
00474 gtk_widget_set_sensitive ((GtkWidget*) bt_usb2, TRUE);
00475
00476
00477 strcpy(usb_device,"ttyUSB0");
00478
00479
00480 sprintf(SHM_config, "7\0");
00481 kill(pid, SIGUSR1);
00482 }
00483
00484
00491 void usb1_handler(GtkWidget *widget, gpointer user_data)
00492 {
00493
00494 GtkWidget * bt_usb0 = GTK_WIDGET(gtk_builder_get_object (builderG, "menu1_bt1"));
00495 gtk_widget_set_sensitive ((GtkWidget*) bt_usb0, TRUE);
00496
00497 GtkWidget * bt_usb1 = GTK_WIDGET(gtk_builder_get_object (builderG, "menu1_bt2"));
00498 gtk_widget_set_sensitive ((GtkWidget*) bt_usb1, FALSE);
00499
00500 GtkWidget *bt_usb2 = GTK_WIDGET(gtk_builder_get_object (builderG, "menu1_bt3"));
00501 gtk_widget_set_sensitive ((GtkWidget*) bt_usb2, TRUE);
00502
00503
00504 strcpy(usb_device,"ttyUSB1");
00505
00506
00507 sprintf(SHM_config, "8\0");
00508 kill(pid, SIGUSR1);
00509 }
00510
00511
00518 void usb2_handler(GtkWidget *widget, gpointer user_data)
00519 {
00520
00521 GtkWidget * bt_usb0 = GTK_WIDGET(gtk_builder_get_object (builderG, "menu1_bt1"));
00522 gtk_widget_set_sensitive ((GtkWidget*) bt_usb0, TRUE);
00523
00524 GtkWidget * bt_usb1 = GTK_WIDGET(gtk_builder_get_object (builderG, "menu1_bt2"));
00525 gtk_widget_set_sensitive ((GtkWidget*) bt_usb1, TRUE);
00526
00527 GtkWidget *bt_usb2 = GTK_WIDGET(gtk_builder_get_object (builderG, "menu1_bt3"));
00528 gtk_widget_set_sensitive ((GtkWidget*) bt_usb2, FALSE);
00529
00530
00531 strcpy(usb_device,"ttyUSB2");
00532
00533
00534 sprintf(SHM_config, "9\0");
00535 kill(pid, SIGUSR1);
00536 }
00537
00538
00545 void error_handler(GtkWidget *widget, gpointer user_data)
00546 {
00547 GtkWidget * error_window = GTK_WIDGET(gtk_builder_get_object (builderG, "error_window"));
00548 gtk_widget_hide (error_window);
00549 }
00550
00551
00558 void quit_handler(GtkWidget *widget, gpointer user_data)
00559 {
00560 gtk_main_quit();
00561 }
00562
00563