homescope_core.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. #include <gtk/gtk.h>
  2. #include <stdlib.h>
  3. #include <signal.h>
  4. #include <unistd.h>
  5. #include <stdio.h>
  6. #include <string.h>
  7. // This is a global variable holding the PID of a child to be created when accessing
  8. // the camera with raspivid program
  9. // to be used in the start_video call back
  10. GPid pid;
  11. void set_gpio_modes(void)
  12. {
  13. /* We use 2 PINS to communicate Z axis changes to the Arduino */
  14. /* DOWN - PIN, wPi n. 24 */
  15. system("gpio mode 24 OUT");
  16. system("gpio write 24 0"); // SET PIN @ LOW by Default
  17. /* UP - PIN wPi n. 29 */
  18. system("gpio mode 29 OUT");
  19. system("gpio write 29 0"); // SET PIN @ LOW by Default
  20. // Set the PWM pin and set it to zero
  21. system("gpio mode 23 pwm");
  22. system("gpio pwm 23 0");
  23. /* Done with setting pins */
  24. g_print("Finished setting up the GPIO PINs with gpio utility \n");
  25. system("gpio readall");
  26. }
  27. // HERE GOES THE ABOUT DIALOG BOX For info at a website: lab wiki on the process
  28. static void show_about(GtkWidget *widget, gpointer user_data)
  29. {
  30. GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file("biotexturas_logo_text.tiff", NULL);
  31. GtkWidget *dialog = gtk_about_dialog_new();
  32. gtk_about_dialog_set_program_name(GTK_ABOUT_DIALOG(dialog),
  33. "HomeScope Core");
  34. gtk_about_dialog_set_version(GTK_ABOUT_DIALOG(dialog), "version 1.0");
  35. gtk_about_dialog_set_copyright(GTK_ABOUT_DIALOG(dialog),"Biotexturas");
  36. gtk_about_dialog_set_comments(GTK_ABOUT_DIALOG(dialog),
  37. "HomeScope is a tool to explore collective intelligence under you own feet!");
  38. gtk_about_dialog_set_website(GTK_ABOUT_DIALOG(dialog),
  39. "http://homescope.biotexturas.org");
  40. gtk_about_dialog_set_logo(GTK_ABOUT_DIALOG(dialog), pixbuf);
  41. g_object_unref(pixbuf), pixbuf = NULL;
  42. gtk_dialog_run(GTK_DIALOG (dialog));
  43. gtk_widget_destroy(dialog);
  44. }
  45. // the call back needs this casting or it doesn't work properly
  46. static void start_video(GObject *switcher, GParamSpec *pspec, gpointer user_data)
  47. {
  48. if(gtk_switch_get_active(GTK_SWITCH(switcher)))
  49. {
  50. /* Here goes the code when the video swich is set ON */
  51. g_print("Video streaming started...\n");
  52. /* Argument vector to run as child process */
  53. gchar *child_argv[] = {"/usr/bin/raspivid", "-t", "0", "-ex", "off", "-p", "10,-120,750,750" ,"-v", "-awb" ,"sun", NULL};
  54. /* we need these also
  55. (besides the global process ID to kill the video raspivid later...) */
  56. gint stdout;
  57. GError *error = NULL;
  58. /* Now we spawn the child pricess to be handled with PID pid */
  59. g_spawn_async(NULL, child_argv, NULL, G_SPAWN_DEFAULT, NULL, NULL, &pid, &error);
  60. g_print("We have spawned a child process which pid is %i,"
  61. "and we did from process %i which was in turn made from parent %i"
  62. "(the running shell)\n", pid, getpid(),getppid());
  63. }else
  64. {
  65. /* Here goes the code when the video swich is set OFF */
  66. g_print("Video streaming stopped...\n");
  67. /* For some wierd rason, the line bellow doesnt close the child process */
  68. g_spawn_close_pid(pid); // in UNIX doesn nieks, but could be useful in Windows
  69. kill(pid, 9); // so we kill the process UNIX-style
  70. }
  71. }
  72. /* not working properly...cannot recognize the switch... need to quit and kill raspivid if on */
  73. static void clean_up_b4_go(GtkWidget *widget, gpointer user_data)
  74. {
  75. if(gtk_switch_get_state(GTK_SWITCH(user_data))){kill(pid, 9);}
  76. }
  77. static void take_picture(GtkWidget *widget, gpointer user_data)
  78. {
  79. if(gtk_switch_get_state(GTK_SWITCH(user_data)))
  80. {
  81. gtk_switch_set_state(GTK_SWITCH(user_data), FALSE);
  82. system("raspistill -o open_scope_capture_picture.jpg -p 10,20,400,400 -v");
  83. gtk_switch_set_state(GTK_SWITCH(user_data), TRUE);
  84. }else{system("raspistill -o open_scope_capture_picture.jpg -p 10,20,400,400 -v");}
  85. }
  86. static void move_up(GtkWidget *widget, gpointer user_data)
  87. {
  88. // LOW blue , HIGH purple goes up
  89. g_print("Hello Open Scope: we are going up!\n");
  90. system("gpio write 24 0");
  91. system("gpio write 29 1");
  92. }
  93. static void move_down(GtkWidget *widget, gpointer user_data)
  94. {
  95. g_print("Hello Open Scope: we are going down!\n");
  96. system("gpio write 24 1");
  97. system("gpio write 29 0");
  98. }
  99. static void stop(GtkWidget *widget, gpointer user_data)
  100. {
  101. g_print("Hello Open Scope: we stop moving!\n");
  102. system("gpio write 24 0");
  103. system("gpio write 29 0");
  104. }
  105. static void pwm_scale_moved(GtkRange *range, gpointer user_data)
  106. {
  107. gdouble pos = gtk_range_get_value(GTK_RANGE(range));
  108. gint pwm_value;
  109. pwm_value = (gint)pos;
  110. gchar *str = g_strdup_printf("gpio pwm 23 %d", pwm_value);
  111. system(str);
  112. }
  113. int main(int argc, char *argv[])
  114. {
  115. /* Notice here we use builder (GLADE) to generate the interface */
  116. GtkBuilder *builder;
  117. GObject *window;
  118. GObject *button;
  119. GObject *switcher;
  120. GObject *scale;
  121. gtk_init(&argc, &argv);
  122. /* Construct a GtkBuilder instance and load our UI description */
  123. builder = gtk_builder_new();
  124. gtk_builder_add_from_file(builder, "builder_Z.ui", NULL);
  125. /* Connect signal handlers to the constructed widgets. */
  126. window = gtk_builder_get_object(builder, "window");
  127. g_signal_connect(window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
  128. button = gtk_builder_get_object(builder, "button_up");
  129. g_signal_connect(button, "clicked", G_CALLBACK (move_up), NULL);
  130. button = gtk_builder_get_object(builder, "button_down");
  131. g_signal_connect(button, "clicked", G_CALLBACK (move_down), NULL);
  132. button = gtk_builder_get_object(builder, "button_stop");
  133. g_signal_connect(button, "clicked", G_CALLBACK(stop), NULL);
  134. button = gtk_builder_get_object(builder, "button_about");
  135. g_signal_connect(button, "clicked", G_CALLBACK(show_about), GTK_WINDOW(window));
  136. /* Here we play with the switcher and THEN with buttons which are connected to it. */
  137. switcher = gtk_builder_get_object(builder, "switch_video");
  138. gtk_switch_set_active(GTK_SWITCH(switcher), FALSE);
  139. g_signal_connect(GTK_SWITCH(switcher), "notify::active",
  140. G_CALLBACK(start_video), switcher);
  141. button = gtk_builder_get_object(builder, "button_take_photo");
  142. g_signal_connect(button, "clicked", G_CALLBACK(take_picture), switcher);
  143. button = gtk_builder_get_object(builder, "quit");
  144. g_signal_connect(button, "clicked", G_CALLBACK(clean_up_b4_go), switcher);
  145. g_signal_connect_swapped(button, "clicked", G_CALLBACK(gtk_main_quit), NULL);
  146. scale = gtk_builder_get_object(builder, "pwm_scale");
  147. g_signal_connect(scale, "value-changed", G_CALLBACK(pwm_scale_moved), NULL);
  148. /*Here we configure the modes of the GPIO PINs*/
  149. set_gpio_modes();
  150. gtk_main();
  151. return 0;
  152. }