27 lines
906 B
C
27 lines
906 B
C
// SPDX-License-Identifier: MIT
|
|
// SPDX-FileCopyrightText: 2026 A.M. Rowsell <amr@frzn.dev>
|
|
|
|
#include "aht.h"
|
|
|
|
#include "hardware/i2c.h"
|
|
#include "pico/stdlib.h" // IWYU pragma: keep
|
|
|
|
static const uint8_t AHT_MEASURE[3] = {0xAC, 0x33, 0x00};
|
|
|
|
void getAHTData(i2c_inst_t *i2c, double *t, double *h) {
|
|
uint8_t result[6];
|
|
uint32_t temperature;
|
|
uint32_t humidity;
|
|
|
|
i2c_write_timeout_us(i2c, AHT_ADDR, AHT_MEASURE, 3, false,
|
|
1200); // trigger measurement
|
|
// now we must delay ~100ms
|
|
sleep_ms(100);
|
|
i2c_read_timeout_us(i2c, AHT_ADDR, result, 6, false,
|
|
3000); // read 6 bytes back
|
|
humidity = (result[1] << 12) | (result[2] << 4) | (result[3] & 0xF0) >> 4;
|
|
temperature = (result[3] & 0x0F) << 16 | (result[4] << 8) | result[5];
|
|
*t = (((double)temperature / 1048576.0f) * 200.0f) - 50.0f;
|
|
*h = ((double)humidity / 1048576.0f) * 100.0f;
|
|
return;
|
|
}
|