|
| 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 | + * Public Functions |
| 34 | + ****************************************************************************/ |
| 35 | + |
| 36 | +/**************************************************************************** |
| 37 | + * Name: tmp112_main |
| 38 | + ****************************************************************************/ |
| 39 | + |
| 40 | +int main(int argc, FAR char *argv[]) |
| 41 | +{ |
| 42 | + int fd; |
| 43 | + int ret; |
| 44 | + float sample; |
| 45 | + |
| 46 | + fd = open(CONFIG_EXAMPLES_TMP112_DEVPATH, O_RDONLY); |
| 47 | + if (fd < 0) |
| 48 | + { |
| 49 | + printf("Failed to open TMP112 sensor at %s: %s (%d)\n", |
| 50 | + CONFIG_EXAMPLES_TMP112_DEVPATH, strerror(errno), errno); |
| 51 | + return EXIT_FAILURE; |
| 52 | + } |
| 53 | + |
| 54 | + ret = read(fd, &sample, sizeof(float)); |
| 55 | + if (ret != sizeof(sample)) |
| 56 | + { |
| 57 | + perror("Could not read"); |
| 58 | + ret = EXIT_FAILURE; |
| 59 | + } |
| 60 | + else |
| 61 | + { |
| 62 | + printf("TMP112 = %.03f degrees Celsius\n", sample); |
| 63 | + ret = EXIT_SUCCESS; |
| 64 | + } |
| 65 | + |
| 66 | + close(fd); |
| 67 | + |
| 68 | + return ret; |
| 69 | +} |
0 commit comments