summaryrefslogtreecommitdiffstats
path: root/src/server/curses.c
blob: cdd2aea2b58314c7f6e303b37ce16fdd496f1d29 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
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();
}