Просмотр исходного кода

Added new sensor - DHT20/AHT20/AM2108

Victor 3 лет назад
Родитель
Сommit
90f155724f
6 измененных файлов с 222 добавлено и 6 удалено
  1. 1 2
      README.md
  2. 12 2
      Sensors.c
  3. 1 0
      Sensors.h
  4. 2 2
      interfaces/SingleWireSensor.c
  5. 144 0
      sensors/DHT20.c
  6. 62 0
      sensors/DHT20.h

+ 1 - 2
README.md

@@ -1,10 +1,9 @@
 ![Flipper usage](https://user-images.githubusercontent.com/10090793/206618263-c1e212e4-58dc-432e-87a8-5c19fd835b35.png)  
 ![Flipper usage](https://user-images.githubusercontent.com/10090793/206618263-c1e212e4-58dc-432e-87a8-5c19fd835b35.png)  
 # Unitemp - Universal temperature sensor reader
 # Unitemp - Universal temperature sensor reader
 [![GitHub release](https://img.shields.io/github/release/quen0n/unitemp-flipperzero?include_prereleases=&sort=semver&color=blue)](https://github.com/quen0n/unitemp-flipperzero/releases/)
 [![GitHub release](https://img.shields.io/github/release/quen0n/unitemp-flipperzero?include_prereleases=&sort=semver&color=blue)](https://github.com/quen0n/unitemp-flipperzero/releases/)
-[![GitHub all releases](https://img.shields.io/github/downloads/quen0n/unitemp-flipperzero/total)]()
 [![GitHub](https://img.shields.io/github/license/quen0n/unitemp-flipperzero)](https://github.com/quen0n/unitemp-flipperzero/blob/dev/LICENSE.md)  
 [![GitHub](https://img.shields.io/github/license/quen0n/unitemp-flipperzero)](https://github.com/quen0n/unitemp-flipperzero/blob/dev/LICENSE.md)  
 [Flipper Zero](https://flipperzero.one/) application for reading temperature, humidity and pressure sensors using Onewire, Singlewire, I2C protocols.   
 [Flipper Zero](https://flipperzero.one/) application for reading temperature, humidity and pressure sensors using Onewire, Singlewire, I2C protocols.   
 ## List of supported sensors (supplemented)
 ## List of supported sensors (supplemented)
-![image](https://user-images.githubusercontent.com/10090793/208480561-e98a6192-d44d-4ad9-8692-a91ccaae47c7.png)
+![image](https://user-images.githubusercontent.com/10090793/208540093-ac966cb4-7a54-4296-a223-07a3c3859408.png)
 ## Installation
 ## Installation
 Copy the contents of the repository to the `applications/plugins/unitemp` folder and build the project. Flash FZ along with resources. [More...](https://github.com/flipperdevices/flipperzero-firmware/blob/dev/documentation/fbt.md)
 Copy the contents of the repository to the `applications/plugins/unitemp` folder and build the project. Flash FZ along with resources. [More...](https://github.com/flipperdevices/flipperzero-firmware/blob/dev/documentation/fbt.md)

+ 12 - 2
Sensors.c

@@ -71,8 +71,18 @@ const Interface ONE_WIRE = {
 //Перечень интерфейсов подключения
 //Перечень интерфейсов подключения
 //static const Interface* interfaces[] = {&SINGLE_WIRE, &I2C, &ONE_WIRE};
 //static const Interface* interfaces[] = {&SINGLE_WIRE, &I2C, &ONE_WIRE};
 //Перечень датчиков
 //Перечень датчиков
-static const SensorType* sensorTypes[] =
-    {&DHT11, &DHT12_SW, &DHT21, &DHT22, &AM2320_SW, &AM2320_I2C, &LM75, &BMP280, &BME280, &Dallas};
+static const SensorType* sensorTypes[] = {
+    &DHT11,
+    &DHT12_SW,
+    &DHT20,
+    &DHT21,
+    &DHT22,
+    &AM2320_SW,
+    &AM2320_I2C,
+    &LM75,
+    &BMP280,
+    &BME280,
+    &Dallas};
 
 
 const SensorType* unitemp_sensors_getTypeFromInt(uint8_t index) {
 const SensorType* unitemp_sensors_getTypeFromInt(uint8_t index) {
     if(index > SENSOR_TYPES_COUNT) return NULL;
     if(index > SENSOR_TYPES_COUNT) return NULL;

+ 1 - 0
Sensors.h

@@ -321,4 +321,5 @@ const GPIO*
 //BMP280, BME280
 //BMP280, BME280
 #include "./sensors/BMx280.h"
 #include "./sensors/BMx280.h"
 #include "./sensors/AM2320.h"
 #include "./sensors/AM2320.h"
+#include "./sensors/DHT20.h"
 #endif
 #endif

+ 2 - 2
interfaces/SingleWireSensor.c

@@ -43,7 +43,7 @@ const SensorType DHT12_SW = {
     .updater = unitemp_singlewire_update};
     .updater = unitemp_singlewire_update};
 const SensorType DHT21 = {
 const SensorType DHT21 = {
     .typename = "DHT21",
     .typename = "DHT21",
-    .altname = "DHT21 (AM2301)",
+    .altname = "DHT21/AM2301",
     .interface = &SINGLE_WIRE,
     .interface = &SINGLE_WIRE,
     .datatype = UT_DATA_TYPE_TEMP_HUM,
     .datatype = UT_DATA_TYPE_TEMP_HUM,
     .pollingInterval = 1000,
     .pollingInterval = 1000,
@@ -54,7 +54,7 @@ const SensorType DHT21 = {
     .updater = unitemp_singlewire_update};
     .updater = unitemp_singlewire_update};
 const SensorType DHT22 = {
 const SensorType DHT22 = {
     .typename = "DHT22",
     .typename = "DHT22",
-    .altname = "DHT22 (AM2302)",
+    .altname = "DHT22/AM2302",
     .interface = &SINGLE_WIRE,
     .interface = &SINGLE_WIRE,
     .datatype = UT_DATA_TYPE_TEMP_HUM,
     .datatype = UT_DATA_TYPE_TEMP_HUM,
     .pollingInterval = 2000,
     .pollingInterval = 2000,

+ 144 - 0
sensors/DHT20.c

@@ -0,0 +1,144 @@
+/*
+    Unitemp - Universal temperature reader
+    Copyright (C) 2022  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/>.
+*/
+#include "DHT20.h"
+#include "../interfaces/I2CSensor.h"
+
+const SensorType DHT20 = {
+    .typename = "DHT20",
+    .altname = "DHT20/AM2108/AHT20",
+    .interface = &I2C,
+    .datatype = UT_TEMPERATURE | UT_HUMIDITY,
+    .pollingInterval = 2000,
+    .allocator = unitemp_DHT20_I2C_alloc,
+    .mem_releaser = unitemp_DHT20_I2C_free,
+    .initializer = unitemp_DHT20_init,
+    .deinitializer = unitemp_DHT20_I2C_deinit,
+    .updater = unitemp_DHT20_I2C_update};
+
+static uint8_t DHT20_get_status(I2CSensor* i2c_sensor) {
+    uint8_t status[1] = {0};
+    unitemp_i2c_readArray(i2c_sensor, 1, status);
+    return status[0];
+}
+
+static uint8_t DHT20_calc_CRC8(uint8_t* message, uint8_t Num) {
+    uint8_t i;
+    uint8_t byte;
+    uint8_t crc = 0xFF;
+    for(byte = 0; byte < Num; byte++) {
+        crc ^= (message[byte]);
+        for(i = 8; i > 0; --i) {
+            if(crc & 0x80)
+                crc = (crc << 1) ^ 0x31;
+            else
+                crc = (crc << 1);
+        }
+    }
+    return crc;
+}
+
+static void DHT20_reset_reg(I2CSensor* i2c_sensor, uint8_t addr) {
+    uint8_t data[3] = {addr, 0x00, 0x00};
+
+    unitemp_i2c_writeArray(i2c_sensor, 3, data);
+
+    furi_delay_ms(5);
+
+    unitemp_i2c_readArray(i2c_sensor, 3, data);
+
+    furi_delay_ms(10);
+
+    data[0] = 0xB0 | addr;
+    unitemp_i2c_writeArray(i2c_sensor, 3, data);
+}
+
+bool unitemp_DHT20_I2C_alloc(Sensor* sensor, char* args) {
+    UNUSED(args);
+    I2CSensor* i2c_sensor = (I2CSensor*)sensor->instance;
+
+    //Адреса на шине I2C (7 бит)
+    i2c_sensor->minI2CAdr = 0x38 << 1;
+    i2c_sensor->maxI2CAdr = 0x38 << 1;
+    return true;
+}
+
+bool unitemp_DHT20_I2C_free(Sensor* sensor) {
+    //Нечего высвобождать, так как ничего не было выделено
+    UNUSED(sensor);
+    return true;
+}
+
+bool unitemp_DHT20_init(Sensor* sensor) {
+    I2CSensor* i2c_sensor = (I2CSensor*)sensor->instance;
+
+    uint8_t data[3] = {0xA8, 0x00, 0x00};
+    if(!unitemp_i2c_writeArray(i2c_sensor, 3, data)) return false;
+    furi_delay_ms(10);
+    data[0] = 0xBE;
+    data[1] = 0x08;
+    if(!unitemp_i2c_writeArray(i2c_sensor, 3, data)) return false;
+    furi_delay_ms(10);
+
+    return true;
+}
+
+bool unitemp_DHT20_I2C_deinit(Sensor* sensor) {
+    //Нечего деинициализировать
+    UNUSED(sensor);
+    return true;
+}
+
+UnitempStatus unitemp_DHT20_I2C_update(Sensor* sensor) {
+    I2CSensor* i2c_sensor = (I2CSensor*)sensor->instance;
+
+    if(DHT20_get_status(i2c_sensor) != 0x18) {
+        DHT20_reset_reg(i2c_sensor, 0x1B);
+        DHT20_reset_reg(i2c_sensor, 0x1C);
+        DHT20_reset_reg(i2c_sensor, 0x1E);
+    }
+    furi_delay_ms(10);
+
+    uint8_t data[7] = {0xAC, 0x33, 0x00};
+    if(!unitemp_i2c_writeArray(i2c_sensor, 3, data)) return UT_SENSORSTATUS_TIMEOUT;
+    furi_delay_ms(80);
+    uint32_t t = furi_get_tick();
+    while(DHT20_get_status(i2c_sensor) == 0x80) {
+        if(furi_get_tick() - t > 10) return UT_SENSORSTATUS_TIMEOUT;
+    }
+
+    if(!unitemp_i2c_readArray(i2c_sensor, 7, data)) return UT_SENSORSTATUS_TIMEOUT;
+
+    if(DHT20_calc_CRC8(data, 6) != data[6]) {
+        return UT_SENSORSTATUS_BADCRC;
+    }
+    uint32_t RetuData = 0;
+    RetuData = (RetuData | data[1]) << 8;
+    RetuData = (RetuData | data[2]) << 8;
+    RetuData = (RetuData | data[3]);
+    RetuData = RetuData >> 4;
+    sensor->hum = RetuData * 100 * 10 / 1024.0f / 1024.0f / 10.0f;
+
+    RetuData = 0;
+    RetuData = (RetuData | data[3]) << 8;
+    RetuData = (RetuData | data[4]) << 8;
+    RetuData = (RetuData | data[5]);
+    RetuData = RetuData & 0xfffff;
+    sensor->temp = (RetuData * 200 * 10.0f / 1024.0f / 1024.0f - 500) / 10.0f;
+
+    return UT_SENSORSTATUS_OK;
+}

+ 62 - 0
sensors/DHT20.h

@@ -0,0 +1,62 @@
+/*
+    Unitemp - Universal temperature reader
+    Copyright (C) 2022  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_DHT20
+#define UNITEMP_DHT20
+
+#include "../unitemp.h"
+#include "../Sensors.h"
+extern const SensorType DHT20;
+/**
+ * @brief Выделение памяти и установка начальных значений датчика DHT20
+ *
+ * @param sensor Указатель на создаваемый датчик
+ * @return Истина при успехе
+ */
+bool unitemp_DHT20_I2C_alloc(Sensor* sensor, char* args);
+
+/**
+ * @brief Инициализации датчика DHT20
+ *
+ * @param sensor Указатель на датчик
+ * @return Истина если инициализация упспешная
+ */
+bool unitemp_DHT20_init(Sensor* sensor);
+
+/**
+ * @brief Деинициализация датчика
+ *
+ * @param sensor Указатель на датчик
+ */
+bool unitemp_DHT20_I2C_deinit(Sensor* sensor);
+
+/**
+ * @brief Обновление значений из датчика
+ *
+ * @param sensor Указатель на датчик
+ * @return Статус обновления
+ */
+UnitempStatus unitemp_DHT20_I2C_update(Sensor* sensor);
+
+/**
+ * @brief Высвободить память датчика
+ *
+ * @param sensor Указатель на датчик
+ */
+bool unitemp_DHT20_I2C_free(Sensor* sensor);
+
+#endif