1 /*
2  * Copyright (c) 2024 Vitrolife A/S
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/device.h>
8 #include <zephyr/kernel.h>
9 #include <zephyr/sys/printk.h>
10 #include <zephyr/drivers/sensor.h>
11 #include <zephyr/drivers/sensor/veaa_x_3.h>
12 
13 static const struct device *const dev = DEVICE_DT_GET_ONE(festo_veaa_x_3);
14 
main(void)15 int main(void)
16 {
17 	int rc;
18 	struct sensor_value range, setpoint, pressure;
19 
20 	printk("Testing %s\n", dev->name);
21 
22 	if (!device_is_ready(dev)) {
23 		printk("%s not ready\n", dev->name);
24 		return -ENODEV;
25 	}
26 
27 	rc = sensor_attr_get(dev, SENSOR_CHAN_PRESS,
28 			     (enum sensor_attribute)SENSOR_ATTR_VEAA_X_3_RANGE, &range);
29 	if (rc != 0) {
30 		printk("get range failed: %d\n", rc);
31 		return rc;
32 	}
33 	printk("Valve range: %u to %u kPa\n", range.val1, range.val2);
34 
35 	if (IS_ENABLED(CONFIG_SAMPLE_USE_SHELL)) {
36 		printk("Loop is disabled. Use the `sensor` command to test %s", dev->name);
37 		return 0;
38 	}
39 
40 	setpoint.val1 = range.val1;
41 	while (1) {
42 		rc = sensor_attr_set(dev, SENSOR_CHAN_PRESS,
43 				     (enum sensor_attribute)SENSOR_ATTR_VEAA_X_3_SETPOINT,
44 				     &setpoint);
45 		if (rc != 0) {
46 			printk("Set setpoint to %u failed: %d\n", setpoint.val1, rc);
47 		}
48 
49 		/* Sleep before get to allow DAC and ADC to stabilize */
50 		k_msleep(CONFIG_SAMPLE_LOOP_INTERVAL);
51 
52 		rc = sensor_sample_fetch(dev);
53 		if (rc != 0) {
54 			printk("Fetch sample failed: %d", rc);
55 		}
56 
57 		rc = sensor_channel_get(dev, SENSOR_CHAN_PRESS, &pressure);
58 		if (rc != 0) {
59 			printk("Get sample failed: %d", rc);
60 		}
61 
62 		printk("Setpoint: %4u kPa, actual: %4u kPa\n", setpoint.val1, pressure.val1);
63 
64 		setpoint.val1 += CONFIG_SAMPLE_LOOP_INCREMENT;
65 		if (setpoint.val1 > range.val2) {
66 			setpoint.val1 = range.val1;
67 		}
68 	}
69 
70 	return 0;
71 }
72