[components][drivers] added sensor device lookup function

This commit is contained in:
yiyi 2024-04-30 05:54:13 +08:00 committed by GitHub
parent a92f971267
commit 4c9d3be98a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 23 additions and 1 deletions

View File

@ -398,6 +398,7 @@ int rt_hw_sensor_register(rt_sensor_t sensor,
const char *name,
rt_uint32_t flag,
void *data);
rt_sensor_t rt_sensor_device_find(const char *name);
#ifdef __cplusplus
}

View File

@ -41,7 +41,8 @@ static char *const sensor_name_str[] =
"sp-", /* SpO2 sensor */
"ia-", /* IAQ sensor */
"et-", /* EtOH sensor */
"bp-" /* Blood Pressure */
"bp-", /* Blood Pressure */
RT_NULL
};
/* sensor interrupt handler function */
@ -519,3 +520,23 @@ int rt_hw_sensor_register(rt_sensor_t sensor,
return RT_EOK;
}
rt_sensor_t rt_sensor_device_find(const char *name)
{
rt_uint8_t index;
char device_name[RT_NAME_MAX];
rt_device_t device;
for (index = 0; sensor_name_str[index] != RT_NULL; index++)
{
rt_memset(device_name, 0, sizeof(device_name));
rt_snprintf(device_name, sizeof(device_name), "%s%s", sensor_name_str[index], name);
device = rt_device_find(device_name);
if (device != RT_NULL)
{
return (rt_sensor_t)device;
}
}
return RT_NULL;
}