1 /*
2  * Copyright (c) 2025 Andreas Klinger
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/kernel.h>
8 
9 #include <zephyr/sys/printk.h>
10 #include <stdio.h>
11 
12 #include <zephyr/device.h>
13 #include <zephyr/drivers/sensor.h>
14 
15 #include <zephyr/drivers/sensor/veml6046.h>
16 
read_with_attr(const struct device * dev,int it,int pdd,int gain)17 static void read_with_attr(const struct device *dev, int it, int pdd, int gain)
18 {
19 	int ret;
20 	struct sensor_value red, green, blue, ir;
21 	struct sensor_value red_raw, green_raw, blue_raw, ir_raw;
22 	struct sensor_value sen;
23 	char result[10];
24 
25 	sen.val2 = 0;
26 
27 	sen.val1 = it;
28 	ret = sensor_attr_set(dev, SENSOR_CHAN_LIGHT,
29 			     (enum sensor_attribute)SENSOR_ATTR_VEML6046_IT, &sen);
30 	if (ret) {
31 		printf("Failed to set it attribute ret: %d\n", ret);
32 	}
33 	sen.val1 = pdd;
34 	ret = sensor_attr_set(dev, SENSOR_CHAN_LIGHT,
35 			     (enum sensor_attribute)SENSOR_ATTR_VEML6046_PDD, &sen);
36 	if (ret) {
37 		printf("Failed to set pdd attribute ret: %d\n", ret);
38 	}
39 	sen.val1 = gain;
40 	ret = sensor_attr_set(dev, SENSOR_CHAN_LIGHT,
41 			     (enum sensor_attribute)SENSOR_ATTR_VEML6046_GAIN, &sen);
42 	if (ret) {
43 		printf("Failed to set gain attribute ret: %d\n", ret);
44 	}
45 
46 	ret = sensor_sample_fetch(dev);
47 	if ((ret < 0) && (ret != -E2BIG)) {
48 		printf("sample update error. ret: %d\n", ret);
49 	}
50 
51 	sensor_channel_get(dev, SENSOR_CHAN_RED, &red);
52 	sensor_channel_get(dev, (enum sensor_channel)SENSOR_CHAN_VEML6046_RED_RAW_COUNTS,
53 										&red_raw);
54 
55 	sensor_channel_get(dev, SENSOR_CHAN_GREEN, &green);
56 	sensor_channel_get(dev, (enum sensor_channel)SENSOR_CHAN_VEML6046_GREEN_RAW_COUNTS,
57 										&green_raw);
58 
59 	sensor_channel_get(dev, SENSOR_CHAN_BLUE, &blue);
60 	sensor_channel_get(dev, (enum sensor_channel)SENSOR_CHAN_VEML6046_BLUE_RAW_COUNTS,
61 										&blue_raw);
62 
63 	sensor_channel_get(dev, SENSOR_CHAN_IR, &ir);
64 	sensor_channel_get(dev, (enum sensor_channel)SENSOR_CHAN_VEML6046_IR_RAW_COUNTS,
65 										&ir_raw);
66 
67 	if (ret == -E2BIG) {
68 		snprintf(result, sizeof(result), "OVERFLOW");
69 	} else if (ret) {
70 		snprintf(result, sizeof(result), "ERROR");
71 	} else {
72 		snprintf(result, sizeof(result), "");
73 	}
74 
75 	printf("Red: %6d lx (%6d) green:  %6d lx (%6d) "
76 	       "blue: %6d lx (%6d) IR:  %6d lx (%6d) "
77 	       "  it: %d pdd: %d gain: %d  --  %s\n",
78 	       red.val1, red_raw.val1,
79 	       green.val1, green_raw.val1,
80 	       blue.val1, blue_raw.val1,
81 	       ir.val1, ir_raw.val1,
82 	       it, pdd, gain,
83 	       result);
84 }
85 
read_with_all_attr(const struct device * dev)86 static void read_with_all_attr(const struct device *dev)
87 {
88 	for (int it = VEML60XX_IT_3_125; it <= VEML60XX_IT_400; it++) {
89 		for (int pdd = VEML6046_SIZE_2_2; pdd <= VEML6046_SIZE_1_2; pdd++) {
90 			for (int gain = VEML60XX_GAIN_1; gain <= VEML60XX_GAIN_0_5; gain++) {
91 				read_with_attr(dev, it, pdd, gain);
92 			}
93 		}
94 	}
95 }
96 
main(void)97 int main(void)
98 {
99 	const struct device *const veml = DEVICE_DT_GET(DT_NODELABEL(rgbir));
100 
101 	if (!device_is_ready(veml)) {
102 		printk("sensor: device not ready.\n");
103 		return 0;
104 	}
105 
106 	printf("Test all attributes for a good guess of attribute usage away of saturation.\n");
107 	read_with_all_attr(veml);
108 	printf("Test finished.\n");
109 
110 	return 0;
111 }
112