summaryrefslogtreecommitdiffstats
path: root/src/server/curses.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/server/curses.c')
-rw-r--r--src/server/curses.c114
1 files changed, 114 insertions, 0 deletions
diff --git a/src/server/curses.c b/src/server/curses.c
new file mode 100644
index 0000000..cdd2aea
--- /dev/null
+++ b/src/server/curses.c
@@ -0,0 +1,114 @@
+/*
+ * Hot Beverage Companion – desktop application curses ui
+ *
+ * Copyright (C) 2018 Gediminas Jakutis
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; version 2.1
+ * of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
+ */
+
+#include <ncurses.h>
+#include "util.h"
+#include "curses.h"
+
+struct settings {
+ int nd;
+ struct timespec period;
+};
+
+struct settings settings;
+
+static void curses_idle(void);
+static void curses_update(const struct tempmodule_state * const state);
+
+int curses_init(int nd, struct timespec period)
+{
+ settings.nd = nd;
+ settings.period = period;
+
+ initscr();
+ nodelay(stdscr, 1);
+ noecho();
+
+ return A_OK;
+}
+
+int curses_loop(void)
+{
+ static int statechange = 1;
+ static struct tempmodule_state state;
+ int status;
+
+ do {
+ if (getch() == 'q') {
+ status = A_OK;
+ break;
+ }
+
+ status = refresh_data(settings.nd, &state);
+ if (status == A_OK) {
+ curses_update(&state);
+ statechange = 1;
+ } else if (status == DEAD && statechange) {
+ curses_idle();
+ statechange = 0;
+ }
+ nanosleep(&settings.period, NULL);
+ } while (status != ERROR);
+
+ endwin();
+
+ return status;
+}
+
+static void curses_idle(void)
+{
+ move(0, 0);
+ clrtoeol();
+ printw("sequence: [waiting for ESP8266...]");
+ move(1, 0);
+ clrtoeol();
+ mvprintw(1, 0, "thermistor temp: [waiting for ESP8266...]");
+ move(2, 0);
+ clrtoeol();
+ mvprintw(2, 0, "thermistor resistance: [waiting for ESP8266...]");
+ move(3, 0);
+ clrtoeol();
+ mvprintw(3, 0, "tap voltage: [waiting for ESP8266...]");
+ move(4, 0);
+ clrtoeol();
+ mvprintw(4, 0, "press 'q' to exit");
+ refresh();
+}
+
+static void curses_update(const struct tempmodule_state * const state)
+{
+
+ move(0, 0);
+ clrtoeol();
+ printw("sequence: %u", state->sequence);
+ move(1, 0);
+ clrtoeol();
+ mvprintw(1, 0, "thermistor temp: %.1fC", state->temperature);
+ move(2, 0);
+ clrtoeol();
+ mvprintw(2, 0, "thermistor resistance: %hd Ohm", state->resistance);
+ move(3, 0);
+ clrtoeol();
+ mvprintw(3, 0, "tap voltage: %.3fV (%hd)", state->voltage, state->voltage_raw);
+ move(4, 0);
+ clrtoeol();
+ mvprintw(4, 0, "press 'q' to exit");
+ refresh();
+}