Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 | /* * Copyright (c) 2017 PHYTEC Messtechnik GmbH * * SPDX-License-Identifier: Apache-2.0 */ #include <zephyr.h> #include <device.h> #include <drivers/sensor.h> #include <sys/printk.h> static struct sensor_value temp_value[64]; #ifdef CONFIG_AMG88XX_TRIGGER K_SEM_DEFINE(sem, 0, 1); static void trigger_handler(struct device *dev, struct sensor_trigger *trigger) { ARG_UNUSED(dev); ARG_UNUSED(trigger); k_sem_give(&sem); } #endif void print_buffer(void *ptr, size_t l) { struct sensor_value *tv = ptr; int ln = 0; printk("---|"); for (int i = 0; i < 8; i++) { printk(" %02d ", i); } printk("\n"); printk("%03d|", ln); for (int i = 0; i < l; i++) { printk("%05d ", (tv[i].val1 * 100 + tv[i].val2 / 10000)); if (!((i + 1) % 8)) { printk("\n"); ln++; printk("%03d|", ln); } } printk("\n"); } void main(void) { int ret; struct device *dev = device_get_binding( DT_INST_0_PANASONIC_AMG88XX_LABEL); if (dev == NULL) { printk("Could not get AMG88XX device\n"); return; } printk("device: %p, name: %s\n", dev, dev->config->name); #ifdef CONFIG_AMG88XX_TRIGGER struct sensor_value attr = { .val1 = 27, .val2 = 0, }; if (sensor_attr_set(dev, SENSOR_CHAN_AMBIENT_TEMP, SENSOR_ATTR_UPPER_THRESH, &attr)) { printk("Could not set threshold\n"); return; } struct sensor_trigger trig = { .type = SENSOR_TRIG_THRESHOLD, .chan = SENSOR_CHAN_AMBIENT_TEMP, }; if (sensor_trigger_set(dev, &trig, trigger_handler)) { printk("Could not set trigger\n"); return; } #endif while (42) { #ifdef CONFIG_AMG88XX_TRIGGER printk("Waiting for a threshold event\n"); k_sem_take(&sem, K_FOREVER); #endif ret = sensor_sample_fetch(dev); if (ret) { printk("Failed to fetch a sample, %d\n", ret); return; } ret = sensor_channel_get(dev, SENSOR_CHAN_AMBIENT_TEMP, (struct sensor_value *)temp_value); if (ret) { printk("Failed to get sensor values, %d\n", ret); return; } printk("new sample:\n"); print_buffer(temp_value, ARRAY_SIZE(temp_value)); k_sleep(K_MSEC(1000)); } } |