From c644a32dc7a9a07ba5fd2d0152a4c74ba3f3954b Mon Sep 17 00:00:00 2001 From: Antonio Lignan Date: Mon, 11 Jan 2016 18:28:20 +0100 Subject: [PATCH] Added support for digital presence/motion sensors --- examples/zolertia/zoul/Makefile | 4 +- examples/zolertia/zoul/project-conf.h | 7 ++ examples/zolertia/zoul/test-motion.c | 102 ++++++++++++++++++++ platform/zoul/dev/motion-sensor.c | 128 ++++++++++++++++++++++++++ platform/zoul/dev/motion-sensor.h | 76 +++++++++++++++ 5 files changed, 315 insertions(+), 2 deletions(-) create mode 100644 examples/zolertia/zoul/test-motion.c create mode 100644 platform/zoul/dev/motion-sensor.c create mode 100644 platform/zoul/dev/motion-sensor.h diff --git a/examples/zolertia/zoul/Makefile b/examples/zolertia/zoul/Makefile index 03dd8103a..dcda7321d 100644 --- a/examples/zolertia/zoul/Makefile +++ b/examples/zolertia/zoul/Makefile @@ -1,7 +1,7 @@ DEFINES+=PROJECT_CONF_H=\"project-conf.h\" CONTIKI_PROJECT = zoul-demo test-tsl2563 test-sht25 test-pwm test-power-mgmt -CONTIKI_PROJECT += test-bmp085 -CONTIKI_TARGET_SOURCEFILES += tsl2563.c sht25.c bmp085.c +CONTIKI_PROJECT += test-bmp085 test-motion +CONTIKI_TARGET_SOURCEFILES += tsl2563.c sht25.c bmp085.c motion-sensor.c all: $(CONTIKI_PROJECT) diff --git a/examples/zolertia/zoul/project-conf.h b/examples/zolertia/zoul/project-conf.h index 126ea1058..230724b2d 100644 --- a/examples/zolertia/zoul/project-conf.h +++ b/examples/zolertia/zoul/project-conf.h @@ -41,6 +41,13 @@ #define BROADCAST_CHANNEL 129 #define NETSTACK_CONF_RDC nullrdc_driver +/* Pin definition for the test-motion example, for the RE-Mote it uses the + * ADC1 pin + */ +#define MOTION_SENSOR_PORT GPIO_A_NUM +#define MOTION_SENSOR_PIN 5 +#define MOTION_SENSOR_VECTOR NVIC_INT_GPIO_PORT_A + #endif /* PROJECT_CONF_H_ */ /** @} */ diff --git a/examples/zolertia/zoul/test-motion.c b/examples/zolertia/zoul/test-motion.c new file mode 100644 index 000000000..b5c9c3f59 --- /dev/null +++ b/examples/zolertia/zoul/test-motion.c @@ -0,0 +1,102 @@ +/* + * Copyright (c) 2016, Zolertia - http://www.zolertia.com + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + */ +/*---------------------------------------------------------------------------*/ +/** + * \addtogroup zoul-examples + * @{ + * + * \defgroup zoul-motion-test Digital motion sensor test + * + * The example application shows how to use any digital motion sensor, basically + * driving a signal high when motion is detected. + * + * @{ + * + * \file + * Test application for the digital motion/presence sensor + * + * \author + * Antonio Lignan + */ +/*---------------------------------------------------------------------------*/ +#include "contiki.h" +#include "cpu.h" +#include "sys/rtimer.h" +#include "dev/leds.h" +#include "dev/motion-sensor.h" + +#include +#include +/*---------------------------------------------------------------------------*/ +#define LEDS_OFF_HYSTERISIS RTIMER_SECOND +/*---------------------------------------------------------------------------*/ +static struct rtimer rt; +/*---------------------------------------------------------------------------*/ +PROCESS(test_presence_sensor, "Test digital motion sensor"); +AUTOSTART_PROCESSES(&test_presence_sensor); +/*---------------------------------------------------------------------------*/ +void +rt_callback(struct rtimer *t, void *ptr) +{ + leds_off(LEDS_RED); +} +/*---------------------------------------------------------------------------*/ +static void +presence_callback(uint8_t value) +{ + printf("*** Presence detected!\n"); + leds_on(LEDS_RED); + rtimer_set(&rt, RTIMER_NOW() + LEDS_OFF_HYSTERISIS, 1, rt_callback, NULL); +} +/*---------------------------------------------------------------------------*/ +PROCESS_THREAD(test_presence_sensor, ev, data) +{ + PROCESS_BEGIN(); + + /* Register the callback handler when presence is detected */ + MOTION_REGISTER_INT(presence_callback); + + /* Enable the sensor, as default it assumes the signal pin has a pull-down + * resistor and a rising interrupt (signal goes high when motion is detected), + * this is the case of the Grove's PIR sensor v.1.0. If the sensor has an + * inverse logic, change at the motion-sensor.c driver file + * GPIO_DETECT_FALLING instead of GPIO_DETECT_RISING if using an external + * pull-up resistor + */ + SENSORS_ACTIVATE(motion_sensor); + + PROCESS_END(); +} +/*---------------------------------------------------------------------------*/ +/** + * @} + * @} + * @} + */ diff --git a/platform/zoul/dev/motion-sensor.c b/platform/zoul/dev/motion-sensor.c new file mode 100644 index 000000000..eb685d27a --- /dev/null +++ b/platform/zoul/dev/motion-sensor.c @@ -0,0 +1,128 @@ +/* + * Copyright (c) 2015, Zolertia + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * This file is part of the Contiki operating system. + * + */ +/*---------------------------------------------------------------------------*/ +/** + * \addtogroup zoul-motion-sensor + * @{ + * + * \file + * Digital motion sensor driver + * \author + * Antonio Lignan + */ +/*---------------------------------------------------------------------------*/ +#include +#include "contiki.h" +#include "dev/i2c.h" +#include "dev/motion-sensor.h" +#include "lib/sensors.h" +#include "dev/sys-ctrl.h" +#include "dev/gpio.h" +#include "dev/ioc.h" +/*---------------------------------------------------------------------------*/ +#define DEBUG 1 +#if DEBUG +#define PRINTF(...) printf(__VA_ARGS__) +#else +#define PRINTF(...) +#endif +/*---------------------------------------------------------------------------*/ +#define MOTION_SENSOR_PORT_BASE GPIO_PORT_TO_BASE(MOTION_SENSOR_PORT) +#define MOTION_SENSOR_PIN_MASK GPIO_PIN_MASK(MOTION_SENSOR_PIN) +/*---------------------------------------------------------------------------*/ +void (*presence_int_callback)(uint8_t value); +/*---------------------------------------------------------------------------*/ +PROCESS(motion_int_process, "Motion interrupt process handler"); +/*---------------------------------------------------------------------------*/ +PROCESS_THREAD(motion_int_process, ev, data) +{ + PROCESS_EXITHANDLER(); + PROCESS_BEGIN(); + + while(1) { + PROCESS_YIELD_UNTIL(ev == PROCESS_EVENT_POLL); + presence_int_callback(0); + } + PROCESS_END(); +} +/*---------------------------------------------------------------------------*/ +static void +motion_interrupt_handler(uint8_t port, uint8_t pin) +{ + process_poll(&motion_int_process); +} +/*---------------------------------------------------------------------------*/ +static int +status(int type) +{ + return MOTION_SUCCESS; +} +/*---------------------------------------------------------------------------*/ +static int +value(int type) +{ + return GPIO_READ_PIN(MOTION_SENSOR_PORT_BASE, MOTION_SENSOR_PIN_MASK); +} +/*---------------------------------------------------------------------------*/ +static int +configure(int type, int value) +{ + if(type != MOTION_ACTIVE) { + PRINTF("Motion: invalid configuration option\n"); + return MOTION_ERROR; + } + + if(!value) { + presence_int_callback = NULL; + GPIO_DISABLE_INTERRUPT(MOTION_SENSOR_PORT_BASE, MOTION_SENSOR_PIN_MASK); + return MOTION_SUCCESS; + } + + /* Configure interruption */ + GPIO_SOFTWARE_CONTROL(MOTION_SENSOR_PORT_BASE, MOTION_SENSOR_PIN_MASK); + GPIO_SET_INPUT(MOTION_SENSOR_PORT_BASE, MOTION_SENSOR_PIN_MASK); + GPIO_DETECT_RISING(MOTION_SENSOR_PORT_BASE, MOTION_SENSOR_PIN_MASK); + GPIO_TRIGGER_SINGLE_EDGE(MOTION_SENSOR_PORT_BASE, MOTION_SENSOR_PIN_MASK); + ioc_set_over(MOTION_SENSOR_PORT, MOTION_SENSOR_PIN, IOC_OVERRIDE_DIS); + gpio_register_callback(motion_interrupt_handler, MOTION_SENSOR_PORT, + MOTION_SENSOR_PIN); + + process_start(&motion_int_process, NULL); + + GPIO_ENABLE_INTERRUPT(MOTION_SENSOR_PORT_BASE, MOTION_SENSOR_PIN_MASK); + nvic_interrupt_enable(MOTION_SENSOR_VECTOR); + return MOTION_SUCCESS; +} +/*---------------------------------------------------------------------------*/ +SENSORS_SENSOR(motion_sensor, MOTION_SENSOR, value, configure, status); +/*---------------------------------------------------------------------------*/ +/** @} */ diff --git a/platform/zoul/dev/motion-sensor.h b/platform/zoul/dev/motion-sensor.h new file mode 100644 index 000000000..ce712cbde --- /dev/null +++ b/platform/zoul/dev/motion-sensor.h @@ -0,0 +1,76 @@ +/* + * Copyright (c) 2015, Zolertia + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * This file is part of the Contiki operating system. + * + */ +/*---------------------------------------------------------------------------*/ +/** + * \addtogroup zoul-sensors + * @{ + * + * \defgroup zoul-motion-sensor Digital motion sensor + * @{ + * + * \file + * Digital motion sensor header file + * \author + * Antonio Lignan + */ +/*---------------------------------------------------------------------------*/ +#include "lib/sensors.h" +/* -------------------------------------------------------------------------- */ +#ifndef SHT25_H_ +#define SHT25_H_ +/* -------------------------------------------------------------------------- */ +/** + * \name Motion sensor return and operation values + * @{ + */ +#define MOTION_ACTIVE SENSORS_ACTIVE +#define MOTION_SUCCESS 0 +#define MOTION_ERROR (-1) +/** @} */ +/* -------------------------------------------------------------------------- */ +/** + * \name Motion sensor interrupt callback macro + * @{ + */ +#define MOTION_REGISTER_INT(ptr) presence_int_callback = ptr; +extern void (*presence_int_callback)(uint8_t value); +/** @} */ +/* -------------------------------------------------------------------------- */ +#define MOTION_SENSOR "Digital motion sensor" +/* -------------------------------------------------------------------------- */ +extern const struct sensors_sensor motion_sensor; +/* -------------------------------------------------------------------------- */ +#endif /* ifndef MOTION_SENSOR_H_ */ +/** + * @} + * @} + */