Skip to content

Commit d697d26

Browse files
mnkpcarlescufi
authored andcommitted
samples: sensor: Add simple quadrature decoder demo
Add simple application to demonstrate quadrature decoder sensor. Tested on Atmel SMART SAM E70 Xplained board. Signed-off-by: Piotr Mienkowski <[email protected]>
1 parent f5163e2 commit d697d26

File tree

7 files changed

+109
-0
lines changed

7 files changed

+109
-0
lines changed

samples/sensor/qdec/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
cmake_minimum_required(VERSION 3.13.1)
4+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
5+
project(thermometer)
6+
7+
FILE(GLOB app_sources src/*.c)
8+
target_sources(app PRIVATE ${app_sources})

samples/sensor/qdec/README.rst

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
.. _qdec:
2+
3+
QDEC: Quadrature Decoder
4+
###########################
5+
6+
Overview
7+
********
8+
A simple quadrature decoder example
9+
10+
Building and Running
11+
********************
12+
13+
This project writes the quadrature decoder position to the console once every
14+
2 seconds.
15+
16+
Building on SAM E70 Xplained board
17+
==================================
18+
19+
.. zephyr-app-commands::
20+
:zephyr-app: samples/sensor/qdec
21+
:host-os: unix
22+
:board: sam_e70_xplained
23+
:goals: build
24+
:compact:
25+
26+
Sample Output
27+
=============
28+
29+
.. code-block:: console
30+
31+
Quadrature Decoder sample application
32+
33+
Position is 6
34+
Position is 12
35+
Position is -45
36+
37+
<repeats endlessly every 2 seconds>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CONFIG_QDEC_SAM=y
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/*
2+
* Copyright (c) 2021, Piotr Mienkowski
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
&tc0 {
8+
status = "okay";
9+
pinctrl-0 = <&pa0b_tc0_tioa0 &pa1b_tc0_tiob0>;
10+
label = "QDEC_0";
11+
};

samples/sensor/qdec/prj.conf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
CONFIG_SENSOR=y
2+
CONFIG_STDOUT_CONSOLE=y

samples/sensor/qdec/sample.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
sample:
2+
name: Quadrature Decoder
3+
tests:
4+
sample.sensor.qdec:
5+
tags: sensors
6+
harness: sensor
7+
platform_allow: sam_e70_xplained

samples/sensor/qdec/src/main.c

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright (c) 2021, Piotr Mienkowski
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <zephyr.h>
8+
#include <drivers/sensor.h>
9+
#include <stdio.h>
10+
11+
void main(void)
12+
{
13+
const struct device *dev;
14+
int ret;
15+
16+
printf("Quadrature Decoder sample application\n\n");
17+
18+
dev = device_get_binding("QDEC_0");
19+
if (!dev) {
20+
printf("error: no QDEC_0 device\n");
21+
return;
22+
}
23+
24+
while (1) {
25+
struct sensor_value value;
26+
27+
ret = sensor_sample_fetch(dev);
28+
if (ret) {
29+
printf("sensor_sample_fetch failed returns: %d\n", ret);
30+
break;
31+
}
32+
33+
ret = sensor_channel_get(dev, SENSOR_CHAN_ROTATION, &value);
34+
if (ret) {
35+
printf("sensor_channel_get failed returns: %d\n", ret);
36+
break;
37+
}
38+
39+
printf("Position is %d\n", value.val1);
40+
41+
k_sleep(K_MSEC(2000));
42+
}
43+
}

0 commit comments

Comments
 (0)