summaryrefslogtreecommitdiffstats
path: root/src/server/util.c
diff options
context:
space:
mode:
authorGravatar Gediminas Jakutis <gediminas@varciai.lt> 2018-05-06 19:01:13 +0300
committerGravatar Gediminas Jakutis <gediminas@varciai.lt> 2018-05-06 19:01:13 +0300
commitdb88777a2f63d7dcd31e5e0442839213784eeb82 (patch)
tree6622d876ba737e3d73cd4d5fa81ba927d050d7f2 /src/server/util.c
parent3736b2e92b63d69f2278de2ad72901413a70012d (diff)
downloadcoffeetemp-db88777a2f63d7dcd31e5e0442839213784eeb82.tar.gz
coffeetemp-db88777a2f63d7dcd31e5e0442839213784eeb82.tar.bz2
coffeetemp-db88777a2f63d7dcd31e5e0442839213784eeb82.zip
server: major refactor with preparations for switchable UIs.
Diffstat (limited to 'src/server/util.c')
-rw-r--r--src/server/util.c59
1 files changed, 59 insertions, 0 deletions
diff --git a/src/server/util.c b/src/server/util.c
new file mode 100644
index 0000000..bb494c8
--- /dev/null
+++ b/src/server/util.c
@@ -0,0 +1,59 @@
+/*
+ * Hot Beverage Companion – desktop application utilities
+ *
+ * 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 <string.h>
+#include "util.h"
+#include "net.h"
+
+static float digest_temp(const short int rawtemp);
+static float digest_volt(const short int rawvolt);
+
+/*
+ * 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 rawtemp)
+{
+ return (rawtemp - 2730) * 0.1f;
+}
+
+/*
+ * raw data is in 1/1024 parts of the max A0 input voltage
+ * on the D1 mini board, which is 3.2V
+ */
+static float digest_volt(const short int rawvolt)
+{
+ return 3.2f / 1024.0f * rawvolt;
+}
+
+int refresh_data(int nd, struct tempmodule_state *state)
+{
+ int ret;
+ char buf[dgsize];
+
+ ret = net_getlastdata(nd, buf);
+ if (ret == A_OK) {
+ memcpy(&state->sequence, buf + 2, sizeof(state->size));
+ state->temperature = digest_temp(state->temperature_raw);
+ state->voltage = digest_volt(state->voltage_raw);
+ }
+
+ return ret;
+}