This repository has been archived on 2024-10-20. You can view files and clone it, but cannot push or open issues or pull requests.
stm32-serial-flash/main.c

165 lines
3.8 KiB
C
Raw Normal View History

2016-06-07 20:21:28 +02:00
/*
* Description: STM32 Microcontroller flashing utility
* Author: Mario Hüttel
*
* Version: 0.1
* License: --
*
*
*/
#include <stdio.h>
#include <gtk/gtk.h>
#include <string.h>
#include <stdint.h>
#include <pthread.h>
#define ISOPT(var) (!strcmp(argv[i], var))
#define GETOPT(var) if (++i < argc) strcpy(var, argv[i])
#define MAXPARAMLENGTH 100
enum ValueAvail {hasNoVal, hasVal};
typedef struct _parameter {
char *name;
enum ValueAvail hasValue;
void *value;
} parameter;
typedef struct _builderWidget {
char *widgetId;
GtkWidget **widget;
}builderWidget;
typedef struct _flasher {
GtkWidget **progressBar;
GtkWidget **fileEntry;
GtkWidget **portEntry;
}flasher;
void parseParams(parameter *progParams, int argc, char **argv);
void parseBuilder(GtkBuilder *builder, builderWidget *builderWidgets);
void startFlash();
int main(int argc, char *argv[]) {
static int gui = 0;
static char port[MAXPARAMLENGTH] = {0};
static char fileName[MAXPARAMLENGTH] = {0};
static GtkWidget *startButton;
static GtkWidget *fileDialogButton;
static GtkWidget *fileEntry;
static GtkWidget *portEntry;
static GtkWidget *progressBar;
static GtkWidget *fileDialog;
static flasher stmFlasher;
static parameter progParams[] = {
{
.name = "--gui",
.hasValue = hasNoVal,
.value = (void*)&gui
},
{
.name = "--port",
.hasValue = hasVal,
.value = (void*)port
},
{
.name = "--file",
.hasValue = hasVal,
.value = (void*)fileName
},
{.name = NULL}
};
static builderWidget builderWidgets[] = {
{
.widgetId = "startButton",
.widget = &startButton
},
{
.widgetId = "fileDialogButton",
.widget = &fileDialogButton
},
{
.widgetId = "fileEntry",
.widget = &fileEntry
},
{
.widgetId = "portEntry",
.widget = &portEntry
},
{
.widgetId = "progressBar",
.widget = &progressBar
},
{
.widgetId = "Dialog",
.widget = &fileDialog
},
{
.widgetId = NULL,
.widget = NULL
}
};
GtkBuilder *builder;
parseParams(progParams, argc, argv);
// Create flasher and fill elements
stmFlasher.fileEntry = &fileEntry;
stmFlasher.portEntry = &portEntry;
// TODO: above
if (gui) {
gtk_init(&argc, &argv);
builder = gtk_builder_new();
gtk_builder_add_from_file(builder, "window.glade", NULL);
//Get Widgets from Builder
parseBuilder(builder, builderWidgets);
g_signal_connect(startButton, "clicked", G_CALLBACK(startFlash), &stmFlasher);
gtk_main();
} else {
startFlash(&stmFlasher);
}
return 0;
}
void startFlash(flasher *flashObj) {
return;
}
void parseParams(parameter* progParams, int argc, char **argv)
{
int i = 0;
parameter* paramptr = NULL;
for (i = 0; i < argc; i++) {
for (paramptr = progParams; paramptr->name; paramptr++) {
if (paramptr->hasValue == hasNoVal) *(int*)paramptr->value = 0;
if (ISOPT(paramptr->name))
{
if (paramptr->hasValue == hasNoVal) {
*(int*)paramptr->value = 1;
} else {
printf("setting\n");
GETOPT((char*)paramptr->value);
}
}
}
}
}
void parseBuilder(GtkBuilder *builder, builderWidget *builderWidgets) {
builderWidget *ptr = NULL;
for (ptr = builderWidgets; ptr->widgetId;ptr++) {
*(ptr->widget) = GTK_WIDGET(gtk_builder_get_object(builder, (gchar*)ptr->widgetId));
}
}