From f160148198d3bb140f597e4b6382f3db2e3f37e4 Mon Sep 17 00:00:00 2001 From: Olaf Rempel Date: Wed, 19 Mar 2008 17:34:33 +0100 Subject: [PATCH] variable editing --- Makefile | 3 +++ gui_variable_tab.c | 50 ++++++++++++++++++++++++++++++------ tdc_parser.c | 4 +-- tdc_variable.c | 64 +++++++++++++++++++++++++++++++++++++++++++++- tdc_variable.h | 1 + 5 files changed, 111 insertions(+), 11 deletions(-) diff --git a/Makefile b/Makefile index 3d96329..bfab644 100644 --- a/Makefile +++ b/Makefile @@ -21,4 +21,7 @@ $(TARGET): $(SRC:.c=.o) clean: rm -rf $(TARGET) *.o *.d +socat: + socat tcp4-listen:5000,fork,reuseaddr /dev/ttyACM0,raw,echo=0,flock-ex-nb + -include $(shell find -name *.d 2> /dev/null) diff --git a/gui_variable_tab.c b/gui_variable_tab.c index 48be082..ce4f532 100644 --- a/gui_variable_tab.c +++ b/gui_variable_tab.c @@ -1,6 +1,7 @@ #include #include "tdc_store.h" +#include "tdc_parser.h" #include "tdc_proto.h" enum { @@ -14,6 +15,10 @@ enum { static GtkListStore *list_store; +static int viewmode = 0; +static int sortmode = GTK_SORT_ASCENDING; +static struct tdc_var *current_edited_var; + static void cell_graph_toggle(GtkCellRendererToggle *cell, gchar *path_string, gpointer user_data) @@ -48,12 +53,38 @@ static gboolean header_toggle_update(GtkTreeModel *model, return FALSE; } -static void cell_value_edited(GtkCellRendererText *cell, - gchar *path_string, - gchar *new_text, +static void value_edit_started(GtkCellRenderer *renderer, + GtkCellEditable *editable, + gchar *path, + gpointer user_data) +{ + GtkTreeIter it; + gtk_tree_model_get_iter_from_string(GTK_TREE_MODEL(list_store), &it, path); + gtk_tree_model_get(GTK_TREE_MODEL(list_store), &it, COL_VALUE, ¤t_edited_var, -1); +} + +static void value_edit_canceled(GtkCellRenderer *renderer, gpointer user_data) +{ + current_edited_var = NULL; +} + +static void value_edit_done(GtkCellRendererText *cell, + gchar *path, + gchar *text, gpointer user_data) { - printf("cell_value_edited\n"); + GtkTreeIter it; + gtk_tree_model_get_iter_from_string(GTK_TREE_MODEL(list_store), &it, path); + + struct tdc_var *var; + gtk_tree_model_get(GTK_TREE_MODEL(list_store), &it, COL_VALUE, &var, -1); + + if (tdcvar_parse_value(var, text) >= 0) { + // FIXME: assuming board 1 + tdcparser_send_setvalue(1, var->id); + } + + current_edited_var = NULL; } static gint sort_by_name_func(GtkTreeModel *model, @@ -80,9 +111,6 @@ static gint sort_by_name_func(GtkTreeModel *model, return ret; } -static int viewmode = 0; -static int sortmode = GTK_SORT_ASCENDING; - static void header_name_toggle(GtkTreeViewColumn *treeviewcolumn, gpointer user_data) { @@ -147,7 +175,10 @@ gint gui_vartab_init(GtkNotebook *notebook) g_object_set(col, "clickable", TRUE, NULL); g_signal_connect(col, "clicked", (GCallback)header_value_toggle, NULL); renderer = gtk_cell_renderer_text_new(); - g_signal_connect(renderer, "edited", (GCallback)cell_value_edited, NULL); + g_signal_connect(renderer, "edited", (GCallback)value_edit_done, NULL); + g_signal_connect(renderer, "editing-started", (GCallback)value_edit_started, NULL); + g_signal_connect(renderer, "editing-canceled", (GCallback)value_edit_canceled, NULL); + gtk_tree_view_column_pack_start(col, renderer, FALSE); gtk_tree_view_column_add_attribute(col, renderer, "editable", COL_VALUE_EDIT); gtk_tree_view_column_set_cell_data_func(col, renderer, cell_value_func, NULL, NULL); @@ -198,6 +229,9 @@ void gui_vartab_add_var(struct tdc_var *var) void gui_vartab_update_var(struct tdc_var *var) { + if (var == current_edited_var) + return; + /* do a dummy write to update cells */ GtkTreeIter *it = (GtkTreeIter *)var->privdata; gtk_list_store_set(list_store, it, -1); diff --git a/tdc_parser.c b/tdc_parser.c index c6d967a..307e5bb 100644 --- a/tdc_parser.c +++ b/tdc_parser.c @@ -65,10 +65,10 @@ int tdcparser_send_setvalue(int address, int id) int datasize = (var->flags & TDC_SIZEMASK); struct tdc_setvalue_request *pkt; - pkt = g_malloc0(sizeof(struct tdc_setvalue_request) + datasize); + pkt = g_malloc0(sizeof(*pkt) + datasize); pkt->cmd = (address << 4) | TDC_SETVALUE; - pkt->size = sizeof(pkt) + datasize; + pkt->size = sizeof(*pkt) + datasize; pkt->id = (id & 0xFF); memcpy(pkt->data, &var->data, datasize); diff --git a/tdc_variable.c b/tdc_variable.c index 27320fd..6013705 100644 --- a/tdc_variable.c +++ b/tdc_variable.c @@ -1,6 +1,8 @@ #include #include +#include #include +#include #include @@ -152,6 +154,66 @@ void tdcvar_get_type(struct tdc_var *var, char *buf, int size) pos = snprintf(buf + pos, size - pos, " (ro)"); } +int tdcvar_parse_value(struct tdc_var *var, char *data) +{ + struct tdc_var tmp; + + int width = (var->flags & TDC_SIZEMASK); + switch (var->flags & TDC_TYPEMASK) { + case TDC_UNSIGNED: + if (strchr(data, '-') != NULL) + return -1; + + errno = 0; + if (width < 8) + tmp.data_uint32 = strtoul(data, NULL, 0); + else + tmp.data_uint64 = strtoull(data, NULL, 0); + + if (errno != 0) { + errno = 0; + return -1; + } + break; + + case TDC_SIGNED: + errno = 0; + if (width < 8) + tmp.data_uint32 = strtol(data, NULL, 0); + else + tmp.data_uint64 = strtoll(data, NULL, 0); + + if (errno != 0) { + errno = 0; + return -1; + } + break; + + case TDC_FP: + errno = 0; + if (width == sizeof(float)) + tmp.data_float = strtod(data, NULL); + else + tmp.data_double = strtod(data, NULL); + + if (errno != 0) { + errno = 0; + return -1; + } + break; + + case TDC_FIXED: + return -1; + break; + + default: + break; + } + + memcpy(&var->data, &tmp.data, width); + return 0; +} + struct tdc_var * tdcvar_create(int id, uint32_t flags, char *name, int len) { struct tdc_var *var = g_malloc0(sizeof(struct tdc_var) + len + 1); @@ -161,7 +223,7 @@ struct tdc_var * tdcvar_create(int id, uint32_t flags, char *name, int len) strncpy(var->name, name, len); var->name[len] = '\0'; - printf("create_var(%d, 0x%x, '%s', %d)\n", var->id, var->flags, var->name, len); +// printf("create_var(%d, 0x%x, '%s', %d)\n", var->id, var->flags, var->name, len); return var; } diff --git a/tdc_variable.h b/tdc_variable.h index da18a92..8649fc7 100644 --- a/tdc_variable.h +++ b/tdc_variable.h @@ -22,6 +22,7 @@ struct tdc_var { void tdcvar_get_value(struct tdc_var *var, char *buf, int size, int viewmode); void tdcvar_get_type(struct tdc_var *var, char *buf, int size); +int tdcvar_parse_value(struct tdc_var *var, char *data); struct tdc_var * tdcvar_create(int id, uint32_t flags, char *name, int len); void tdcvar_update(struct tdc_var *var, uint8_t *data, int len);