1 /*
2  * Copyright (c) 2017 PHYTEC Messtechnik GmbH
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 
12 static struct sensor_value temp_value[64];
13 
14 #ifdef CONFIG_AMG88XX_TRIGGER
15 K_SEM_DEFINE(sem, 0, 1);
16 
trigger_handler(const struct device * dev,const struct sensor_trigger * trigger)17 static void trigger_handler(const struct device *dev,
18 			    const struct sensor_trigger *trigger)
19 {
20 	ARG_UNUSED(dev);
21 	ARG_UNUSED(trigger);
22 
23 	k_sem_give(&sem);
24 }
25 #endif
26 
27 
print_buffer(void * ptr,size_t l)28 void print_buffer(void *ptr, size_t l)
29 {
30 	struct sensor_value *tv = ptr;
31 	int ln = 0;
32 
33 	printk("---|");
34 	for (int i = 0; i < 8; i++) {
35 		printk("  %02d  ", i);
36 	}
37 	printk("\n");
38 
39 	printk("%03d|", ln);
40 	for (int i = 0; i < l; i++) {
41 		printk("%05d ", (tv[i].val1 * 100 + tv[i].val2 / 10000));
42 		if (!((i + 1) % 8)) {
43 			printk("\n");
44 			ln++;
45 			printk("%03d|", ln);
46 		}
47 	}
48 	printk("\n");
49 }
50 
main(void)51 int main(void)
52 {
53 	int ret;
54 	const struct device *const dev = DEVICE_DT_GET_ONE(panasonic_amg88xx);
55 
56 	if (!device_is_ready(dev)) {
57 		printk("sensor: device not ready.\n");
58 		return 0;
59 	}
60 
61 	printk("device: %p, name: %s\n", dev, dev->name);
62 
63 #ifdef CONFIG_AMG88XX_TRIGGER
64 	struct sensor_value attr = {
65 		.val1 = 27,
66 		.val2 = 0,
67 	};
68 
69 	if (sensor_attr_set(dev, SENSOR_CHAN_AMBIENT_TEMP,
70 			    SENSOR_ATTR_UPPER_THRESH, &attr)) {
71 		printk("Could not set threshold\n");
72 		return 0;
73 	}
74 
75 	struct sensor_trigger trig = {
76 		.type = SENSOR_TRIG_THRESHOLD,
77 		.chan = SENSOR_CHAN_AMBIENT_TEMP,
78 	};
79 
80 	if (sensor_trigger_set(dev, &trig, trigger_handler)) {
81 		printk("Could not set trigger\n");
82 		return 0;
83 	}
84 #endif
85 
86 	while (42) {
87 #ifdef CONFIG_AMG88XX_TRIGGER
88 		printk("Waiting for a threshold event\n");
89 		k_sem_take(&sem, K_FOREVER);
90 #endif
91 
92 		ret = sensor_sample_fetch(dev);
93 		if (ret) {
94 			printk("Failed to fetch a sample, %d\n", ret);
95 			return 0;
96 		}
97 
98 		ret = sensor_channel_get(dev, SENSOR_CHAN_AMBIENT_TEMP,
99 					 (struct sensor_value *)temp_value);
100 		if (ret) {
101 			printk("Failed to get sensor values, %d\n", ret);
102 			return 0;
103 		}
104 
105 		printk("new sample:\n");
106 		print_buffer(temp_value, ARRAY_SIZE(temp_value));
107 
108 		k_sleep(K_MSEC(1000));
109 	}
110 }
111