From e517dfd672b0c7c30d08627c610bf8bbafb18d04 Mon Sep 17 00:00:00 2001 From: Robert-Ionut Alexa Date: Sun, 9 Oct 2022 16:20:29 +0300 Subject: [PATCH] apps/ltr308: add simple application to test ltr308 sensor Signed-off-by: Robert-Ionut Alexa --- examples/ltr308/Kconfig | 30 ++++++++++++ examples/ltr308/Make.defs | 23 +++++++++ examples/ltr308/Makefile | 34 +++++++++++++ examples/ltr308/ltr308_main.c | 92 +++++++++++++++++++++++++++++++++++ 4 files changed, 179 insertions(+) create mode 100644 examples/ltr308/Kconfig create mode 100644 examples/ltr308/Make.defs create mode 100644 examples/ltr308/Makefile create mode 100644 examples/ltr308/ltr308_main.c diff --git a/examples/ltr308/Kconfig b/examples/ltr308/Kconfig new file mode 100644 index 000000000..05148af33 --- /dev/null +++ b/examples/ltr308/Kconfig @@ -0,0 +1,30 @@ +# +# For a description of the syntax of this configuration file, +# see the file kconfig-language.txt in the NuttX tools repository. +# + +config EXAMPLES_LTR308 + tristate "LTR308 ambient light sensor example" + default n + depends on SENSORS_LTR308 + ---help--- + Enable the LTR308 example + +if EXAMPLES_LTR308 + +config EXAMPLES_LTR308_PROGNAME + string "Program name" + default "ltr308" + ---help--- + This is the name of the program that will be used when the NSH ELF + program is installed. + +config EXAMPLES_LTR308_PRIORITY + int "LTR308 task priority" + default 100 + +config EXAMPLES_LTR308_STACKSIZE + int "LTR308 stack size" + default DEFAULT_TASK_STACKSIZE + +endif diff --git a/examples/ltr308/Make.defs b/examples/ltr308/Make.defs new file mode 100644 index 000000000..6fc8ea336 --- /dev/null +++ b/examples/ltr308/Make.defs @@ -0,0 +1,23 @@ +############################################################################ +# apps/examples/ltr308/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_LTR308),) +CONFIGURED_APPS += $(APPDIR)/examples/ltr308 +endif diff --git a/examples/ltr308/Makefile b/examples/ltr308/Makefile new file mode 100644 index 000000000..3eef9a3fc --- /dev/null +++ b/examples/ltr308/Makefile @@ -0,0 +1,34 @@ +############################################################################ +# apps/examples/ltr308/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 $(APPDIR)/Make.defs + +# LTR308 ambient light sensor example built-in application info + +PROGNAME = $(CONFIG_EXAMPLES_LTR308_PROGNAME) +PRIORITY = $(CONFIG_EXAMPLES_LTR308_PRIORITY) +STACKSIZE = $(CONFIG_EXAMPLES_LTR308_STACKSIZE) +MODULE = $(CONFIG_EXAMPLES_LTR308) + +# LTR308 ambient light sensor example + +MAINSRC = ltr308_main.c + +include $(APPDIR)/Application.mk diff --git a/examples/ltr308/ltr308_main.c b/examples/ltr308/ltr308_main.c new file mode 100644 index 000000000..43b9bc153 --- /dev/null +++ b/examples/ltr308/ltr308_main.c @@ -0,0 +1,92 @@ +/**************************************************************************** + * apps/examples/ltr308/ltr308_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 +#include +#include +#include + +#include +#include +#include +#include +#include + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * ltr308_main + ****************************************************************************/ + +int main(int argc, FAR char *argv[]) +{ + struct ltr308_calibval calibval; + struct sensor_light light; + struct pollfd pfd; + int ret; + int fd; + + fd = open("/dev/uorb/sensor_light0", O_RDONLY); + if (fd < 0) + { + perror("Could not open file"); + return fd; + } + + calibval.integration_time = 1; + calibval.measurement_rate = 2; + calibval.gain = 1; + ret = ioctl(fd, SNIOC_CALIBRATE, &calibval); + if (ret < 0) + { + perror("Could not calibrate sensor"); + goto err_out; + } + + memset(&pfd, 0, sizeof(struct pollfd)); + pfd.fd = fd; + pfd.events = POLLIN; + ret = poll(&pfd, 1, -1); + if (ret < 0) + { + perror("Could not poll sensor"); + goto err_out; + } + + ret = read(fd, &light, sizeof(struct sensor_light)); + if (ret < 0) + { + perror("Could not read from sensor"); + goto err_out; + } + + printf("timestamp: %llu, lux: %f\n", light.timestamp, light.light); + return OK; + +err_out: + close(fd); + return ret; +}