Skip to content

examples/tmp112: Add new example for TMP112 temperature sensor #3162

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions examples/tmp112/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# ##############################################################################
# apps/examples/tmp112/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_TMP112)
nuttx_add_application(
NAME
${CONFIG_EXAMPLES_TMP112_PROGNAME}
PRIORITY
${CONFIG_EXAMPLES_TMP112_PRIORITY}
STACKSIZE
${CONFIG_EXAMPLES_TMP112_STACKSIZE}
MODULE
${CONFIG_EXAMPLES_TMP112}
SRCS
tmp112_main.c)
endif()
57 changes: 57 additions & 0 deletions examples/tmp112/Kconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#
# For a description of the syntax of this configuration file,
# see the file kconfig-language.txt in the NuttX tools repository.
#

config EXAMPLES_TMP112
tristate "TMP112 temperature sensor example"
default n
depends on SENSORS_TMP112
---help---
Enable the TMP112 example

if EXAMPLES_TMP112

config EXAMPLES_TMP112_PROGNAME
string "Program name"
default "tmp112"
---help---
This is the name of the program that will be used when the NSH ELF
program is installed.

config EXAMPLES_TMP112_PRIORITY
int "TMP112 task priority"
default 100

config EXAMPLES_TMP112_STACKSIZE
int "TMP112 stack size"
default DEFAULT_TASK_STACKSIZE

config EXAMPLES_TMP112_DEVPATH
string "TMP112 device path"
default "/dev/temp0"
---help---
The default path to the first TMP112 temperature sensor device

config EXAMPLES_TMP112_DEVPATH_2
string "TMP112 device path (second sensor)"
default "/dev/temp1"
depends on TMP112_ENABLE_2
---help---
The default path to the second TMP112 temperature sensor device

config EXAMPLES_TMP112_DEVPATH_3
string "TMP112 device path (third sensor)"
default "/dev/temp2"
depends on TMP112_ENABLE_3
---help---
The default path to the third TMP112 temperature sensor device

config EXAMPLES_TMP112_DEVPATH_4
string "TMP112 device path (fourth sensor)"
default "/dev/temp3"
depends on TMP112_ENABLE_4
---help---
The default path to the fourth TMP112 temperature sensor device

endif
25 changes: 25 additions & 0 deletions examples/tmp112/Make.defs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
############################################################################
# apps/examples/tmp112/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_TMP112),)
CONFIGURED_APPS += $(APPDIR)/examples/tmp112
endif
36 changes: 36 additions & 0 deletions examples/tmp112/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
############################################################################
# apps/examples/tmp112/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

# TMP112 temperature sensor example built-in application info

PROGNAME = $(CONFIG_EXAMPLES_TMP112_PROGNAME)
PRIORITY = $(CONFIG_EXAMPLES_TMP112_PRIORITY)
STACKSIZE = $(CONFIG_EXAMPLES_TMP112_STACKSIZE)
MODULE = $(CONFIG_EXAMPLES_TMP112)

# TMP112 temperature sensor example

MAINSRC = tmp112_main.c

include $(APPDIR)/Application.mk
105 changes: 105 additions & 0 deletions examples/tmp112/tmp112_main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/****************************************************************************
* apps/examples/tmp112/tmp112_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>

/****************************************************************************
* Private Functions
****************************************************************************/

/****************************************************************************
* Name: tmp112_read_temperature
****************************************************************************/

static int tmp112_read_temperature(FAR const char *devpath, int devno)
{
int fd;
int ret;
float sample;

fd = open(devpath, O_RDONLY);
if (fd < 0)
{
printf("Failed to open TMP112 sensor #%d at %s: %s (%d)\n",
devno, devpath, strerror(errno), errno);
return ERROR;
}

ret = read(fd, &sample, sizeof(float));
if (ret != sizeof(sample))
{
perror("Could not read");
return ERROR;
}

printf("Sensor #%d = %.03f degrees Celsius\n", devno, sample);

close(fd);

return OK;
}

/****************************************************************************
* Public Functions
****************************************************************************/

/****************************************************************************
* Name: tmp112_main
****************************************************************************/

int main(int argc, FAR char *argv[])
{
if (tmp112_read_temperature(CONFIG_EXAMPLES_TMP112_DEVPATH, 1) < 0)
{
return EXIT_FAILURE;
}

#ifdef CONFIG_TMP112_ENABLE_2
if (tmp112_read_temperature(CONFIG_EXAMPLES_TMP112_DEVPATH_2, 2) < 0)
{
return EXIT_FAILURE;
}
#endif

#ifdef CONFIG_TMP112_ENABLE_3
if (tmp112_read_temperature(CONFIG_EXAMPLES_TMP112_DEVPATH_3, 3) < 0)
{
return EXIT_FAILURE;
}
#endif

#ifdef CONFIG_TMP112_ENABLE_4
if (tmp112_read_temperature(CONFIG_EXAMPLES_TMP112_DEVPATH_4, 4) < 0)
{
return EXIT_FAILURE;
}
#endif

return EXIT_SUCCESS;
}
Loading