goldfish_sensor_uorb.c: add set_interval for goldfish sensor

Added the ability to set sampling rate for goldfish

Signed-off-by: chenzihan1 <chenzihan1@xiaomi.com>
This commit is contained in:
chenzihan1 2024-11-22 16:35:27 +08:00 committed by Xiang Xiao
parent 8c9162ef8c
commit 724797e05c

View file

@ -83,6 +83,7 @@ struct goldfish_sensor_s
struct sensor_lowerhalf_s lower_humi; struct sensor_lowerhalf_s lower_humi;
struct sensor_lowerhalf_s lower_temp; struct sensor_lowerhalf_s lower_temp;
struct sensor_lowerhalf_s lower_hrate; struct sensor_lowerhalf_s lower_hrate;
uint32_t interval;
}; };
/**************************************************************************** /****************************************************************************
@ -91,6 +92,9 @@ struct goldfish_sensor_s
static int goldfish_sensor_activate(FAR struct sensor_lowerhalf_s *lower, static int goldfish_sensor_activate(FAR struct sensor_lowerhalf_s *lower,
FAR struct file *filep, bool enabled); FAR struct file *filep, bool enabled);
static int goldfish_sensor_set_interval(FAR struct sensor_lowerhalf_s *lower,
FAR struct file *filep,
FAR uint32_t *period_us);
static int goldfish_sensor_thread(int argc, FAR char** argv); static int goldfish_sensor_thread(int argc, FAR char** argv);
/**************************************************************************** /****************************************************************************
@ -100,6 +104,7 @@ static int goldfish_sensor_thread(int argc, FAR char** argv);
static const struct sensor_ops_s g_goldfish_sensor_ops = static const struct sensor_ops_s g_goldfish_sensor_ops =
{ {
.activate = goldfish_sensor_activate, .activate = goldfish_sensor_activate,
.set_interval = goldfish_sensor_set_interval,
}; };
FAR static const char *const g_goldfish_sensor_name[] = FAR static const char *const g_goldfish_sensor_name[] =
@ -486,104 +491,106 @@ static void goldfish_sensor_parse_event(FAR struct goldfish_sensor_s *sensor)
} }
} }
static int goldfish_sensor_activate(FAR struct sensor_lowerhalf_s *lower, static int goldfish_get_priv(FAR struct sensor_lowerhalf_s *lower,
FAR struct file *filep, bool enabled) FAR struct goldfish_sensor_s **priv)
{ {
FAR struct goldfish_sensor_s *priv;
switch (lower->type) switch (lower->type)
{ {
case SENSOR_TYPE_ACCELEROMETER: case SENSOR_TYPE_ACCELEROMETER:
if (lower->uncalibrated) if (lower->uncalibrated)
{ {
priv = container_of(lower, *priv = container_of(lower, struct goldfish_sensor_s,
struct goldfish_sensor_s,
lower_accel_uncalibrated); lower_accel_uncalibrated);
return return GOLDFISH_ACCELERATION_UNCALIBRATED;
goldfish_sensor_do_activate(&priv->pipe,
GOLDFISH_ACCELERATION_UNCALIBRATED,
enabled);
} }
else else
{ {
priv = container_of(lower, struct goldfish_sensor_s, lower_accel); *priv = container_of(lower, struct goldfish_sensor_s, lower_accel);
return goldfish_sensor_do_activate(&priv->pipe, return GOLDFISH_ACCELERATION;
GOLDFISH_ACCELERATION,
enabled);
} }
case SENSOR_TYPE_MAGNETIC_FIELD: case SENSOR_TYPE_MAGNETIC_FIELD:
if (lower->uncalibrated) if (lower->uncalibrated)
{ {
priv = container_of(lower, *priv = container_of(lower, struct goldfish_sensor_s,
struct goldfish_sensor_s,
lower_mag_uncalibrated); lower_mag_uncalibrated);
return return GOLDFISH_MAGNETIC_FIELD_UNCALIBRATED;
goldfish_sensor_do_activate(&priv->pipe,
GOLDFISH_MAGNETIC_FIELD_UNCALIBRATED,
enabled);
} }
else else
{ {
priv = container_of(lower, struct goldfish_sensor_s, lower_mag); *priv = container_of(lower, struct goldfish_sensor_s, lower_mag);
return goldfish_sensor_do_activate(&priv->pipe, return GOLDFISH_MAGNETIC_FIELD;
GOLDFISH_MAGNETIC_FIELD,
enabled);
} }
case SENSOR_TYPE_GYROSCOPE: case SENSOR_TYPE_GYROSCOPE:
if (lower->uncalibrated) if (lower->uncalibrated)
{ {
priv = container_of(lower, *priv = container_of(lower, struct goldfish_sensor_s,
struct goldfish_sensor_s,
lower_gyro_uncalibrated); lower_gyro_uncalibrated);
return return GOLDFISH_GYROSCOPE_FIELD_UNCALIBRATED;
goldfish_sensor_do_activate(&priv->pipe,
GOLDFISH_GYROSCOPE_FIELD_UNCALIBRATED,
enabled);
} }
else else
{ {
priv = container_of(lower, struct goldfish_sensor_s, lower_gyro); *priv = container_of(lower, struct goldfish_sensor_s, lower_gyro);
return goldfish_sensor_do_activate(&priv->pipe, return GOLDFISH_GYROSCOPE;
GOLDFISH_GYROSCOPE,
enabled);
} }
case SENSOR_TYPE_PROXIMITY: case SENSOR_TYPE_PROXIMITY:
priv = container_of(lower, struct goldfish_sensor_s, lower_prox); *priv = container_of(lower, struct goldfish_sensor_s, lower_prox);
return goldfish_sensor_do_activate(&priv->pipe, return GOLDFISH_PROXIMITY;
GOLDFISH_PROXIMITY,
enabled);
case SENSOR_TYPE_LIGHT: case SENSOR_TYPE_LIGHT:
priv = container_of(lower, struct goldfish_sensor_s, lower_light); *priv = container_of(lower, struct goldfish_sensor_s, lower_light);
return goldfish_sensor_do_activate(&priv->pipe, return GOLDFISH_LIGHT;
GOLDFISH_LIGHT,
enabled);
case SENSOR_TYPE_BAROMETER: case SENSOR_TYPE_BAROMETER:
priv = container_of(lower, struct goldfish_sensor_s, lower_baro); *priv = container_of(lower, struct goldfish_sensor_s, lower_baro);
return goldfish_sensor_do_activate(&priv->pipe, return GOLDFISH_PRESSURE;
GOLDFISH_PRESSURE,
enabled);
case SENSOR_TYPE_RELATIVE_HUMIDITY: case SENSOR_TYPE_RELATIVE_HUMIDITY:
priv = container_of(lower, struct goldfish_sensor_s, lower_humi); *priv = container_of(lower, struct goldfish_sensor_s, lower_humi);
return goldfish_sensor_do_activate(&priv->pipe, return GOLDFISH_RELATIVE_HUMIDITY;
GOLDFISH_RELATIVE_HUMIDITY,
enabled);
case SENSOR_TYPE_AMBIENT_TEMPERATURE: case SENSOR_TYPE_AMBIENT_TEMPERATURE:
priv = container_of(lower, struct goldfish_sensor_s, lower_temp); *priv = container_of(lower, struct goldfish_sensor_s, lower_temp);
return goldfish_sensor_do_activate(&priv->pipe, return GOLDFISH_AMBIENT_TEMPERATURE;
GOLDFISH_AMBIENT_TEMPERATURE,
enabled);
case SENSOR_TYPE_HEART_RATE: case SENSOR_TYPE_HEART_RATE:
priv = container_of(lower, struct goldfish_sensor_s, lower_hrate); *priv = container_of(lower, struct goldfish_sensor_s, lower_hrate);
return goldfish_sensor_do_activate(&priv->pipe, return GOLDFISH_HEART_RATE;
GOLDFISH_HEART_RATE,
enabled);
default: default:
return -EINVAL; return -EINVAL;
} }
}
static int goldfish_sensor_activate(FAR struct sensor_lowerhalf_s *lower,
FAR struct file *filep, bool enabled)
{
FAR struct goldfish_sensor_s *priv;
int handle = goldfish_get_priv(lower, &priv);
if (handle < 0)
{
return handle;
}
return goldfish_sensor_do_activate(&priv->pipe, handle, enabled);
}
static int goldfish_sensor_set_interval(FAR struct sensor_lowerhalf_s *lower,
FAR struct file *filep,
FAR uint32_t *period_us)
{
struct FAR goldfish_sensor_s *priv;
char buffer[64];
int handle;
int len;
handle = goldfish_get_priv(lower, &priv);
if (handle < 0)
{
return handle;
}
len = snprintf(buffer, sizeof(buffer), "set-delay: %d",
(int)(*period_us / 1000));
goldfish_sensor_send(&priv->pipe, buffer, len);
priv->interval = *period_us;
return OK; return OK;
} }