Skip to content

Commit

Permalink
fix(sensors): SGP40 swapped t/h compensation
Browse files Browse the repository at this point in the history
  • Loading branch information
SanaaHamel committed Sep 20, 2023
1 parent d384b6d commit 9b820ea
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions src/sensors/sgp40.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,17 @@ bool sgp40_self_test(i2c_inst_t& bus) {
return false;
}

constexpr uint16_t to_tick(double n, double min, double max) {
return uint16_t((clamp(n, min, max) - min) / (max - min) * UINT16_MAX);
}
static_assert(to_tick(50, 0, 100) == 0x8000 - 1, "humidity check"); // -1 b/c of truncation
static_assert(to_tick(25, -45, 130) == 0x6666, "temperature check");

bool sgp40_measure_issue(i2c_inst_t& bus, double temperature, double humidity) {
auto to_tick = [](double n, double min, double max) {
return byteswap(uint16_t((clamp(n, min, max) - min) / (max - min) * UINT16_MAX));
};

auto temperature_tick = to_tick(temperature, -45, 130);
auto humidity_tick = to_tick(humidity, 0, 100);
PackedTuple cmd{Cmd::SGP40_MEASURE, temperature_tick, crc8(temperature_tick, 0xFF), humidity_tick,
crc8(humidity_tick, 0xFF)};
uint16_t temperature_tick = byteswap(to_tick(temperature, -45, 130));
uint16_t humidity_tick = byteswap(to_tick(humidity, 0, 100));
PackedTuple cmd{Cmd::SGP40_MEASURE, humidity_tick, crc8(humidity_tick, 0xFF), temperature_tick,
crc8(temperature_tick, 0xFF)};
return i2c_write("SGP40", bus, SGP40_ADDRESS, cmd);
}

Expand Down

0 comments on commit 9b820ea

Please sign in to comment.