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 <zephyr/sys_clock.h>
11 #include <stdio.h>
12
13 #include <zephyr/device.h>
14 #include <zephyr/drivers/sensor.h>
15 #include <zephyr/drivers/i2c.h>
16
17 #include <zephyr/drivers/sensor/veml6031.h>
18
read_with_attr(const struct device * dev,int it,int div4,int gain)19 static void read_with_attr(const struct device *dev, int it, int div4, int gain)
20 {
21 int ret;
22 struct sensor_value light;
23 struct sensor_value als_raw;
24 struct sensor_value ir_raw;
25 struct sensor_value sen;
26
27 sen.val2 = 0;
28
29 sen.val1 = it;
30 ret = sensor_attr_set(dev, SENSOR_CHAN_LIGHT, SENSOR_ATTR_VEML6031_IT, &sen);
31 if (ret) {
32 printf("Failed to set it attribute ret: %d\n", ret);
33 }
34 sen.val1 = div4;
35 ret = sensor_attr_set(dev, SENSOR_CHAN_LIGHT, SENSOR_ATTR_VEML6031_DIV4, &sen);
36 if (ret) {
37 printf("Failed to set div4 attribute ret: %d\n", ret);
38 }
39 sen.val1 = gain;
40 ret = sensor_attr_set(dev, SENSOR_CHAN_LIGHT, SENSOR_ATTR_VEML6031_GAIN, &sen);
41 if (ret) {
42 printf("Failed to set gain attribute ret: %d\n", ret);
43 }
44
45 ret = sensor_sample_fetch(dev);
46 if ((ret < 0) && (ret != -E2BIG)) {
47 printf("sample update error. ret: %d\n", ret);
48 }
49
50 sensor_channel_get(dev, SENSOR_CHAN_LIGHT, &light);
51
52 sensor_channel_get(dev, SENSOR_CHAN_VEML6031_ALS_RAW_COUNTS, &als_raw);
53
54 sensor_channel_get(dev, SENSOR_CHAN_VEML6031_IR_RAW_COUNTS, &ir_raw);
55
56 printf("Light (lux): %6d ALS (raw): %6d IR (raw): %6d "
57 " it: %d div4: %d gain: %d -- %s\n",
58 light.val1, als_raw.val1, ir_raw.val1, it, div4, gain,
59 ret == -E2BIG ? "OVERFLOW"
60 : ret ? "ERROR"
61 : "");
62 }
63
read_with_all_attr(const struct device * dev)64 static void read_with_all_attr(const struct device *dev)
65 {
66 int it, div4, gain;
67
68 for (it = VEML6031_IT_3_125; it <= VEML6031_IT_400; it++) {
69 for (div4 = VEML6031_SIZE_4_4; div4 <= VEML6031_SIZE_1_4; div4++) {
70 for (gain = VEML6031_GAIN_1; gain <= VEML6031_GAIN_0_5; gain++) {
71 read_with_attr(dev, it, div4, gain);
72 }
73 }
74 }
75 }
76
main(void)77 int main(void)
78 {
79 const struct device *const veml = DEVICE_DT_GET(DT_NODELABEL(light));
80
81 if (!device_is_ready(veml)) {
82 printk("sensor: device not ready.\n");
83 return 0;
84 }
85
86 printf("Test all attributes for a good guess of attribute usage away of saturation.\n");
87 read_with_all_attr(veml);
88 printf("Test finished.\n");
89
90 return 0;
91 }
92