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 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 | /*
* Copyright (c) 2018 Intel Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr.h>
#include <drivers/gpio.h>
#include <drivers/sensor.h>
#include "board.h"
#include "mesh.h"
#include <bluetooth/mesh.h>
struct device_info {
struct device *dev;
char *name;
};
static struct device_info dev_info[] = {
{ NULL, DT_LABEL(DT_INST(0, ti_hdc1010)) },
{ NULL, DT_LABEL(DT_INST(0, nxp_mma8652fc)) },
{ NULL, DT_LABEL(DT_INST(0, avago_apds9960)) },
{ NULL, DT_LABEL(DT_INST(0, solomon_ssd16xxfb)) },
};
int get_hdc1010_val(struct sensor_value *val)
{
if (sensor_sample_fetch(dev_info[DEV_IDX_HDC1010].dev)) {
printk("Failed to fetch sample for device %s\n",
dev_info[DEV_IDX_HDC1010].name);
return -1;
}
if (sensor_channel_get(dev_info[DEV_IDX_HDC1010].dev,
SENSOR_CHAN_AMBIENT_TEMP, &val[0])) {
return -1;
}
if (sensor_channel_get(dev_info[DEV_IDX_HDC1010].dev,
SENSOR_CHAN_HUMIDITY, &val[1])) {
return -1;
}
return 0;
}
int get_mma8652_val(struct sensor_value *val)
{
if (sensor_sample_fetch(dev_info[DEV_IDX_MMA8652].dev)) {
printk("Failed to fetch sample for device %s\n",
dev_info[DEV_IDX_MMA8652].name);
return -1;
}
if (sensor_channel_get(dev_info[DEV_IDX_MMA8652].dev,
SENSOR_CHAN_ACCEL_XYZ, &val[0])) {
return -1;
}
return 0;
}
int get_apds9960_val(struct sensor_value *val)
{
if (sensor_sample_fetch(dev_info[DEV_IDX_APDS9960].dev)) {
printk("Failed to fetch sample for device %s\n",
dev_info[DEV_IDX_APDS9960].name);
return -1;
}
if (sensor_channel_get(dev_info[DEV_IDX_APDS9960].dev,
SENSOR_CHAN_LIGHT, &val[0])) {
return -1;
}
if (sensor_channel_get(dev_info[DEV_IDX_APDS9960].dev,
SENSOR_CHAN_PROX, &val[1])) {
return -1;
}
return 0;
}
#define MOTION_TIMEOUT K_MINUTES(30)
static struct k_delayed_work motion_work;
static void motion_timeout(struct k_work *work)
{
int err;
printk("power save\n");
if (!mesh_is_initialized()) {
k_delayed_work_submit(&motion_work, MOTION_TIMEOUT);
return;
}
err = bt_mesh_suspend();
if (err && err != -EALREADY) {
printk("failed to suspend mesh (err %d)\n", err);
}
}
static void motion_handler(struct device *dev, struct sensor_trigger *trig)
{
int err;
printk("motion!\n");
if (!mesh_is_initialized()) {
return;
}
err = bt_mesh_resume();
if (err && err != -EALREADY) {
printk("failed to resume mesh (err %d)\n", err);
}
k_delayed_work_submit(&motion_work, MOTION_TIMEOUT);
}
static void configure_accel(void)
{
struct device_info *accel = &dev_info[DEV_IDX_MMA8652];
struct sensor_trigger trig_motion = {
.type = SENSOR_TRIG_DELTA,
.chan = SENSOR_CHAN_ACCEL_XYZ,
};
int err;
err = sensor_trigger_set(accel->dev, &trig_motion, motion_handler);
if (err) {
printk("setting motion trigger failed, err %d\n", err);
return;
}
k_delayed_work_init(&motion_work, motion_timeout);
k_delayed_work_submit(&motion_work, MOTION_TIMEOUT);
}
int periphs_init(void)
{
unsigned int i;
/* Bind sensors */
for (i = 0U; i < ARRAY_SIZE(dev_info); i++) {
dev_info[i].dev = device_get_binding(dev_info[i].name);
if (dev_info[i].dev == NULL) {
printk("Failed to get %s device\n", dev_info[i].name);
return -EBUSY;
}
}
configure_accel();
return 0;
}
|