diff --git a/applications/plugins/unitemp/README.md b/applications/plugins/unitemp/README.md index 668aae40a..436f3600e 100644 --- a/applications/plugins/unitemp/README.md +++ b/applications/plugins/unitemp/README.md @@ -4,14 +4,21 @@ [![GitHub](https://img.shields.io/github/license/quen0n/unitemp-flipperzero)](https://github.com/quen0n/unitemp-flipperzero/blob/dev/LICENSE.md) [![Build dev](https://github.com/quen0n/unitemp-flipperzero/actions/workflows/build_dev.yml/badge.svg?branch=dev)](https://github.com/quen0n/unitemp-flipperzero/actions/workflows/build_dev.yml) [Flipper Zero](https://flipperzero.one/) application for reading temperature, humidity and pressure sensors like a DHT11/22, DS18B20, BMP280, HTU21 and more. -## List of supported sensors (supplemented) -![image](https://user-images.githubusercontent.com/10090793/211131502-c1560eb5-f59c-4cfa-86f7-27f037490a35.png) +## List of supported sensors +![image](https://user-images.githubusercontent.com/10090793/215605424-54b1c08c-e41b-4fb4-b966-dd959507200b.png) + ## Installation 1) Download [latest version](https://cloud.quenon.ru/index.php/s/h98rT9UnaOL4wxR/download?path=%2F&files=unitemp-latest.fap) 2) Copy `unitemp-latest.fap` to `SD card/apps/GPIO` with qFlipper or mobile application 3) Open application on your Flipper: `Applications->GPIO->Temp sensors reader` -Note: If you get the message "API version mismatch" after updating the firmware, download and install Unitemp again +Note: If you get the message "API version mismatch" after updating the firmware, download and install Unitemp again +## Need help? Discussions? +Join the discussion, ask a question or just send a photo of the flipper with sensors to [Discord](https://discord.com/channels/740930220399525928/1056727938747351060) +## Gratitudes +Thanks to [@Svaarich](https://github.com/Svaarich) for the UI design and to the Unleashed firmware community for sensors testing and feedbacks. + ## Some community photos ![image](https://user-images.githubusercontent.com/10090793/210120132-7ddbc937-0a6b-4472-bd1c-7fbc3ecdf2ad.png) ![image](https://user-images.githubusercontent.com/10090793/210120135-12fc5810-77ff-49db-b799-e9479e1f57a7.png) ![image](https://user-images.githubusercontent.com/10090793/210120143-a2bae3ce-4190-421f-8c4f-c7c744903bd6.png) +![image](https://user-images.githubusercontent.com/10090793/215224085-8099408e-b3de-4a0c-854e-fe4e4faa8ea3.png) \ No newline at end of file diff --git a/applications/plugins/unitemp/Sensors.c b/applications/plugins/unitemp/Sensors.c index 2b1eaf6bf..d9304ab32 100644 --- a/applications/plugins/unitemp/Sensors.c +++ b/applications/plugins/unitemp/Sensors.c @@ -75,25 +75,10 @@ const Interface SPI = { //Перечень интерфейсов подключения //static const Interface* interfaces[] = {&SINGLE_WIRE, &I2C, &ONE_WIRE, &SPI}; //Перечень датчиков -static const SensorType* sensorTypes[] = { - &DHT11, - &DHT12_SW, - &DHT20, - &DHT21, - &DHT22, - &Dallas, - &AM2320_SW, - &AM2320_I2C, - &HTU21x, - &AHT10, - &SHT30, - &GXHT30, - &LM75, - &HDC1080, - &BMP180, - &BMP280, - &BME280, - &MAX31855}; +static const SensorType* sensorTypes[] = {&DHT11, &DHT12_SW, &DHT20, &DHT21, &DHT22, + &Dallas, &AM2320_SW, &AM2320_I2C, &HTU21x, &AHT10, + &SHT30, &GXHT30, &LM75, &HDC1080, &BMP180, + &BMP280, &BME280, &BME680, &MAX31855, &MAX6675}; const SensorType* unitemp_sensors_getTypeFromInt(uint8_t index) { if(index > SENSOR_TYPES_COUNT) return NULL; diff --git a/applications/plugins/unitemp/Sensors.h b/applications/plugins/unitemp/Sensors.h index 3e4152e33..d2b7c07af 100644 --- a/applications/plugins/unitemp/Sensors.h +++ b/applications/plugins/unitemp/Sensors.h @@ -318,8 +318,9 @@ const GPIO* //DS18x2x #include "./interfaces/OneWireSensor.h" #include "./sensors/LM75.h" -//BMP280, BME280 +//BMP280, BME280, BME680 #include "./sensors/BMx280.h" +#include "./sensors/BME680.h" #include "./sensors/AM2320.h" #include "./sensors/DHT20.h" #include "./sensors/SHT30.h" @@ -327,4 +328,5 @@ const GPIO* #include "./sensors/HTU21x.h" #include "./sensors/HDC1080.h" #include "./sensors/MAX31855.h" +#include "./sensors/MAX6675.h" #endif diff --git a/applications/plugins/unitemp/sensors/BME680.c b/applications/plugins/unitemp/sensors/BME680.c new file mode 100644 index 000000000..397e702cb --- /dev/null +++ b/applications/plugins/unitemp/sensors/BME680.c @@ -0,0 +1,431 @@ +/* + Unitemp - Universal temperature reader + Copyright (C) 2022-2023 Victor Nikitchuk (https://github.com/quen0n) + Contributed by g0gg0 (https://github.com/g3gg0) + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ +#include "BME680.h" + +const SensorType BME680 = { + .typename = "BME680", + .interface = &I2C, + .datatype = UT_TEMPERATURE | UT_HUMIDITY | UT_PRESSURE, + .pollingInterval = 500, + .allocator = unitemp_BME680_alloc, + .mem_releaser = unitemp_BME680_free, + .initializer = unitemp_BME680_init, + .deinitializer = unitemp_BME680_deinit, + .updater = unitemp_BME680_update}; + +//Интервал обновления калибровочных значений +#define BOSCH_CAL_UPDATE_INTERVAL 60000 + +#define BME680_ID 0x61 + +#define BME680_I2C_ADDR_MIN (0x76 << 1) +#define BME680_I2C_ADDR_MAX (0x77 << 1) + +#define BME680_REG_STATUS 0x1D +#define BME680_REG_CTRL_MEAS 0x74 +#define BME680_REG_CONFIG 0x75 +#define BME680_REG_CTRL_HUM 0x72 +//Преддескретизация температуры +#define BME680_TEMP_OVERSAMPLING_SKIP 0b00000000 +#define BME680_TEMP_OVERSAMPLING_1 0b00100000 +#define BME680_TEMP_OVERSAMPLING_2 0b01000000 +#define BME680_TEMP_OVERSAMPLING_4 0b01100000 +#define BME680_TEMP_OVERSAMPLING_8 0b10000000 +#define BME680_TEMP_OVERSAMPLING_16 0b10100000 +//Преддескретизация давления +#define BME680_PRESS_OVERSAMPLING_SKIP 0b00000000 +#define BME680_PRESS_OVERSAMPLING_1 0b00000100 +#define BME680_PRESS_OVERSAMPLING_2 0b00001000 +#define BME680_PRESS_OVERSAMPLING_4 0b00001100 +#define BME680_PRESS_OVERSAMPLING_8 0b00010000 +#define BME680_PRESS_OVERSAMPLING_16 0b00010100 +//Преддескретизация влажности +#define BME680_HUM_OVERSAMPLING_SKIP 0b00000000 +#define BME680_HUM_OVERSAMPLING_1 0b00000001 +#define BME680_HUM_OVERSAMPLING_2 0b00000010 +#define BME680_HUM_OVERSAMPLING_4 0b00000011 +#define BME680_HUM_OVERSAMPLING_8 0b00000100 +#define BME680_HUM_OVERSAMPLING_16 0b00000101 +//Режимы работы датчика +#define BME680_MODE_SLEEP 0b00000000 //Наелся и спит +#define BME680_MODE_FORCED 0b00000001 //Обновляет значения 1 раз, после чего уходит в сон +//Коэффициент фильтрации значений +#define BME680_FILTER_COEFF_1 0b00000000 +#define BME680_FILTER_COEFF_2 0b00000100 +#define BME680_FILTER_COEFF_4 0b00001000 +#define BME680_FILTER_COEFF_8 0b00001100 +#define BME680_FILTER_COEFF_16 0b00010000 +//Разрешить работу по SPI +#define BME680_SPI_3W_ENABLE 0b00000001 +#define BME680_SPI_3W_DISABLE 0b00000000 + +/* https://github.com/boschsensortec/BME680_driver/blob/master/bme680.c or + https://github.com/boschsensortec/BME68x-Sensor-API */ +static float BME680_compensate_temperature(I2CSensor* i2c_sensor, int32_t temp_adc) { + BME680_instance* bme680_instance = (BME680_instance*)i2c_sensor->sensorInstance; + float var1 = 0; + float var2 = 0; + float calc_temp = 0; + + /* calculate var1 data */ + var1 = + ((((float)temp_adc / 16384.0f) - ((float)bme680_instance->temp_cal.dig_T1 / 1024.0f)) * + ((float)bme680_instance->temp_cal.dig_T2)); + + /* calculate var2 data */ + var2 = + (((((float)temp_adc / 131072.0f) - ((float)bme680_instance->temp_cal.dig_T1 / 8192.0f)) * + (((float)temp_adc / 131072.0f) - ((float)bme680_instance->temp_cal.dig_T1 / 8192.0f))) * + ((float)bme680_instance->temp_cal.dig_T3 * 16.0f)); + + /* t_fine value*/ + bme680_instance->t_fine = (var1 + var2); + + /* compensated temperature data*/ + calc_temp = ((bme680_instance->t_fine) / 5120.0f); + + return calc_temp; +} + +static float BME680_compensate_pressure(I2CSensor* i2c_sensor, int32_t pres_adc) { + BME680_instance* bme680_instance = (BME680_instance*)i2c_sensor->sensorInstance; + + float var1; + float var2; + float var3; + float calc_pres; + + var1 = (((float)bme680_instance->t_fine / 2.0f) - 64000.0f); + var2 = var1 * var1 * (((float)bme680_instance->press_cal.dig_P6) / (131072.0f)); + var2 = var2 + (var1 * ((float)bme680_instance->press_cal.dig_P5) * 2.0f); + var2 = (var2 / 4.0f) + (((float)bme680_instance->press_cal.dig_P4) * 65536.0f); + var1 = + (((((float)bme680_instance->press_cal.dig_P3 * var1 * var1) / 16384.0f) + + ((float)bme680_instance->press_cal.dig_P2 * var1)) / + 524288.0f); + var1 = ((1.0f + (var1 / 32768.0f)) * ((float)bme680_instance->press_cal.dig_P1)); + calc_pres = (1048576.0f - ((float)pres_adc)); + + /* Avoid exception caused by division by zero */ + if((int)var1 != 0) { + calc_pres = (((calc_pres - (var2 / 4096.0f)) * 6250.0f) / var1); + var1 = + (((float)bme680_instance->press_cal.dig_P9) * calc_pres * calc_pres) / 2147483648.0f; + var2 = calc_pres * (((float)bme680_instance->press_cal.dig_P8) / 32768.0f); + var3 = + ((calc_pres / 256.0f) * (calc_pres / 256.0f) * (calc_pres / 256.0f) * + (bme680_instance->press_cal.dig_P10 / 131072.0f)); + calc_pres = + (calc_pres + + (var1 + var2 + var3 + ((float)bme680_instance->press_cal.dig_P7 * 128.0f)) / 16.0f); + } else { + calc_pres = 0; + } + + return calc_pres; +} + +static float BME680_compensate_humidity(I2CSensor* i2c_sensor, int32_t hum_adc) { + BME680_instance* bme680_instance = (BME680_instance*)i2c_sensor->sensorInstance; + float calc_hum; + float var1; + float var2; + float var3; + float var4; + float temp_comp; + + /* compensated temperature data*/ + temp_comp = ((bme680_instance->t_fine) / 5120.0f); + var1 = + (float)((float)hum_adc) - (((float)bme680_instance->hum_cal.dig_H1 * 16.0f) + + (((float)bme680_instance->hum_cal.dig_H3 / 2.0f) * temp_comp)); + var2 = var1 * + ((float)(((float)bme680_instance->hum_cal.dig_H2 / 262144.0f) * + (1.0f + (((float)bme680_instance->hum_cal.dig_H4 / 16384.0f) * temp_comp) + + (((float)bme680_instance->hum_cal.dig_H5 / 1048576.0f) * temp_comp * temp_comp)))); + var3 = (float)bme680_instance->hum_cal.dig_H6 / 16384.0f; + var4 = (float)bme680_instance->hum_cal.dig_H7 / 2097152.0f; + calc_hum = var2 + ((var3 + (var4 * temp_comp)) * var2 * var2); + if(calc_hum > 100.0f) { + calc_hum = 100.0f; + } else if(calc_hum < 0.0f) { + calc_hum = 0.0f; + } + + return calc_hum; +} + +/* https://github.com/boschsensortec/BME680_driver/blob/master/bme680_defs.h */ +#define BME680_COEFF_SIZE UINT8_C(41) +#define BME680_COEFF_ADDR1_LEN UINT8_C(25) +#define BME680_COEFF_ADDR2_LEN UINT8_C(16) +#define BME680_COEFF_ADDR1 UINT8_C(0x89) +#define BME680_COEFF_ADDR2 UINT8_C(0xe1) +#define BME680_CONCAT_BYTES(msb, lsb) (((uint16_t)msb << 8) | (uint16_t)lsb) +#define BME680_T2_LSB_REG (1) +#define BME680_T2_MSB_REG (2) +#define BME680_T3_REG (3) +#define BME680_P1_LSB_REG (5) +#define BME680_P1_MSB_REG (6) +#define BME680_P2_LSB_REG (7) +#define BME680_P2_MSB_REG (8) +#define BME680_P3_REG (9) +#define BME680_P4_LSB_REG (11) +#define BME680_P4_MSB_REG (12) +#define BME680_P5_LSB_REG (13) +#define BME680_P5_MSB_REG (14) +#define BME680_P7_REG (15) +#define BME680_P6_REG (16) +#define BME680_P8_LSB_REG (19) +#define BME680_P8_MSB_REG (20) +#define BME680_P9_LSB_REG (21) +#define BME680_P9_MSB_REG (22) +#define BME680_P10_REG (23) +#define BME680_H2_MSB_REG (25) +#define BME680_H2_LSB_REG (26) +#define BME680_H1_LSB_REG (26) +#define BME680_H1_MSB_REG (27) +#define BME680_H3_REG (28) +#define BME680_H4_REG (29) +#define BME680_H5_REG (30) +#define BME680_H6_REG (31) +#define BME680_H7_REG (32) +#define BME680_T1_LSB_REG (33) +#define BME680_T1_MSB_REG (34) +#define BME680_GH2_LSB_REG (35) +#define BME680_GH2_MSB_REG (36) +#define BME680_GH1_REG (37) +#define BME680_GH3_REG (38) +#define BME680_HUM_REG_SHIFT_VAL UINT8_C(4) +#define BME680_BIT_H1_DATA_MSK UINT8_C(0x0F) + +static bool BME680_readCalValues(I2CSensor* i2c_sensor) { + BME680_instance* bme680_instance = (BME680_instance*)i2c_sensor->sensorInstance; + uint8_t coeff_array[BME680_COEFF_SIZE] = {0}; + + if(!unitemp_i2c_readRegArray( + i2c_sensor, BME680_COEFF_ADDR1, BME680_COEFF_ADDR1_LEN, &coeff_array[0])) + return false; + if(!unitemp_i2c_readRegArray( + i2c_sensor, + BME680_COEFF_ADDR2, + BME680_COEFF_ADDR2_LEN, + &coeff_array[BME680_COEFF_ADDR1_LEN])) + return false; + + /* Temperature related coefficients */ + bme680_instance->temp_cal.dig_T1 = (uint16_t)(BME680_CONCAT_BYTES( + coeff_array[BME680_T1_MSB_REG], coeff_array[BME680_T1_LSB_REG])); + bme680_instance->temp_cal.dig_T2 = (int16_t)(BME680_CONCAT_BYTES( + coeff_array[BME680_T2_MSB_REG], coeff_array[BME680_T2_LSB_REG])); + bme680_instance->temp_cal.dig_T3 = (int8_t)(coeff_array[BME680_T3_REG]); + + /* Pressure related coefficients */ + bme680_instance->press_cal.dig_P1 = (uint16_t)(BME680_CONCAT_BYTES( + coeff_array[BME680_P1_MSB_REG], coeff_array[BME680_P1_LSB_REG])); + bme680_instance->press_cal.dig_P2 = (int16_t)(BME680_CONCAT_BYTES( + coeff_array[BME680_P2_MSB_REG], coeff_array[BME680_P2_LSB_REG])); + bme680_instance->press_cal.dig_P3 = (int8_t)coeff_array[BME680_P3_REG]; + bme680_instance->press_cal.dig_P4 = (int16_t)(BME680_CONCAT_BYTES( + coeff_array[BME680_P4_MSB_REG], coeff_array[BME680_P4_LSB_REG])); + bme680_instance->press_cal.dig_P5 = (int16_t)(BME680_CONCAT_BYTES( + coeff_array[BME680_P5_MSB_REG], coeff_array[BME680_P5_LSB_REG])); + bme680_instance->press_cal.dig_P6 = (int8_t)(coeff_array[BME680_P6_REG]); + bme680_instance->press_cal.dig_P7 = (int8_t)(coeff_array[BME680_P7_REG]); + bme680_instance->press_cal.dig_P8 = (int16_t)(BME680_CONCAT_BYTES( + coeff_array[BME680_P8_MSB_REG], coeff_array[BME680_P8_LSB_REG])); + bme680_instance->press_cal.dig_P9 = (int16_t)(BME680_CONCAT_BYTES( + coeff_array[BME680_P9_MSB_REG], coeff_array[BME680_P9_LSB_REG])); + bme680_instance->press_cal.dig_P10 = (uint8_t)(coeff_array[BME680_P10_REG]); + + /* Humidity related coefficients */ + bme680_instance->hum_cal.dig_H1 = + (uint16_t)(((uint16_t)coeff_array[BME680_H1_MSB_REG] << BME680_HUM_REG_SHIFT_VAL) | (coeff_array[BME680_H1_LSB_REG] & BME680_BIT_H1_DATA_MSK)); + bme680_instance->hum_cal.dig_H2 = + (uint16_t)(((uint16_t)coeff_array[BME680_H2_MSB_REG] << BME680_HUM_REG_SHIFT_VAL) | ((coeff_array[BME680_H2_LSB_REG]) >> BME680_HUM_REG_SHIFT_VAL)); + bme680_instance->hum_cal.dig_H3 = (int8_t)coeff_array[BME680_H3_REG]; + bme680_instance->hum_cal.dig_H4 = (int8_t)coeff_array[BME680_H4_REG]; + bme680_instance->hum_cal.dig_H5 = (int8_t)coeff_array[BME680_H5_REG]; + bme680_instance->hum_cal.dig_H6 = (uint8_t)coeff_array[BME680_H6_REG]; + bme680_instance->hum_cal.dig_H7 = (int8_t)coeff_array[BME680_H7_REG]; + + /* Gas heater related coefficients */ + bme680_instance->gas_cal.dig_GH1 = (int8_t)coeff_array[BME680_GH1_REG]; + bme680_instance->gas_cal.dig_GH2 = (int16_t)(BME680_CONCAT_BYTES( + coeff_array[BME680_GH2_MSB_REG], coeff_array[BME680_GH2_LSB_REG])); + bme680_instance->gas_cal.dig_GH3 = (int8_t)coeff_array[BME680_GH3_REG]; + +#ifdef UNITEMP_DEBUG + FURI_LOG_D( + APP_NAME, + "Sensor BME680 T1-T3: %d, %d, %d", + bme680_instance->temp_cal.dig_T1, + bme680_instance->temp_cal.dig_T2, + bme680_instance->temp_cal.dig_T3); + + FURI_LOG_D( + APP_NAME, + "Sensor BME680: P1-P10: %d, %d, %d, %d, %d, %d, %d, %d, %d, %d", + bme680_instance->press_cal.dig_P1, + bme680_instance->press_cal.dig_P2, + bme680_instance->press_cal.dig_P3, + bme680_instance->press_cal.dig_P4, + bme680_instance->press_cal.dig_P5, + bme680_instance->press_cal.dig_P6, + bme680_instance->press_cal.dig_P7, + bme680_instance->press_cal.dig_P8, + bme680_instance->press_cal.dig_P9, + bme680_instance->press_cal.dig_P10); + + FURI_LOG_D( + APP_NAME, + "Sensor BME680: H1-H7: %d, %d, %d, %d, %d, %d, %d", + bme680_instance->hum_cal.dig_H1, + bme680_instance->hum_cal.dig_H2, + bme680_instance->hum_cal.dig_H3, + bme680_instance->hum_cal.dig_H4, + bme680_instance->hum_cal.dig_H5, + bme680_instance->hum_cal.dig_H6, + bme680_instance->hum_cal.dig_H7); + + FURI_LOG_D( + APP_NAME, + "Sensor BME680 GH1-GH3: %d, %d, %d", + bme680_instance->gas_cal.dig_GH1, + bme680_instance->gas_cal.dig_GH2, + bme680_instance->gas_cal.dig_GH3); + +#endif + + bme680_instance->last_cal_update_time = furi_get_tick(); + return true; +} +static bool BME680_isMeasuring(Sensor* sensor) { + I2CSensor* i2c_sensor = (I2CSensor*)sensor->instance; + return (bool)(unitemp_i2c_readReg(i2c_sensor, BME680_REG_STATUS) & 0x20); +} + +bool unitemp_BME680_alloc(Sensor* sensor, char* args) { + UNUSED(args); + I2CSensor* i2c_sensor = (I2CSensor*)sensor->instance; + BME680_instance* bme680_instance = malloc(sizeof(BME680_instance)); + if(bme680_instance == NULL) { + FURI_LOG_E(APP_NAME, "Failed to allocation sensor %s instance", sensor->name); + return false; + } + + if(sensor->type == &BME680) bme680_instance->chip_id = BME680_ID; + + i2c_sensor->sensorInstance = bme680_instance; + + i2c_sensor->minI2CAdr = BME680_I2C_ADDR_MIN; + i2c_sensor->maxI2CAdr = BME680_I2C_ADDR_MAX; + return true; +} + +bool unitemp_BME680_init(Sensor* sensor) { + I2CSensor* i2c_sensor = (I2CSensor*)sensor->instance; + //Перезагрузка + unitemp_i2c_writeReg(i2c_sensor, 0xE0, 0xB6); + //Чтение ID датчика + uint8_t id = unitemp_i2c_readReg(i2c_sensor, 0xD0); + if(id != BME680_ID) { + FURI_LOG_E( + APP_NAME, + "Sensor %s returned wrong ID 0x%02X, expected 0x%02X", + sensor->name, + id, + BME680_ID); + return false; + } + + unitemp_i2c_writeReg( + i2c_sensor, + BME680_REG_CTRL_HUM, + (unitemp_i2c_readReg(i2c_sensor, BME680_REG_CTRL_HUM) & ~7) | BME680_HUM_OVERSAMPLING_1); + unitemp_i2c_writeReg( + i2c_sensor, + BME680_REG_CTRL_MEAS, + BME680_TEMP_OVERSAMPLING_2 | BME680_PRESS_OVERSAMPLING_4 | BME680_MODE_FORCED); + //Настройка периода опроса и фильтрации значений + unitemp_i2c_writeReg( + i2c_sensor, BME680_REG_CONFIG, BME680_FILTER_COEFF_16 | BME680_SPI_3W_DISABLE); + //Чтение калибровочных значений + if(!BME680_readCalValues(i2c_sensor)) { + FURI_LOG_E(APP_NAME, "Failed to read calibration values sensor %s", sensor->name); + return false; + } + return true; +} + +bool unitemp_BME680_deinit(Sensor* sensor) { + I2CSensor* i2c_sensor = (I2CSensor*)sensor->instance; + //Перевод в сон + unitemp_i2c_writeReg(i2c_sensor, BME680_REG_CTRL_MEAS, BME680_MODE_SLEEP); + return true; +} + +UnitempStatus unitemp_BME680_update(Sensor* sensor) { + I2CSensor* i2c_sensor = (I2CSensor*)sensor->instance; + BME680_instance* instance = i2c_sensor->sensorInstance; + + uint32_t t = furi_get_tick(); + + uint8_t buff[3]; + //Проверка инициализированности датчика + unitemp_i2c_readRegArray(i2c_sensor, 0xF4, 2, buff); + if(buff[0] == 0) { + FURI_LOG_W(APP_NAME, "Sensor %s is not initialized!", sensor->name); + return UT_SENSORSTATUS_ERROR; + } + + unitemp_i2c_writeReg( + i2c_sensor, + BME680_REG_CTRL_MEAS, + unitemp_i2c_readReg(i2c_sensor, BME680_REG_CTRL_MEAS) | 1); + + while(BME680_isMeasuring(sensor)) { + if(furi_get_tick() - t > 100) { + return UT_SENSORSTATUS_TIMEOUT; + } + } + + if(furi_get_tick() - instance->last_cal_update_time > BOSCH_CAL_UPDATE_INTERVAL) { + BME680_readCalValues(i2c_sensor); + } + + if(!unitemp_i2c_readRegArray(i2c_sensor, 0x1F, 3, buff)) return UT_SENSORSTATUS_TIMEOUT; + int32_t adc_P = ((int32_t)buff[0] << 12) | ((int32_t)buff[1] << 4) | ((int32_t)buff[2] >> 4); + if(!unitemp_i2c_readRegArray(i2c_sensor, 0x22, 3, buff)) return UT_SENSORSTATUS_TIMEOUT; + int32_t adc_T = ((int32_t)buff[0] << 12) | ((int32_t)buff[1] << 4) | ((int32_t)buff[2] >> 4); + if(!unitemp_i2c_readRegArray(i2c_sensor, 0x25, 2, buff)) return UT_SENSORSTATUS_TIMEOUT; + int32_t adc_H = ((uint16_t)buff[0] << 8) | buff[1]; + + sensor->temp = BME680_compensate_temperature(i2c_sensor, adc_T); + sensor->pressure = BME680_compensate_pressure(i2c_sensor, adc_P); + sensor->hum = BME680_compensate_humidity(i2c_sensor, adc_H); + + return UT_SENSORSTATUS_OK; +} + +bool unitemp_BME680_free(Sensor* sensor) { + I2CSensor* i2c_sensor = (I2CSensor*)sensor->instance; + free(i2c_sensor->sensorInstance); + return true; +} \ No newline at end of file diff --git a/applications/plugins/unitemp/sensors/BME680.h b/applications/plugins/unitemp/sensors/BME680.h new file mode 100644 index 000000000..b126c7c84 --- /dev/null +++ b/applications/plugins/unitemp/sensors/BME680.h @@ -0,0 +1,112 @@ +/* + Unitemp - Universal temperature reader + Copyright (C) 2022-2023 Victor Nikitchuk (https://github.com/quen0n) + Contributed by g0gg0 (https://github.com/g3gg0) + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ +#ifndef UNITEMP_BME680 +#define UNITEMP_BME680 + +#include "../unitemp.h" +#include "../Sensors.h" +#include "../interfaces/I2CSensor.h" + +typedef struct { + uint16_t dig_T1; + int16_t dig_T2; + int16_t dig_T3; +} BME680_temp_cal; + +typedef struct { + uint16_t dig_GH1; + int16_t dig_GH2; + int16_t dig_GH3; +} BME680_gas_cal; + +typedef struct { + uint16_t dig_P1; + int16_t dig_P2; + int16_t dig_P3; + int16_t dig_P4; + int16_t dig_P5; + int16_t dig_P6; + int16_t dig_P7; + int16_t dig_P8; + int16_t dig_P9; + int16_t dig_P10; +} BME680_press_cal; + +typedef struct { + uint16_t dig_H1; + uint16_t dig_H2; + int8_t dig_H3; + int8_t dig_H4; + int8_t dig_H5; + uint8_t dig_H6; + int8_t dig_H7; +} BME680_hum_cal; + +typedef struct { + //Калибровочные значения температуры + BME680_temp_cal temp_cal; + //Калибровочные значения давления + BME680_press_cal press_cal; + //Калибровочные значения влажности воздуха + BME680_hum_cal hum_cal; + BME680_gas_cal gas_cal; + //Время последнего обновления калибровочных значений + uint32_t last_cal_update_time; + //Индификатор датчика + uint8_t chip_id; + //Корректировочное значение температуры + int32_t t_fine; +} BME680_instance; + +extern const SensorType BMP280; +extern const SensorType BME680; +/** + * @brief Выделение памяти и установка начальных значений датчика BMP280 + * @param sensor Указатель на создаваемый датчик + * @return Истина при успехе + */ +bool unitemp_BME680_alloc(Sensor* sensor, char* args); + +/** + * @brief Инициализации датчика BMP280 + * @param sensor Указатель на датчик + * @return Истина если инициализация упспешная + */ +bool unitemp_BME680_init(Sensor* sensor); + +/** + * @brief Деинициализация датчика + * @param sensor Указатель на датчик + */ +bool unitemp_BME680_deinit(Sensor* sensor); + +/** + * @brief Обновление значений из датчика + * @param sensor Указатель на датчик + * @return Статус опроса датчика + */ +UnitempStatus unitemp_BME680_update(Sensor* sensor); + +/** + * @brief Высвободить память датчика + * @param sensor Указатель на датчик + */ +bool unitemp_BME680_free(Sensor* sensor); + +#endif \ No newline at end of file diff --git a/applications/plugins/unitemp/sensors/MAX6675.c b/applications/plugins/unitemp/sensors/MAX6675.c new file mode 100644 index 000000000..e97a96eb5 --- /dev/null +++ b/applications/plugins/unitemp/sensors/MAX6675.c @@ -0,0 +1,81 @@ +/* + Unitemp - Universal temperature reader + Copyright (C) 2022-2023 Victor Nikitchuk (https://github.com/quen0n) + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ +#include "MAX6675.h" + +const SensorType MAX6675 = { + .typename = "MAX6675", + .altname = "MAX6675 (Thermocouple)", + .interface = &SPI, + .datatype = UT_TEMPERATURE, + .pollingInterval = 500, + .allocator = unitemp_MAX6675_alloc, + .mem_releaser = unitemp_MAX6675_free, + .initializer = unitemp_MAX6675_init, + .deinitializer = unitemp_MAX6675_deinit, + .updater = unitemp_MAX6675_update}; + +bool unitemp_MAX6675_alloc(Sensor* sensor, char* args) { + UNUSED(sensor); + UNUSED(args); + return true; +} + +bool unitemp_MAX6675_free(Sensor* sensor) { + UNUSED(sensor); + return true; +} + +bool unitemp_MAX6675_init(Sensor* sensor) { + SPISensor* instance = sensor->instance; + furi_hal_spi_bus_handle_init(instance->spi); + UNUSED(instance); + return true; +} + +bool unitemp_MAX6675_deinit(Sensor* sensor) { + UNUSED(sensor); + return true; +} + +UnitempStatus unitemp_MAX6675_update(Sensor* sensor) { + SPISensor* instance = sensor->instance; + + furi_hal_spi_acquire(instance->spi); + furi_hal_gpio_write(instance->CS_pin->pin, false); + + uint8_t buff[2] = {0}; + + furi_hal_spi_bus_rx(instance->spi, buff, 2, 0xFF); + furi_hal_spi_release(instance->spi); + + uint32_t raw = (buff[0] << 8) | buff[1]; + + if(raw == 0xFFFFFFFF || raw == 0) return UT_SENSORSTATUS_TIMEOUT; + + //Определение состояния термопары + uint8_t state = raw & 0b100; + //Обрыв + if(state == 0b100) { + UNITEMP_DEBUG("%s has thermocouple open circuit", sensor->name); + return UT_SENSORSTATUS_ERROR; + } + + sensor->temp = (int16_t)(raw) / 32.0f; + + return UT_SENSORSTATUS_OK; +} diff --git a/applications/plugins/unitemp/sensors/MAX6675.h b/applications/plugins/unitemp/sensors/MAX6675.h new file mode 100644 index 000000000..cce346590 --- /dev/null +++ b/applications/plugins/unitemp/sensors/MAX6675.h @@ -0,0 +1,65 @@ +/* + Unitemp - Universal temperature reader + Copyright (C) 2022-2023 Victor Nikitchuk (https://github.com/quen0n) + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ +#ifndef UNITEMP_MAX6675 +#define UNITEMP_MAX6675 + +#include "../unitemp.h" +#include "../Sensors.h" +#include "../interfaces/SPISensor.h" + +extern const SensorType MAX6675; + +/** + * @brief Выделение памяти и установка начальных значений датчика MAX6675 + * + * @param sensor Указатель на создаваемый датчик + * @return Истина при успехе + */ +bool unitemp_MAX6675_alloc(Sensor* sensor, char* args); + +/** + * @brief Инициализации датчика MAX6675 + * + * @param sensor Указатель на датчик + * @return Истина если инициализация упспешная + */ +bool unitemp_MAX6675_init(Sensor* sensor); + +/** + * @brief Деинициализация датчика + * + * @param sensor Указатель на датчик + */ +bool unitemp_MAX6675_deinit(Sensor* sensor); + +/** + * @brief Обновление значений из датчика + * + * @param sensor Указатель на датчик + * @return Статус обновления + */ +UnitempStatus unitemp_MAX6675_update(Sensor* sensor); + +/** + * @brief Высвободить память датчика + * + * @param sensor Указатель на датчик + */ +bool unitemp_MAX6675_free(Sensor* sensor); + +#endif \ No newline at end of file diff --git a/applications/plugins/unitemp/sensors/Sensors.xlsx b/applications/plugins/unitemp/sensors/Sensors.xlsx index bd47d4948..c314098a8 100644 Binary files a/applications/plugins/unitemp/sensors/Sensors.xlsx and b/applications/plugins/unitemp/sensors/Sensors.xlsx differ diff --git a/applications/plugins/unitemp/unitemp.h b/applications/plugins/unitemp/unitemp.h index bcb2e4ac9..bde8b0c17 100644 --- a/applications/plugins/unitemp/unitemp.h +++ b/applications/plugins/unitemp/unitemp.h @@ -40,7 +40,7 @@ //Имя приложения #define APP_NAME "Unitemp" //Версия приложения -#define UNITEMP_APP_VER "1.1.2-dev" +#define UNITEMP_APP_VER "1.2" //Путь хранения файлов плагина #define APP_PATH_FOLDER "/ext/unitemp" //Имя файла с настройками