zephyr/samples/sensor/thermometer/src/main.c
Tomasz Bursztyka e18fcbba5a device: Const-ify all device driver instance pointers
Now that device_api attribute is unmodified at runtime, as well as all
the other attributes, it is possible to switch all device driver
instance to be constant.

A coccinelle rule is used for this:

@r_const_dev_1
  disable optional_qualifier
@
@@
-struct device *
+const struct device *

@r_const_dev_2
 disable optional_qualifier
@
@@
-struct device * const
+const struct device *

Fixes #27399

Signed-off-by: Tomasz Bursztyka <tomasz.bursztyka@linux.intel.com>
2020-09-02 13:48:13 +02:00

49 lines
898 B
C

/*
* Copyright (c) 2016 ARM Ltd.
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr.h>
#include <drivers/sensor.h>
#include <stdio.h>
void main(void)
{
const struct device *temp_dev;
printf("Thermometer Example! %s\n", CONFIG_ARCH);
temp_dev = device_get_binding("TEMP_0");
if (!temp_dev) {
printf("error: no temp device\n");
return;
}
printf("temp device is %p, name is %s\n",
temp_dev, temp_dev->name);
while (1) {
int r;
struct sensor_value temp_value;
r = sensor_sample_fetch(temp_dev);
if (r) {
printf("sensor_sample_fetch failed return: %d\n", r);
break;
}
r = sensor_channel_get(temp_dev, SENSOR_CHAN_AMBIENT_TEMP,
&temp_value);
if (r) {
printf("sensor_channel_get failed return: %d\n", r);
break;
}
printf("Temperature is %gC\n",
sensor_value_to_double(&temp_value));
k_sleep(K_MSEC(1000));
}
}