Sfoglia il codice sorgente

Merge branch 'dev' into dev

Victor Nikitchuk 3 anni fa
parent
commit
a84edd7fae

+ 14 - 4
README.md

@@ -1,14 +1,24 @@
-![Flipper usage](https://user-images.githubusercontent.com/10090793/206618263-c1e212e4-58dc-432e-87a8-5c19fd835b35.png)  
+![Flipper usage](https://user-images.githubusercontent.com/10090793/211182642-e41919c5-3091-4125-815a-2d6a77a859f6.png)
 # 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](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 using Onewire, Singlewire, I2C protocols.   
+[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/210119924-51119deb-f950-40ee-bc27-22b971243527.png)
+![image](https://user-images.githubusercontent.com/10090793/215224104-1f39dcb5-f732-4632-a49a-d9a0e72bcc04.png)
+
 ## 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)
+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
+## 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)

+ 39 - 31
Sensors.c

@@ -1,6 +1,6 @@
 /*
     Unitemp - Universal temperature reader
-    Copyright (C) 2022  Victor Nikitchuk (https://github.com/quen0n)
+    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
@@ -66,9 +66,14 @@ const Interface ONE_WIRE = {
     .allocator = unitemp_onewire_sensor_alloc,
     .mem_releaser = unitemp_onewire_sensor_free,
     .updater = unitemp_onewire_sensor_update};
+const Interface SPI = {
+    .name = "SPI",
+    .allocator = unitemp_spi_sensor_alloc,
+    .mem_releaser = unitemp_spi_sensor_free,
+    .updater = unitemp_spi_sensor_update};
 
 //Перечень интерфейсов подключения
-//static const Interface* interfaces[] = {&SINGLE_WIRE, &I2C, &ONE_WIRE};
+//static const Interface* interfaces[] = {&SINGLE_WIRE, &I2C, &ONE_WIRE, &SPI};
 //Перечень датчиков
 static const SensorType* sensorTypes[] = {
     &DHT11,
@@ -79,14 +84,18 @@ static const SensorType* sensorTypes[] = {
     &Dallas,
     &AM2320_SW,
     &AM2320_I2C,
+    &HTU21x,
     &AHT10,
     &SHT30,
     &GXHT30,
     &LM75,
+    &HDC1080,
     &BMP180,
     &BMP280,
     &BME280,
-    &BME680};
+    &BME680,
+    &MAX31855,
+    &MAX6675};
 
 const SensorType* unitemp_sensors_getTypeFromInt(uint8_t index) {
     if(index > SENSOR_TYPES_COUNT) return NULL;
@@ -165,7 +174,7 @@ uint8_t unitemp_gpio_getAviablePortsCount(const Interface* interface, const GPIO
         }
 
         //Проверка для single wire
-        if(interface == &SINGLE_WIRE) {
+        if(interface == &SINGLE_WIRE || interface == &SPI) {
             if(gpio_interfaces_list[i] == NULL || (unitemp_gpio_getFromIndex(i) == extraport)) {
                 aviable_ports_count++;
             }
@@ -204,6 +213,13 @@ const GPIO*
             return NULL;
         }
     }
+    if(interface == &SPI) {
+        if(!((gpio_interfaces_list[0] == NULL || gpio_interfaces_list[0] == &SPI) &&
+             (gpio_interfaces_list[1] == NULL || gpio_interfaces_list[1] == &SPI) &&
+             (gpio_interfaces_list[3] == NULL || gpio_interfaces_list[3] == &SPI))) {
+            return NULL;
+        }
+    }
 
     uint8_t aviable_index = 0;
     for(uint8_t i = 0; i < GPIO_ITEMS; i++) {
@@ -221,7 +237,7 @@ const GPIO*
             }
         }
         //Проверка для single wire
-        if(interface == &SINGLE_WIRE) {
+        if(interface == &SINGLE_WIRE || interface == &SPI) {
             if(gpio_interfaces_list[i] == NULL || unitemp_gpio_getFromIndex(i) == extraport) {
                 if(aviable_index == index) {
                     return unitemp_gpio_getFromIndex(i);
@@ -282,9 +298,7 @@ void unitemp_sensors_add(Sensor* sensor) {
 }
 
 bool unitemp_sensors_load(void) {
-#ifdef UNITEMP_DEBUG
-    FURI_LOG_D(APP_NAME, "Loading sensors...");
-#endif
+    UNITEMP_DEBUG("Loading sensors...");
 
     //Выделение памяти на поток
     app->file_stream = file_stream_alloc(app->storage);
@@ -393,9 +407,7 @@ bool unitemp_sensors_load(void) {
 }
 
 bool unitemp_sensors_save(void) {
-#ifdef UNITEMP_DEBUG
-    FURI_LOG_D(APP_NAME, "Saving sensors...");
-#endif
+    UNITEMP_DEBUG("Saving sensors...");
 
     //Выделение памяти для потока
     app->file_stream = file_stream_alloc(app->storage);
@@ -438,6 +450,11 @@ bool unitemp_sensors_save(void) {
             stream_write_format(
                 app->file_stream, "%d\n", unitemp_singlewire_sensorGetGPIO(sensor)->num);
         }
+        if(sensor->type->interface == &SPI) {
+            uint8_t gpio_num = ((SPISensor*)sensor->instance)->CS_pin->num;
+            stream_write_format(app->file_stream, "%d\n", gpio_num);
+        }
+
         if(sensor->type->interface == &I2C) {
             stream_write_format(
                 app->file_stream, "%X\n", ((I2CSensor*)sensor->instance)->currentI2CAdr);
@@ -515,7 +532,7 @@ Sensor* unitemp_sensor_alloc(char* name, const SensorType* type, char* args) {
 
     //Выход если датчик успешно развёрнут
     if(status) {
-        FURI_LOG_I(APP_NAME, "Sensor %s allocated", name);
+        UNITEMP_DEBUG("Sensor %s allocated", name);
         return sensor;
     }
     //Выход с очисткой если память для датчика не была выделена
@@ -541,17 +558,13 @@ void unitemp_sensor_free(Sensor* sensor) {
     bool status = false;
     //Высвобождение памяти под инстанс
     status = sensor->type->interface->mem_releaser(sensor);
-    UNUSED(status);
-#ifdef UNITEMP_DEBUG
 
     if(status) {
-        FURI_LOG_D(APP_NAME, "Sensor %s memory successfully released", sensor->name);
+        UNITEMP_DEBUG("Sensor %s memory successfully released", sensor->name);
     } else {
         FURI_LOG_E(APP_NAME, "Sensor %s memory is not released", sensor->name);
     }
-#endif
     free(sensor->name);
-    //free(sensor);
 }
 
 void unitemp_sensors_free(void) {
@@ -570,9 +583,7 @@ bool unitemp_sensors_init(void) {
         //Может пропасть при отключении USB
         if(furi_hal_power_is_otg_enabled() != true) {
             furi_hal_power_enable_otg();
-#ifdef UNITEMP_DEBUG
-            FURI_LOG_D(APP_NAME, "OTG enabled");
-#endif
+            UNITEMP_DEBUG("OTG enabled");
         }
         if(!(*app->sensors[i]->type->initializer)(app->sensors[i])) {
             FURI_LOG_E(
@@ -581,9 +592,7 @@ bool unitemp_sensors_init(void) {
                 app->sensors[i]->name);
             result = false;
         }
-#ifdef UNITEMP_DEBUG
-        FURI_LOG_D(APP_NAME, "Sensor %s successfully initialized", app->sensors[i]->name);
-#endif
+        FURI_LOG_I(APP_NAME, "Sensor %s successfully initialized", app->sensors[i]->name);
     }
     app->sensors_ready = true;
     return result;
@@ -594,9 +603,7 @@ bool unitemp_sensors_deInit(void) {
     //Выключение 5 В если до этого оно не было включено
     if(app->settings.lastOTGState != true) {
         furi_hal_power_disable_otg();
-#ifdef UNITEMP_DEBUG
-        FURI_LOG_D(APP_NAME, "OTG disabled");
-#endif
+        UNITEMP_DEBUG("OTG disabled");
     }
 
     //Перебор датчиков из списка
@@ -632,13 +639,14 @@ UnitempStatus unitemp_sensor_updateData(Sensor* sensor) {
 
     sensor->status = sensor->type->interface->updater(sensor);
 
-#ifdef UNITEMP_DEBUG
-    if(sensor->status != UT_SENSORSTATUS_OK && sensor->status != UT_SENSORSTATUS_POLLING)
-        FURI_LOG_D(APP_NAME, "Sensor %s update status %d", sensor->name, sensor->status);
-#endif
+    if(sensor->status != UT_SENSORSTATUS_OK && sensor->status != UT_SENSORSTATUS_POLLING) {
+        UNITEMP_DEBUG("Sensor %s update status %d", sensor->name, sensor->status);
+    }
 
-    if(app->settings.temp_unit == UT_TEMP_FAHRENHEIT && sensor->status == UT_SENSORSTATUS_OK)
+    if(app->settings.temp_unit == UT_TEMP_FAHRENHEIT && sensor->status == UT_SENSORSTATUS_OK) {
         uintemp_celsiumToFarengate(sensor);
+    }
+
     if(sensor->status == UT_SENSORSTATUS_OK) {
         sensor->temp += sensor->temp_offset / 10.f;
         if(app->settings.pressure_unit == UT_PRESSURE_MM_HG) {

+ 6 - 2
Sensors.h

@@ -1,6 +1,6 @@
 /*
     Unitemp - Universal temperature reader
-    Copyright (C) 2022  Victor Nikitchuk (https://github.com/quen0n)
+    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
@@ -136,7 +136,7 @@ typedef struct Sensor {
 extern const Interface SINGLE_WIRE; //Собственный однопроводной протокол датчиков DHTXX и AM23XX
 extern const Interface ONE_WIRE; //Однопроводной протокол Dallas
 extern const Interface I2C; //I2C_2 (PC0, PC1)
-//extern const Interface SPI;
+extern const Interface SPI; //SPI_1 (MOSI - 2, MISO - 3, CS - 4, SCK - 5)
 
 /* ============================= Датчик(и) ============================= */
 /**
@@ -325,4 +325,8 @@ const GPIO*
 #include "./sensors/DHT20.h"
 #include "./sensors/SHT30.h"
 #include "./sensors/BMP180.h"
+#include "./sensors/HTU21x.h"
+#include "./sensors/HDC1080.h"
+#include "./sensors/MAX31855.h"
+#include "./sensors/MAX6675.h"
 #endif

+ 1 - 1
interfaces/I2CSensor.c

@@ -1,6 +1,6 @@
 /*
     Unitemp - Universal temperature reader
-    Copyright (C) 2022  Victor Nikitchuk (https://github.com/quen0n)
+    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

+ 1 - 1
interfaces/I2CSensor.h

@@ -1,6 +1,6 @@
 /*
     Unitemp - Universal temperature reader
-    Copyright (C) 2022  Victor Nikitchuk (https://github.com/quen0n)
+    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

+ 9 - 22
interfaces/OneWireSensor.c

@@ -1,6 +1,6 @@
 /*
     Unitemp - Universal temperature reader
-    Copyright (C) 2022  Victor Nikitchuk (https://github.com/quen0n)
+    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
@@ -58,9 +58,7 @@ OneWireBus* uintemp_onewire_bus_alloc(const GPIO* gpio) {
     bus->device_count = 0;
     bus->gpio = gpio;
     bus->powerMode = PWR_PASSIVE;
-#ifdef UNITEMP_DEBUG
-    FURI_LOG_D(APP_NAME, "one wire bus (port %d) allocated", gpio->num);
-#endif
+    UNITEMP_DEBUG("one wire bus (port %d) allocated", gpio->num);
 
     return bus;
 }
@@ -84,9 +82,7 @@ bool unitemp_onewire_bus_init(OneWireBus* bus) {
     return true;
 }
 bool unitemp_onewire_bus_deinit(OneWireBus* bus) {
-#ifdef UNITEMP_DEBUG
-    FURI_LOG_D(APP_NAME, "devices on wire %d: %d", bus->gpio->num, bus->device_count);
-#endif
+    UNITEMP_DEBUG("devices on wire %d: %d", bus->gpio->num, bus->device_count);
     bus->device_count--;
     if(bus->device_count <= 0) {
         bus->device_count = 0;
@@ -236,15 +232,11 @@ void unitemp_onewire_bus_enum_init(void) {
 uint8_t* unitemp_onewire_bus_enum_next(OneWireBus* bus) {
     furi_delay_ms(10);
     if(!onewire_enum_fork_bit) { // Если на предыдущем шаге уже не было разногласий
-#ifdef UNITEMP_DEBUG
-        FURI_LOG_D(APP_NAME, "All devices on wire %d is found", unitemp_gpio_toInt(bus->gpio));
-#endif
+        UNITEMP_DEBUG("All devices on wire %d is found", unitemp_gpio_toInt(bus->gpio));
         return 0; // то просто выходим ничего не возвращая
     }
     if(!unitemp_onewire_bus_start(bus)) {
-#ifdef UNITEMP_DEBUG
-        FURI_LOG_D(APP_NAME, "Wire %d is empty", unitemp_gpio_toInt(bus->gpio));
-#endif
+        UNITEMP_DEBUG("Wire %d is empty", unitemp_gpio_toInt(bus->gpio));
         return 0;
     }
     uint8_t bp = 8;
@@ -278,9 +270,8 @@ uint8_t* unitemp_onewire_bus_enum_next(OneWireBus* bus) {
             if(!not1) { // Присутствует единица
                 next |= 0x80;
             } else { // Нет ни нулей ни единиц - ошибочная ситуация
-#ifdef UNITEMP_DEBUG
-                FURI_LOG_D(APP_NAME, "Wrong wire %d situation", unitemp_gpio_toInt(bus->gpio));
-#endif
+
+                UNITEMP_DEBUG("Wrong wire %d situation", unitemp_gpio_toInt(bus->gpio));
                 return 0;
             }
         }
@@ -421,9 +412,7 @@ UnitempStatus unitemp_onewire_sensor_update(Sensor* sensor) {
             unitemp_onewire_bus_send_byte(instance->bus, 0xBE); // Read Scratch-pad
             unitemp_onewire_bus_read_byteArray(instance->bus, buff, 9);
             if(!unitemp_onewire_CRC_check(buff, 9)) {
-#ifdef UNITEMP_DEBUG
-                FURI_LOG_D(APP_NAME, "Sensor %s is not found", sensor->name);
-#endif
+                UNITEMP_DEBUG("Sensor %s is not found", sensor->name);
                 return UT_SENSORSTATUS_TIMEOUT;
             }
         }
@@ -462,9 +451,7 @@ UnitempStatus unitemp_onewire_sensor_update(Sensor* sensor) {
         unitemp_onewire_bus_send_byte(instance->bus, 0xBE); // Read Scratch-pad
         unitemp_onewire_bus_read_byteArray(instance->bus, buff, 9);
         if(!unitemp_onewire_CRC_check(buff, 9)) {
-#ifdef UNITEMP_DEBUG
-            FURI_LOG_D(APP_NAME, "Failed CRC check: %s", sensor->name);
-#endif
+            UNITEMP_DEBUG("Failed CRC check: %s", sensor->name);
             return UT_SENSORSTATUS_BADCRC;
         }
         int16_t raw = buff[0] | ((int16_t)buff[1] << 8);

+ 1 - 1
interfaces/OneWireSensor.h

@@ -1,6 +1,6 @@
 /*
     Unitemp - Universal temperature reader
-    Copyright (C) 2022  Victor Nikitchuk (https://github.com/quen0n)
+    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

+ 89 - 0
interfaces/SPISensor.c

@@ -0,0 +1,89 @@
+/*
+    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 <https://www.gnu.org/licenses/>.
+*/
+
+#include <furi.h>
+#include <furi_hal.h>
+#include "SPISensor.h"
+
+static uint8_t sensors_count = 0;
+
+bool unitemp_spi_sensor_alloc(Sensor* sensor, char* args) {
+    if(args == NULL) return false;
+
+    //Создание инстанса датчика SPI
+    SPISensor* instance = malloc(sizeof(SPISensor));
+    if(instance == NULL) {
+        FURI_LOG_E(APP_NAME, "Sensor %s instance allocation error", sensor->name);
+        return false;
+    }
+    sensor->instance = instance;
+
+    //Определение GPIO chip select
+    int gpio = 255;
+    sscanf(args, "%d", &gpio);
+    instance->CS_pin = unitemp_gpio_getFromInt(gpio);
+    if(instance->CS_pin == NULL) {
+        FURI_LOG_E(APP_NAME, "Sensor %s GPIO setting error", sensor->name);
+        free(instance);
+        return false;
+    }
+
+    instance->spi = malloc(sizeof(FuriHalSpiBusHandle));
+    memcpy(instance->spi, &furi_hal_spi_bus_handle_external, sizeof(FuriHalSpiBusHandle));
+
+    instance->spi->cs = instance->CS_pin->pin;
+
+    bool status = sensor->type->allocator(sensor, args);
+
+    //Блокировка портов GPIO
+    sensors_count++;
+    unitemp_gpio_lock(unitemp_gpio_getFromInt(2), &SPI);
+    unitemp_gpio_lock(unitemp_gpio_getFromInt(3), &SPI);
+    unitemp_gpio_lock(unitemp_gpio_getFromInt(5), &SPI);
+    unitemp_gpio_lock(instance->CS_pin, &SPI);
+    return status;
+}
+
+bool unitemp_spi_sensor_free(Sensor* sensor) {
+    bool status = sensor->type->mem_releaser(sensor);
+    unitemp_gpio_unlock(((SPISensor*)sensor->instance)->CS_pin);
+    free(((SPISensor*)(sensor->instance))->spi);
+    free(sensor->instance);
+
+    if(--sensors_count == 0) {
+        unitemp_gpio_unlock(unitemp_gpio_getFromInt(2));
+        unitemp_gpio_unlock(unitemp_gpio_getFromInt(3));
+        unitemp_gpio_unlock(unitemp_gpio_getFromInt(5));
+    }
+
+    return status;
+}
+
+bool unitemp_spi_sensor_init(Sensor* sensor) {
+    return sensor->type->initializer(sensor);
+}
+
+bool unitemp_spi_sensor_deinit(Sensor* sensor) {
+    UNUSED(sensor);
+
+    return true;
+}
+
+UnitempStatus unitemp_spi_sensor_update(Sensor* sensor) {
+    return sensor->type->updater(sensor);
+}

+ 66 - 0
interfaces/SPISensor.h

@@ -0,0 +1,66 @@
+/*
+    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 <https://www.gnu.org/licenses/>.
+*/
+#ifndef UNITEMP_SPI
+#define UNITEMP_SPI
+
+#include "../unitemp.h"
+#include <furi_hal_spi.h>
+
+//Структура SPI датчика
+typedef struct SPISensor {
+    //Указатель на интерфейс SPI
+    FuriHalSpiBusHandle* spi;
+    //Порт подключения CS
+    const GPIO* CS_pin;
+} SPISensor;
+
+/**
+ * @brief Выделение памяти для датчика с интерфейсом SPI
+ * @param sensor Указатель на датчик
+ * @param args Указатель на массив аргументов с параметрами датчика
+ * @return Истина если всё ок
+ */
+bool unitemp_spi_sensor_alloc(Sensor* sensor, char* args);
+
+/**
+ * @brief Высвобождение памяти инстанса датчика
+ * @param sensor Указатель на датчик
+ */
+bool unitemp_spi_sensor_free(Sensor* sensor);
+
+/**
+ * @brief Инициализации датчика с интерфейсом one wire
+ * @param sensor Указатель на датчик
+ * @return Истина если инициализация упспешная
+ */
+bool unitemp_spi_sensor_init(Sensor* sensor);
+
+/**
+ * @brief Деинициализация датчика
+ * @param sensor Указатель на датчик
+ */
+bool unitemp_spi_sensor_deinit(Sensor* sensor);
+
+/**
+ * @brief Обновить значение с датчка
+ * @param sensor Указатель на датчик
+ * @return Статус обновления
+ */
+UnitempStatus unitemp_spi_sensor_update(Sensor* sensor);
+
+#endif

+ 2 - 2
interfaces/SingleWireSensor.c

@@ -1,6 +1,6 @@
 /*
     Unitemp - Universal temperature reader
-    Copyright (C) 2022  Victor Nikitchuk (https://github.com/quen0n)
+    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
@@ -262,7 +262,7 @@ UnitempStatus unitemp_singlewire_update(Sensor* sensor) {
         //Проверка на отрицательность температуры
         if(READ_BIT(raw, 1 << 15)) {
             //Проверка на способ кодирования данных
-            if(READ_BIT(raw, 0x60)) {
+            if(READ_BIT(raw, 0x6000)) {
                 //Не оригинал
                 sensor->temp = (float)((int16_t)raw) / 10;
             } else {

+ 1 - 1
interfaces/SingleWireSensor.h

@@ -1,6 +1,6 @@
 /*
     Unitemp - Universal temperature reader
-    Copyright (C) 2022  Victor Nikitchuk (https://github.com/quen0n)
+    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

+ 1 - 1
sensors/AM2320.c

@@ -1,6 +1,6 @@
 /*
     Unitemp - Universal temperature reader
-    Copyright (C) 2022  Victor Nikitchuk (https://github.com/quen0n)
+    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

+ 1 - 1
sensors/AM2320.h

@@ -1,6 +1,6 @@
 /*
     Unitemp - Universal temperature reader
-    Copyright (C) 2022  Victor Nikitchuk (https://github.com/quen0n)
+    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

+ 3 - 5
sensors/BMP180.c

@@ -1,6 +1,6 @@
 /*
     Unitemp - Universal temperature reader
-    Copyright (C) 2022  Victor Nikitchuk (https://github.com/quen0n)
+    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
@@ -101,9 +101,8 @@ bool unitemp_BMP180_init(Sensor* sensor) {
     bmp180_instance->bmp180_cal.MC = (buff[18] << 8) | buff[19];
     bmp180_instance->bmp180_cal.MD = (buff[20] << 8) | buff[21];
 
-#ifdef UNITEMP_DEBUG
-    FURI_LOG_D(
-        APP_NAME,
+
+UNITEMP_DEBUG(
         "Sensor BMP180 (0x%02X) calibration values: %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d",
         i2c_sensor->currentI2CAdr,
         bmp180_instance->bmp180_cal.AC1,
@@ -117,7 +116,6 @@ bool unitemp_BMP180_init(Sensor* sensor) {
         bmp180_instance->bmp180_cal.MB,
         bmp180_instance->bmp180_cal.MC,
         bmp180_instance->bmp180_cal.MD);
-#endif
     return true;
 }
 

+ 1 - 1
sensors/BMP180.h

@@ -1,6 +1,6 @@
 /*
     Unitemp - Universal temperature reader
-    Copyright (C) 2022  Victor Nikitchuk (https://github.com/quen0n)
+    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

+ 6 - 13
sensors/BMx280.c

@@ -1,6 +1,6 @@
 /*
     Unitemp - Universal temperature reader
-    Copyright (C) 2022  Victor Nikitchuk (https://github.com/quen0n)
+    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
@@ -180,22 +180,19 @@ static bool bmx280_readCalValues(I2CSensor* i2c_sensor) {
     if(!unitemp_i2c_readRegArray(
            i2c_sensor, TEMP_CAL_START_ADDR, 6, (uint8_t*)&bmx280_instance->temp_cal))
         return false;
-#ifdef UNITEMP_DEBUG
-    FURI_LOG_D(
-        APP_NAME,
+
+    UNITEMP_DEBUG(
         "Sensor BMx280 (0x%02X) T1-T3: %d, %d, %d",
         i2c_sensor->currentI2CAdr,
         bmx280_instance->temp_cal.dig_T1,
         bmx280_instance->temp_cal.dig_T2,
         bmx280_instance->temp_cal.dig_T3);
-#endif
 
     if(!unitemp_i2c_readRegArray(
            i2c_sensor, PRESS_CAL_START_ADDR, 18, (uint8_t*)&bmx280_instance->press_cal))
         return false;
-#ifdef UNITEMP_DEBUG
-    FURI_LOG_D(
-        APP_NAME,
+
+    UNITEMP_DEBUG(
         "Sensor BMx280 (0x%02X): P1-P9: %d, %d, %d, %d, %d, %d, %d, %d, %d",
         i2c_sensor->currentI2CAdr,
         bmx280_instance->press_cal.dig_P1,
@@ -207,7 +204,6 @@ static bool bmx280_readCalValues(I2CSensor* i2c_sensor) {
         bmx280_instance->press_cal.dig_P7,
         bmx280_instance->press_cal.dig_P8,
         bmx280_instance->press_cal.dig_P9);
-#endif
 
     if(bmx280_instance->chip_id == BME280_ID) {
         uint8_t buff[7] = {0};
@@ -221,9 +217,7 @@ static bool bmx280_readCalValues(I2CSensor* i2c_sensor) {
         bmx280_instance->hum_cal.dig_H5 = (buff[4] & 0x0F) | ((int16_t)buff[5] << 4);
         bmx280_instance->hum_cal.dig_H6 = buff[6];
 
-#ifdef UNITEMP_DEBUG
-        FURI_LOG_D(
-            APP_NAME,
+        UNITEMP_DEBUG(
             "Sensor BMx280 (0x%02X): H1-H6: %d, %d, %d, %d, %d, %d",
             i2c_sensor->currentI2CAdr,
             bmx280_instance->hum_cal.dig_H1,
@@ -232,7 +226,6 @@ static bool bmx280_readCalValues(I2CSensor* i2c_sensor) {
             bmx280_instance->hum_cal.dig_H4,
             bmx280_instance->hum_cal.dig_H5,
             bmx280_instance->hum_cal.dig_H6);
-#endif
     }
 
     bmx280_instance->last_cal_update_time = furi_get_tick();

+ 1 - 1
sensors/BMx280.h

@@ -1,6 +1,6 @@
 /*
     Unitemp - Universal temperature reader
-    Copyright (C) 2022  Victor Nikitchuk (https://github.com/quen0n)
+    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

+ 1 - 1
sensors/DHT20.c

@@ -1,6 +1,6 @@
 /*
     Unitemp - Universal temperature reader
-    Copyright (C) 2022  Victor Nikitchuk (https://github.com/quen0n)
+    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

+ 1 - 1
sensors/DHT20.h

@@ -1,6 +1,6 @@
 /*
     Unitemp - Universal temperature reader
-    Copyright (C) 2022  Victor Nikitchuk (https://github.com/quen0n)
+    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

+ 94 - 0
sensors/HDC1080.c

@@ -0,0 +1,94 @@
+/*
+    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/>.
+*/
+#include "HDC1080.h"
+#include "../interfaces/I2CSensor.h"
+
+const SensorType HDC1080 = {
+    .typename = "HDC1080",
+    .interface = &I2C,
+    .datatype = UT_DATA_TYPE_TEMP_HUM,
+    .pollingInterval = 250,
+    .allocator = unitemp_HDC1080_alloc,
+    .mem_releaser = unitemp_HDC1080_free,
+    .initializer = unitemp_HDC1080_init,
+    .deinitializer = unitemp_HDC1080_deinit,
+    .updater = unitemp_HDC1080_update};
+
+bool unitemp_HDC1080_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_HDC1080_free(Sensor* sensor) {
+    //Нечего высвобождать, так как ничего не было выделено
+    UNUSED(sensor);
+    return true;
+}
+
+bool unitemp_HDC1080_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 != 0x1050) {
+        FURI_LOG_E(
+            APP_NAME,
+            "Sensor %s returned wrong ID 0x%02X, expected 0x1050",
+            sensor->name,
+            device_id);
+        return false;
+    }
+    data[0] = 0b0001000;
+    data[1] = 0;
+    //Установка режима работы и разрядности измерений
+    if(!unitemp_i2c_writeRegArray(i2c_sensor, 0x02, 2, data)) return UT_SENSORSTATUS_TIMEOUT;
+
+    return true;
+}
+
+bool unitemp_HDC1080_deinit(Sensor* sensor) {
+    I2CSensor* i2c_sensor = (I2CSensor*)sensor->instance;
+    UNUSED(i2c_sensor);
+    return true;
+}
+
+UnitempStatus unitemp_HDC1080_update(Sensor* sensor) {
+    I2CSensor* i2c_sensor = (I2CSensor*)sensor->instance;
+
+    uint8_t data[2] = {0};
+    //Запуск измерения
+    if(!unitemp_i2c_writeArray(i2c_sensor, 1, data)) return UT_SENSORSTATUS_TIMEOUT;
+    furi_delay_ms(10);
+    if(!unitemp_i2c_readArray(i2c_sensor, 2, data)) return UT_SENSORSTATUS_TIMEOUT;
+
+    sensor->temp = ((float)(((uint16_t)data[0] << 8) | data[1]) / 65536) * 165 - 40;
+
+    data[0] = 1;
+    if(!unitemp_i2c_writeArray(i2c_sensor, 1, data)) return UT_SENSORSTATUS_TIMEOUT;
+    furi_delay_ms(10);
+    if(!unitemp_i2c_readArray(i2c_sensor, 2, data)) return UT_SENSORSTATUS_TIMEOUT;
+    sensor->hum = ((float)(((uint16_t)data[0] << 8) | data[1]) / 65536) * 100;
+
+    return UT_SENSORSTATUS_OK;
+}

+ 62 - 0
sensors/HDC1080.h

@@ -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_HDC1080
+#define UNITEMP_HDC1080
+
+#include "../unitemp.h"
+#include "../Sensors.h"
+extern const SensorType HDC1080;
+/**
+ * @brief Выделение памяти и установка начальных значений датчика HDC1080
+ *
+ * @param sensor Указатель на создаваемый датчик
+ * @return Истина при успехе
+ */
+bool unitemp_HDC1080_alloc(Sensor* sensor, char* args);
+
+/**
+ * @brief Инициализации датчика HDC1080
+ *
+ * @param sensor Указатель на датчик
+ * @return Истина если инициализация упспешная
+ */
+bool unitemp_HDC1080_init(Sensor* sensor);
+
+/**
+ * @brief Деинициализация датчика
+ *
+ * @param sensor Указатель на датчик
+ */
+bool unitemp_HDC1080_deinit(Sensor* sensor);
+
+/**
+ * @brief Обновление значений из датчика
+ *
+ * @param sensor Указатель на датчик
+ * @return Статус обновления
+ */
+UnitempStatus unitemp_HDC1080_update(Sensor* sensor);
+
+/**
+ * @brief Высвободить память датчика
+ *
+ * @param sensor Указатель на датчик
+ */
+bool unitemp_HDC1080_free(Sensor* sensor);
+
+#endif

+ 107 - 0
sensors/HTU21x.c

@@ -0,0 +1,107 @@
+/*
+    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/>.
+*/
+#include "HTU21x.h"
+#include "../interfaces/I2CSensor.h"
+
+const SensorType HTU21x = {
+    .typename = "HTU21x",
+    .altname = "HTU21x/SI70xx/SHT2x",
+    .interface = &I2C,
+    .datatype = UT_DATA_TYPE_TEMP_HUM,
+    .pollingInterval = 250,
+    .allocator = unitemp_HTU21x_alloc,
+    .mem_releaser = unitemp_HTU21x_free,
+    .initializer = unitemp_HTU21x_init,
+    .deinitializer = unitemp_HTU21x_deinit,
+    .updater = unitemp_HTU21x_update};
+
+static uint8_t checkCRC(uint16_t data) {
+    for(uint8_t i = 0; i < 16; i++) {
+        if(data & 0x8000)
+            data = (data << 1) ^ 0x13100;
+        else
+            data <<= 1;
+    }
+    return (data >> 8);
+}
+
+bool unitemp_HTU21x_alloc(Sensor* sensor, char* args) {
+    UNUSED(args);
+    I2CSensor* i2c_sensor = (I2CSensor*)sensor->instance;
+
+    //Адреса на шине I2C (7 бит)
+    i2c_sensor->minI2CAdr = 0x40 << 1;
+    i2c_sensor->maxI2CAdr = 0x41 << 1;
+    return true;
+}
+
+bool unitemp_HTU21x_free(Sensor* sensor) {
+    //Нечего высвобождать, так как ничего не было выделено
+    UNUSED(sensor);
+    return true;
+}
+
+bool unitemp_HTU21x_init(Sensor* sensor) {
+    I2CSensor* i2c_sensor = (I2CSensor*)sensor->instance;
+
+    UNUSED(i2c_sensor);
+    return true;
+}
+
+bool unitemp_HTU21x_deinit(Sensor* sensor) {
+    I2CSensor* i2c_sensor = (I2CSensor*)sensor->instance;
+    UNUSED(i2c_sensor);
+    return true;
+}
+
+UnitempStatus unitemp_HTU21x_update(Sensor* sensor) {
+    I2CSensor* i2c_sensor = (I2CSensor*)sensor->instance;
+
+    //Датчик может быть всего один, так что норм
+    static bool temp_hum = false;
+
+    uint8_t data[3];
+
+    if(sensor->status == UT_SENSORSTATUS_POLLING) {
+        if(!unitemp_i2c_readArray(i2c_sensor, 3, data)) return UT_SENSORSTATUS_TIMEOUT;
+
+        uint16_t raw = ((uint16_t)data[0] << 8) | data[1];
+        if(checkCRC(raw) != data[2]) return UT_SENSORSTATUS_BADCRC;
+
+        if(temp_hum) {
+            sensor->temp = (0.002681f * raw - 46.85f);
+        } else {
+            sensor->hum = ((0.001907 * (raw ^ 0x02)) - 6);
+        }
+        temp_hum = !temp_hum;
+        if(temp_hum) return UT_SENSORSTATUS_EARLYPOOL;
+        return UT_SENSORSTATUS_OK;
+    }
+
+    if(temp_hum) {
+        //Запрос температуры
+        data[0] = 0xF3;
+        if(!unitemp_i2c_writeArray(i2c_sensor, 1, data)) return UT_SENSORSTATUS_TIMEOUT;
+    } else {
+        //Запрос влажности
+        data[0] = 0xF5;
+        if(!unitemp_i2c_writeArray(i2c_sensor, 1, data)) return UT_SENSORSTATUS_TIMEOUT;
+    }
+
+    return UT_SENSORSTATUS_POLLING;
+}

+ 62 - 0
sensors/HTU21x.h

@@ -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_HTU21x
+#define UNITEMP_HTU21x
+
+#include "../unitemp.h"
+#include "../Sensors.h"
+extern const SensorType HTU21x;
+/**
+ * @brief Выделение памяти и установка начальных значений датчика HTU21x
+ *
+ * @param sensor Указатель на создаваемый датчик
+ * @return Истина при успехе
+ */
+bool unitemp_HTU21x_alloc(Sensor* sensor, char* args);
+
+/**
+ * @brief Инициализации датчика HTU21x
+ *
+ * @param sensor Указатель на датчик
+ * @return Истина если инициализация упспешная
+ */
+bool unitemp_HTU21x_init(Sensor* sensor);
+
+/**
+ * @brief Деинициализация датчика
+ *
+ * @param sensor Указатель на датчик
+ */
+bool unitemp_HTU21x_deinit(Sensor* sensor);
+
+/**
+ * @brief Обновление значений из датчика
+ *
+ * @param sensor Указатель на датчик
+ * @return Статус обновления
+ */
+UnitempStatus unitemp_HTU21x_update(Sensor* sensor);
+
+/**
+ * @brief Высвободить память датчика
+ *
+ * @param sensor Указатель на датчик
+ */
+bool unitemp_HTU21x_free(Sensor* sensor);
+
+#endif

+ 1 - 1
sensors/LM75.c

@@ -1,6 +1,6 @@
 /*
     Unitemp - Universal temperature reader
-    Copyright (C) 2022  Victor Nikitchuk (https://github.com/quen0n)
+    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

+ 1 - 1
sensors/LM75.h

@@ -1,6 +1,6 @@
 /*
     Unitemp - Universal temperature reader
-    Copyright (C) 2022  Victor Nikitchuk (https://github.com/quen0n)
+    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

+ 93 - 0
sensors/MAX31855.c

@@ -0,0 +1,93 @@
+/*
+    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 <https://www.gnu.org/licenses/>.
+*/
+#include "MAX31855.h"
+
+const SensorType MAX31855 = {
+    .typename = "MAX31855",
+    .altname = "MAX31855 (Thermocouple)",
+    .interface = &SPI,
+    .datatype = UT_TEMPERATURE,
+    .pollingInterval = 500,
+    .allocator = unitemp_MAX31855_alloc,
+    .mem_releaser = unitemp_MAX31855_free,
+    .initializer = unitemp_MAX31855_init,
+    .deinitializer = unitemp_MAX31855_deinit,
+    .updater = unitemp_MAX31855_update};
+
+bool unitemp_MAX31855_alloc(Sensor* sensor, char* args) {
+    UNUSED(sensor);
+    UNUSED(args);
+    return true;
+}
+
+bool unitemp_MAX31855_free(Sensor* sensor) {
+    UNUSED(sensor);
+    return true;
+}
+
+bool unitemp_MAX31855_init(Sensor* sensor) {
+    SPISensor* instance = sensor->instance;
+    furi_hal_spi_bus_handle_init(instance->spi);
+    UNUSED(instance);
+    return true;
+}
+
+bool unitemp_MAX31855_deinit(Sensor* sensor) {
+    UNUSED(sensor);
+    return true;
+}
+
+UnitempStatus unitemp_MAX31855_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[4] = {0};
+
+    furi_hal_spi_bus_rx(instance->spi, buff, 4, 0xFF);
+    furi_hal_spi_release(instance->spi);
+
+    uint32_t raw = (buff[0] << 24) | (buff[1] << 16) | (buff[2] << 8) | buff[3];
+
+    if(raw == 0xFFFFFFFF || raw == 0) return UT_SENSORSTATUS_TIMEOUT;
+
+    //Определение состояния термопары
+    uint8_t state = raw & 0b111;
+    //Обрыв
+    if(state == 0x01) {
+        UNITEMP_DEBUG("%s has thermocouple open circuit", sensor->name);
+        return UT_SENSORSTATUS_ERROR;
+    }
+    //Короткое замыкание к земле
+    if(state == 0x02) {
+        UNITEMP_DEBUG("%s has thermocouple short to GND", sensor->name);
+        return UT_SENSORSTATUS_ERROR;
+    }
+    //Короткое замыкание к питанию
+    if(state == 0x04) {
+        UNITEMP_DEBUG("%s has thermocouple short to VCC", sensor->name);
+        return UT_SENSORSTATUS_ERROR;
+    }
+
+    raw = (raw >> 16) & 0xFFFC;
+
+    sensor->temp = (int16_t)(raw) / 16.0f;
+
+    return UT_SENSORSTATUS_OK;
+}

+ 65 - 0
sensors/MAX31855.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 <https://www.gnu.org/licenses/>.
+*/
+#ifndef UNITEMP_MAX31855
+#define UNITEMP_MAX31855
+
+#include "../unitemp.h"
+#include "../Sensors.h"
+#include "../interfaces/SPISensor.h"
+
+extern const SensorType MAX31855;
+
+/**
+ * @brief Выделение памяти и установка начальных значений датчика MAX31855
+ *
+ * @param sensor Указатель на создаваемый датчик
+ * @return Истина при успехе
+ */
+bool unitemp_MAX31855_alloc(Sensor* sensor, char* args);
+
+/**
+ * @brief Инициализации датчика MAX31855
+ *
+ * @param sensor Указатель на датчик
+ * @return Истина если инициализация упспешная
+ */
+bool unitemp_MAX31855_init(Sensor* sensor);
+
+/**
+ * @brief Деинициализация датчика
+ *
+ * @param sensor Указатель на датчик
+ */
+bool unitemp_MAX31855_deinit(Sensor* sensor);
+
+/**
+ * @brief Обновление значений из датчика
+ *
+ * @param sensor Указатель на датчик
+ * @return Статус обновления
+ */
+UnitempStatus unitemp_MAX31855_update(Sensor* sensor);
+
+/**
+ * @brief Высвободить память датчика
+ *
+ * @param sensor Указатель на датчик
+ */
+bool unitemp_MAX31855_free(Sensor* sensor);
+
+#endif

+ 83 - 0
sensors/MAX6675.c

@@ -0,0 +1,83 @@
+/*
+    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 <https://www.gnu.org/licenses/>.
+*/
+#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;
+
+    UNITEMP_DEBUG("RAW: %04lX", raw);
+
+    return UT_SENSORSTATUS_OK;
+}

+ 65 - 0
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 <https://www.gnu.org/licenses/>.
+*/
+#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

+ 1 - 1
sensors/SHT30.c

@@ -1,6 +1,6 @@
 /*
     Unitemp - Universal temperature reader
-    Copyright (C) 2022  Victor Nikitchuk (https://github.com/quen0n)
+    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

+ 1 - 1
sensors/SHT30.h

@@ -1,6 +1,6 @@
 /*
     Unitemp - Universal temperature reader
-    Copyright (C) 2022  Victor Nikitchuk (https://github.com/quen0n)
+    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

BIN
sensors/Sensors.xlsx


+ 4 - 4
unitemp.c

@@ -1,6 +1,6 @@
 /*
     Unitemp - Universal temperature reader
-    Copyright (C) 2022  Victor Nikitchuk (https://github.com/quen0n)
+    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
@@ -78,9 +78,7 @@ bool unitemp_saveSettings(void) {
 }
 
 bool unitemp_loadSettings(void) {
-#ifdef UNITEMP_DEBUG
-    FURI_LOG_D(APP_NAME, "Loading settings...");
-#endif
+    UNITEMP_DEBUG("Loading settings...");
 
     //Выделение памяти на поток
     app->file_stream = file_stream_alloc(app->storage);
@@ -235,6 +233,8 @@ static bool unitemp_alloc(void) {
  */
 static void unitemp_free(void) {
     popup_free(app->popup);
+    //Удаление вида после обработки
+    view_dispatcher_remove_view(app->view_dispatcher, UnitempViewPopup);
     unitemp_widgets_free();
 
     unitemp_SensorActions_free();

+ 10 - 4
unitemp.h

@@ -1,6 +1,6 @@
 /*
     Unitemp - Universal temperature reader
-    Copyright (C) 2022  Victor Nikitchuk (https://github.com/quen0n)
+    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
@@ -39,19 +39,25 @@
 /* Объявление макроподстановок */
 //Имя приложения
 #define APP_NAME "Unitemp"
+//Версия приложения
+#define UNITEMP_APP_VER "1.1.2-dev"
 //Путь хранения файлов плагина
 #define APP_PATH_FOLDER "/ext/unitemp"
 //Имя файла с настройками
 #define APP_FILENAME_SETTINGS "settings.cfg"
 //Имя файла с датчиками
 #define APP_FILENAME_SENSORS "sensors.cfg"
-//Версия приложения
-#define UNITEMP_APP_VER "1.0"
 
 //Размер буффера текста
 #define BUFF_SIZE 32
 
-#define UNITEMP_DEBUG
+#define UNITEMP_D
+
+#ifdef FURI_DEBUG
+#define UNITEMP_DEBUG(msg, ...) FURI_LOG_D(APP_NAME, msg, ##__VA_ARGS__)
+#else
+#define UNITEMP_DEBUG(msg, ...)
+#endif
 
 /* Объявление перечислений */
 //Единицы измерения температуры

+ 32 - 2
views/General_view.c

@@ -1,6 +1,6 @@
 /*
     Unitemp - Universal temperature reader
-    Copyright (C) 2022  Victor Nikitchuk (https://github.com/quen0n)
+    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
@@ -76,7 +76,7 @@ static void _draw_temperature(Canvas* canvas, Sensor* sensor, uint8_t x, uint8_t
         app->buff[0] = '-';
         offset = 1;
     }
-    snprintf((char*)(app->buff + offset), BUFF_SIZE, "%d", (int8_t)sensor->temp);
+    snprintf((char*)(app->buff + offset), BUFF_SIZE, "%d", (int16_t)sensor->temp);
     canvas_set_font(canvas, FontBigNumbers);
     canvas_draw_str_aligned(
         canvas,
@@ -237,6 +237,7 @@ static void _draw_carousel_values(Canvas* canvas) {
         canvas_draw_icon(canvas, 34, 23, frames[furi_get_tick() % 2250 / 750]);
 
         canvas_set_font(canvas, FontSecondary);
+        //TODO: Оптимизировать эту срань
         if(unitemp_sensor_getActive(generalview_sensor_index)->type->interface == &SINGLE_WIRE) {
             snprintf(
                 app->buff,
@@ -256,6 +257,9 @@ static void _draw_carousel_values(Canvas* canvas) {
         if(unitemp_sensor_getActive(generalview_sensor_index)->type->interface == &I2C) {
             snprintf(app->buff, BUFF_SIZE, "Waiting for module on I2C pins");
         }
+        if(unitemp_sensor_getActive(generalview_sensor_index)->type->interface == &SPI) {
+            snprintf(app->buff, BUFF_SIZE, "Waiting for module on SPI pins");
+        }
         canvas_draw_str_aligned(canvas, 64, 19, AlignCenter, AlignCenter, app->buff);
         return;
     }
@@ -304,6 +308,8 @@ static void _draw_carousel_values(Canvas* canvas) {
         break;
     }
 }
+
+//TODO: Оптимизировать вывод информации
 static void _draw_carousel_info(Canvas* canvas) {
     canvas_set_font(canvas, FontPrimary);
     canvas_draw_str(canvas, 10, 23, "Type:");
@@ -351,6 +357,25 @@ static void _draw_carousel_info(Canvas* canvas) {
                 ->gpio->name);
     }
 
+    if(unitemp_sensor_getActive(generalview_sensor_index)->type->interface == &SPI) {
+        canvas_set_font(canvas, FontPrimary);
+        canvas_draw_str(canvas, 10, 35, "MISO pin:");
+        canvas_draw_str(canvas, 10, 46, "CS pin:");
+        canvas_draw_str(canvas, 10, 58, "SCK pin:");
+
+        canvas_set_font(canvas, FontSecondary);
+        canvas_draw_str(
+            canvas, 41, 23, unitemp_sensor_getActive(generalview_sensor_index)->type->typename);
+        canvas_draw_str(canvas, 60, 35, unitemp_gpio_getFromInt(3)->name);
+        canvas_draw_str(
+            canvas,
+            47,
+            46,
+            ((SPISensor*)unitemp_sensor_getActive(generalview_sensor_index)->instance)
+                ->CS_pin->name);
+        canvas_draw_str(canvas, 54, 58, unitemp_gpio_getFromInt(5)->name);
+    }
+
     if(unitemp_sensor_getActive(generalview_sensor_index)->type->interface == &I2C) {
         canvas_set_font(canvas, FontPrimary);
         canvas_draw_str(canvas, 10, 35, "I2C addr:");
@@ -542,6 +567,10 @@ static bool _input_callback(InputEvent* event, void* context) {
             return true;
         }
     }
+    //Обработка длинного нажатия "Ок"
+    if(event->key == InputKeyOk && event->type == InputTypeLong) {
+        app->settings.temp_unit = !app->settings.temp_unit;
+    }
 
     return true;
 }
@@ -561,5 +590,6 @@ void unitemp_General_switch(void) {
 }
 
 void unitemp_General_free(void) {
+    view_dispatcher_remove_view(app->view_dispatcher, UnitempViewGeneral);
     view_free(view);
 }

+ 1 - 1
views/MainMenu_view.c

@@ -1,6 +1,6 @@
 /*
     Unitemp - Universal temperature reader
-    Copyright (C) 2022  Victor Nikitchuk (https://github.com/quen0n)
+    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

+ 1 - 1
views/Popup_view.c

@@ -1,6 +1,6 @@
 /*
     Unitemp - Universal temperature reader
-    Copyright (C) 2022  Victor Nikitchuk (https://github.com/quen0n)
+    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

+ 1 - 1
views/SensorActions_view.c

@@ -1,6 +1,6 @@
 /*
     Unitemp - Universal temperature reader
-    Copyright (C) 2022  Victor Nikitchuk (https://github.com/quen0n)
+    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

+ 18 - 15
views/SensorEdit_view.c

@@ -1,6 +1,6 @@
 /*
     Unitemp - Universal temperature reader
-    Copyright (C) 2022  Victor Nikitchuk (https://github.com/quen0n)
+    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
@@ -63,13 +63,9 @@ bool _onewire_id_exist(uint8_t* id) {
 
 static void _onewire_scan(void) {
     OneWireSensor* ow_sensor = editable_sensor->instance;
-#ifdef UNITEMP_DEBUG
-    FURI_LOG_D(
-        APP_NAME,
-        "devices on wire %d: %d",
-        ow_sensor->bus->gpio->num,
-        ow_sensor->bus->device_count);
-#endif
+
+    UNITEMP_DEBUG(
+        "devices on wire %d: %d", ow_sensor->bus->gpio->num, ow_sensor->bus->device_count);
 
     //Сканирование шины one wire
     unitemp_onewire_bus_init(ow_sensor->bus);
@@ -101,9 +97,8 @@ static void _onewire_scan(void) {
 
     memcpy(ow_sensor->deviceID, id, 8);
     ow_sensor->familyCode = id[0];
-#ifdef UNITEMP_DEBUG
-    FURI_LOG_D(
-        APP_NAME,
+
+    UNITEMP_DEBUG(
         "Found sensor's ID: %02X%02X%02X%02X%02X%02X%02X%02X",
         id[0],
         id[1],
@@ -113,7 +108,6 @@ static void _onewire_scan(void) {
         id[5],
         id[6],
         id[7]);
-#endif
 
     if(ow_sensor->familyCode != 0) {
         char id_buff[10];
@@ -199,6 +193,12 @@ static void _gpio_change_callback(VariableItem* item) {
             unitemp_gpio_getAviablePort(editable_sensor->type->interface, index, initial_gpio);
         variable_item_set_current_value_text(item, instance->gpio->name);
     }
+    if(editable_sensor->type->interface == &SPI) {
+        SPISensor* instance = editable_sensor->instance;
+        instance->CS_pin =
+            unitemp_gpio_getAviablePort(editable_sensor->type->interface, index, initial_gpio);
+        variable_item_set_current_value_text(item, instance->CS_pin->name);
+    }
     if(editable_sensor->type->interface == &ONE_WIRE) {
         OneWireSensor* instance = editable_sensor->instance;
         instance->bus->gpio =
@@ -302,12 +302,15 @@ void unitemp_SensorEdit_switch(Sensor* sensor) {
         offset_buff, OFFSET_BUFF_SIZE, "%+1.1f", (double)(editable_sensor->temp_offset / 10.0));
     variable_item_set_current_value_text(temp_offset_item, offset_buff);
 
-    //Порт подключения датчка (для one wire и single wire)
-    if(sensor->type->interface == &ONE_WIRE || sensor->type->interface == &SINGLE_WIRE) {
+    //Порт подключения датчка (для one wire, SPI и single wire)
+    if(sensor->type->interface == &ONE_WIRE || sensor->type->interface == &SINGLE_WIRE ||
+       sensor->type->interface == &SPI) {
         if(sensor->type->interface == &ONE_WIRE) {
             initial_gpio = ((OneWireSensor*)editable_sensor->instance)->bus->gpio;
-        } else {
+        } else if(sensor->type->interface == &SINGLE_WIRE) {
             initial_gpio = ((SingleWireSensor*)editable_sensor->instance)->gpio;
+        } else if(sensor->type->interface == &SPI) {
+            initial_gpio = ((SPISensor*)editable_sensor->instance)->CS_pin;
         }
 
         uint8_t aviable_gpio_count =

+ 2 - 1
views/SensorNameEdit_view.c

@@ -1,6 +1,6 @@
 /*
     Unitemp - Universal temperature reader
-    Copyright (C) 2022  Victor Nikitchuk (https://github.com/quen0n)
+    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
@@ -42,5 +42,6 @@ void unitemp_SensorNameEdit_switch(Sensor* sensor) {
     view_dispatcher_switch_to_view(app->view_dispatcher, VIEW_ID);
 }
 void unitemp_SensorNameEdit_free(void) {
+    view_dispatcher_remove_view(app->view_dispatcher, VIEW_ID);
     text_input_free(text_input);
 }

+ 3 - 3
views/SensorsList_view.c

@@ -1,6 +1,6 @@
 /*
     Unitemp - Universal temperature reader
-    Copyright (C) 2022  Victor Nikitchuk (https://github.com/quen0n)
+    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
@@ -86,8 +86,8 @@ static void _enter_callback(void* context, uint32_t index) {
         return;
     }
 
-    //Выбор первого доступного порта для датчика single wire
-    if(type->interface == &SINGLE_WIRE) {
+    //Выбор первого доступного порта для датчика single wire и SPI
+    if(type->interface == &SINGLE_WIRE || type->interface == &SPI) {
         snprintf(
             args,
             4,

+ 1 - 1
views/Settings_view.c

@@ -1,6 +1,6 @@
 /*
     Unitemp - Universal temperature reader
-    Copyright (C) 2022  Victor Nikitchuk (https://github.com/quen0n)
+    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

+ 1 - 1
views/UnitempViews.h

@@ -1,6 +1,6 @@
 /*
     Unitemp - Universal temperature reader
-    Copyright (C) 2022  Victor Nikitchuk (https://github.com/quen0n)
+    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

+ 2 - 1
views/Widgets_view.c

@@ -1,6 +1,6 @@
 /*
     Unitemp - Universal temperature reader
-    Copyright (C) 2022  Victor Nikitchuk (https://github.com/quen0n)
+    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
@@ -27,6 +27,7 @@ void unitemp_widgets_alloc(void) {
 }
 
 void unitemp_widgets_free(void) {
+    view_dispatcher_remove_view(app->view_dispatcher, UnitempViewWidget);
     widget_free(app->widget);
 }