From 19edea21d7cd2d02b116e5441bc5e4a8cc154dd7 Mon Sep 17 00:00:00 2001 From: Wang_Weigen Date: Wed, 15 Dec 2021 15:01:30 +0800 Subject: [PATCH] add 'TB600B_TVOC10' sensor of tvoc to sensor framework --- APP_Framework/Applications/framework_init.c | 5 + APP_Framework/Applications/sensor_app/Kconfig | 10 + .../Applications/sensor_app/Makefile | 4 + .../sensor_app/tvoc_tb600b_tvoc10.c | 39 +++ APP_Framework/Framework/sensor/Kconfig | 7 + APP_Framework/Framework/sensor/Makefile | 4 + .../sensor/iaq/tb600b_iaq10/tb600b_iaq10.c | 10 +- APP_Framework/Framework/sensor/sensor.h | 2 + APP_Framework/Framework/sensor/tvoc/Kconfig | 43 ++++ APP_Framework/Framework/sensor/tvoc/Makefile | 5 + .../sensor/tvoc/tb600b_tvoc10/Makefile | 3 + .../sensor/tvoc/tb600b_tvoc10/tb600b_tvoc10.c | 227 ++++++++++++++++++ 12 files changed, 354 insertions(+), 5 deletions(-) create mode 100644 APP_Framework/Applications/sensor_app/tvoc_tb600b_tvoc10.c create mode 100644 APP_Framework/Framework/sensor/tvoc/Kconfig create mode 100644 APP_Framework/Framework/sensor/tvoc/Makefile create mode 100644 APP_Framework/Framework/sensor/tvoc/tb600b_tvoc10/Makefile create mode 100644 APP_Framework/Framework/sensor/tvoc/tb600b_tvoc10/tb600b_tvoc10.c diff --git a/APP_Framework/Applications/framework_init.c b/APP_Framework/Applications/framework_init.c index be2cfbb98..6ba1d6978 100644 --- a/APP_Framework/Applications/framework_init.c +++ b/APP_Framework/Applications/framework_init.c @@ -31,6 +31,7 @@ extern int Ps5308Pm1_0Init(void); extern int Zg09Co2Init(void); extern int As830Ch4Init(void); extern int Tb600bIaq10IaqInit(void); +extern int Tb600bTvoc10TvocInit(void); typedef int (*InitFunc)(void); struct InitDesc @@ -99,6 +100,10 @@ static struct InitDesc sensor_desc[] = { "iaq_tb600b_iaq10", Tb600bIaq10IaqInit }, #endif +#ifdef SENSOR_TB600B_TVOC10 + { "tvoc_tb600b_tvoc10", Tb600bTvoc10TvocInit }, +#endif + { "NULL", NULL }, }; diff --git a/APP_Framework/Applications/sensor_app/Kconfig b/APP_Framework/Applications/sensor_app/Kconfig index ad47611d0..9dafba604 100755 --- a/APP_Framework/Applications/sensor_app/Kconfig +++ b/APP_Framework/Applications/sensor_app/Kconfig @@ -5,6 +5,16 @@ menu "sensor app" default n if APPLICATION_SENSOR + menuconfig APPLICATION_SENSOR_TVOC + bool "Using sensor TVOC apps" + default n + + if APPLICATION_SENSOR_TVOC + config APPLICATION_SENSOR_TVOC_TB600B_TVOC10 + bool "Using sensor TB600B_TVOC10 apps" + default n + endif + menuconfig APPLICATION_SENSOR_IAQ bool "Using sensor IAQ apps" default n diff --git a/APP_Framework/Applications/sensor_app/Makefile b/APP_Framework/Applications/sensor_app/Makefile index 832a15126..d457b729a 100644 --- a/APP_Framework/Applications/sensor_app/Makefile +++ b/APP_Framework/Applications/sensor_app/Makefile @@ -1,5 +1,9 @@ SRC_FILES := +ifeq ($(CONFIG_APPLICATION_SENSOR_TVOC_TB600B_TVOC10), y) + SRC_FILES += tvoc_tb600b_tvoc10.c +endif + ifeq ($(CONFIG_APPLICATION_SENSOR_IAQ_TB600B_IAQ10), y) SRC_FILES += iaq_tb600b_iaq10.c endif diff --git a/APP_Framework/Applications/sensor_app/tvoc_tb600b_tvoc10.c b/APP_Framework/Applications/sensor_app/tvoc_tb600b_tvoc10.c new file mode 100644 index 000000000..6787717d7 --- /dev/null +++ b/APP_Framework/Applications/sensor_app/tvoc_tb600b_tvoc10.c @@ -0,0 +1,39 @@ +/* +* Copyright (c) 2020 AIIT XUOS Lab +* XiUOS is licensed under Mulan PSL v2. +* You can use this software according to the terms and conditions of the Mulan PSL v2. +* You may obtain a copy of Mulan PSL v2 at: +* http://license.coscl.org.cn/MulanPSL2 +* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, +* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, +* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. +* See the Mulan PSL v2 for more details. +*/ + +/** + * @file tvoc_tb600b_tvoc10.c + * @brief tvoc10 example + * @version 1.0 + * @author AIIT XUOS Lab + * @date 2021.12.15 + */ + +#include +#include + + +/** + * @description: Read a tvoc + * @return 0 + */ +void TvocTb600bTvoc10(void) +{ + struct SensorQuantity *tvoc = SensorQuantityFind(SENSOR_QUANTITY_TB600B_TVOC, SENSOR_QUANTITY_TVOC); + SensorQuantityOpen(tvoc); + int32_t result = 0; + + result = SensorQuantityRead(tvoc); + + printf("tvoc concentration is : %dppb\n", result); + SensorQuantityClose(tvoc); +} \ No newline at end of file diff --git a/APP_Framework/Framework/sensor/Kconfig b/APP_Framework/Framework/sensor/Kconfig index 334eb3f65..f65b6cc68 100644 --- a/APP_Framework/Framework/sensor/Kconfig +++ b/APP_Framework/Framework/sensor/Kconfig @@ -4,6 +4,13 @@ menuconfig SUPPORT_SENSOR_FRAMEWORK select TRANSFORM_LAYER_ATTRIUBUTE if SUPPORT_SENSOR_FRAMEWORK + menuconfig SENSOR_TVOC + bool "Using TVOC sensor device" + default n + if SENSOR_TVOC + source "$APP_DIR/Framework/sensor/tvoc/Kconfig" + endif + menuconfig SENSOR_IAQ bool "Using IAQ sensor device" default n diff --git a/APP_Framework/Framework/sensor/Makefile b/APP_Framework/Framework/sensor/Makefile index 8dc31ead9..c1764df68 100644 --- a/APP_Framework/Framework/sensor/Makefile +++ b/APP_Framework/Framework/sensor/Makefile @@ -1,5 +1,9 @@ SRC_FILES := sensor.c +ifeq ($(CONFIG_SENSOR_TVOC),y) + SRC_DIR += tvoc +endif + ifeq ($(CONFIG_SENSOR_IAQ),y) SRC_DIR += iaq endif diff --git a/APP_Framework/Framework/sensor/iaq/tb600b_iaq10/tb600b_iaq10.c b/APP_Framework/Framework/sensor/iaq/tb600b_iaq10/tb600b_iaq10.c index 3fade7633..2760efe28 100644 --- a/APP_Framework/Framework/sensor/iaq/tb600b_iaq10/tb600b_iaq10.c +++ b/APP_Framework/Framework/sensor/iaq/tb600b_iaq10/tb600b_iaq10.c @@ -39,7 +39,7 @@ static struct SensorProductInfo info = }; /** - * @description: Open AS830 sensor device + * @description: Open tb600b_iaq10 sensor device * @param sdev - sensor device pointer * @return success: 1 , failure: other */ @@ -130,7 +130,7 @@ static struct SensorDone done = }; /** - * @description: Init AS830 sensor and register + * @description: Init tb600b_iaq10 sensor and register * @return void */ static void SensorDeviceTb600bIaq10Init(void) @@ -159,7 +159,7 @@ static uint8_t getCheckSum(uint8_t *packet) } /** - * @description: Analysis AS830 CH4 result + * @description: Analysis tb600b_iaq10 result * @param quant - sensor quantity pointer * @return quantity value */ @@ -185,7 +185,7 @@ static int32_t QuantityRead(struct SensorQuantity *quant) checksum = getCheckSum(quant->sdev->buffer); if(checksum == quant->sdev->buffer[12]) { - result.gas = (uint16_t)quant->sdev->buffer[6] * 256 + (uint16)quant->sdev->buffer[7]; + result.gas = (uint16_t)quant->sdev->buffer[6] * 256 + (uint16_t)quant->sdev->buffer[7]; result.TH = ((int)((quant->sdev->buffer[8] << 8)|quant->sdev->buffer[9]))/100; result.TL = ((int)((quant->sdev->buffer[8] << 8)|quant->sdev->buffer[9]))%100; result.RhH = ((unsigned int)((quant->sdev->buffer[10] << 8)|quant->sdev->buffer[11]))/100; @@ -211,7 +211,7 @@ static int32_t QuantityRead(struct SensorQuantity *quant) } /** - * @description: Init AS830 CH4 quantity and register + * @description: Init tb600b_iaq10 quantity and register * @return 0 */ int Tb600bIaq10IaqInit(void) diff --git a/APP_Framework/Framework/sensor/sensor.h b/APP_Framework/Framework/sensor/sensor.h index 96edfd275..1da734c7c 100644 --- a/APP_Framework/Framework/sensor/sensor.h +++ b/APP_Framework/Framework/sensor/sensor.h @@ -53,6 +53,7 @@ extern "C" { #define SENSOR_ABILITY_VOICE ((uint32_t)(1 << SENSOR_QUANTITY_VOICE)) #define SENSOR_ABILITY_CH4 ((uint32_t)(1 << SENSOR_QUANTITY_CH4)) #define SENSOR_ABILITY_IAQ ((uint32_t)(1 << SENSOR_QUANTITY_IAQ)) +#define SENSOR_ABILITY_TVOC ((uint32_t)(1 << SENSOR_QUANTITY_TVOC)) struct SensorProductInfo { uint32_t ability; /* Bitwise OR of sensor ability */ @@ -93,6 +94,7 @@ enum SensorQuantityType { SENSOR_QUANTITY_VOICE, SENSOR_QUANTITY_CH4, SENSOR_QUANTITY_IAQ, + SENSOR_QUANTITY_TVOC, /* ...... */ SENSOR_QUANTITY_END, }; diff --git a/APP_Framework/Framework/sensor/tvoc/Kconfig b/APP_Framework/Framework/sensor/tvoc/Kconfig new file mode 100644 index 000000000..8d069425d --- /dev/null +++ b/APP_Framework/Framework/sensor/tvoc/Kconfig @@ -0,0 +1,43 @@ + +config SENSOR_TB600B_TVOC10 + bool "Using TB600B TVOC10" + default n + + if SENSOR_TB600B_TVOC10 + config SENSOR_DEVICE_TB600B_TVOC10 + string "tb600b tvoc10 sensor name" + default "tb600b_tvoc10_1" + + config SENSOR_QUANTITY_TB600B_TVOC + string "tb600b tvoc10 quantity name" + default "tvoc_1" + + if ADD_XIUOS_FETURES + config SENSOR_TB600B_TVOC10_DRIVER_EXTUART + bool "Using extra uart to support tb600b tvoc10" + default y + + config SENSOR_DEVICE_TB600B_TVOC10_DEV + string "tb600b tvoc10 device uart path" + default "/dev/uart2_dev2" + depends on !SENSOR_TB600B_TVOC10_DRIVER_EXTUART + + if SENSOR_TB600B_TVOC10_DRIVER_EXTUART + config SENSOR_DEVICE_TB600B_TVOC10_DEV + string "tb600b tvoc10 device extra uart path" + default "/dev/extuart_dev6" + + config SENSOR_DEVICE_TB600B_TVOC10_DEV_EXT_PORT + int "if TB600B_TVOC10 device using extuart, choose port" + default "6" + endif + endif + + if ADD_NUTTX_FETURES + + endif + + if ADD_RTTHREAD_FETURES + + endif + endif diff --git a/APP_Framework/Framework/sensor/tvoc/Makefile b/APP_Framework/Framework/sensor/tvoc/Makefile new file mode 100644 index 000000000..8ef12bb44 --- /dev/null +++ b/APP_Framework/Framework/sensor/tvoc/Makefile @@ -0,0 +1,5 @@ +ifeq ($(CONFIG_SENSOR_TB600B_TVOC10),y) + SRC_DIR += tb600b_tvoc10 +endif + +include $(KERNEL_ROOT)/compiler.mk diff --git a/APP_Framework/Framework/sensor/tvoc/tb600b_tvoc10/Makefile b/APP_Framework/Framework/sensor/tvoc/tb600b_tvoc10/Makefile new file mode 100644 index 000000000..6dbfd41f2 --- /dev/null +++ b/APP_Framework/Framework/sensor/tvoc/tb600b_tvoc10/Makefile @@ -0,0 +1,3 @@ +SRC_FILES := tb600b_tvoc10.c + +include $(KERNEL_ROOT)/compiler.mk diff --git a/APP_Framework/Framework/sensor/tvoc/tb600b_tvoc10/tb600b_tvoc10.c b/APP_Framework/Framework/sensor/tvoc/tb600b_tvoc10/tb600b_tvoc10.c new file mode 100644 index 000000000..ace49c577 --- /dev/null +++ b/APP_Framework/Framework/sensor/tvoc/tb600b_tvoc10/tb600b_tvoc10.c @@ -0,0 +1,227 @@ +/* +* Copyright (c) 2020 AIIT XUOS Lab +* XiUOS is licensed under Mulan PSL v2. +* You can use this software according to the terms and conditions of the Mulan PSL v2. +* You may obtain a copy of Mulan PSL v2 at: +* http://license.coscl.org.cn/MulanPSL2 +* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, +* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, +* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. +* See the Mulan PSL v2 for more details. +*/ + +/** + * @file tb600b_tvoc10.c + * @brief tb600b_tvoc10 driver base sensor + * @version 1.0 + * @author AIIT XUOS Lab + * @date 2021.12.15 + */ + +#include + +static struct SensorDevice tb600b_tvoc10; +static uint8_t ReadInstruction[9]; + +static struct SensorProductInfo info = +{ + SENSOR_ABILITY_TVOC, + "AQS", + "TB600B_TVOC10", +}; + +/** + * @description: Open TB600B TVOC10 sensor device + * @param sdev - sensor device pointer + * @return success: 1 , failure: other + */ +static int SensorDeviceOpen(struct SensorDevice *sdev) +{ + int result = 0; + + sdev->fd = PrivOpen(SENSOR_DEVICE_TB600B_TVOC10_DEV, O_RDWR); + if (sdev->fd < 0) { + printf("open %s error\n", SENSOR_DEVICE_TB600B_TVOC10_DEV); + return -1; + } + + struct SerialDataCfg cfg; + cfg.serial_baud_rate = BAUD_RATE_9600; + cfg.serial_data_bits = DATA_BITS_8; + cfg.serial_stop_bits = STOP_BITS_1; + cfg.serial_buffer_size = 128; + cfg.serial_parity_mode = PARITY_NONE; + cfg.serial_bit_order = 0; + cfg.serial_invert_mode = 0; +#ifdef SENSOR_TB600B_TVOC10_DRIVER_EXTUART + cfg.ext_uart_no = SENSOR_DEVICE_TB600B_TVOC10_DEV_EXT_PORT; + cfg.port_configure = PORT_CFG_INIT; +#endif + + struct PrivIoctlCfg ioctl_cfg; + ioctl_cfg.ioctl_driver_type = SERIAL_TYPE; + ioctl_cfg.args = &cfg; + result = PrivIoctl(sdev->fd, OPE_INT, &ioctl_cfg); + + return result; +} + +/** + * @description: Read sensor device + * @param sdev - sensor device pointer + * @param len - the length of the read data + * @return get data length + */ +static int SensorDeviceRead(struct SensorDevice *sdev, size_t len) +{ + uint8_t tmp = 0; + uint8_t idx = 0; + + /* this instruction will read gas with temperature and humidity,return 13 datas*/ + ReadInstruction[0] = 0xFF; + ReadInstruction[1] = 0x01; + ReadInstruction[2] = 0x87; + ReadInstruction[3] = 0x00; + ReadInstruction[4] = 0x00; + ReadInstruction[5] = 0x00; + ReadInstruction[6] = 0x00; + ReadInstruction[7] = 0x00; + ReadInstruction[8] = 0x78; + + PrivWrite(sdev->fd, ReadInstruction, 9); + + for(idx = 0; idx < 13; idx++) + { + if (PrivRead(sdev->fd, &tmp, 1) == 1) { + sdev->buffer[idx] = tmp; + } + } + + return idx; +} +/** + * @description: Read sensor device + * @param sdev - sensor device pointer + * @param len - the length of the read data + * @return get data length + */ +static int SensorDeviceWrite(struct SensorDevice *sdev, const void *buf, size_t len) +{ + return PrivWrite(sdev->fd, buf, len); + +} + +static struct SensorDone done = +{ + SensorDeviceOpen, + NULL, + SensorDeviceRead, + SensorDeviceWrite, + NULL, +}; + +/** + * @description: Init TB600B TVOC10 sensor and register + * @return void + */ +static void SensorDeviceTb600bTvoc10Init(void) +{ + tb600b_tvoc10.name = SENSOR_DEVICE_TB600B_TVOC10; + tb600b_tvoc10.info = &info; + tb600b_tvoc10.done = &done; + + SensorDeviceRegister(&tb600b_tvoc10); +} + +static struct SensorQuantity tb600b_tvoc10_tvoc; + +/* check data*/ +static uint8_t getCheckSum(uint8_t *packet) +{ + uint8_t i; + uint8_t checksum = 0; + for( i = 1; i < 12; i++) + { + checksum += packet[i]; + } + checksum = ~checksum + 1; + + return checksum; +} + +/** + * @description: Analysis TB600B TVOC10 result + * @param quant - sensor quantity pointer + * @return quantity value + */ +static int32_t QuantityRead(struct SensorQuantity *quant) +{ + if (!quant) + return -1; + + uint32_t len = 0; + uint8_t checksum = 0; + uint8_t TH, TL, RhH, RhL; + uint16_t ppb, ugm3; + + if (quant->sdev->done->read != NULL) { + if(quant->sdev->status == SENSOR_DEVICE_PASSIVE) { + len = quant->sdev->done->read(quant->sdev, 13); + if (len == 0) + { + printf("error read data length = 0.\n"); + return -1; + } + checksum = getCheckSum(quant->sdev->buffer); + if(checksum == quant->sdev->buffer[12]) + { + ugm3 = (uint16_t)quant->sdev->buffer[2] * 256 + (uint16_t)quant->sdev->buffer[3]; + ppb = (uint16_t)quant->sdev->buffer[6] * 256 + (uint16_t)quant->sdev->buffer[7]; + TH = ((int)((quant->sdev->buffer[8] << 8)|quant->sdev->buffer[9]))/100; + TL = ((int)((quant->sdev->buffer[8] << 8)|quant->sdev->buffer[9]))%100; + RhH = ((unsigned int)((quant->sdev->buffer[10] << 8)|quant->sdev->buffer[11]))/100; + RhL = ((unsigned int)((quant->sdev->buffer[10] << 8)|quant->sdev->buffer[11]))%100; + + printf("tvoc concentration is : %dug/m³(%dppb)\nThe temperature is : %d.%d℃\nThe humidity is : %d.%drh%%\n", ugm3, ppb, TH, TL, RhH, RhL); + return ppb; + } + else + { + printf("This reading is wrong\n"); + return SENSOR_QUANTITY_VALUE_ERROR; + } + } + if (quant->sdev->status == SENSOR_DEVICE_ACTIVE) + { + printf("Please set passive mode.\n"); + } + }else{ + printf("%s don't have read done.\n", quant->name); + } + + return -1; +} + +/** + * @description: Init TB600B TVOC10 quantity and register + * @return 0 + */ +int Tb600bTvoc10TvocInit(void) +{ + SensorDeviceTb600bTvoc10Init(); + + tb600b_tvoc10_tvoc.name = SENSOR_QUANTITY_TB600B_TVOC; + tb600b_tvoc10_tvoc.type = SENSOR_QUANTITY_TVOC; + tb600b_tvoc10_tvoc.value.decimal_places = 0; + tb600b_tvoc10_tvoc.value.max_std = SENSOR_QUANTITY_VALUE_ERROR; + tb600b_tvoc10_tvoc.value.min_std = SENSOR_QUANTITY_VALUE_ERROR; + tb600b_tvoc10_tvoc.value.last_value = SENSOR_QUANTITY_VALUE_ERROR; + tb600b_tvoc10_tvoc.value.max_value = SENSOR_QUANTITY_VALUE_ERROR; + tb600b_tvoc10_tvoc.value.min_value = SENSOR_QUANTITY_VALUE_ERROR; + tb600b_tvoc10_tvoc.sdev = &tb600b_tvoc10; + tb600b_tvoc10_tvoc.ReadValue = QuantityRead; + + SensorQuantityRegister(&tb600b_tvoc10_tvoc); + + return 0; +} \ No newline at end of file