1 /*
2  * Copyright (c) 2016 Firmwave
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/kernel.h>
8 #include <zephyr/device.h>
9 #include <zephyr/drivers/sensor.h>
10 #include <zephyr/sys/printk.h>
11 #include <zephyr/sys/__assert.h>
12 
do_main(const struct device * dev)13 static void do_main(const struct device *dev)
14 {
15 	int ret;
16 	struct sensor_value temp_value;
17 	struct sensor_value attr;
18 
19 	attr.val1 = 150;
20 	attr.val2 = 0;
21 	ret = sensor_attr_set(dev, SENSOR_CHAN_AMBIENT_TEMP,
22 			      SENSOR_ATTR_FULL_SCALE, &attr);
23 	if (ret) {
24 		printk("sensor_attr_set failed ret %d\n", ret);
25 		return;
26 	}
27 
28 	attr.val1 = 8;
29 	attr.val2 = 0;
30 	ret = sensor_attr_set(dev, SENSOR_CHAN_AMBIENT_TEMP,
31 			      SENSOR_ATTR_SAMPLING_FREQUENCY, &attr);
32 	if (ret) {
33 		printk("sensor_attr_set failed ret %d\n", ret);
34 		return;
35 	}
36 
37 	while (1) {
38 		ret = sensor_sample_fetch(dev);
39 		if (ret) {
40 			printk("sensor_sample_fetch failed ret %d\n", ret);
41 			return;
42 		}
43 
44 		ret = sensor_channel_get(dev, SENSOR_CHAN_AMBIENT_TEMP, &temp_value);
45 		if (ret) {
46 			printk("sensor_channel_get failed ret %d\n", ret);
47 			return;
48 		}
49 
50 		printk("temp is %d (%d micro)\n", temp_value.val1,
51 		       temp_value.val2);
52 
53 		k_sleep(K_MSEC(1000));
54 	}
55 }
56 
main(void)57 int main(void)
58 {
59 	const struct device *const dev = DEVICE_DT_GET_ANY(ti_tmp112);
60 
61 	__ASSERT(dev != NULL, "Failed to get device binding");
62 	__ASSERT(device_is_ready(dev), "Device %s is not ready", dev->name);
63 	printk("device is %p, name is %s\n", dev, dev->name);
64 
65 	do_main(dev);
66 	return 0;
67 }
68