/* * Hot Beverage Companion – server module * * Copyright (C) 2017 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 #include #include #include #include #include #include #include #include #include #include #include #include #include "net.h" static struct netstate *init(const unsigned int port); static void draw_idle(void); static void draw_busy(const char * const data); static float digest_temp(const short int rawdata); int main(void) { struct netstate *state; char data[32] = {0}; int status; struct timespec wait = {0, 100 * 1000 * 1000}; state = init(2191); do { status = state->getlastdata(data, state); 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'); fail: net_close(&state); endwin(); return 0; } static struct netstate *init(const unsigned int port) { struct netstate *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) { 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(); } /* * 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; ret = (rawdata - 2730) * 0.1f; return ret; }