You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by ag...@apache.org on 2020/05/07 02:09:56 UTC

[incubator-nuttx-apps] branch master updated: Example application for the hdc1008 driver.

This is an automated email from the ASF dual-hosted git repository.

aguettouche pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx-apps.git


The following commit(s) were added to refs/heads/master by this push:
     new 0704bd2  Example application for the hdc1008 driver.
0704bd2 is described below

commit 0704bd2e49d2fa5bc6630ce4f9e98aa221a11899
Author: Pelle <ic...@gmail.com>
AuthorDate: Mon May 4 21:13:22 2020 +0200

    Example application for the hdc1008 driver.
---
 examples/hdc1008_demo/Kconfig        |  11 +++
 examples/hdc1008_demo/Make.defs      |  23 +++++
 examples/hdc1008_demo/Makefile       |  32 +++++++
 examples/hdc1008_demo/hdc1008_main.c | 169 +++++++++++++++++++++++++++++++++++
 4 files changed, 235 insertions(+)

diff --git a/examples/hdc1008_demo/Kconfig b/examples/hdc1008_demo/Kconfig
new file mode 100644
index 0000000..fbf736f
--- /dev/null
+++ b/examples/hdc1008_demo/Kconfig
@@ -0,0 +1,11 @@
+#
+# For a description of the syntax of this configuration file,
+# see the file kconfig-language.txt in the NuttX tools repository.
+#
+
+config EXAMPLES_HDC1008
+	tristate "HDC1008 driver example"
+	default n
+	---help---
+		Enable the example application 
+
diff --git a/examples/hdc1008_demo/Make.defs b/examples/hdc1008_demo/Make.defs
new file mode 100644
index 0000000..fa1dd8e
--- /dev/null
+++ b/examples/hdc1008_demo/Make.defs
@@ -0,0 +1,23 @@
+############################################################################
+# apps/examples/hdc1008_demo/Make.defs
+#
+# 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_HDC1008),)
+CONFIGURED_APPS += $(APPDIR)/examples/hdc1008_demo
+endif
diff --git a/examples/hdc1008_demo/Makefile b/examples/hdc1008_demo/Makefile
new file mode 100644
index 0000000..1921ca6
--- /dev/null
+++ b/examples/hdc1008_demo/Makefile
@@ -0,0 +1,32 @@
+############################################################################
+# apps/examples/hdc1008_demo/Makefile
+#
+# 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 $(TOPDIR)/Make.defs
+
+# hdc1008 example built-in application info
+
+PROGNAME  = hdc1008
+PRIORITY  = SCHED_PRIORITY_DEFAULT
+STACKSIZE = $(CONFIG_DEFAULT_TASK_STACKSIZE)
+MODULE    = $(CONFIG_EXAMPLES_HDC1008)
+
+MAINSRC = hdc1008_main.c
+
+include $(APPDIR)/Application.mk
diff --git a/examples/hdc1008_demo/hdc1008_main.c b/examples/hdc1008_demo/hdc1008_main.c
new file mode 100644
index 0000000..e669466
--- /dev/null
+++ b/examples/hdc1008_demo/hdc1008_main.c
@@ -0,0 +1,169 @@
+/****************************************************************************
+ * examples/hdc1008_demo/hdc1008_main.c
+ *
+ * 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 <nuttx/sensors/hdc1008.h>
+
+#include <sys/ioctl.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+
+#include <fcntl.h>
+#include <stdio.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define DEV_PATH  "/dev/hdc1008"
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * hdc1008_main
+ ****************************************************************************/
+
+int main(int argc, FAR char *argv[])
+{
+  struct hdc1008_conv_data_s data;
+  int fd;
+  int ret;
+  int i;
+  uint8_t res[3];
+  char buf[16];
+
+  /* Available resolutions: 8/11/14 bits */
+
+  res[0] = 8;
+  res[1] = 11;
+  res[2] = 14;
+
+  fd = open(DEV_PATH, O_RDONLY);
+  if (fd < 0)
+    {
+      printf("Failed to open device driver at '%s'\n", DEV_PATH);
+    }
+
+  /* Read both t and rh */
+
+  ret = read(fd, buf, sizeof(buf));
+  if (ret < 0)
+    {
+      printf("FAILED to read temperature and rh: %d\n", errno);
+      goto out;
+    }
+
+  printf("Temperature and humidity\n"
+         "========================\n");
+  printf("data=%s%d\n\n", buf);
+
+  /* Measure using ioctl */
+
+  ret = ioctl(fd, SNIOC_MEASURE, (unsigned long)&data);
+  if (ret < 0)
+    {
+      printf("Failed to measure using ioctl: %d\n", errno);
+      goto out;
+    }
+
+  printf("Temperature and humidity (ioctl)\n"
+         "================================\n");
+  printf("t=%d.%d, h=%d.%d\n\n",
+          data.temperature / 100, data.temperature % 100,
+          data.humidity / 10, data.humidity % 10);
+
+  /* Enable heater */
+
+  ret = ioctl(fd, SNIOC_SET_HEATER_MODE, 1);
+  if (ret < 0)
+    {
+      printf("Failed to enable heater: %d\n", errno);
+      goto out;
+    }
+
+  /* Read only t */
+
+  ret = ioctl(fd, SNIOC_SET_OPERATIONAL_MODE, HDC1008_MEAS_TEMPERATURE);
+  if (ret < 0)
+    {
+      printf("Failed to set temperature measurement only: %d\n", errno);
+      goto out;
+    }
+
+  for (i = 1; i < 3; ++i)
+    {
+      ret = ioctl(fd, SNIOC_SET_RESOLUTION_T, res[i]);
+      if (ret < 0)
+        {
+          printf("Failed to set temperature resolution: %d\n", errno);
+          goto out;
+        }
+
+      ret = read(fd, buf, sizeof(buf));
+      printf("Temperature, %d bit resolution\n"
+             "==============================\n", res[i]);
+      printf("data=%s\n\n", buf);
+    }
+
+  /* Disable heater */
+
+  ret = ioctl(fd, SNIOC_SET_HEATER_MODE, 0);
+  if (ret < 0)
+    {
+      printf("Failed to disable heater: %d\n", errno);
+      goto out;
+    }
+
+  /* Read only rh */
+
+  ret = ioctl(fd, SNIOC_SET_OPERATIONAL_MODE, HDC1008_MEAS_HUMIDITY);
+  if (ret < 0)
+    {
+      printf("Failed to set humidity measurement only: %d\n", errno);
+      goto out;
+    }
+
+  for (i = 0; i < 3; ++i)
+    {
+      ret = ioctl(fd, SNIOC_SET_RESOLUTION_RH, res[i]);
+      if (ret < 0)
+        {
+          printf("Failed to set humidity resolution: %d\n", errno);
+          goto out;
+        }
+
+      ret = read(fd, buf, sizeof(buf));
+      printf("Humidity, %d bit resolution\n"
+             "===========\n", res[i]);
+      printf("data=%s\n\n", buf);
+    }
+
+  ret = 0;
+
+out:
+  close(fd);
+  return ret;
+}