examples/ads7046: Add new example for ADS7046 ADC

Add a new example that shows how to read a sample from
an ADS7046 ADC sensor registered on the SPI bus.

Signed-off-by: Niccolò Maggioni <nicco.maggioni+nuttx@gmail.com>
diff --git a/examples/ads7046/CMakeLists.txt b/examples/ads7046/CMakeLists.txt
new file mode 100644
index 0000000..6e875a1
--- /dev/null
+++ b/examples/ads7046/CMakeLists.txt
@@ -0,0 +1,35 @@
+# ##############################################################################
+# apps/examples/ads7046/CMakeLists.txt
+#
+# SPDX-License-Identifier: Apache-2.0
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more contributor
+# license agreements.  See the NOTICE file distributed with this work for
+# additional information regarding copyright ownership.  The ASF licenses this
+# file to you under the Apache License, Version 2.0 (the "License"); you may not
+# use this file except in compliance with the License.  You may obtain a copy of
+# the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+# License for the specific language governing permissions and limitations under
+# the License.
+#
+# ##############################################################################
+
+if(CONFIG_EXAMPLES_ADS7046)
+  nuttx_add_application(
+    NAME
+    ${CONFIG_EXAMPLES_ADS7046_PROGNAME}
+    PRIORITY
+    ${CONFIG_EXAMPLES_ADS7046_PRIORITY}
+    STACKSIZE
+    ${CONFIG_EXAMPLES_ADS7046_STACKSIZE}
+    MODULE
+    ${CONFIG_EXAMPLES_ADS7046}
+    SRCS
+    ads7046_main.c)
+endif()
diff --git a/examples/ads7046/Kconfig b/examples/ads7046/Kconfig
new file mode 100644
index 0000000..c2d825a
--- /dev/null
+++ b/examples/ads7046/Kconfig
@@ -0,0 +1,36 @@
+#
+# For a description of the syntax of this configuration file,
+# see the file kconfig-language.txt in the NuttX tools repository.
+#
+
+config EXAMPLES_ADS7046
+	tristate "ADS7046 ADC example"
+	default n
+	depends on ADC_ADS7046
+	---help---
+		Enable the ADS7046 example
+
+if EXAMPLES_ADS7046
+
+config EXAMPLES_ADS7046_PROGNAME
+	string "Program name"
+	default "ads7046"
+	---help---
+		This is the name of the program that will be used when the NSH ELF
+		program is installed.
+
+config EXAMPLES_ADS7046_PRIORITY
+	int "ADS7046 task priority"
+	default 100
+
+config EXAMPLES_ADS7046_STACKSIZE
+	int "ADS7046 stack size"
+	default DEFAULT_TASK_STACKSIZE
+
+config EXAMPLES_ADS7046_DEVPATH
+	string "ADS7046 device path"
+	default "/dev/adc0"
+	---help---
+		The default path to the ADS7046 ADC device
+
+endif
diff --git a/examples/ads7046/Make.defs b/examples/ads7046/Make.defs
new file mode 100644
index 0000000..171c570
--- /dev/null
+++ b/examples/ads7046/Make.defs
@@ -0,0 +1,25 @@
+############################################################################
+# apps/examples/ads7046/Make.defs
+#
+# SPDX-License-Identifier: Apache-2.0
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.  The
+# ASF licenses this file to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance with the
+# License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+# License for the specific language governing permissions and limitations
+# under the License.
+#
+############################################################################
+
+ifneq ($(CONFIG_EXAMPLES_ADS7046),)
+CONFIGURED_APPS += $(APPDIR)/examples/ads7046
+endif
diff --git a/examples/ads7046/Makefile b/examples/ads7046/Makefile
new file mode 100644
index 0000000..310ccea
--- /dev/null
+++ b/examples/ads7046/Makefile
@@ -0,0 +1,36 @@
+############################################################################
+# apps/examples/ads7046/Makefile
+#
+# SPDX-License-Identifier: Apache-2.0
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.  The
+# ASF licenses this file to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance with the
+# License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+# License for the specific language governing permissions and limitations
+# under the License.
+#
+############################################################################
+
+include $(APPDIR)/Make.defs
+
+# ADS7046 ADC example built-in application info
+
+PROGNAME = $(CONFIG_EXAMPLES_ADS7046_PROGNAME)
+PRIORITY = $(CONFIG_EXAMPLES_ADS7046_PRIORITY)
+STACKSIZE = $(CONFIG_EXAMPLES_ADS7046_STACKSIZE)
+MODULE = $(CONFIG_EXAMPLES_ADS7046)
+
+# ADS7046 ADC example
+
+MAINSRC = ads7046_main.c
+
+include $(APPDIR)/Application.mk
diff --git a/examples/ads7046/ads7046_main.c b/examples/ads7046/ads7046_main.c
new file mode 100644
index 0000000..1b0ca72
--- /dev/null
+++ b/examples/ads7046/ads7046_main.c
@@ -0,0 +1,90 @@
+/****************************************************************************
+ * apps/examples/ads7046/ads7046_main.c
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <sys/ioctl.h>
+#include <nuttx/analog/ioctl.h>
+#include <nuttx/analog/ads7046.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/* As per the datasheet, the ADS7046 returns the following readings:
+ * INPUT VOLTAGE (AINP - AINM)               DESCRIPTION                 HEX
+ * --------------------------------------    ------------------------    ---
+ * <= 1 LSB                                  Negative full-scale code    000
+ * 1 LSB to 2 LSB                            -                           001
+ * V_REF / 2 to V_REF / 2 + 1 LSB            Mid code                    7FF
+ * V_REF / 2 + 1 LSB to V_REF / 2 + 2 LSB    -                           800
+ * >= V_REF - 1 LSB                          Positive full-scale code    FFF
+ */
+
+#define SAMPLE_TO_PCT(sample)  (((sample) * 100) / 0xfff)
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: ads7046_main
+ ****************************************************************************/
+
+int main(const int argc, FAR char *argv[])
+{
+  UNUSED(argc);
+  UNUSED(argv);
+
+  int ret;
+  int fd;
+  uint16_t sample;
+
+  fd = open(CONFIG_EXAMPLES_ADS7046_DEVPATH, O_RDONLY);
+  if (fd < 0)
+    {
+      printf("Failed to open %s: %s (%d)\n", CONFIG_EXAMPLES_ADS7046_DEVPATH,
+             strerror(errno), errno);
+      return EXIT_FAILURE;
+    }
+
+  ret = ioctl(fd, ANIOC_ADS7046_READ, &sample);
+  if (ret != OK)
+    {
+      perror("Could not ioctl fd");
+      close(fd);
+      return EXIT_FAILURE;
+    }
+
+  printf("ADS7046: hex=%x, dec=%"PRIu16", adc_percentage=%u%%\n",
+         sample, sample, SAMPLE_TO_PCT(sample));
+
+  close(fd);
+
+  return EXIT_SUCCESS;
+}