zephyr/samples/sensor/th02/src/main.c
Gerard Marull-Paretas c7b5b3c419 samples: migrate includes to contain <zephyr/...> prefix
In order to bring consistency in-tree, migrate all samples to the use
the new prefix <zephyr/...>. Note that the conversion has been scripted:

```python
from pathlib import Path
import re

EXTENSIONS = ("c", "h", "cpp", "rst")

for p in Path(".").glob("samples/**/*"):
    if not p.is_file() or p.suffix and p.suffix[1:] not in EXTENSIONS:
        continue

    content = ""
    with open(p) as f:
        for line in f:
            m = re.match(r"^(.*)#include <(.*)>(.*)$", line)
            if (m and
                not m.group(2).startswith("zephyr/") and
                (Path(".") / "include" / "zephyr" / m.group(2)).exists()):
                content += (
                    m.group(1) +
                    "#include <zephyr/" + m.group(2) +">" +
                    m.group(3) + "\n"
                )
            else:
                content += line

    with open(p, "w") as f:
        f.write(content)
```

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2022-05-06 11:29:59 +02:00

98 lines
2.3 KiB
C

/*
* Copyright (c) 2016 Intel Corporation.
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr/zephyr.h>
#include <zephyr/device.h>
#include <zephyr/drivers/sensor.h>
#include <zephyr/sys/printk.h>
#include <zephyr/sys/util.h>
#include <zephyr/drivers/misc/grove_lcd/grove_lcd.h>
#include <stdio.h>
#include <string.h>
struct channel_info {
int chan;
char *dev_name;
};
/* change device names if you want to use different sensors */
static struct channel_info info[] = {
{ SENSOR_CHAN_AMBIENT_TEMP, "TH02" },
{ SENSOR_CHAN_HUMIDITY, "TH02" },
};
void main(void)
{
const struct device *glcd = DEVICE_DT_GET(DT_NODELABEL(glcd));
const struct device *dev[ARRAY_SIZE(info)];
struct sensor_value val[ARRAY_SIZE(info)];
unsigned int i;
int rc;
for (i = 0U; i < ARRAY_SIZE(info); i++) {
dev[i] = device_get_binding(info[i].dev_name);
if (dev[i] == NULL) {
printk("Failed to get \"%s\" device\n",
info[i].dev_name);
return;
}
}
if (!device_is_ready(glcd)) {
printk("Grove LCD not ready\n");
return;
}
/* configure LCD */
glcd_function_set(glcd, GLCD_FS_ROWS_2 | GLCD_FS_DOT_SIZE_LITTLE |
GLCD_FS_8BIT_MODE);
glcd_display_state_set(glcd, GLCD_DS_DISPLAY_ON);
while (1) {
/* fetch sensor samples */
for (i = 0U; i < ARRAY_SIZE(info); i++) {
rc = sensor_sample_fetch(dev[i]);
if (rc) {
printk("Failed to fetch sample for device %s (%d)\n",
info[i].dev_name, rc);
}
}
for (i = 0U; i < ARRAY_SIZE(info); i++) {
rc = sensor_channel_get(dev[i], info[i].chan, &val[i]);
if (rc) {
printk("Failed to get data for device %s (%d)\n",
info[i].dev_name, rc);
continue;
}
}
char row[16];
/* clear LCD */
(void)memset(row, ' ', sizeof(row));
glcd_cursor_pos_set(glcd, 0, 0);
glcd_print(glcd, row, sizeof(row));
glcd_cursor_pos_set(glcd, 0, 1);
glcd_print(glcd, row, sizeof(row));
/* display temperature on LCD */
glcd_cursor_pos_set(glcd, 0, 0);
sprintf(row, "T:%.1f%cC", sensor_value_to_double(val),
223 /* degree symbol */);
glcd_print(glcd, row, strlen(row));
/* display humidity on LCD */
glcd_cursor_pos_set(glcd, 17 - strlen(row), 0);
sprintf(row, "RH:%.0f%c", sensor_value_to_double(val + 1),
37 /* percent symbol */);
glcd_print(glcd, row, strlen(row));
k_sleep(K_MSEC(2000));
}
}