Skip to content

Commit 63c9ac0

Browse files
committed
examples/tmp112: Add new example for TMP112 temperature sensor
Add a new example that shows how to read the temperature from a TMP112 sensor registered on the I2C bus. Signed-off-by: Niccolò Maggioni <[email protected]>
1 parent d8dcc99 commit 63c9ac0

File tree

5 files changed

+258
-0
lines changed

5 files changed

+258
-0
lines changed

examples/tmp112/CMakeLists.txt

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# ##############################################################################
2+
# apps/examples/tmp112/CMakeLists.txt
3+
#
4+
# SPDX-License-Identifier: Apache-2.0
5+
#
6+
# Licensed to the Apache Software Foundation (ASF) under one or more contributor
7+
# license agreements. See the NOTICE file distributed with this work for
8+
# additional information regarding copyright ownership. The ASF licenses this
9+
# file to you under the Apache License, Version 2.0 (the "License"); you may not
10+
# use this file except in compliance with the License. You may obtain a copy of
11+
# the License at
12+
#
13+
# http://www.apache.org/licenses/LICENSE-2.0
14+
#
15+
# Unless required by applicable law or agreed to in writing, software
16+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
18+
# License for the specific language governing permissions and limitations under
19+
# the License.
20+
#
21+
# ##############################################################################
22+
23+
if(CONFIG_EXAMPLES_TMP112)
24+
nuttx_add_application(
25+
NAME
26+
${CONFIG_EXAMPLES_TMP112_PROGNAME}
27+
PRIORITY
28+
${CONFIG_EXAMPLES_TMP112_PRIORITY}
29+
STACKSIZE
30+
${CONFIG_EXAMPLES_TMP112_STACKSIZE}
31+
MODULE
32+
${CONFIG_EXAMPLES_TMP112}
33+
SRCS
34+
tmp112_main.c)
35+
endif()

examples/tmp112/Kconfig

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#
2+
# For a description of the syntax of this configuration file,
3+
# see the file kconfig-language.txt in the NuttX tools repository.
4+
#
5+
6+
config EXAMPLES_TMP112
7+
tristate "TMP112 temperature sensor example"
8+
default n
9+
depends on SENSORS_TMP112
10+
---help---
11+
Enable the TMP112 example
12+
13+
if EXAMPLES_TMP112
14+
15+
config EXAMPLES_TMP112_PROGNAME
16+
string "Program name"
17+
default "tmp112"
18+
---help---
19+
This is the name of the program that will be used when the NSH ELF
20+
program is installed.
21+
22+
config EXAMPLES_TMP112_PRIORITY
23+
int "TMP112 task priority"
24+
default 100
25+
26+
config EXAMPLES_TMP112_STACKSIZE
27+
int "TMP112 stack size"
28+
default DEFAULT_TASK_STACKSIZE
29+
30+
config EXAMPLES_TMP112_DEVPATH
31+
string "TMP112 device path"
32+
default "/dev/temp0"
33+
---help---
34+
The default path to the first TMP112 temperature sensor device
35+
36+
config EXAMPLES_TMP112_DEVPATH_2
37+
string "TMP112 device path (second sensor)"
38+
default "/dev/temp1"
39+
depends on TMP112_ENABLE_2
40+
---help---
41+
The default path to the second TMP112 temperature sensor device
42+
43+
config EXAMPLES_TMP112_DEVPATH_3
44+
string "TMP112 device path (third sensor)"
45+
default "/dev/temp2"
46+
depends on TMP112_ENABLE_3
47+
---help---
48+
The default path to the third TMP112 temperature sensor device
49+
50+
config EXAMPLES_TMP112_DEVPATH_4
51+
string "TMP112 device path (fourth sensor)"
52+
default "/dev/temp3"
53+
depends on TMP112_ENABLE_4
54+
---help---
55+
The default path to the fourth TMP112 temperature sensor device
56+
57+
endif

examples/tmp112/Make.defs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
############################################################################
2+
# apps/examples/tmp112/Make.defs
3+
#
4+
# SPDX-License-Identifier: Apache-2.0
5+
#
6+
# Licensed to the Apache Software Foundation (ASF) under one or more
7+
# contributor license agreements. See the NOTICE file distributed with
8+
# this work for additional information regarding copyright ownership. The
9+
# ASF licenses this file to you under the Apache License, Version 2.0 (the
10+
# "License"); you may not use this file except in compliance with the
11+
# License. You may obtain a copy of the License at
12+
#
13+
# http://www.apache.org/licenses/LICENSE-2.0
14+
#
15+
# Unless required by applicable law or agreed to in writing, software
16+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
18+
# License for the specific language governing permissions and limitations
19+
# under the License.
20+
#
21+
############################################################################
22+
23+
ifneq ($(CONFIG_EXAMPLES_TMP112),)
24+
CONFIGURED_APPS += $(APPDIR)/examples/tmp112
25+
endif

examples/tmp112/Makefile

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
############################################################################
2+
# apps/examples/tmp112/Makefile
3+
#
4+
# SPDX-License-Identifier: Apache-2.0
5+
#
6+
# Licensed to the Apache Software Foundation (ASF) under one or more
7+
# contributor license agreements. See the NOTICE file distributed with
8+
# this work for additional information regarding copyright ownership. The
9+
# ASF licenses this file to you under the Apache License, Version 2.0 (the
10+
# "License"); you may not use this file except in compliance with the
11+
# License. You may obtain a copy of the License at
12+
#
13+
# http://www.apache.org/licenses/LICENSE-2.0
14+
#
15+
# Unless required by applicable law or agreed to in writing, software
16+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
18+
# License for the specific language governing permissions and limitations
19+
# under the License.
20+
#
21+
############################################################################
22+
23+
include $(APPDIR)/Make.defs
24+
25+
# TMP112 temperature sensor example built-in application info
26+
27+
PROGNAME = $(CONFIG_EXAMPLES_TMP112_PROGNAME)
28+
PRIORITY = $(CONFIG_EXAMPLES_TMP112_PRIORITY)
29+
STACKSIZE = $(CONFIG_EXAMPLES_TMP112_STACKSIZE)
30+
MODULE = $(CONFIG_EXAMPLES_TMP112)
31+
32+
# TMP112 temperature sensor example
33+
34+
MAINSRC = tmp112_main.c
35+
36+
include $(APPDIR)/Application.mk

examples/tmp112/tmp112_main.c

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/****************************************************************************
2+
* apps/examples/tmp112/tmp112_main.c
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Licensed to the Apache Software Foundation (ASF) under one or more
7+
* contributor license agreements. See the NOTICE file distributed with
8+
* this work for additional information regarding copyright ownership. The
9+
* ASF licenses this file to you under the Apache License, Version 2.0 (the
10+
* "License"); you may not use this file except in compliance with the
11+
* License. You may obtain a copy of the License at
12+
*
13+
* http://www.apache.org/licenses/LICENSE-2.0
14+
*
15+
* Unless required by applicable law or agreed to in writing, software
16+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
18+
* License for the specific language governing permissions and limitations
19+
* under the License.
20+
*
21+
****************************************************************************/
22+
23+
/****************************************************************************
24+
* Included Files
25+
****************************************************************************/
26+
27+
#include <nuttx/config.h>
28+
#include <stdio.h>
29+
#include <fcntl.h>
30+
#include <unistd.h>
31+
32+
/****************************************************************************
33+
* Private Functions
34+
****************************************************************************/
35+
36+
/****************************************************************************
37+
* Name: tmp112_read_temperature
38+
****************************************************************************/
39+
40+
static int tmp112_read_temperature(FAR const char *devpath, int devno)
41+
{
42+
int fd;
43+
int ret;
44+
float sample;
45+
46+
fd = open(devpath, O_RDONLY);
47+
if (fd < 0)
48+
{
49+
printf("Failed to open TMP112 sensor #%d at %s: %s (%d)\n",
50+
devno, devpath, strerror(errno), errno);
51+
return ERROR;
52+
}
53+
54+
ret = read(fd, &sample, sizeof(float));
55+
if (ret != sizeof(sample))
56+
{
57+
perror("Could not read");
58+
return ERROR;
59+
}
60+
61+
printf("Sensor #%d = %.03f degrees Celsius\n", devno, sample);
62+
63+
close(fd);
64+
65+
return OK;
66+
}
67+
68+
/****************************************************************************
69+
* Public Functions
70+
****************************************************************************/
71+
72+
/****************************************************************************
73+
* Name: tmp112_main
74+
****************************************************************************/
75+
76+
int main(int argc, FAR char *argv[])
77+
{
78+
if (tmp112_read_temperature(CONFIG_EXAMPLES_TMP112_DEVPATH, 1) < 0)
79+
{
80+
return EXIT_FAILURE;
81+
}
82+
83+
#ifdef CONFIG_TMP112_ENABLE_2
84+
if (tmp112_read_temperature(CONFIG_EXAMPLES_TMP112_DEVPATH_2, 2) < 0)
85+
{
86+
return EXIT_FAILURE;
87+
}
88+
#endif
89+
90+
#ifdef CONFIG_TMP112_ENABLE_3
91+
if (tmp112_read_temperature(CONFIG_EXAMPLES_TMP112_DEVPATH_3, 3) < 0)
92+
{
93+
return EXIT_FAILURE;
94+
}
95+
#endif
96+
97+
#ifdef CONFIG_TMP112_ENABLE_4
98+
if (tmp112_read_temperature(CONFIG_EXAMPLES_TMP112_DEVPATH_4, 4) < 0)
99+
{
100+
return EXIT_FAILURE;
101+
}
102+
#endif
103+
104+
return EXIT_SUCCESS;
105+
}

0 commit comments

Comments
 (0)