Sfoglia il codice sorgente

Sensors API update

Victor 3 anni fa
parent
commit
30c79d75c6
7 ha cambiato i file con 57 aggiunte e 45 eliminazioni
  1. 28 22
      Sensors.c
  2. 17 12
      Sensors.h
  3. 3 2
      interfaces/I2CSensor.c
  4. 2 2
      interfaces/I2CSensor.h
  5. 5 5
      interfaces/SingleWireSensor.c
  6. 1 1
      sensors/BMP280.c
  7. 1 1
      sensors/LM75.c

+ 28 - 22
Sensors.c

@@ -13,12 +13,13 @@ const GpioPin RX_14 = {.pin = LL_GPIO_PIN_7, .port = GPIOB};
 
 
 //Количество доступных портов ввода/вывода
 //Количество доступных портов ввода/вывода
 #define GPIO_ITEMS (sizeof(GPIOList) / sizeof(GPIO))
 #define GPIO_ITEMS (sizeof(GPIOList) / sizeof(GPIO))
+//Количество интерфейсов
+#define INTERFACES_TYPES_COUNT (int)(sizeof(interfaces) / sizeof(const Interface*))
 //Количество типов датчиков
 //Количество типов датчиков
 #define SENSOR_TYPES_COUNT (int)(sizeof(sensorTypes) / sizeof(const SensorType*))
 #define SENSOR_TYPES_COUNT (int)(sizeof(sensorTypes) / sizeof(const SensorType*))
 
 
 //Перечень достуных портов ввода/вывода
 //Перечень достуных портов ввода/вывода
-//static
-const GPIO GPIOList[] = {
+static const GPIO GPIOList[] = {
     {2, "2 (A7)", &gpio_ext_pa7},
     {2, "2 (A7)", &gpio_ext_pa7},
     {3, "3 (A6)", &gpio_ext_pa6},
     {3, "3 (A6)", &gpio_ext_pa6},
     {4, "4 (A4)", &gpio_ext_pa4},
     {4, "4 (A4)", &gpio_ext_pa4},
@@ -33,6 +34,19 @@ const GPIO GPIOList[] = {
     {16, "16 (C0)", &gpio_ext_pc0},
     {16, "16 (C0)", &gpio_ext_pc0},
     {17, "17 (1W)", &ibutton_gpio}};
     {17, "17 (1W)", &ibutton_gpio}};
 
 
+const Interface SINGLE_WIRE = {
+    .name = "Single wire",
+    .allocator = unitemp_singleWire_alloc,
+    .mem_releaser = unitemp_singleWire_free,
+    .updater = unitemp_singleWire_update};
+const Interface I2C = {
+    .name = "I2C",
+    .allocator = unitemp_I2C_sensor_alloc,
+    .mem_releaser = unitemp_I2C_sensor_free,
+    .updater = unitemp_I2C_sensor_update};
+
+//Перечень интерфейсов подключения
+//static const Interface* interfaces[] = {&SINGLE_WIRE, &I2C};
 //Перечень датчиков
 //Перечень датчиков
 static const SensorType* sensorTypes[] =
 static const SensorType* sensorTypes[] =
     {&DHT11, &DHT12_SW, &DHT21, &DHT22, &AM2320_SW, &LM75, &BMP280};
     {&DHT11, &DHT12_SW, &DHT21, &DHT22, &AM2320_SW, &LM75, &BMP280};
@@ -189,7 +203,7 @@ bool unitemp_sensors_save(void) {
 
 
     //Сохранение датчиков
     //Сохранение датчиков
     for(size_t i = 0; i < app->sensors_count; i++) {
     for(size_t i = 0; i < app->sensors_count; i++) {
-        if(app->sensors[i]->type->interface == SINGLE_WIRE) {
+        if(app->sensors[i]->type->interface == &SINGLE_WIRE) {
             stream_write_format(
             stream_write_format(
                 app->file_stream,
                 app->file_stream,
                 "%s %d %d\n",
                 "%s %d %d\n",
@@ -197,6 +211,14 @@ bool unitemp_sensors_save(void) {
                 unitemp_getIntFromType(app->sensors[i]->type),
                 unitemp_getIntFromType(app->sensors[i]->type),
                 unitemp_GPIO_toInt(unitemp_singleWire_sensorGetGPIO(app->sensors[i])->pin));
                 unitemp_GPIO_toInt(unitemp_singleWire_sensorGetGPIO(app->sensors[i])->pin));
         }
         }
+        if(app->sensors[i]->type->interface == &I2C) {
+            stream_write_format(
+                app->file_stream,
+                "%s %d %d\n",
+                app->sensors[i]->name,
+                unitemp_getIntFromType(app->sensors[i]->type),
+                ((I2CSensor*)app->sensors[i]->instance)->currentI2CAdr);
+        }
     }
     }
 
 
     //Закрытие потока и освобождение памяти
     //Закрытие потока и освобождение памяти
@@ -236,12 +258,7 @@ Sensor* unitemp_sensor_alloc(char* name, const SensorType* type, uint16_t* anoth
     sensor->hum = -128.0f;
     sensor->hum = -128.0f;
 
 
     //Выделение памяти под инстанс датчика в зависимости от его интерфейса
     //Выделение памяти под инстанс датчика в зависимости от его интерфейса
-    if(sensor->type->interface == SINGLE_WIRE) {
-        status = sensor->type->allocator(sensor, anotherValues);
-    }
-    if(sensor->type->interface == I2C) {
-        status = unitemp_I2C_sensorAlloc(sensor, anotherValues);
-    }
+    status = sensor->type->interface->allocator(sensor, anotherValues);
 
 
     //Если датчик успешно развёрнут, то добавление его в общий список и выход
     //Если датчик успешно развёрнут, то добавление его в общий список и выход
     if(status) {
     if(status) {
@@ -270,13 +287,7 @@ void unitemp_sensor_free(Sensor* sensor) {
     }
     }
     bool status = false;
     bool status = false;
     //Высвобождение памяти под инстанс
     //Высвобождение памяти под инстанс
-    if(sensor->type->interface == SINGLE_WIRE) {
-        status = sensor->type->mem_releaser(sensor);
-    }
-
-    if(sensor->type->interface == I2C) {
-        status = unitemp_I2C_sensorFree(sensor);
-    }
+    status = sensor->type->interface->mem_releaser(sensor);
     if(status) {
     if(status) {
         FURI_LOG_D(APP_NAME, "Sensor %s memory successfully released", sensor->name);
         FURI_LOG_D(APP_NAME, "Sensor %s memory successfully released", sensor->name);
     } else {
     } else {
@@ -355,12 +366,7 @@ UnitempStatus unitemp_sensor_updateData(Sensor* sensor) {
         furi_hal_power_enable_otg();
         furi_hal_power_enable_otg();
     }
     }
 
 
-    if(sensor->type->interface == I2C) {
-        sensor->status = unitemp_I2C_sensor_update(sensor);
-    }
-    if(sensor->type->interface == SINGLE_WIRE) {
-        sensor->status = sensor->type->updater(sensor);
-    }
+    sensor->status = sensor->type->interface->updater(sensor);
 
 
     FURI_LOG_D(APP_NAME, "Sensor %s update status %d", sensor->name, sensor->status);
     FURI_LOG_D(APP_NAME, "Sensor %s update status %d", sensor->name, sensor->status);
     if(app->settings.unit == FAHRENHEIT && sensor->status == UT_OK)
     if(app->settings.unit == FAHRENHEIT && sensor->status == UT_OK)

+ 17 - 12
Sensors.h

@@ -12,16 +12,6 @@ typedef enum {
     UT_ERROR, //Прочие ошибки
     UT_ERROR, //Прочие ошибки
 } UnitempStatus;
 } UnitempStatus;
 
 
-//Типы подключения датчиков
-typedef enum Interface {
-    SINGLE_WIRE, //Собственный однопроводной протокол датчиков DHTXX и AM23XX
-    ONE_WIRE, //Однопроводной протокол Dallas
-    I2C,
-    SPI,
-
-    CONNECTION_TYPES_COUNT //Общее количество типов подключений
-} Interface;
-
 //Порт ввода/вывода Flipper Zero
 //Порт ввода/вывода Flipper Zero
 typedef struct GPIO {
 typedef struct GPIO {
     const uint8_t num;
     const uint8_t num;
@@ -50,12 +40,22 @@ typedef bool(SensorDeinitializer)(void* sensor);
  */
  */
 typedef UnitempStatus(SensorUpdater)(void* sensor);
 typedef UnitempStatus(SensorUpdater)(void* sensor);
 
 
+//Типы подключения датчиков
+typedef struct Interface {
+    const char* name;
+    SensorAllocator* allocator;
+    //Функция высвыбождения памяти для датчика
+    SensorFree* mem_releaser;
+    //Функция обновления значения датчка
+    SensorUpdater* updater;
+} Interface;
+
 //Типы датчиков
 //Типы датчиков
 typedef struct {
 typedef struct {
     //Имя типа датчика
     //Имя типа датчика
-    char* typename;
+    const char* typename;
     //Интерфейс подключения
     //Интерфейс подключения
-    Interface interface;
+    const Interface* interface;
     //Интервал опроса датчика
     //Интервал опроса датчика
     uint16_t pollingInterval;
     uint16_t pollingInterval;
     //Функция выделения памяти для датчика
     //Функция выделения памяти для датчика
@@ -90,6 +90,11 @@ typedef struct Sensor {
 
 
 } Sensor;
 } 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;
+
 /**
 /**
  * @brief Конвертация номера порта на корпусе FZ в GPIO 
  * @brief Конвертация номера порта на корпусе FZ в GPIO 
  * 
  * 

+ 3 - 2
interfaces/I2CSensor.c

@@ -27,7 +27,7 @@ bool writeReg(I2CSensor* i2c_sensor, uint8_t reg, uint8_t value) {
     return status;
     return status;
 }
 }
 
 
-bool unitemp_I2C_sensorAlloc(void* s, uint16_t* anotherValues) {
+bool unitemp_I2C_sensor_alloc(void* s, uint16_t* anotherValues) {
     Sensor* sensor = (Sensor*)s;
     Sensor* sensor = (Sensor*)s;
     bool status = false;
     bool status = false;
     I2CSensor* instance = malloc(sizeof(I2CSensor));
     I2CSensor* instance = malloc(sizeof(I2CSensor));
@@ -50,7 +50,8 @@ bool unitemp_I2C_sensorAlloc(void* s, uint16_t* anotherValues) {
     return status;
     return status;
 }
 }
 
 
-bool unitemp_I2C_sensorFree(Sensor* sensor) {
+bool unitemp_I2C_sensor_free(void* s) {
+    Sensor* sensor = (Sensor*)s;
     bool status = sensor->type->mem_releaser(sensor);
     bool status = sensor->type->mem_releaser(sensor);
     free(sensor->instance);
     free(sensor->instance);
     return status;
     return status;

+ 2 - 2
interfaces/I2CSensor.h

@@ -26,14 +26,14 @@ typedef struct I2CSensor {
  * @param st Тип датчика
  * @param st Тип датчика
  * @return Истина если всё ок
  * @return Истина если всё ок
  */
  */
-bool unitemp_I2C_sensorAlloc(void* s, uint16_t* anotherValues);
+bool unitemp_I2C_sensor_alloc(void* s, uint16_t* anotherValues);
 
 
 /**
 /**
  * @brief Высвобождение памяти инстанса датчика
  * @brief Высвобождение памяти инстанса датчика
  * 
  * 
  * @param sensor Указатель на датчик
  * @param sensor Указатель на датчик
  */
  */
-bool unitemp_I2C_sensorFree(Sensor* sensor);
+bool unitemp_I2C_sensor_free(void* sensor);
 
 
 /**
 /**
  * @brief Обновить значение с датчка
  * @brief Обновить значение с датчка

+ 5 - 5
interfaces/SingleWireSensor.c

@@ -10,7 +10,7 @@
 //DHT11
 //DHT11
 const SensorType DHT11 = {
 const SensorType DHT11 = {
     .typename = "DHT11",
     .typename = "DHT11",
-    .interface = SINGLE_WIRE,
+    .interface = &SINGLE_WIRE,
     .pollingInterval = 2000,
     .pollingInterval = 2000,
     .allocator = unitemp_singleWire_alloc,
     .allocator = unitemp_singleWire_alloc,
     .mem_releaser = unitemp_singleWire_free,
     .mem_releaser = unitemp_singleWire_free,
@@ -19,7 +19,7 @@ const SensorType DHT11 = {
     .updater = unitemp_singleWire_update};
     .updater = unitemp_singleWire_update};
 const SensorType DHT12_SW = {
 const SensorType DHT12_SW = {
     .typename = "DHT12 (1 Wire)",
     .typename = "DHT12 (1 Wire)",
-    .interface = SINGLE_WIRE,
+    .interface = &SINGLE_WIRE,
     .pollingInterval = 2000,
     .pollingInterval = 2000,
     .allocator = unitemp_singleWire_alloc,
     .allocator = unitemp_singleWire_alloc,
     .mem_releaser = unitemp_singleWire_free,
     .mem_releaser = unitemp_singleWire_free,
@@ -29,7 +29,7 @@ const SensorType DHT12_SW = {
 
 
 const SensorType DHT21 = {
 const SensorType DHT21 = {
     .typename = "DHT21",
     .typename = "DHT21",
-    .interface = SINGLE_WIRE,
+    .interface = &SINGLE_WIRE,
     .pollingInterval = 2000,
     .pollingInterval = 2000,
     .allocator = unitemp_singleWire_alloc,
     .allocator = unitemp_singleWire_alloc,
     .mem_releaser = unitemp_singleWire_free,
     .mem_releaser = unitemp_singleWire_free,
@@ -38,7 +38,7 @@ const SensorType DHT21 = {
     .updater = unitemp_singleWire_update};
     .updater = unitemp_singleWire_update};
 const SensorType DHT22 = {
 const SensorType DHT22 = {
     .typename = "DHT22",
     .typename = "DHT22",
-    .interface = SINGLE_WIRE,
+    .interface = &SINGLE_WIRE,
     .pollingInterval = 2000,
     .pollingInterval = 2000,
     .allocator = unitemp_singleWire_alloc,
     .allocator = unitemp_singleWire_alloc,
     .mem_releaser = unitemp_singleWire_free,
     .mem_releaser = unitemp_singleWire_free,
@@ -47,7 +47,7 @@ const SensorType DHT22 = {
     .updater = unitemp_singleWire_update};
     .updater = unitemp_singleWire_update};
 const SensorType AM2320_SW = {
 const SensorType AM2320_SW = {
     .typename = "AM2320 (1 Wire)",
     .typename = "AM2320 (1 Wire)",
-    .interface = SINGLE_WIRE,
+    .interface = &SINGLE_WIRE,
     .pollingInterval = 2000,
     .pollingInterval = 2000,
     .allocator = unitemp_singleWire_alloc,
     .allocator = unitemp_singleWire_alloc,
     .mem_releaser = unitemp_singleWire_free,
     .mem_releaser = unitemp_singleWire_free,

+ 1 - 1
sensors/BMP280.c

@@ -3,7 +3,7 @@
 
 
 const SensorType BMP280 = {
 const SensorType BMP280 = {
     .typename = "BMP280",
     .typename = "BMP280",
-    .interface = I2C,
+    .interface = &I2C,
     .pollingInterval = 500,
     .pollingInterval = 500,
     .allocator = unitemp_BMP280_alloc,
     .allocator = unitemp_BMP280_alloc,
     .mem_releaser = unitemp_BMP280_free,
     .mem_releaser = unitemp_BMP280_free,

+ 1 - 1
sensors/LM75.c

@@ -15,7 +15,7 @@
 
 
 const SensorType LM75 = {
 const SensorType LM75 = {
     .typename = "LM75",
     .typename = "LM75",
-    .interface = I2C,
+    .interface = &I2C,
     .pollingInterval = 500,
     .pollingInterval = 500,
     .allocator = unitemp_LM75_alloc,
     .allocator = unitemp_LM75_alloc,
     .mem_releaser = unitemp_LM75_free,
     .mem_releaser = unitemp_LM75_free,