Commit 94721b31 authored by Jamie McCrae's avatar Jamie McCrae Committed by Maureen Helm
Browse files

samples: sensor: sm351lt: Add new SM351LT sensor sample



Adds sample for SM351LT sensor and outputs to console.

Signed-off-by: default avatarJamie McCrae <jamie.mccrae@lairdconnect.com>
parent 030456c2
Loading
Loading
Loading
Loading
+12 −0
Original line number Diff line number Diff line
#
# Copyright (c) 2020, Laird Connectivity
#
# SPDX-License-Identifier: Apache-2.0
#

cmake_minimum_required(VERSION 3.13.1)
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(sm351lt)

FILE(GLOB app_sources src/*.c)
target_sources(app PRIVATE ${app_sources})
+51 −0
Original line number Diff line number Diff line
.. _sm351lt:

SM351LT: Magnetoresistive Sensor Example
########################################

Overview
********

This sample application periodically polls an SM351LT magnetoresistive sensor
and displays if a magnet near to the sensor, via the console.

Requirements
************

This sample uses the Honeywell SM351LT sensor.

References
**********

For more information about the SM351LT magnetoresistive sensor see
https://sensing.honeywell.com/SM351LT-low-power

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

The SM351LT (or compatible) sensors are available on the following boards:

* :ref:`bt510`

Building on bt510
==================

:ref:`bt510` includes a Honeywell SM351LT magnetoresistive sensor.

.. zephyr-app-commands::
   :zephyr-app: samples/sensor/sm351lt
   :board: bt510
   :goals: build flash
   :compact:

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

.. code-block:: console

    Polling at 0.5 Hz
    #1 @ 6 ms: 1
    #2 @ 2007 ms: 0
    #3 @ 4009 ms: 0
    #4 @ 6010 ms: 1
    #5 @ 8012 ms: 1
+4 −0
Original line number Diff line number Diff line
CONFIG_STDOUT_CONSOLE=y
CONFIG_GPIO=y
CONFIG_SENSOR=y
CONFIG_SM351LT=y
+7 −0
Original line number Diff line number Diff line
sample:
  name: SM351LT Magnetoresistive Sensor Example
tests:
  sample.sensor.sm351lt:
    harness: sensor
    tags: samples sensor
    depends_on: gpio sm351lt
+90 −0
Original line number Diff line number Diff line
/*
 * Copyright (c) 2020 Laird Connectivity
 * Based on code by Nordic Semiconductor ASA
 *
 * SPDX-License-Identifier: Apache-2.0
 */

#include <stdio.h>
#include <zephyr.h>
#include <device.h>
#include <drivers/gpio.h>
#include <drivers/sensor.h>

static void fetch_and_display(struct device *sensor)
{
	static unsigned int count;
	struct sensor_value mag;
	int rc = sensor_sample_fetch(sensor);

	++count;
	if (rc == 0) {
		rc = sensor_channel_get(sensor,
					SENSOR_CHAN_PROX,
					&mag);
	}
	if (rc < 0) {
		printf("ERROR: Update failed: %d\n", rc);
	} else {
		printf("#%u @ %u ms: %d\n",
		       count, k_uptime_get_32(), mag.val1);
	}
}

#ifdef CONFIG_SM351LT_TRIGGER
static void trigger_handler(struct device *dev,
			    struct sensor_trigger *trig)
{
	fetch_and_display(dev);
}
#endif

void main(void)
{
	struct device *sensor = device_get_binding(DT_LABEL(DT_INST(0, honeywell_sm351lt)));

	if (sensor == NULL) {
		printf("Could not get %s device\n",
		       DT_LABEL(DT_INST(0, honeywell_sm351lt)));
		return;
	}

#if CONFIG_SM351LT_TRIGGER
	{
		struct sensor_trigger trig;
		int rc;

		trig.type = SENSOR_TRIG_NEAR_FAR;
		trig.chan = SENSOR_CHAN_PROX;

		struct sensor_value trigger_type = {
			.val1 = GPIO_INT_EDGE_BOTH,
		};

		rc = sensor_attr_set(sensor, trig.chan,
				     SENSOR_ATTR_PRIV_START,
				     &trigger_type);
		if (rc != 0) {
			printf("Failed to set trigger type: %d\n", rc);
			return;
		}

		rc = sensor_trigger_set(sensor, &trig, trigger_handler);
		if (rc != 0) {
			printf("Failed to set trigger: %d\n", rc);
			return;
		}

		printf("Waiting for triggers\n");
		while (true) {
			k_sleep(K_MSEC(2000));
		}
	}
#else /* CONFIG_SM351LT_TRIGGER */
	printf("Polling at 0.5 Hz\n");
	while (true) {
		fetch_and_display(sensor);
		k_sleep(K_MSEC(2000));
	}
#endif /* CONFIG_SM351LT_TRIGGER */
}