1 /*
2 * Copyright (c) 2016 Intel Corporation
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 <stdio.h>
11 #include <zephyr/sys/printk.h>
12 #ifdef CONFIG_SX9500_TRIGGER
13
sensor_trigger_handler(const struct device * dev,const struct sensor_trigger * trig)14 static void sensor_trigger_handler(const struct device *dev,
15 const struct sensor_trigger *trig)
16 {
17 struct sensor_value prox_value;
18 int ret;
19
20 ret = sensor_sample_fetch(dev);
21 if (ret) {
22 printk("sensor_sample_fetch failed ret %d\n", ret);
23 return;
24 }
25
26 ret = sensor_channel_get(dev, SENSOR_CHAN_PROX, &prox_value);
27 printk("prox is %d\n", prox_value.val1);
28 }
29
setup_trigger(const struct device * dev)30 static void setup_trigger(const struct device *dev)
31 {
32 int ret;
33 struct sensor_trigger trig = {
34 .type = SENSOR_TRIG_NEAR_FAR,
35 };
36
37 ret = sensor_trigger_set(dev, &trig, sensor_trigger_handler);
38 if (ret) {
39 printk("sensor_trigger_set err %d\n", ret);
40 }
41 }
42
do_main(const struct device * dev)43 void do_main(const struct device *dev)
44 {
45 setup_trigger(dev);
46
47 while (1) {
48 k_sleep(K_MSEC(1000));
49 }
50 }
51
52 #else /* CONFIG_SX9500_TRIGGER */
53
do_main(const struct device * dev)54 static void do_main(const struct device *dev)
55 {
56 int ret;
57 struct sensor_value prox_value;
58
59 while (1) {
60 ret = sensor_sample_fetch(dev);
61 if (ret) {
62 printk("sensor_sample_fetch failed ret %d\n", ret);
63 return;
64 }
65
66 ret = sensor_channel_get(dev, SENSOR_CHAN_PROX, &prox_value);
67 printk("prox is %d\n", prox_value.val1);
68
69 k_sleep(K_MSEC(1000));
70 }
71 }
72
73 #endif /* CONFIG_SX9500_TRIGGER */
74
main(void)75 int main(void)
76 {
77 const struct device *const dev = DEVICE_DT_GET_ONE(semtech_sx9500);
78
79 if (!device_is_ready(dev)) {
80 printk("sensor: device not ready.\n");
81 return 0;
82 }
83
84 printk("device is %p, name is %s\n", dev, dev->name);
85
86 do_main(dev);
87 return 0;
88 }
89