Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions drivers/sensor/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ add_subdirectory_ifdef(CONFIG_PMS7003 pms7003)
add_subdirectory_ifdef(CONFIG_QDEC_NRFX qdec_nrfx)
add_subdirectory_ifdef(CONFIG_TEMP_NRF5 nrf5)
add_subdirectory_ifdef(CONFIG_SHT3XD sht3xd)
add_subdirectory_ifdef(CONFIG_SIMACCEL simaccel)
add_subdirectory_ifdef(CONFIG_SX9500 sx9500)
add_subdirectory_ifdef(CONFIG_TH02 th02)
add_subdirectory_ifdef(CONFIG_TMP007 tmp007)
Expand Down
3 changes: 3 additions & 0 deletions drivers/sensor/simaccel/CMakeList.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
zephyr_library()

zephyr_library_sources_ifdef(CONFIG_SIMACCEL simaccel.c)
36 changes: 36 additions & 0 deletions drivers/sensor/simaccel/Kconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Kconfig - SIMACCEL simulated 3-axis accelerometer sensor driver

#
# Copyright (c) 2019 Kevin Townsend
#
# SPDX-License-Identifier: Apache-2.0
#

menuconfig SIMACCEL
bool "SIMACCEL simulated accelerometer sensor driver"
help
Enable simulated 3-axis accelerometer sensor driver.

if SIMACCEL

choice
prompt "Measurements per second"
default SIMACCEL_MPS_1
help
Number of measurements per second.

config SIMACCEL_MPS_1
bool "1"

config SIMACCEL_MPS_10
bool "10"

config SIMACCEL_MPS_100
bool "100"

config SIMACCEL_MPS_1000
bool "1000"

endchoice

endif # SIMACCEL
81 changes: 81 additions & 0 deletions drivers/sensor/simaccel/simaccel.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/* simaccel.c - Simulated driver for a 3-axis accelerometer. */

/*
* Copyright (c) 2019 Kevin Townsend.
*
* SPDX-License-Identifier: Apache-2.0
*/

#include <kernel.h>
#include <sensor.h>
#include <init.h>
#include <misc/byteorder.h>
#include <misc/__assert.h>

#include "simaccel.h"

/**
* @brief Retrieves a set of data samples from the sensor.
*
* @param dev Pointer to the `device` used when executing the read attempt.
* @param chan The `sensor_channel` to request data from. This should
* normally be `SENSOR_CHAN_ALL`.
*
* @return 0 on success, otherwise an appropriate error code.
*/
static int simaccel_sample_fetch(struct device *dev, enum sensor_channel chan)
{
struct simaccel_data *data = dev->driver_data;
u8_t buf[6] = { 0 };
int ret;

__ASSERT_NO_MSG(chan == SENSOR_CHAN_ALL);

/* TODO: Generate sensible random data or use a known lookup table. */
data->range = 2;
data->xdata = (s16_t)buf[1] << 8 | buf[0];
data->ydata = (s16_t)buf[3] << 8 | buf[2];
data->zdata = (s16_t)buf[5] << 8 | buf[4];

return 0;
}

static int simaccel_channel_get(struct device *dev,
enum sensor_channel chan,
struct sensor_value *val)
{
struct simaccel_data *data = dev->driver_data;

switch (chan) {
default:
return -EINVAL;
}

return 0;
}

static const struct sensor_driver_api simaccel_api_funcs = {
.sample_fetch = simaccel_sample_fetch,
.channel_get = simaccel_channel_get,
};

/**
* Attempts to initialise a simaccel device.
*
* @param dev The 'device' to bind the driver instance to.
*
* @return 0 on success, otherwise an appropriate error code.
*/
int simaccel_init(struct device *dev)
{
struct simaccel_data *data = dev->driver_data;

return 0;
}

static struct simaccel_data simaccel_data;

DEVICE_AND_API_INIT(simaccel, "simaccel",
simaccel_init, &simaccel_data,
NULL, POST_KERNEL, CONFIG_SENSOR_INIT_PRIORITY,
&simaccel_api_funcs);
24 changes: 24 additions & 0 deletions drivers/sensor/simaccel/simaccel.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright (c) 2019 Kevin Townsend
*
* SPDX-License-Identifier: Apache-2.0
*/

#ifndef ZEPHYR_DRIVERS_SENSOR_SIMACCEL_SIMACCEL_H_
#define ZEPHYR_DRIVERS_SENSOR_SIMACCEL_SIMACCEL_H_

#include <zephyr/types.h>
#include <device.h>

struct simaccel_data {
/** Accelerometer measurement (+/-G). Valid values are 2, 4, 8 or 16. */
u8_t range;
/** Raw x-axis data. */
s16_t xdata;
/** Raw y-axis data. */
s16_t ydata;
/** Raw z-axis data. */
s16_t zdata;
};

#endif /* ZEPHYR_DRIVERS_SENSOR_SIMACCEL_SIMACCEL_H_ */