46 lines
1.2 KiB
C
46 lines
1.2 KiB
C
#include <sensors/mlx90614.h>
|
|
|
|
#include <zephyr/kernel.h>
|
|
#include <zephyr/drivers/i2c.h>
|
|
#include <zephyr/devicetree.h>
|
|
#include <zephyr/logging/log.h>
|
|
|
|
LOG_MODULE_REGISTER(mlx90614);
|
|
|
|
static const struct device *i2c_dev;
|
|
static const uint16_t i2c_addr = 0x5A;
|
|
static uint8_t read_data_buffer[3];
|
|
|
|
int init_mlx90614() {
|
|
i2c_dev = DEVICE_DT_GET(DT_NODELABEL(i2c1));
|
|
|
|
if (!i2c_dev) {
|
|
LOG_ERR("I2C device not found!");
|
|
return;
|
|
} else {
|
|
LOG_INF("I2C device initialized.");
|
|
}
|
|
}
|
|
|
|
void request_sensor_data_mlx90614(uint8_t target_addr) {
|
|
uint8_t err;
|
|
for (int i = 0; i < MLX90614_RETRY_COUNT; i++)
|
|
{
|
|
err = i2c_write_read(i2c_dev, 0x5A, &target_addr, sizeof(target_addr), &read_data_buffer, sizeof(read_data_buffer));
|
|
if (err == 0)
|
|
{
|
|
LOG_INF("MLX90614 sensor values successfully read after %d tries", i+1);
|
|
return;
|
|
}
|
|
else
|
|
{
|
|
k_sleep(K_MSEC(100));
|
|
}
|
|
}
|
|
LOG_ERR("MLX90614 reading sensor values failed %d times with error %d!", MLX90614_RETRY_COUNT, err);
|
|
}
|
|
|
|
float get_temp_from_raw_data_mlx90614() {
|
|
int16_t temperature = (read_data_buffer[1] << 8) | read_data_buffer[0]; // Temperaturwert aus den empfangenen Daten
|
|
return temperature * 0.02 - 273.15; // Umrechnung in Celsius
|
|
} |