diff options
author | Nao Pross <naopross@thearcway.org> | 2017-08-23 16:43:56 +0200 |
---|---|---|
committer | Nao Pross <naopross@thearcway.org> | 2017-08-23 16:43:56 +0200 |
commit | c7925cbb73f319a390507b33e4df44f106822683 (patch) | |
tree | b7890e18d481822e6f38f6739b43fbd9d02b587f /sw/programmer/linux/src/ui.c | |
parent | updated .gitignore (diff) | |
download | z80uPC-c7925cbb73f319a390507b33e4df44f106822683.tar.gz z80uPC-c7925cbb73f319a390507b33e4df44f106822683.zip |
new programmer interface for linux
there is a new program with GUI (gtk3) to send data to its avr
conterpart.
it needs to be tested, since there may be a few bugs in it;
building a GUI was a terrible idea.
Diffstat (limited to '')
-rw-r--r-- | sw/programmer/linux/src/ui.c | 126 |
1 files changed, 89 insertions, 37 deletions
diff --git a/sw/programmer/linux/src/ui.c b/sw/programmer/linux/src/ui.c index b33aa04..87dc0c7 100644 --- a/sw/programmer/linux/src/ui.c +++ b/sw/programmer/linux/src/ui.c @@ -1,90 +1,142 @@ #include "ui.h" static bool ui_connected, ui_fileset; -static GtkTextBuffer *ui_logbuf; static GtkBuilder *ui_builder; void ui_init(int *argc, char **argv[]) { GtkWindow *window; - GtkTextView *logview; GtkFileChooserButton *filechoosebtn; GtkButton *connectbtn, *flashbtn; - /* set ui global variables */ ui_connected = ui_fileset = false; - /* start gtk */ gtk_init(argc, argv); - /* load glade gtk ui */ ui_builder = gtk_builder_new(); - gtk_builder_add_from_file(ui_builder, "gbprog.ui", NULL); + gtk_builder_add_from_file(ui_builder, "z80prog.ui", NULL); - /* connect logger buffer (extern variable) */ - ui_logbuf = gtk_text_buffer_new(NULL); - logview = GTK_TEXT_VIEW(gtk_builder_get_object(ui_builder, "logview")); - - gtk_text_view_set_buffer(logview, ui_logbuf); - - /* connect objects to function calls */ - // window + /* connect objects to callbacks */ window = GTK_WINDOW(gtk_builder_get_object(ui_builder, "window")); g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL); - // connectbtn - connectbtn = GTK_BUTTON(gtk_builder_get_object(ui_builder, "flashbtn")); - g_signal_connect(connectbtn, "clicked", - G_CALLBACK(ui_connect_clicked), NULL); + connectbtn = GTK_BUTTON(gtk_builder_get_object(ui_builder, "connectbtn")); + g_signal_connect(connectbtn, "clicked", G_CALLBACK(ui_connect_clicked), NULL); - // flashbtn flashbtn = GTK_BUTTON(gtk_builder_get_object(ui_builder, "flashbtn")); g_signal_connect(flashbtn, "clicked", G_CALLBACK(ui_flash_clicked), NULL); - // file chooser - filechoosebtn = GTK_FILE_CHOOSER_BUTTON( - gtk_builder_get_object(ui_builder, "filechoosebtn")); + filechoosebtn = GTK_FILE_CHOOSER_BUTTON(gtk_builder_get_object(ui_builder, "filechoosebtn")); g_signal_connect(filechoosebtn, "file-set", G_CALLBACK(ui_file_set), NULL); /* start gtk window */ gtk_main(); } -void ui_log(const char *msg, char type) +void ui_log(const char *msg) { GtkTextIter end; - gchar typech[4] = "[ ] "; + GtkTextView *ui_log; + + static GtkTextBuffer *ui_logbuf = NULL; + + ui_log = GTK_TEXT_VIEW(gtk_builder_get_object(ui_builder, "logview")); - switch (type) { - case 'm': typech[1] = '@'; break; // message - case 'w': typech[1] = '#'; break; // warning - case 'e': typech[1] = '!'; break; // error + if (ui_logbuf == NULL) { + ui_logbuf = gtk_text_buffer_new(NULL); + gtk_text_view_set_buffer(ui_log, ui_logbuf); } gtk_text_buffer_get_end_iter(ui_logbuf, &end); - gtk_text_buffer_insert(ui_logbuf, &end, typech, 4); - gtk_text_buffer_get_end_iter(ui_logbuf, &end); - gtk_text_buffer_insert(ui_logbuf, &end, (const gchar *) msg, strlen(msg)); + gtk_text_buffer_insert(ui_logbuf, &end, msg, -1); + gtk_text_view_scroll_to_iter(ui_log, &end, .0, TRUE, .0, .1); } -void ui_file_set(GtkFileChooserButton *btn, gpointer user_data) +void ui_check_enable_flashbtn(void) { - GtkEntry *filepath = - GTK_ENTRY(gtk_builder_get_object(ui_builder, "filepath")); + GtkWidget *ui_flashbtn = GTK_WIDGET(gtk_builder_get_object(ui_builder, "flashbtn")); - gtk_entry_set_text(filepath, - gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(btn))); + if (ui_connected && ui_fileset) + gtk_widget_set_sensitive(ui_flashbtn, TRUE); + else + gtk_widget_set_sensitive(ui_flashbtn, FALSE); +} - ui_log("File set\n", 'm'); +void ui_file_set(GtkFileChooserButton *btn, gpointer user_data) +{ + GtkEntry *filepath = GTK_ENTRY(gtk_builder_get_object(ui_builder, "filepath")); + + gtk_entry_set_text(filepath, gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(btn))); + ui_log(UI_LOG_MSG "file set\n"); ui_fileset = true; + + ui_check_enable_flashbtn(); } void ui_connect_clicked(void) { + GtkEntry *ui_devpath = GTK_ENTRY(gtk_builder_get_object(ui_builder, "devpath")); + GtkSpinButton *ui_baudrate = GTK_SPIN_BUTTON(gtk_builder_get_object(ui_builder, "devbaudrate")); + GtkButton *ui_connectbtn = GTK_BUTTON(gtk_builder_get_object(ui_builder, "connectbtn")); + + unsigned long baudrate = gtk_spin_button_get_value_as_int(ui_baudrate); + const char *devpath = gtk_entry_get_text(ui_devpath); + + if (flash_open(devpath, baudrate) != 0) { + ui_log(UI_LOG_ERR "failed to open serial device\n"); + } else { + ui_connected = true; + ui_log(UI_LOG_MSG "connected to serial device "); + ui_log(devpath); + ui_log("\n"); + + g_signal_handlers_disconnect_by_func(ui_connectbtn, G_CALLBACK(ui_connect_clicked), NULL); + g_signal_connect(ui_connectbtn, "clicked", G_CALLBACK(ui_disconnect_clicked), NULL); + + gtk_button_set_label(ui_connectbtn, "gtk-disconnect"); + ui_check_enable_flashbtn(); + } +} +void ui_disconnect_clicked(void) +{ + GtkButton *ui_connectbtn = GTK_BUTTON(gtk_builder_get_object(ui_builder, "connectbtn")); + + flash_close(); + + ui_connected = false; + ui_log(UI_LOG_MSG "disconnected\n"); + + g_signal_handlers_disconnect_by_func(ui_connectbtn, G_CALLBACK(ui_disconnect_clicked), NULL); + g_signal_connect(ui_connectbtn, "clicked", G_CALLBACK(ui_connect_clicked), NULL); + + gtk_button_set_label(ui_connectbtn, "gtk-connect"); + ui_check_enable_flashbtn(); +} + +int ui_flash_write_start(void *ptr) +{ + const char *filepath = (const char *) ptr; + + flash_write(filepath, ui_log); + ui_check_enable_flashbtn(); + + return 0; } void ui_flash_clicked(void) { + GtkWidget *ui_flashbtn = GTK_WIDGET(gtk_builder_get_object(ui_builder, "flashbtn")); + GtkEntry *ui_filepath = GTK_ENTRY(gtk_builder_get_object(ui_builder, "filepath")); + + const char *filepath = gtk_entry_get_text(ui_filepath); + + gdk_threads_add_idle_full( + G_PRIORITY_HIGH_IDLE, + ui_flash_write_start, + (void *) filepath, + NULL + ); + gtk_widget_set_sensitive(ui_flashbtn, FALSE); } |