|
| 1 | +/* |
| 2 | + * Copyright (c) 2017 STMicroelectronics |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#include <zephyr.h> |
| 8 | +#include <device.h> |
| 9 | +#include <sensor.h> |
| 10 | +#include <misc/printk.h> |
| 11 | + |
| 12 | +static void process_sample(struct device *dev) |
| 13 | +{ |
| 14 | + int ret; |
| 15 | + struct sensor_value value; |
| 16 | + |
| 17 | + ret = sensor_sample_fetch(dev); |
| 18 | + |
| 19 | + if (ret) { |
| 20 | + printk("sensor_sample_fetch failed ret %d\n", ret); |
| 21 | + return; |
| 22 | + } |
| 23 | + |
| 24 | + ret = sensor_channel_get(dev, SENSOR_CHAN_PROX, &value); |
| 25 | + printk("prox is %d\n", value.val1); |
| 26 | + |
| 27 | + ret = sensor_channel_get(dev, |
| 28 | + SENSOR_CHAN_DISTANCE, |
| 29 | + &value); |
| 30 | + printk("distance is %d.%06dm\n", value.val1, value.val2); |
| 31 | +} |
| 32 | + |
| 33 | +static void vl53l1x_handler(struct device *dev, |
| 34 | + struct sensor_trigger *trig) |
| 35 | +{ |
| 36 | + process_sample(dev); |
| 37 | +} |
| 38 | + |
| 39 | + |
| 40 | +void main(void) |
| 41 | +{ |
| 42 | + struct device *dev = device_get_binding(DT_ST_VL53L1X_0_LABEL); |
| 43 | + |
| 44 | + if (dev == NULL) { |
| 45 | + printk("Could not get VL53L1X device\n"); |
| 46 | + return; |
| 47 | + } |
| 48 | + |
| 49 | + if (IS_ENABLED(CONFIG_VL53L1X_TRIGGER)) { |
| 50 | + struct sensor_trigger trig = { |
| 51 | + .type = SENSOR_TRIG_DATA_READY, |
| 52 | + .chan = SENSOR_CHAN_ALL, |
| 53 | + }; |
| 54 | + if (sensor_trigger_set(dev, &trig, vl53l1x_handler) < 0) { |
| 55 | + printk("Cannot configure trigger\n"); |
| 56 | + return; |
| 57 | + }; |
| 58 | + } |
| 59 | + |
| 60 | + while (!IS_ENABLED(CONFIG_VL53L1X_TRIGGER)) { |
| 61 | + process_sample(dev); |
| 62 | + k_sleep(1000); |
| 63 | + } |
| 64 | + k_sleep(K_FOREVER); |
| 65 | +} |
0 commit comments