Skip to content
Permalink
Browse files

samples: add ADXL362 sensor sample application

Add a sample application that demonstrates how to use the ADXL362 with
data ready and threshold triggers.

Signed-off-by: Brett Witherspoon <spoonb@cdspooner.com>
  • Loading branch information...
bwitherspoon authored and MaureenHelm committed May 9, 2019
1 parent bedfcf4 commit 07d21bef0de3be3c5f2e8d2e3cc7b5010f5d1b85
@@ -0,0 +1,10 @@
# Copyright (c) 2019 Brett Witherspoon
#
# SPDX-License-Identifier: Apache-2.0

cmake_minimum_required(VERSION 3.13.1)

include($ENV{ZEPHYR_BASE}/cmake/app/boilerplate.cmake NO_POLICY_SCOPE)
project(adxl362)

target_sources(app PRIVATE src/main.c)
@@ -0,0 +1,49 @@
.. _adxl362:

ADXL362: Three Axis Accelerometer
#################################

Overview
********

This sample application demonstrates how to use the ADXL362 with data ready and
threshold triggers. The upper and lower threshold triggers are configured in
link mode with referenced detection. See the `ADXL362 Datasheet`_ for additional
details.

Building and Running
********************

This sample requires an ADXL362 sensor. It should work with any platform
featuring a I2C peripheral interface. It does not work on QEMU.

.. zephyr-app-commands::
:zephyr-app: samples/sensors/adxl362
:board: <board to use>
:goals: build flash
:compact:

Sample Output
=============

The application will print acceleration values to the console at the default
sampling rate of 12.5 Hz. Shake the board continuously to trigger an upper
threshold event. Stop shaking the board to trigger a lower threshold event. In
both cases, ``Threshold trigger`` will be printed to the console.

.. code-block:: console

x: -0.1, y: -0.0, z: 16.0 (m/s^2)
x: -1.0, y: 7.0, z: 21.0 (m/s^2)
Threshold trigger
x: -3.1, y: 4.0, z: 0.0 (m/s^2)
x: 1.1, y: 4.0, z: 15.1 (m/s^2)

References
**********

ADXL362 Datasheet and Product Info:
https://www.analog.com/en/products/adxl362.html

.. _ADXL362 Datasheet:
https://www.analog.com/media/en/technical-documentation/data-sheets/ADXL362.pdf
@@ -0,0 +1,7 @@
CONFIG_STDOUT_CONSOLE=y
CONFIG_SPI=y
CONFIG_SENSOR=y
CONFIG_ADXL362=y
CONFIG_ADXL362_TRIGGER_GLOBAL_THREAD=y
CONFIG_ADXL362_INTERRUPT_MODE=1
CONFIG_ADXL362_ABS_REF_MODE=1
@@ -0,0 +1,8 @@
sample:
name: adxl362 sample
description: ADXL362 accelerometer sample application
tests:
sample.sensor.adxl362:
harness: sensor
tags: sensors
depends_on: spi adxl362
@@ -0,0 +1,88 @@
/*
* Copyright (c) 2019 Brett Witherspoon
*
* SPDX-License-Identifier: Apache-2.0
*/

#include <zephyr.h>
#include <stdio.h>
#include <device.h>
#include <sensor.h>

K_SEM_DEFINE(sem, 0, 1);

static void trigger_handler(struct device *dev, struct sensor_trigger *trig)
{
switch (trig->type) {
case SENSOR_TRIG_DATA_READY:
if (sensor_sample_fetch(dev) < 0) {
printf("Sample fetch error\n");
return;
}
k_sem_give(&sem);
break;
case SENSOR_TRIG_THRESHOLD:
printf("Threshold trigger\n");
break;
default:
printf("Unknown trigger\n");
}
}

void main(void)
{
struct sensor_value accel[3];

struct device *dev = device_get_binding(DT_ADI_ADXL362_0_LABEL);
if (dev == NULL) {
printf("Device get binding device\n");
return;
}

if (IS_ENABLED(CONFIG_ADXL362_TRIGGER)) {
struct sensor_trigger trig = { .chan = SENSOR_CHAN_ACCEL_XYZ };

trig.type = SENSOR_TRIG_THRESHOLD;
if (sensor_trigger_set(dev, &trig, trigger_handler)) {
printf("Trigger set error\n");
return;
}

trig.type = SENSOR_TRIG_DATA_READY;
if (sensor_trigger_set(dev, &trig, trigger_handler)) {
printf("Trigger set error\n");
}
}

while (true) {
if (IS_ENABLED(CONFIG_ADXL362_TRIGGER)) {
k_sem_take(&sem, K_FOREVER);
} else {
k_sleep(1000);
if (sensor_sample_fetch(dev) < 0) {
printf("Sample fetch error\n");
return;
}
}

if (sensor_channel_get(dev, SENSOR_CHAN_ACCEL_X, &accel[0]) < 0) {
printf("Channel get error\n");
return;
}

if (sensor_channel_get(dev, SENSOR_CHAN_ACCEL_Y, &accel[1]) < 0) {
printf("Channel get error\n");
return;
}

if (sensor_channel_get(dev, SENSOR_CHAN_ACCEL_Z, &accel[2]) < 0) {
printf("Channel get error\n");
return;
}

printf("x: %.1f, y: %.1f, z: %.1f (m/s^2)\n",
sensor_value_to_double(&accel[0]),
sensor_value_to_double(&accel[1]),
sensor_value_to_double(&accel[2]));
}
}

0 comments on commit 07d21be

Please sign in to comment.
You can’t perform that action at this time.