From 3ffc19bbe6a399654e642ed7941cb81ed23f6920 Mon Sep 17 00:00:00 2001 From: Matthias Vogelgesang Date: Mon, 16 Apr 2012 12:25:23 +0200 Subject: Port control application to libuca 0.6 --- test/CMakeLists.txt | 20 ++- test/control.c | 344 ++++++++++++++++------------------------------------ test/control.glade | 110 ++++++++++++++++- 3 files changed, 218 insertions(+), 256 deletions(-) (limited to 'test') diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 096c287..1e6eb28 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -26,17 +26,15 @@ target_link_libraries(grab uca-gobject ${GLIB2_LIBRARIES} ${GOBJECT2_LIBRARIES}) #target_link_libraries(grab-async uca) #target_link_libraries(benchmark uca) -#if (GTK2_FOUND) -# include_directories(${GTK2_INCLUDE_DIRS}) -# add_executable(control control.c) -# target_link_libraries(control -# uca -# ${GTK2_LIBRARIES} -# ${GTHREAD2_LIBRARIES} -# ) -#endif() -# -# >>> TEMPORARY TEST EXECUTABLE <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< +if (GTK2_FOUND) + include_directories(${GTK2_INCLUDE_DIRS}) + add_executable(control control.c) + target_link_libraries(control + uca-gobject + ${GTK2_LIBRARIES} + ${GTHREAD2_LIBRARIES} + ) +endif() include_directories( diff --git a/test/control.c b/test/control.c index 4397dbe..0614071 100644 --- a/test/control.c +++ b/test/control.c @@ -21,8 +21,7 @@ #include #include #include - -#include "uca.h" +#include "uca-camera.h" typedef struct { @@ -32,6 +31,7 @@ typedef struct { guchar *buffer, *pixels; GdkPixbuf *pixbuf; GtkWidget *image; + UcaCamera *camera; GtkStatusbar *statusbar; guint statusbar_context_id; @@ -40,18 +40,14 @@ typedef struct { int width; int height; int pixel_size; - uca_camera *cam; - uca *u; float scale; } ThreadData; - typedef struct { ThreadData *thread_data; GtkTreeStore *tree_store; } ValueCellData; - enum { COLUMN_NAME = 0, COLUMN_VALUE, @@ -72,9 +68,9 @@ void convert_8bit_to_rgb(guchar *output, guchar *input, int width, int height) void convert_16bit_to_rgb(guchar *output, guchar *input, int width, int height, float scale) { - uint16_t *in = (uint16_t *) input; + guint16 *in = (guint16 *) input; for (int i = 0, j = 0; i < width*height; i++) { - guchar val = (uint8_t) ((in[i]/scale)*256.0f); + guchar val = (guint8) ((in[i]/scale)*256.0f); output[j++] = val; output[j++] = val; output[j++] = val; @@ -95,23 +91,16 @@ void reallocate_buffers(ThreadData *td, int width, int height) td->pixels = gdk_pixbuf_get_pixels(td->pixbuf); gtk_image_set_from_pixbuf(GTK_IMAGE(td->image), td->pixbuf); memset(td->buffer, 0, num_bytes); - - if (uca_cam_alloc(td->cam, 20) != UCA_NO_ERROR) - g_print("Couldn't allocate buffer for 20 frames\n"); } void *grab_thread(void *args) { ThreadData *data = (ThreadData *) args; - uca_camera *cam = data->cam; - char filename[FILENAME_MAX] = {0,}; - int counter = 0; + gchar filename[FILENAME_MAX] = {0,}; + gint counter = 0; while (data->running) { - if (uca_cam_grab(cam, (char *) data->buffer, NULL) != UCA_NO_ERROR) { - sleep(1); - continue; - } + uca_camera_grab(data->camera, (gpointer) &data->buffer, NULL); if (data->store) { snprintf(filename, FILENAME_MAX, "frame-%i-%08i.raw", data->timestamp, counter++); @@ -146,7 +135,7 @@ void on_destroy(GtkWidget *widget, gpointer data) { ThreadData *td = (ThreadData *) data; td->running = FALSE; - uca_destroy(td->u); + g_object_unref(td->camera); gtk_main_quit(); } @@ -159,15 +148,23 @@ void on_adjustment_scale_value_changed(GtkAdjustment* adjustment, gpointer user_ void on_toolbutton_run_clicked(GtkWidget *widget, gpointer args) { ThreadData *data = (ThreadData *) args; + if (data->running) return; GError *error = NULL; data->running = TRUE; - uca_cam_start_recording(data->cam); + + uca_camera_start_recording(data->camera, &error); + + if (error != NULL) { + g_printerr("Failed to start recording: %s\n", error->message); + return; + } + if (!g_thread_create(grab_thread, data, FALSE, &error)) { g_printerr("Failed to create thread: %s\n", error->message); - uca_destroy(data->u); + return; } } @@ -176,7 +173,7 @@ void on_toolbutton_stop_clicked(GtkWidget *widget, gpointer args) ThreadData *data = (ThreadData *) args; data->running = FALSE; data->store = FALSE; - uca_cam_stop_recording(data->cam); + uca_camera_stop_recording(data->camera, NULL); } void on_toolbutton_record_clicked(GtkWidget *widget, gpointer args) @@ -190,270 +187,135 @@ void on_toolbutton_record_clicked(GtkWidget *widget, gpointer args) if (data->running != TRUE) { data->running = TRUE; - uca_cam_start_recording(data->cam); + uca_camera_start_recording(data->cam, &error); + if (!g_thread_create(grab_thread, data, FALSE, &error)) { g_printerr("Failed to create thread: %s\n", error->message); - uca_destroy(data->u); } } } -void on_valuecell_edited(GtkCellRendererText *renderer, gchar *path, gchar *new_text, gpointer data) +static void create_main_window(GtkBuilder *builder, const gchar* camera_name) { - ValueCellData *value_data = (ValueCellData *) data; - - if (value_data->thread_data->running) - return; - - GtkTreeModel *tree_model = GTK_TREE_MODEL(value_data->tree_store); - GtkTreePath *tree_path = gtk_tree_path_new_from_string(path); - GtkTreeIter iter; + static ThreadData td; - if (gtk_tree_model_get_iter(tree_model, &iter, tree_path)) { - uca_camera *cam = value_data->thread_data->cam; - uint32_t prop_id; - gtk_tree_model_get(tree_model, &iter, COLUMN_UCA_ID, &prop_id, -1); - - /* TODO: extensive value checking */ - uint32_t val = (uint32_t) g_ascii_strtoull(new_text, NULL, 10); - uca_cam_set_property(cam, prop_id, &val); - if ((prop_id == UCA_PROP_WIDTH) || (prop_id == UCA_PROP_HEIGHT)) { - uint32_t width, height; - uca_cam_get_property(cam, UCA_PROP_WIDTH, &width, 0); - uca_cam_get_property(cam, UCA_PROP_HEIGHT, &height, 0); - reallocate_buffers(value_data->thread_data, width, height); - } + GError *error = NULL; + UcaCamera *camera = uca_camera_new(camera_name, &error); - gtk_tree_store_set(value_data->tree_store, &iter, COLUMN_VALUE, new_text, -1); + if (error != NULL) { + g_error("%s\n", error->message); + gtk_main_quit(); } -} -static void get_first_level_root(GtkTreeStore *store, GtkTreeIter *iter, gchar *group) -{ - GtkTreeIter root; - if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(store), &root)) { - gchar *str; - gtk_tree_model_get(GTK_TREE_MODEL(store), &root, 0, &str, -1); - if (g_strcmp0(group, str) == 0) { - *iter = root; - return; - } + guint bits_per_sample; + g_object_get(camera, + "roi-width", &td.width, + "roi-height", &td.height, + "sensor-bitdepth", &bits_per_sample, + NULL); - /* Iterate through all groups */ - while (gtk_tree_model_iter_next(GTK_TREE_MODEL(store), &root)) { - gtk_tree_model_get(GTK_TREE_MODEL(store), &root, 0, &str, -1); - if (g_strcmp0(group, str) == 0) { - *iter = root; - g_free(str); - return; - } - } + GtkWidget *window = GTK_WIDGET(gtk_builder_get_object(builder, "window")); + GtkWidget *image = GTK_WIDGET(gtk_builder_get_object(builder, "image")); + GdkPixbuf *pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, td.width, td.height); + gtk_image_set_from_pixbuf(GTK_IMAGE(image), pixbuf); - /* Not found, append the group */ - g_free(str); - } + td.pixel_size = bits_per_sample == 16 ? 2 : 1; + td.image = image; + td.pixbuf = pixbuf; + td.buffer = (guchar *) g_malloc(td.pixel_size * td.width * td.height); + td.pixels = gdk_pixbuf_get_pixels(pixbuf); + td.running = FALSE; + td.scale = 65535.0f; + td.statusbar = GTK_STATUSBAR(gtk_builder_get_object(builder, "statusbar")); + td.statusbar_context_id = gtk_statusbar_get_context_id(td.statusbar, "Recording Information"); + td.store = FALSE; + td.camera = camera; - /* Tree is empty or group is not found */ - gtk_tree_store_append(store, iter, NULL); - gtk_tree_store_set(store, iter, 0, group, -1); + g_signal_connect(window, "destroy", G_CALLBACK(on_destroy), &td); + g_signal_connect(gtk_builder_get_object(builder, "toolbutton_run"), + "clicked", G_CALLBACK(on_toolbutton_run_clicked), &td); + g_signal_connect(gtk_builder_get_object(builder, "toolbutton_stop"), + "clicked", G_CALLBACK(on_toolbutton_stop_clicked), &td); + g_signal_connect(gtk_builder_get_object(builder, "toolbutton_record"), + "clicked", G_CALLBACK(on_toolbutton_record_clicked), &td); + + gtk_widget_show(image); + gtk_widget_show(window); } -static void find_recursively(GtkTreeStore *store, GtkTreeIter *root, GtkTreeIter *result, gchar **tokens, int depth) +static void on_button_proceed_clicked(GtkWidget *widget, gpointer data) { + GtkBuilder *builder = GTK_BUILDER(data); + GtkWidget *choice_window = GTK_WIDGET(gtk_builder_get_object(builder, "choice-window")); + GtkTreeView *treeview = GTK_TREE_VIEW(gtk_builder_get_object(builder, "treeview-cameras")); + GtkListStore *list_store = GTK_LIST_STORE(gtk_builder_get_object(builder, "camera-types")); + + GtkTreeSelection *selection = gtk_tree_view_get_selection(treeview); + GList *selected_rows = gtk_tree_selection_get_selected_rows(selection, NULL); GtkTreeIter iter; - gchar *str; - gchar *current_token = tokens[depth]; - if (current_token == NULL) { - *result = *root; - return; - } + gtk_widget_destroy(choice_window); + gboolean valid = gtk_tree_model_get_iter(GTK_TREE_MODEL(list_store), &iter, selected_rows->data); - if (!gtk_tree_model_iter_has_child(GTK_TREE_MODEL(store), root)) { - gtk_tree_store_append(store, &iter, root); - if (tokens[depth+1] == NULL) { - *result = iter; - return; - } - else { - gtk_tree_store_set(store, &iter, 0, current_token, -1); - find_recursively(store, &iter, result, tokens, depth+1); - } + if (valid) { + gchar *data; + gtk_tree_model_get(GTK_TREE_MODEL(list_store), &iter, 0, &data, -1); + create_main_window(builder, data); + g_free(data); } - gtk_tree_model_iter_children(GTK_TREE_MODEL(store), &iter, root); - do { - gtk_tree_model_get(GTK_TREE_MODEL(store), &iter, 0, &str, -1); - if (g_strcmp0(current_token, str) == 0) { - find_recursively(store, &iter, result, tokens, depth+1); - g_free(str); - return; - } - } while (gtk_tree_model_iter_next(GTK_TREE_MODEL(store), &iter)); - - g_free(str); - gtk_tree_store_append(store, &iter, root); - gtk_tree_store_set(store, &iter, COLUMN_NAME, current_token, -1); - find_recursively(store, &iter, result, tokens, depth+1); + g_list_foreach(selected_rows, (GFunc) gtk_tree_path_free, NULL); + g_list_free(selected_rows); } -static void fill_tree_store(GtkTreeStore *tree_store, uca_camera *cam) +static void create_choice_window(GtkBuilder *builder) { - GtkTreeIter iter, child; - uca_property *property; - const size_t num_bytes = 256; - gchar *value_string = g_malloc(num_bytes); - guint8 value_8; - guint32 value_32; - - for (int prop_id = 0; prop_id < UCA_PROP_LAST; prop_id++) { - property = uca_get_full_property(prop_id); - uint32_t result = UCA_NO_ERROR; - switch (property->type) { - case uca_string: - result = uca_cam_get_property(cam, prop_id, value_string, num_bytes); - break; - - case uca_uint8t: - result = uca_cam_get_property(cam, prop_id, &value_8, 0); - g_sprintf(value_string, "%d", value_8); - break; - - case uca_uint32t: - result = uca_cam_get_property(cam, prop_id, &value_32, 0); - g_sprintf(value_string, "%d", value_32); - break; - } - - /* Find first level root */ - gchar **tokens = g_strsplit(property->name, ".", 0); - get_first_level_root(tree_store, &iter, tokens[0]); - find_recursively(tree_store, &iter, &child, tokens, 1); - - if (result != UCA_NO_ERROR) - g_sprintf(value_string, "n/a"); + gchar **camera_types = uca_camera_get_types(); - int count = 0; - while (tokens[count++] != NULL); - - gtk_tree_store_set(tree_store, &child, - COLUMN_NAME, tokens[count-2], - COLUMN_VALUE, value_string, - COLUMN_UNIT, uca_unit_map[property->unit], - COLUMN_UCA_ID, prop_id, - -1); + GtkWidget *choice_window = GTK_WIDGET(gtk_builder_get_object(builder, "choice-window")); + GtkTreeView *treeview = GTK_TREE_VIEW(gtk_builder_get_object(builder, "treeview-cameras")); + GtkListStore *list_store = GTK_LIST_STORE(gtk_builder_get_object(builder, "camera-types")); + GtkButton *proceed_button = GTK_BUTTON(gtk_builder_get_object(builder, "button-proceed")); + GtkTreeIter iter; - g_strfreev(tokens); + for (guint i = 0; camera_types[i] != NULL; i++) { + gtk_list_store_append(list_store, &iter); + gtk_list_store_set(list_store, &iter, 0, camera_types[i], -1); } - g_free(value_string); -} + gboolean valid = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(list_store), &iter); -static void value_cell_data_func(GtkTreeViewColumn *column, GtkCellRenderer *cell, GtkTreeModel *model, GtkTreeIter *iter, gpointer data) -{ - uint32_t prop_id; - - gtk_tree_model_get(model, iter, COLUMN_UCA_ID, &prop_id, -1); - uca_property *property = uca_get_full_property(prop_id); - if (property->access & uca_write) { - g_object_set(cell, "mode", GTK_CELL_RENDERER_MODE_EDITABLE, NULL); - g_object_set(GTK_CELL_RENDERER_TEXT(cell), "editable", TRUE, NULL); - g_object_set(GTK_CELL_RENDERER_TEXT(cell), "style", PANGO_STYLE_NORMAL, NULL); - } - else { - g_object_set(cell, "mode", GTK_CELL_RENDERER_MODE_INERT, NULL); - g_object_set(GTK_CELL_RENDERER_TEXT(cell), "editable", FALSE, NULL); - g_object_set(GTK_CELL_RENDERER_TEXT(cell), "style", PANGO_STYLE_ITALIC, NULL); + if (valid) { + GtkTreeSelection *selection = gtk_tree_view_get_selection(treeview); + gtk_tree_selection_unselect_all(selection); + gtk_tree_selection_select_path(selection, gtk_tree_model_get_path(GTK_TREE_MODEL(list_store), &iter)); } + + g_strfreev(camera_types); + g_signal_connect(proceed_button, "clicked", G_CALLBACK(on_button_proceed_clicked), builder); + gtk_widget_show_all(GTK_WIDGET(choice_window)); } int main(int argc, char *argv[]) { - uca *u = uca_init(NULL); - if (u == NULL) { - g_print("Couldn't initialize frame grabber and/or cameras\n"); - return 1; - } - - int width, height, bits_per_sample; - uca_camera *cam = u->cameras; - uca_cam_get_property(cam, UCA_PROP_WIDTH, &width, 0); - uca_cam_get_property(cam, UCA_PROP_HEIGHT, &height, 0); - uca_cam_get_property(cam, UCA_PROP_BITDEPTH, &bits_per_sample, 0); - - uint32_t mode = UCA_TIMESTAMP_ASCII | UCA_TIMESTAMP_BINARY; - uca_cam_set_property(cam, UCA_PROP_TIMESTAMP_MODE, &mode); - uint32_t val = 1; - uca_cam_set_property(cam, UCA_PROP_GRAB_AUTO, &val); - val = 0; - uca_cam_set_property(cam, UCA_PROP_GRAB_SYNCHRONOUS, &val); + GError *error = NULL; g_thread_init(NULL); gdk_threads_init(); - gtk_init (&argc, &argv); + gtk_init(&argc, &argv); GtkBuilder *builder = gtk_builder_new(); - GError *error = NULL; + if (!gtk_builder_add_from_file(builder, "control.glade", &error)) { - g_print("Couldn't load UI file!\n"); - g_print("Message: %s\n", error->message); - g_free(error); + g_print("Error: %s\n", error->message); + return 1; } - GtkWidget *window = GTK_WIDGET(gtk_builder_get_object(builder, "window")); - GtkWidget *image = GTK_WIDGET(gtk_builder_get_object(builder, "image")); - GdkPixbuf *pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, width, height); - gtk_image_set_from_pixbuf(GTK_IMAGE(image), pixbuf); - - GtkTreeStore *tree_store = GTK_TREE_STORE(gtk_builder_get_object(builder, "cameraproperties")); - GtkTreeViewColumn *value_column = GTK_TREE_VIEW_COLUMN(gtk_builder_get_object(builder, "valuecolumn")); - GtkCellRendererText *value_renderer = GTK_CELL_RENDERER_TEXT(gtk_builder_get_object(builder, "valuecell")); - fill_tree_store(tree_store, cam); - - gtk_tree_view_column_set_cell_data_func(value_column, GTK_CELL_RENDERER(value_renderer), value_cell_data_func, NULL, NULL); - - /* start grabbing and thread */ - int pixel_size = bits_per_sample == 8 ? 1 : 2; - if (uca_cam_alloc(cam, 20) != UCA_NO_ERROR) - g_print("Couldn't allocate buffer for 20 frames\n"); - - ThreadData td; - td.image = image; - td.pixbuf = pixbuf; - td.buffer = (guchar *) g_malloc(pixel_size * width * height); - td.pixels = gdk_pixbuf_get_pixels(pixbuf); - td.width = width; - td.height = height; - td.cam = cam; - td.u = u; - td.running = FALSE; - td.pixel_size = pixel_size; - td.scale = 65535.0f; - td.statusbar = GTK_STATUSBAR(gtk_builder_get_object(builder, "statusbar")); - td.statusbar_context_id = gtk_statusbar_get_context_id(td.statusbar, "Recording Information"); - td.store = FALSE; - - gtk_builder_connect_signals(builder, &td); - - /* Configure value cell */ - ValueCellData value_cell_data; - value_cell_data.thread_data = &td; - value_cell_data.tree_store = tree_store; - - g_signal_connect(gtk_builder_get_object(builder, "valuecell"), "edited", - G_CALLBACK(on_valuecell_edited), &value_cell_data); - - /* Configure scale adjustment */ - GtkAdjustment *adjustment = (GtkAdjustment *) gtk_builder_get_object(builder, "adjustment_scale"); - gtk_adjustment_configure(adjustment, 65535.0, 1.0, 65535.0, 0.5, 10.0, 0.0); - g_signal_connect(adjustment, "value-changed", - G_CALLBACK(on_adjustment_scale_value_changed), &td); - - gtk_widget_show(image); - gtk_widget_show(window); + create_choice_window(builder); + gtk_builder_connect_signals(builder, NULL); gdk_threads_enter(); - gtk_main (); + gtk_main(); gdk_threads_leave(); return 0; diff --git a/test/control.glade b/test/control.glade index b9fab85..eeeeaaf 100644 --- a/test/control.glade +++ b/test/control.glade @@ -14,11 +14,16 @@ + + + + + + Camera Control 800 600 - @@ -119,7 +124,6 @@ Run True gtk-media-play - False @@ -132,7 +136,6 @@ Record True gtk-media-record - False @@ -145,7 +148,6 @@ Stop True gtk-media-stop - False @@ -311,4 +313,104 @@ 1 10 + + + + True + 2 + + + True + True + camera-types + + + column + + + + 0 + + + + + + + 0 + + + + + True + 6 + end + + + gtk-quit + True + True + True + True + + + + False + False + 0 + + + + + gtk-ok + True + True + True + True + + + False + False + 1 + + + + + False + 6 + 1 + + + + + + + 5 + normal + + + True + 2 + + + + + + True + end + + + + + + + + + False + end + 0 + + + + + -- cgit v1.2.3