zephyr/samples/sensor/tmp112/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

67 lines
1.3 KiB
C

/*
* Copyright (c) 2016 Firmwave
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr.h>
#include <device.h>
#include <drivers/sensor.h>
#include <sys/printk.h>
#include <sys/__assert.h>
static void do_main(const struct device *dev)
{
int ret;
struct sensor_value temp_value;
struct sensor_value attr;
attr.val1 = 150;
attr.val2 = 0;
ret = sensor_attr_set(dev, SENSOR_CHAN_AMBIENT_TEMP,
SENSOR_ATTR_FULL_SCALE, &attr);
if (ret) {
printk("sensor_attr_set failed ret %d\n", ret);
return;
}
attr.val1 = 8;
attr.val2 = 0;
ret = sensor_attr_set(dev, SENSOR_CHAN_AMBIENT_TEMP,
SENSOR_ATTR_SAMPLING_FREQUENCY, &attr);
if (ret) {
printk("sensor_attr_set failed ret %d\n", ret);
return;
}
while (1) {
ret = sensor_sample_fetch(dev);
if (ret) {
printk("sensor_sample_fetch failed ret %d\n", ret);
return;
}
ret = sensor_channel_get(dev, SENSOR_CHAN_AMBIENT_TEMP, &temp_value);
if (ret) {
printk("sensor_channel_get failed ret %d\n", ret);
return;
}
printk("temp is %d (%d micro)\n", temp_value.val1,
temp_value.val2);
k_sleep(K_MSEC(1000));
}
}
void main(void)
{
const struct device *dev;
dev = device_get_binding("TMP112");
__ASSERT(dev != NULL, "Failed to get device binding");
printk("device is %p, name is %s\n", dev, dev->name);
do_main(dev);
}