samples: sensor: sensor_shell: add fake sensor driver

Add a simple fake sensor driver so that the sample is more fun
for boards without real sensor.

Signed-off-by: Yong Cong Sin <ycsin@meta.com>
This commit is contained in:
Yong Cong Sin 2024-05-16 12:28:43 +08:00 committed by Johan Hedberg
commit d462ed4d95
6 changed files with 146 additions and 0 deletions

View file

@ -7,4 +7,8 @@ project(sensor_shell)
target_sources(app PRIVATE src/main.c)
target_sources_ifdef(CONFIG_SAMPLES_SENSOR_SHELL_FAKE_SENSOR app PRIVATE
src/fake_sensor.c
)
target_include_directories(app PRIVATE include)

View file

@ -1,4 +1,10 @@
# Copyright (c) 2023 Google LLC
# SPDX-License-Identifier: Apache-2.0
config SAMPLES_SENSOR_SHELL_FAKE_SENSOR
bool "Enable fake sensor"
help
On boards that do not have a sensor, enabling this will build a fake
sensor that can be interacted with via the sensor shell.
source "Kconfig.zephyr"

View file

@ -18,6 +18,14 @@ enabled, for example:
:board: reel_board
:goals: build flash
For boards that do not have a sensor, a simple fake sensor driver is provided, for example:
.. zephyr-app-commands::
:zephyr-app: samples/sensor/sensor_shell
:board: qemu_riscv64
:goals: run
:gen-args: -DCONFIG_SAMPLES_SENSOR_SHELL_FAKE_SENSOR=y
Shell Module Command Help
=========================

View file

@ -0,0 +1,26 @@
/*
* Copyright (c) 2024 Meta Platforms
*
* SPDX-License-Identifier: Apache-2.0
*/
/ {
app {
#address-cells = <1>;
#size-cells = <0>;
vsensor0: sensor@0 {
compatible = "vnd,fake-sensor";
reg = <0>;
friendly-name = "Fake sensor 0";
status = "okay";
};
vsensor1: sensor@1 {
compatible = "vnd,fake-sensor";
reg = <1>;
friendly-name = "Fake sensor 1";
status = "okay";
};
};
};

View file

@ -0,0 +1,8 @@
# Copyright (c) 2024 Meta Platforms
# SPDX-License-Identifier: Apache-2.0
description: Fake sensor
include: sensor-device.yaml
compatible: "vnd,fake-sensor"

View file

@ -0,0 +1,94 @@
/*
* Copyright (c) 2024 Meta Platforms
*
* SPDX-License-Identifier: Apache-2.0
*/
#define DT_DRV_COMPAT vnd_fake_sensor
#include <zephyr/device.h>
#include <zephyr/drivers/sensor.h>
#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(fake_sensor);
static int init(const struct device *dev)
{
ARG_UNUSED(dev);
return 0;
}
static int attr_set(const struct device *dev, enum sensor_channel chan, enum sensor_attribute attr,
const struct sensor_value *val)
{
LOG_DBG("[%s] dev: %p, chan: %d, attr: %d, val1: %d, val2: %d", __func__, dev, chan, attr,
val->val1, val->val2);
return 0;
}
static int attr_get(const struct device *dev, enum sensor_channel chan, enum sensor_attribute attr,
struct sensor_value *val)
{
LOG_DBG("[%s] dev: %p, chan: %d, attr: %d", __func__, dev, chan, attr);
val->val1 = chan;
val->val2 = attr * 100000;
return 0;
}
static int sample_fetch(const struct device *dev, enum sensor_channel chan)
{
LOG_DBG("[%s] dev: %p, chan: %d", __func__, dev, chan);
return 0;
}
static int channel_get(const struct device *dev, enum sensor_channel chan, struct sensor_value *val)
{
LOG_DBG("[%s] dev: %p, chan: %d", __func__, dev, chan);
switch (chan) {
case SENSOR_CHAN_ACCEL_XYZ:
__fallthrough;
case SENSOR_CHAN_GYRO_XYZ:
__fallthrough;
case SENSOR_CHAN_MAGN_XYZ:
for (int i = 0; i < 3; i++, val++) {
val->val1 = chan;
val->val2 = 1;
}
break;
default:
val->val1 = chan;
val->val2 = 1;
break;
}
return 0;
}
static int trigger_set(const struct device *dev, const struct sensor_trigger *trig,
sensor_trigger_handler_t handler)
{
LOG_DBG("[%s - %s] dev: %p, trig->chan: %d, trig->type: %d, handler: %p", __func__,
(handler == NULL) ? "off" : "on", dev, trig->chan, trig->type, handler);
return 0;
}
static const struct sensor_driver_api api = {
.attr_get = attr_get,
.attr_set = attr_set,
.sample_fetch = sample_fetch,
.channel_get = channel_get,
.trigger_set = trigger_set,
};
#define VND_SENSOR_INIT(n) \
SENSOR_DEVICE_DT_INST_DEFINE(n, init, NULL, NULL, NULL, POST_KERNEL, \
CONFIG_SENSOR_INIT_PRIORITY, &api);
DT_INST_FOREACH_STATUS_OKAY(VND_SENSOR_INIT)