aboutsummaryrefslogtreecommitdiff
path: root/shared
diff options
context:
space:
mode:
Diffstat (limited to 'shared')
-rw-r--r--shared/protocol.h2
-rw-r--r--shared/util.c26
-rw-r--r--shared/util.h17
3 files changed, 44 insertions, 1 deletions
diff --git a/shared/protocol.h b/shared/protocol.h
index 96c039a..e5ddf05 100644
--- a/shared/protocol.h
+++ b/shared/protocol.h
@@ -148,4 +148,6 @@ static ws_protocol_res_handler_t* g_ws_protocol_res_handlers[WS_PROTOCOL_CMD_AMO
[WS_PROTOCOL_CMD_LAST_RECORDS] = &ws_protocol_res_last_records,
};
+/** @brief return length of custom protocol header */
unsigned short ws_protocol_get_header_size(ws_s_protocol_res* response);
+
diff --git a/shared/util.c b/shared/util.c
index ea972b0..648631a 100644
--- a/shared/util.c
+++ b/shared/util.c
@@ -4,4 +4,28 @@ unsigned int ws_log16(unsigned int x) {
unsigned int l = 0;
while (x >>= 4) ++l; // bitshift right by 4 until x == 0
return l;
-} \ No newline at end of file
+}
+
+uint8_t ws_sensor_tmp_to_8(uint16_t temperature) {
+ return temperature / 256;
+}
+
+uint8_t ws_sensor_hum_to_8(uint16_t humidity) {
+ return humidity / 256;
+}
+
+uint8_t ws_sensor_atm_to_8(uint16_t atmospheric_pressure) {
+ return atmospheric_pressure / 256;
+}
+
+float ws_sensor_tmp_to_f(uint8_t temperature) {
+ return ((175.72 * temperature) / 256.0) - 46.85;
+}
+
+float ws_sensor_hum_to_f(uint8_t humidity) {
+ return ((125.0 * humidity) / 256.0) - 6.0;
+}
+
+float ws_sensor_atm_to_f(uint8_t atmospheric_pressure) {
+ return ((20.0 * atmospheric_pressure) / 256.0) + 990.0; // datasheet no?
+}
diff --git a/shared/util.h b/shared/util.h
index 94a3dfe..1bee487 100644
--- a/shared/util.h
+++ b/shared/util.h
@@ -1,6 +1,23 @@
#pragma once
+#include <stdint.h>
+
#define WS_MIN(a, b) (((a) < (b)) ? (a) : (b))
#define WS_MAX(a, b) (((a) > (b)) ? (a) : (b))
+/** @brief take the log base 16 of `x` */
unsigned int ws_log16(unsigned int x);
+
+/** @brief convert 16-bit temperature value to uint8_t */
+uint8_t ws_sensor_tmp_to_8(uint16_t temperature);
+/** @brief convert 16-bit humidity value to uint8_t */
+uint8_t ws_sensor_hum_to_8(uint16_t humidity);
+/** @brief convert 16-bit atmospheric pressure value to uint8_t */
+uint8_t ws_sensor_atm_to_8(uint16_t atmospheric_pressure);
+
+/** @brief convert 8-bit temperature value back to float */
+float ws_sensor_tmp_to_f(uint8_t temperature);
+/** @brief convert 8-bit humidity value back to float */
+float ws_sensor_hum_to_f(uint8_t humidity);
+/** @brief convert 8-bit atmospheric pressure value back to float */
+float ws_sensor_atm_to_f(uint8_t atmospheric_pressure);