From ce10413ff5601f4b86ee67f8ed79b61be2d01720 Mon Sep 17 00:00:00 2001
From: Jake Drahos <j@kedrahos.com>
Date: Mon, 27 Feb 2017 16:02:22 -0600
Subject: [PATCH] GUI work

Can start/stop backend
---
 .gitignore                                 |  4 +++-
 groundStation/gui/MicroCART/MicroCART.pro  |  6 +++--
 groundStation/gui/MicroCART/mainwindow.cpp | 26 ++++++++++++++++++++++
 groundStation/gui/MicroCART/mainwindow.h   |  9 ++++++++
 groundStation/gui/MicroCART/mainwindow.ui  |  3 +++
 groundStation/gui/MicroCART/wrappers.c     | 26 ++++++++++++++++++++++
 groundStation/gui/MicroCART/wrappers.h     | 16 +++++++++++++
 7 files changed, 87 insertions(+), 3 deletions(-)
 create mode 100644 groundStation/gui/MicroCART/wrappers.c
 create mode 100644 groundStation/gui/MicroCART/wrappers.h

diff --git a/.gitignore b/.gitignore
index 44f996c51..e6a64c445 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1 +1,3 @@
-test.log
\ No newline at end of file
+test.log
+groundStation/gui/build*
+.gdb_history
diff --git a/groundStation/gui/MicroCART/MicroCART.pro b/groundStation/gui/MicroCART/MicroCART.pro
index cd3d24f69..874beeab9 100644
--- a/groundStation/gui/MicroCART/MicroCART.pro
+++ b/groundStation/gui/MicroCART/MicroCART.pro
@@ -13,8 +13,10 @@ TEMPLATE = app
 
 
 SOURCES += main.cpp\
-        mainwindow.cpp
+        mainwindow.cpp \
+    wrappers.c
 
-HEADERS  += mainwindow.h
+HEADERS  += mainwindow.h \
+    wrappers.h
 
 FORMS    += mainwindow.ui
diff --git a/groundStation/gui/MicroCART/mainwindow.cpp b/groundStation/gui/MicroCART/mainwindow.cpp
index 49d64fce7..01aa006e0 100644
--- a/groundStation/gui/MicroCART/mainwindow.cpp
+++ b/groundStation/gui/MicroCART/mainwindow.cpp
@@ -1,6 +1,8 @@
 #include "mainwindow.h"
 #include "ui_mainwindow.h"
 
+#include "wrappers.h"
+
 MainWindow::MainWindow(QWidget *parent) :
     QMainWindow(parent),
     ui(new Ui::MainWindow)
@@ -12,3 +14,27 @@ MainWindow::~MainWindow()
 {
     delete ui;
 }
+
+void MainWindow::on_pbStart_clicked()
+{
+    this->backendPid = startBackend();
+    findChild<QPushButton *>("pbStart")->setEnabled(false);
+    findChild<QPushButton *>("pbConnect")->setEnabled(false);
+    findChild<QPushButton *>("pbStop")->setEnabled(true);
+}
+
+void MainWindow::on_pbConnect_clicked()
+{
+    // TODO: Connect to existing backend (what is connected?)
+    findChild<QPushButton *>("pbStart")->setEnabled(false);
+    findChild<QPushButton *>("pbConnect")->setEnabled(false);
+    findChild<QPushButton *>("pbStop")->setEnabled(true);
+}
+
+void MainWindow::on_pbStop_clicked()
+{
+    stopBackend(backendPid);
+    findChild<QPushButton *>("pbStart")->setEnabled(true);
+    //findChild<QPushButton *>("pbConnect")->setEnabled(true);
+    findChild<QPushButton *>("pbStop")->setEnabled(false);
+}
diff --git a/groundStation/gui/MicroCART/mainwindow.h b/groundStation/gui/MicroCART/mainwindow.h
index a3948a917..08c56474f 100644
--- a/groundStation/gui/MicroCART/mainwindow.h
+++ b/groundStation/gui/MicroCART/mainwindow.h
@@ -15,8 +15,17 @@ public:
     explicit MainWindow(QWidget *parent = 0);
     ~MainWindow();
 
+private slots:
+    void on_pbStart_clicked();
+
+    void on_pbConnect_clicked();
+
+    void on_pbStop_clicked();
+
 private:
     Ui::MainWindow *ui;
+    pid_t backendPid;
+    int backendState;
 };
 
 #endif // MAINWINDOW_H
diff --git a/groundStation/gui/MicroCART/mainwindow.ui b/groundStation/gui/MicroCART/mainwindow.ui
index f3ddeb823..c613f8104 100644
--- a/groundStation/gui/MicroCART/mainwindow.ui
+++ b/groundStation/gui/MicroCART/mainwindow.ui
@@ -54,6 +54,9 @@
          </item>
          <item>
           <widget class="QPushButton" name="pbConnect">
+           <property name="enabled">
+            <bool>false</bool>
+           </property>
            <property name="text">
             <string>Connect</string>
            </property>
diff --git a/groundStation/gui/MicroCART/wrappers.c b/groundStation/gui/MicroCART/wrappers.c
new file mode 100644
index 000000000..58f718fc8
--- /dev/null
+++ b/groundStation/gui/MicroCART/wrappers.c
@@ -0,0 +1,26 @@
+#include "wrappers.h"
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <unistd.h>
+#include <signal.h>
+#include <stdlib.h>
+
+int stopBackend(int pid)
+{
+    kill(pid, SIGTERM);
+    int status;
+    wait(&status);
+    return status;
+}
+
+int startBackend()
+{
+    int pid = fork();
+    if (!pid) {
+        // TODO: Make dynamic
+        // TODO: Redirect stdout to pipe to GUI
+        execl("backEnd", "backEnd", NULL);
+        exit(0);
+    }
+    return pid;
+}
diff --git a/groundStation/gui/MicroCART/wrappers.h b/groundStation/gui/MicroCART/wrappers.h
new file mode 100644
index 000000000..0ac1d1151
--- /dev/null
+++ b/groundStation/gui/MicroCART/wrappers.h
@@ -0,0 +1,16 @@
+#ifndef WRAPPERS_H
+#define WRAPPERS_H
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
+int startBackend(void);
+int stopBackend(int pid);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif // WRAPPERS_H
-- 
GitLab