aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorlonkaars <loek@pipeframe.xyz>2022-10-30 17:22:54 +0100
committerlonkaars <loek@pipeframe.xyz>2022-10-30 17:22:54 +0100
commitd629902140406ac26fa65c1a7d559e6f1798206f (patch)
tree345bdcc0866312ec1af5480e25a205fff5bc5be2
parent64d742443bfacf5b1fad29f44642780b623f9a15 (diff)
add sensor range en/decoding functions
-rw-r--r--shared/util.c26
-rw-r--r--shared/util.h15
-rw-r--r--stm32f091/sensor.c15
-rw-r--r--todo.md6
4 files changed, 46 insertions, 16 deletions
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 690a999..1bee487 100644
--- a/shared/util.h
+++ b/shared/util.h
@@ -1,8 +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);
diff --git a/stm32f091/sensor.c b/stm32f091/sensor.c
index 1c94e2a..3695939 100644
--- a/stm32f091/sensor.c
+++ b/stm32f091/sensor.c
@@ -13,42 +13,33 @@
uint8_t ws_sensor_temperature() {
uint8_t buf[12];
- int16_t val;
- float temp_c;
buf[0] = SI7021_REG_TEMP;
HAL_I2C_Master_Transmit(&hi2c1, SI7021_ADDRESS, buf, 1, HAL_MAX_DELAY);
HAL_I2C_Master_Receive(&hi2c1, SI7021_ADDRESS, buf, 2, HAL_MAX_DELAY);
- val = ((int16_t)buf[0]<< 8 ) | (buf[1]);
- temp_c= ((175.72*val)/65536) - 46.85;
- return (uint8_t) temp_c; //TODO: convert with range -> util.h
+ return ws_sensor_tmp_to_8(((int16_t)buf[0]<< 8 ) | (buf[1]));
}
uint8_t ws_sensor_humidity() {
uint8_t buf[12];
- int16_t val;
buf[0] = SI7021_REG_HUM;
HAL_I2C_Master_Transmit(&hi2c1, SI7021_ADDRESS, buf, 1, HAL_MAX_DELAY);
HAL_I2C_Master_Receive(&hi2c1, SI7021_ADDRESS, buf, 2, HAL_MAX_DELAY);
- val = ((int16_t)buf[0]<< 8 ) | (buf[1]);
- float humidity = (( 125 * (float) val ) / 65536 ) - 6;
- return (uint8_t) humidity; //TODO: convert with range -> util.h
+ return ws_sensor_hum_to_8(((int16_t)buf[0]<< 8 ) | (buf[1]));
}
uint8_t ws_sensor_atmospheric_pressure() {
uint8_t buf[12];
uint8_t buffer[12];
- int16_t val;
buf[0]= 0xF4;
buf[1]= 0x07;
buffer[0] = 0xF7;
HAL_I2C_Master_Transmit(&hi2c1, BMP280_ADDRESS, buf , 2, HAL_MAX_DELAY);
HAL_I2C_Mem_Read(&hi2c1, BMP280_ADDRESS, 0xF7, 1, buffer, 2, 100 );
- val = (buffer[0] << 8 | buffer[1]);
- return (uint8_t) val; // TODO: convert with range
+ return ws_sensor_atm_to_8((uint16_t) buffer[0] << 8 | buffer[1]);
}
void ws_sensor_read() {
diff --git a/todo.md b/todo.md
index 5924b20..c19f3ae 100644
--- a/todo.md
+++ b/todo.md
@@ -16,9 +16,9 @@
## `// TODO:`'s
-- [ ] `sensor.c:24: return (uint8_t) temp_c; //TODO: convert with range -> util.h`
-- [ ] `sensor.c:36: return (uint8_t) humidity; //TODO: convert with range -> util.h`
-- [ ] `sensor.c:51: return (uint8_t) val; // TODO: convert with range`
+- [x] `sensor.c:24: return (uint8_t) temp_c; //TODO: convert with range -> util.h`
+- [x] `sensor.c:36: return (uint8_t) humidity; //TODO: convert with range -> util.h`
+- [x] `sensor.c:51: return (uint8_t) val; // TODO: convert with range`
- [x] `server.c:47:// TODO: next_few_bytes_are assumes that the complete search string is in the`
- [ ] `server.c:146: // TODO: buffer overrun protection`
- [x] `server.c:152:// TODO: refactor this`