Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added HDC2080 Support #27

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Sensors.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,9 @@ const Interface 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, &BME680, &MAX31855, &MAX6675};
&SHT30, &GXHT30, &LM75, &HDC1080, &HDC2080,
&BMP180, &BMP280, &BME280, &BME680, &MAX31855,
&MAX6675};

const SensorType* unitemp_sensors_getTypeFromInt(uint8_t index) {
if(index > SENSOR_TYPES_COUNT) return NULL;
Expand Down
1 change: 1 addition & 0 deletions Sensors.h
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,7 @@ const GPIO*
#include "./sensors/BMP180.h"
#include "./sensors/HTU21x.h"
#include "./sensors/HDC1080.h"
#include "./sensors/HDC2080.h"
#include "./sensors/MAX31855.h"
#include "./sensors/MAX6675.h"
#endif
106 changes: 106 additions & 0 deletions sensors/HDC2080.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
Unitemp - Universal temperature reader
Copyright (C) 2023 Victor Nikitchuk (https://github.com/quen0n) & zhiyan114 (https://github.com/zhiyan114)

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 <https://www.gnu.org/licenses/>.
*/
#include "HDC2080.h"
#include "../interfaces/I2CSensor.h"

const SensorType HDC2080 = {
.typename = "HDC2080",
.interface = &I2C,
.datatype = UT_DATA_TYPE_TEMP_HUM,
.pollingInterval = 250,
.allocator = unitemp_HDC2080_alloc,
.mem_releaser = unitemp_HDC2080_free,
.initializer = unitemp_HDC2080_init,
.deinitializer = unitemp_HDC2080_deinit,
.updater = unitemp_HDC2080_update};

bool unitemp_HDC2080_alloc(Sensor* sensor, char* args) {
UNUSED(args);
I2CSensor* i2c_sensor = (I2CSensor*)sensor->instance;

//Адреса на шине I2C (7 бит)
i2c_sensor->minI2CAdr = 0x40 << 1;
i2c_sensor->maxI2CAdr = 0x40 << 1;
return true;
}

bool unitemp_HDC2080_free(Sensor* sensor) {
//Нечего высвобождать, так как ничего не было выделено
UNUSED(sensor);
return true;
}

bool unitemp_HDC2080_init(Sensor* sensor) {
I2CSensor* i2c_sensor = (I2CSensor*)sensor->instance;

uint8_t data[2];
if(!unitemp_i2c_readRegArray(i2c_sensor, 0xFF, 2, data)) return UT_SENSORSTATUS_TIMEOUT;
uint16_t device_id = ((uint16_t)data[0] << 8) | data[1];
if(device_id != 0x700) {
FURI_LOG_E(
APP_NAME,
"Sensor %s returned wrong ID 0x%02X, expected 0x700",
sensor->name,
device_id);
return false;
}

uint8_t config[1] = {0};

// Soft Reset the sensor
config[0] = 0b10000000;
if(!unitemp_i2c_writeRegArray(i2c_sensor, 0x0E, 1, config)) return UT_SENSORSTATUS_TIMEOUT;
furi_delay_ms(50);
// Set the sample rate to 5Hz
config[0] = 0b01110000;
if(!unitemp_i2c_writeRegArray(i2c_sensor, 0x0E, 1, config)) return UT_SENSORSTATUS_TIMEOUT;
// Enable Sensor Mode
config[0] = 0b00000001;
if(!unitemp_i2c_writeRegArray(i2c_sensor, 0x0F, 1, config)) return UT_SENSORSTATUS_TIMEOUT;

return true;
}

bool unitemp_HDC2080_deinit(Sensor* sensor) {
I2CSensor* i2c_sensor = (I2CSensor*)sensor->instance;
UNUSED(i2c_sensor);
return true;
}

// Function helps read the data while keeping the code clean...
uint8_t getAddrVal(I2CSensor* sensor, uint8_t addr) {
uint8_t data[1] = {addr};
if(!unitemp_i2c_writeArray(sensor, 1, data)) return UT_SENSORSTATUS_TIMEOUT;
furi_delay_ms(10);
if(!unitemp_i2c_readArray(sensor, 1, data)) return UT_SENSORSTATUS_TIMEOUT;
return data[0];
}
UnitempStatus unitemp_HDC2080_update(Sensor* sensor) {
I2CSensor* i2c_sensor = (I2CSensor*)sensor->instance;

uint8_t data[2] = {0};
data[0] = getAddrVal(i2c_sensor, 0);
data[1] = getAddrVal(i2c_sensor, 1);
sensor->temp = ((float)(((uint16_t)data[1] << 8) | data[0]) / 65536) * 165 - (40.0+ 0.08*(3.3-1.8)); // 3.3v offset correction

data[0] = getAddrVal(i2c_sensor, 2);
data[1] = getAddrVal(i2c_sensor, 3);
sensor->hum = ((float)(((uint16_t)data[1] << 8) | data[0]) / 65536) * 100;

return UT_SENSORSTATUS_OK;
}
62 changes: 62 additions & 0 deletions sensors/HDC2080.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
Unitemp - Universal temperature reader
Copyright (C) 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 <https://www.gnu.org/licenses/>.
*/
#ifndef UNITEMP_HDC2080
#define UNITEMP_HDC2080

#include "../unitemp.h"
#include "../Sensors.h"
extern const SensorType HDC2080;
/**
* @brief Выделение памяти и установка начальных значений датчика HDC2080
*
* @param sensor Указатель на создаваемый датчик
* @return Истина при успехе
*/
bool unitemp_HDC2080_alloc(Sensor* sensor, char* args);

/**
* @brief Инициализации датчика HDC2080
*
* @param sensor Указатель на датчик
* @return Истина если инициализация упспешная
*/
bool unitemp_HDC2080_init(Sensor* sensor);

/**
* @brief Деинициализация датчика
*
* @param sensor Указатель на датчик
*/
bool unitemp_HDC2080_deinit(Sensor* sensor);

/**
* @brief Обновление значений из датчика
*
* @param sensor Указатель на датчик
* @return Статус обновления
*/
UnitempStatus unitemp_HDC2080_update(Sensor* sensor);

/**
* @brief Высвободить память датчика
*
* @param sensor Указатель на датчик
*/
bool unitemp_HDC2080_free(Sensor* sensor);

#endif