From db88777a2f63d7dcd31e5e0442839213784eeb82 Mon Sep 17 00:00:00 2001 From: Gediminas Jakutis Date: Sun, 6 May 2018 19:01:13 +0300 Subject: server: major refactor with preparations for switchable UIs. --- src/server/main.c | 122 ++++++++++-------------------------------------------- 1 file changed, 23 insertions(+), 99 deletions(-) (limited to 'src/server/main.c') diff --git a/src/server/main.c b/src/server/main.c index 7237643..8406d11 100644 --- a/src/server/main.c +++ b/src/server/main.c @@ -27,126 +27,50 @@ #include #include #include -#include #include #include #include +#include "util.h" #include "net.h" +#include "ui.h" -int init(const unsigned short int port); -static void draw_idle(void); -static void draw_busy(const char * const data); -static float digest_temp(const short int rawdata); +static int init(int argc, char ***argv, const unsigned short int port, enum uitype ui); -int main(void) +int main(int argc, char **argv) { - static const struct timespec wait = {0, 200 * 1000 * 1000}; /* 200 ms */ - char data[dgsize] = {0}; - int status; + int ret; int nd; - nd = init(2191); + nd = init(argc, &argv, 2191, UI_DEFAULT); - if (nd == NET_ERROR) { + if (nd == ERROR) { + ret = nd; goto fail; } - do { - status = net_getlastdata(nd, data); - switch (status) { - case NET_OK: - /*fall-through */ - case NET_NONEWDATA: - draw_busy(data); - break; - case NET_ERROR: - goto fail; - default: - draw_idle(); - ; /* noop */ - } - nanosleep(&wait, NULL); - } while (getch() != 'q'); - + ret = ui_startloop(); fail: net_close(nd); - endwin(); - - return 0; -} - -int init(const unsigned short int port) -{ - int ret; - ret = net_init(port); - - initscr(); - nodelay(stdscr, 1); - noecho(); - return ret; } -static void draw_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 draw_busy(const char * const data) +static int init(int argc, char ***argv, const unsigned short int port, enum uitype ui) { - unsigned int sequence; - short int thermistor_data; - short int voltage; - float temp; - - memcpy(&sequence, data + 2, sizeof(sequence)); - memcpy(&thermistor_data, data + 6, sizeof(thermistor_data)); - temp = digest_temp(thermistor_data); - memcpy(&thermistor_data, data + 8, sizeof(thermistor_data)); - memcpy(&voltage, data + 10, sizeof(voltage)); - - move(0, 0); - clrtoeol(); - printw("sequence: %u", sequence); - move(1, 0); - clrtoeol(); - mvprintw(1, 0, "thermistor temp: %.1fC", temp); - move(2, 0); - clrtoeol(); - mvprintw(2, 0, "thermistor resistance: %hd", thermistor_data); - move(3, 0); - clrtoeol(); - mvprintw(3, 0, "tap voltage: %hd", voltage); - move(4, 0); - clrtoeol(); - mvprintw(4, 0, "press 'q' to exit"); - refresh(); -} + int ret = A_OK; + int nd; -/* - * raw data is in 0.1°K per 1. Subtract 2730 to get Celsius. - * Multiply by 0.1f (divide by 10) to get the correct scale. - */ -static float digest_temp(const short int rawdata) -{ - float ret; + nd = net_init(port); + if (nd == ERROR) { + ret = nd; + goto fail; + } - ret = (rawdata - 2730) * 0.1f; + ret = ui_init(argc, argv, ui, nd, DEFAULT_PERIOD); + if (ret == ERROR) { + net_close(nd); + goto fail; + } +fail: return ret; } -- cgit v1.2.3