Added support for digital presence/motion sensors

This commit is contained in:
Antonio Lignan 2016-01-11 18:28:20 +01:00
parent 48eb2a4fe2
commit c644a32dc7
5 changed files with 315 additions and 2 deletions

View file

@ -1,7 +1,7 @@
DEFINES+=PROJECT_CONF_H=\"project-conf.h\" DEFINES+=PROJECT_CONF_H=\"project-conf.h\"
CONTIKI_PROJECT = zoul-demo test-tsl2563 test-sht25 test-pwm test-power-mgmt CONTIKI_PROJECT = zoul-demo test-tsl2563 test-sht25 test-pwm test-power-mgmt
CONTIKI_PROJECT += test-bmp085 CONTIKI_PROJECT += test-bmp085 test-motion
CONTIKI_TARGET_SOURCEFILES += tsl2563.c sht25.c bmp085.c CONTIKI_TARGET_SOURCEFILES += tsl2563.c sht25.c bmp085.c motion-sensor.c
all: $(CONTIKI_PROJECT) all: $(CONTIKI_PROJECT)

View file

@ -41,6 +41,13 @@
#define BROADCAST_CHANNEL 129 #define BROADCAST_CHANNEL 129
#define NETSTACK_CONF_RDC nullrdc_driver #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_ */ #endif /* PROJECT_CONF_H_ */
/** @} */ /** @} */

View file

@ -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 <alinan@zolertia.com>
*/
/*---------------------------------------------------------------------------*/
#include "contiki.h"
#include "cpu.h"
#include "sys/rtimer.h"
#include "dev/leds.h"
#include "dev/motion-sensor.h"
#include <stdio.h>
#include <stdint.h>
/*---------------------------------------------------------------------------*/
#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();
}
/*---------------------------------------------------------------------------*/
/**
* @}
* @}
* @}
*/

View file

@ -0,0 +1,128 @@
/*
* Copyright (c) 2015, 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.
*
* This file is part of the Contiki operating system.
*
*/
/*---------------------------------------------------------------------------*/
/**
* \addtogroup zoul-motion-sensor
* @{
*
* \file
* Digital motion sensor driver
* \author
* Antonio Lignan <alinan@zolertia.com>
*/
/*---------------------------------------------------------------------------*/
#include <stdio.h>
#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);
/*---------------------------------------------------------------------------*/
/** @} */

View file

@ -0,0 +1,76 @@
/*
* Copyright (c) 2015, 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.
*
* 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 <alinan@zolertia.com>
*/
/*---------------------------------------------------------------------------*/
#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_ */
/**
* @}
* @}
*/