From 4d1f2f045bcac16f98277db520ddce4daef45674 Mon Sep 17 00:00:00 2001 From: chenchong Date: Fri, 17 Dec 2021 18:15:11 +0800 Subject: [PATCH] =?UTF-8?q?style=EF=BC=9Amodify=20sensor=20als=20driver=20?= =?UTF-8?q?style?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: chenchong --- model/sensor/driver/als/sensor_als_driver.c | 45 +++++-------- model/sensor/driver/als/sensor_als_driver.h | 20 +++--- model/sensor/driver/chipset/als/als_bh1745.c | 66 ++++++++++++++------ model/sensor/driver/chipset/als/als_bh1745.h | 4 +- 4 files changed, 76 insertions(+), 59 deletions(-) diff --git a/model/sensor/driver/als/sensor_als_driver.c b/model/sensor/driver/als/sensor_als_driver.c index cd91f4ae..51403495 100755 --- a/model/sensor/driver/als/sensor_als_driver.c +++ b/model/sensor/driver/als/sensor_als_driver.c @@ -33,64 +33,49 @@ static char *g_extendedAlsGroupName[EXTENDED_ALS_GROUP_MAX] = { "gain", }; -int32_t GetTimeByRegValue(uint8_t regValue, struct TimeMap *table, int32_t itemNum) +int32_t GetTimeByRegValue(uint8_t regValue, struct TimeRegAddrValueMap *map, int32_t itemNum) { int32_t i; - CHECK_NULL_PTR_RETURN_VALUE(table, HDF_FAILURE); + CHECK_NULL_PTR_RETURN_VALUE(map, HDF_FAILURE); for (i = 0; i < itemNum; i++) { - if (regValue == table[i].timeRegValue) { - return table[i].timeValue; + if (regValue == map[i].timeRegKey) { + return map[i].timeValue; } } - return HDF_FAILURE; + return ERR_NO_TIME_VALUE; } -int32_t GetRegValueByTime(uint32_t timeValue, struct TimeMap *table, int32_t itemNum) +int32_t GetRegGroupIndexByTime(uint32_t timeValue, struct TimeRegAddrValueMap *map, int32_t itemNum) { int32_t i; - CHECK_NULL_PTR_RETURN_VALUE(table, HDF_FAILURE); + CHECK_NULL_PTR_RETURN_VALUE(map, HDF_FAILURE); for (i = 0; i < itemNum; i++) { - if (timeValue == table[i].timeValue) { - return table[i].timeRegValue; - } - } - - return HDF_FAILURE; -} - -int32_t GetRegGroupIndexByTime(uint32_t timeValue, struct TimeMap *table, int32_t itemNum) -{ - int32_t i; - - CHECK_NULL_PTR_RETURN_VALUE(table, HDF_FAILURE); - - for (i = 0; i < itemNum; i++) { - if (timeValue == table[i].timeValue) { + if (timeValue == map[i].timeValue) { return i; } } - return HDF_FAILURE; + return ERR_NO_INDEX_VALUE; } -int32_t GetGainByRegValue(uint8_t regValue, struct GainMap *table, int32_t itemNum) +int32_t GetGainByRegValue(uint8_t regValue, struct GainRegAddrValueMap *map, int32_t itemNum) { int32_t i; - CHECK_NULL_PTR_RETURN_VALUE(table, HDF_FAILURE); + CHECK_NULL_PTR_RETURN_VALUE(map, HDF_FAILURE); for (i = 0; i < itemNum; i++) { - if (regValue == table[i].gainRegValue) { - return table[i].gainValue; + if (regValue == map[i].gainRegKey) { + return map[i].gainValue; } } - return HDF_FAILURE; + return ERR_NO_GAIN_VALUE; } static int32_t GetExtendedAlsRegGroupNameIndex(const char *name) @@ -155,7 +140,7 @@ int32_t ParseExtendedAlsRegConfig(struct SensorCfgData *config) } if (ParseSensorRegGroup(parser, extendedRegCfgNode, extendedRegAttr->name, - &config->extendedRegCfgGroup[index]) != HDF_SUCCESS) { + &config->extendedRegCfgGroup[index]) != HDF_SUCCESS) { HDF_LOGE("%s: parse sensor register group failed", __func__); goto ERROR; } diff --git a/model/sensor/driver/als/sensor_als_driver.h b/model/sensor/driver/als/sensor_als_driver.h index 6f02f9f1..290def22 100755 --- a/model/sensor/driver/als/sensor_als_driver.h +++ b/model/sensor/driver/als/sensor_als_driver.h @@ -15,7 +15,10 @@ #include "sensor_platform_if.h" #define ALS_DEFAULT_SAMPLING_200_MS 200000000 -#define ALS_CHIP_NAME_BH1745 "bh1745" +#define ALS_CHIP_NAME_BH1745 "bh1745" +#define ERR_NO_TIME_VALUE -1 +#define ERR_NO_INDEX_VALUE -1 +#define ERR_NO_GAIN_VALUE -1 enum ExtendedAlsRegGroupType { EXTENDED_ALS_TIME_GROUP = 0, @@ -49,13 +52,13 @@ struct AlsReportData { int32_t irData; }; -struct TimeMap { - uint8_t timeRegValue; +struct TimeRegAddrValueMap { + uint8_t timeRegKey; uint32_t timeValue; }; -struct GainMap { - uint8_t gainRegValue; +struct GainRegAddrValueMap { + uint8_t gainRegKey; uint32_t gainValue; }; @@ -87,8 +90,7 @@ struct AlsDrvData { int32_t AlsRegisterChipOps(const struct AlsOpsCall *ops); struct SensorCfgData *AlsCreateCfgData(const struct DeviceResourceNode *node); void AlsReleaseCfgData(struct SensorCfgData *sensorCfgData); -int32_t GetTimeByRegValue(uint8_t regValue, struct TimeMap *table, int32_t itemNum); -int32_t GetRegValueByTime(uint32_t timeValue, struct TimeMap *table, int32_t itemNum); -int32_t GetRegGroupIndexByTime(uint32_t timeValue, struct TimeMap *table, int32_t itemNum); -int32_t GetGainByRegValue(uint8_t regValue, struct GainMap *table, int32_t itemNum); +int32_t GetTimeByRegValue(uint8_t regValue, struct TimeMap *map, int32_t itemNum); +int32_t GetRegGroupIndexByTime(uint32_t timeValue, struct TimeMap *map, int32_t itemNum); +int32_t GetGainByRegValue(uint8_t regValue, struct GainMap *map, int32_t itemNum); #endif /* SENSOR_ALS_DRIVER_H */ diff --git a/model/sensor/driver/chipset/als/als_bh1745.c b/model/sensor/driver/chipset/als/als_bh1745.c index 4555a71b..b0da1f43 100755 --- a/model/sensor/driver/chipset/als/als_bh1745.c +++ b/model/sensor/driver/chipset/als/als_bh1745.c @@ -20,9 +20,9 @@ #define SENSOR_I2C_REG_CFG 0x403 static struct Bh1745DrvData *g_bh1745DrvData = NULL; -static uint32_t g_timeMap_flag = 1; +static uint32_t g_timeMapFlag = 1; -static struct TimeMap g_timeMap[EXTENDED_ALS_TIME_GROUP_INDEX_MAX] = { +static struct TimeRegAddrValueMap g_timeMap[EXTENDED_ALS_TIME_GROUP_INDEX_MAX] = { { EXTENDED_ALS_TIME_GROUP_ATTR_VALUE_0, BH1745_TIME_160MSEC }, { EXTENDED_ALS_TIME_GROUP_ATTR_VALUE_1, BH1745_TIME_320MSEC }, { EXTENDED_ALS_TIME_GROUP_ATTR_VALUE_2, BH1745_TIME_640MSEC }, @@ -31,7 +31,7 @@ static struct TimeMap g_timeMap[EXTENDED_ALS_TIME_GROUP_INDEX_MAX] = { { EXTENDED_ALS_TIME_GROUP_ATTR_VALUE_5, BH1745_TIME_5120MSEC } }; -static struct GainMap g_gainMap[EXTENDED_ALS_GAIN_GROUP_INDEX_MAX] = { +static struct GainRegAddrValueMap g_gainMap[EXTENDED_ALS_GAIN_GROUP_INDEX_MAX] = { { EXTENDED_ALS_GAIN_GROUP_ATTR_VALUE_0, BH1745_GAIN_1X }, { EXTENDED_ALS_GAIN_GROUP_ATTR_VALUE_1, BH1745_GAIN_2X }, { EXTENDED_ALS_GAIN_GROUP_ATTR_VALUE_2, BH1745_GAIN_16X } @@ -54,6 +54,7 @@ struct Bh1745DrvData *Bh1745GetDrvData(void) static int32_t DynamicRangCovert(struct SensorCfgData *CfgData, uint32_t *rgbcData) { + int32_t ret; uint8_t regValue; uint32_t temp; uint8_t timeItemNum; @@ -67,7 +68,11 @@ static int32_t DynamicRangCovert(struct SensorCfgData *CfgData, uint32_t *rgbcDa return HDF_FAILURE; } - ReadSensorRegCfgArray(&CfgData->busCfg, timeGroupNode, index, ®Value, sizeof(uint8_t)); + ret = ReadSensorRegCfgArray(&CfgData->busCfg, timeGroupNode, index, ®Value, sizeof(regValue)); + if (ret != HDF_SUCCESS) { + HDF_LOGE("%s: Failed to read sensor register array ", __func__); + return HDF_FAILURE; + } regValue &= timeGroupNode->regCfgItem->mask; temp = GetTimeByRegValue(regValue, g_timeMap, timeItemNum); @@ -78,15 +83,19 @@ static int32_t DynamicRangCovert(struct SensorCfgData *CfgData, uint32_t *rgbcDa } if (((rgbcData[ALS_R] * BH1745_MULTIPLE_100 > BH1745_TIME_MAX) || - (rgbcData[ALS_G] * BH1745_MULTIPLE_100 > BH1745_TIME_MAX)) && (temp >= BH1745_TIME_320MSEC)) { - g_timeMap_flag = 1; + (rgbcData[ALS_G] * BH1745_MULTIPLE_100 > BH1745_TIME_MAX)) && (temp >= BH1745_TIME_320MSEC)) { + g_timeMapFlag = 1; index = GetRegGroupIndexByTime(temp, g_timeMap, timeItemNum); index--; - WriteSensorRegCfgArray(&CfgData->busCfg, timeGroupNode, index, sizeof(uint8_t)); - } else if ((g_timeMap_flag == 1) && ((rgbcData[ALS_R] * BH1745_MULTIPLE_100 < BH1745_TIME_MIN) || - (rgbcData[ALS_G] * BH1745_MULTIPLE_100 < BH1745_TIME_MIN))) { - g_timeMap_flag = 0; + WriteSensorRegCfgArray(&CfgData->busCfg, timeGroupNode, index, sizeof(regValue)); + if (ret != HDF_SUCCESS) { + HDF_LOGE("%s: Failed to write sensor register array ", __func__); + return HDF_FAILURE; + } + } else if ((g_timeMapFlag == 1) && ((rgbcData[ALS_R] * BH1745_MULTIPLE_100 < BH1745_TIME_MIN) || + (rgbcData[ALS_G] * BH1745_MULTIPLE_100 < BH1745_TIME_MIN))) { + g_timeMapFlag = 0; index = GetRegGroupIndexByTime(temp, g_timeMap, timeItemNum); index++; if (index >= timeItemNum) { @@ -94,13 +103,19 @@ static int32_t DynamicRangCovert(struct SensorCfgData *CfgData, uint32_t *rgbcDa return HDF_FAILURE; } - WriteSensorRegCfgArray(&CfgData->busCfg, timeGroupNode, index, sizeof(uint8_t)); + WriteSensorRegCfgArray(&CfgData->busCfg, timeGroupNode, index, sizeof(regValue)); + if (ret != HDF_SUCCESS) { + HDF_LOGE("%s: Failed to write sensor register array ", __func__); + return HDF_FAILURE; + } } + return HDF_SUCCESS; } -static uint32_t CalLux(struct SensorCfgData *CfgData, uint32_t *rgbcData) +static int32_t CalLux(struct SensorCfgData *CfgData, uint32_t *rgbcData, int32_t als) { + int32_t ret; uint32_t time; uint32_t gain; uint8_t regValue; @@ -112,7 +127,8 @@ static uint32_t CalLux(struct SensorCfgData *CfgData, uint32_t *rgbcData) int32_t gainIndex = EXTENDED_ALS_GAIN_GROUP_INDEX_0; if (rgbcData[ALS_G] < 1) { - return 0; + HDF_LOGE("%s: RgbcData out of range ", __func__); + return HDF_FAILURE; } if (BH1745_MULTIPLE_100 * rgbcData[ALS_C] / rgbcData[ALS_G] < BH1745_COEFFICIENT_JUDGE) { @@ -128,7 +144,11 @@ static uint32_t CalLux(struct SensorCfgData *CfgData, uint32_t *rgbcData) return HDF_FAILURE; } - ReadSensorRegCfgArray(&CfgData->busCfg, GroupNode, timeIndex, ®Value, sizeof(uint8_t)); + ret = ReadSensorRegCfgArray(&CfgData->busCfg, GroupNode, timeIndex, ®Value, sizeof(regValue)); + if (ret != HDF_SUCCESS) { + HDF_LOGE("%s: Failed to read sensor register array ", __func__); + return HDF_FAILURE; + } regValue &= GroupNode->regCfgItem->mask; time = GetTimeByRegValue(regValue, g_timeMap, itemNum); @@ -140,18 +160,28 @@ static uint32_t CalLux(struct SensorCfgData *CfgData, uint32_t *rgbcData) return HDF_FAILURE; } - ReadSensorRegCfgArray(&CfgData->busCfg, GroupNode, gainIndex, ®Value, sizeof(uint8_t)); + ret = ReadSensorRegCfgArray(&CfgData->busCfg, GroupNode, gainIndex, ®Value, sizeof(regValue)); + if (ret != HDF_SUCCESS) { + HDF_LOGE("%s: Failed to read sensor register array ", __func__); + return HDF_FAILURE; + } regValue &= GroupNode->regCfgItem->mask; gain = GetGainByRegValue(regValue, g_gainMap, itemNum); - return (((luxTemp * BH1745_TIME_160MSEC * BH1745_GAIN_16X) / gain) / time); + als = ((luxTemp * BH1745_TIME_160MSEC * BH1745_GAIN_16X) / gain) / time; + + return HDF_SUCCESS; } -static int32_t RawDataConvert(struct SensorCfgData *CfgData, struct AlsReportData *reportData, uint32_t* rgbcData) +static int32_t RawDataConvert(struct SensorCfgData *CfgData, struct AlsReportData *reportData, uint32_t RawDataConvert*rgbcData) { int ret; - reportData->als = (uint32_t)CalLux(CfgData, rgbcData); + ret = CalLux(CfgData, rgbcData, &reportData->als); + if (ret != HDF_SUCCESS) { + HDF_LOGE("%s: Failed to calculate sensor brightness ", __func__); + return HDF_FAILURE; + } reportData->als = (reportData->als > 0) ? reportData->als : 0; ret = DynamicRangCovert(CfgData, rgbcData); diff --git a/model/sensor/driver/chipset/als/als_bh1745.h b/model/sensor/driver/chipset/als/als_bh1745.h index 34105dee..28effdd9 100755 --- a/model/sensor/driver/chipset/als/als_bh1745.h +++ b/model/sensor/driver/chipset/als/als_bh1745.h @@ -66,7 +66,7 @@ #define EXTENDED_ALS_GAIN_GROUP_ATTR_VALUE_1 0x01 #define EXTENDED_ALS_GAIN_GROUP_ATTR_VALUE_2 0x02 -enum ExtendedAlsTimeRegGroupIdex { +enum ExtendedAlsTimeRegGroupIndex { EXTENDED_ALS_TIME_GROUP_INDEX_0 = 0, EXTENDED_ALS_TIME_GROUP_INDEX_1, EXTENDED_ALS_TIME_GROUP_INDEX_2, @@ -76,7 +76,7 @@ enum ExtendedAlsTimeRegGroupIdex { EXTENDED_ALS_TIME_GROUP_INDEX_MAX, }; -enum ExtendedAlsGainRegGroupIdex { +enum ExtendedAlsGainRegGroupIndex { EXTENDED_ALS_GAIN_GROUP_INDEX_0 = 0, EXTENDED_ALS_GAIN_GROUP_INDEX_1, EXTENDED_ALS_GAIN_GROUP_INDEX_2,