Added Reed Sensor driver and example

ico
Antonio Lignan 2015-03-25 13:51:50 +01:00
parent c76316adf2
commit b9334a3b0e
6 changed files with 277 additions and 4 deletions

View File

@ -7,7 +7,7 @@ ZOLERTIA_Z1SP=0
CONTIKI_PROJECT = test-phidgets blink test-adxl345 test-tmp102 test-light-ziglet
CONTIKI_PROJECT += test-battery test-relay-phidget test-tlc59116 test-sht25
CONTIKI_SOURCEFILES += sht11.c
CONTIKI_SOURCEFILES += sht11.c reed-sensor.c
APPS=serial-shell
ifeq ($(ZOLERTIA_Z1SP),1)

View File

@ -0,0 +1,73 @@
/*
* 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.
*
*/
/**
* \file
* A quick program for testing a reed sensor.
* \author
* Antonio Lignan <alinan@zolertia.com>
*/
#include <stdio.h>
#include "contiki.h"
#include "reed-sensor.h"
#define REED_READ_INTERVAL (CLOCK_SECOND / 4)
#define REED_EXAMPLE_EVENT 1
/*---------------------------------------------------------------------------*/
PROCESS(test_process, "Reed test process");
AUTOSTART_PROCESSES(&test_process);
/*---------------------------------------------------------------------------*/
static struct etimer et;
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(test_process, ev, data)
{
PROCESS_BEGIN();
SENSORS_ACTIVATE(reed_sensor);
#if REED_EXAMPLE_EVENT
reed_sensor.configure(REED_SENSOR_MODE, REED_SENSOR_EVENT_MODE);
#else
etimer_set(&et, REED_READ_INTERVAL);
#endif
while(1) {
PROCESS_YIELD();
if(ev == PROCESS_EVENT_TIMER) {
printf("Reed poll status [%d]\n", reed_sensor.value(REED_SENSOR_VAL));
etimer_restart(&et);
} else if(ev == reed_sensor_event_changed) {
printf("Reed sensor event --> %d\n", (*((int *)data)));
}
}
PROCESS_END();
}

View File

@ -0,0 +1,122 @@
/*
* 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.
*
*/
/**
* \file
* Reed sensor driver file
* \author
* Antonio Lignan <alinan@zolertia.com>
*/
#include "contiki.h"
#include "lib/sensors.h"
#include "dev/reed-sensor.h"
#include "sys/process.h"
#include "sys/ctimer.h"
/*---------------------------------------------------------------------------*/
#ifndef REED_CHECK_PERIOD
#define REED_CHECK_PERIOD CLOCK_SECOND
#endif
/*---------------------------------------------------------------------------*/
static int current_status = -1;
static struct ctimer change_timer;
process_event_t reed_sensor_event_changed;
/*---------------------------------------------------------------------------*/
static int
status(int type)
{
switch(type) {
case SENSORS_ACTIVE:
case SENSORS_READY:
return ~(REED_PORT_DIR & REED_READ_PIN);
}
return REED_SENSOR_ERROR;
}
/*---------------------------------------------------------------------------*/
static int
value(int type)
{
if((!status(SENSORS_ACTIVE)) || (type != REED_SENSOR_VAL)) {
return REED_SENSOR_ERROR;
}
return (REED_PORT_READ & REED_READ_PIN) ? REED_CLOSED : REED_OPEN;
}
/*---------------------------------------------------------------------------*/
static void
check_callback(void *data)
{
static int new_status;
if(current_status == -1) {
ctimer_stop(&change_timer);
return;
}
new_status = value(REED_SENSOR_VAL);
if(new_status != current_status) {
current_status = new_status;
process_post(PROCESS_BROADCAST, reed_sensor_event_changed, &current_status);
}
ctimer_reset(&change_timer);
}
/*---------------------------------------------------------------------------*/
static int
configure(int type, int c)
{
switch(type) {
case SENSORS_ACTIVE:
if(c) {
if(!status(SENSORS_ACTIVE)) {
REED_PORT_SEL |= REED_READ_PIN;
REED_PORT_DIR &= ~REED_READ_PIN;
REED_PORT_REN |= REED_READ_PIN;
REED_PORT_PRES |= REED_READ_PIN;
}
} else {
REED_PORT_DIR |= REED_READ_PIN;
REED_PORT_REN &= ~REED_READ_PIN;
}
return REED_SENSOR_SUCCESS;
case REED_SENSOR_MODE:
if(c == REED_SENSOR_EVENT_MODE) {
current_status = value(REED_SENSOR_VAL);
ctimer_set(&change_timer, REED_CHECK_PERIOD, check_callback, NULL);
} else if(c == REED_SENSOR_EVENT_POLL) {
current_status = -1;
ctimer_stop(&change_timer);
} else {
return REED_SENSOR_ERROR;
}
return REED_SENSOR_SUCCESS;
}
return REED_SENSOR_ERROR;
}
/*---------------------------------------------------------------------------*/
SENSORS_SENSOR(reed_sensor, REED_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.
*
*/
/**
* Header file for the reed sensor
*
* The Reed sensor allows to be used either by polling the sensor status or by
* setting up a timer on the background ticking every REED_CHECK_PERIOD, posting
* a reed_sensor_event_changed event, informing the application about a change
* in the sensor status (basically open or closed). To enable each mode
* (default is polling) call the configure() function with REED_SENSOR_MODE
* using REED_SENSOR_EVENT_MODE or REED_SENSOR_POLL_MODE, after having
* initialized the device using SENSORS_ACTIVATE(reed_sensor).
*
* \file
* Reed sensor header file
* \author
* Antonio Lignan <alinan@zolertia.com>
*/
#include "lib/sensors.h"
#ifndef REED_SENSOR_H_
#define REED_SENSOR_H_
/* -------------------------------------------------------------------------- */
#define REED_SENSOR_ERROR -1
#define REED_SENSOR_SUCCESS 0x00
#define REED_SENSOR_VAL 0x01
/* -------------------------------------------------------------------------- */
#define REED_OPEN 0x00
#define REED_CLOSED 0x01
/* -------------------------------------------------------------------------- */
#define REED_SENSOR_MODE 0x01
#define REED_SENSOR_EVENT_MODE 0x0A
#define REED_SENSOR_EVENT_POLL 0x0B
/* -------------------------------------------------------------------------- */
#define REED_PORT_DIR P4DIR
#define REED_PORT_SEL P4SEL
#define REED_PORT_REN P4REN
#define REED_PORT_READ P4IN
#define REED_PORT_PRES P4OUT
#define REED_READ_PIN (1 << 2)
/* -------------------------------------------------------------------------- */
#define REED_SENSOR "Reed Sensor"
/* -------------------------------------------------------------------------- */
extern const struct sensors_sensor reed_sensor;
extern process_event_t reed_sensor_event_changed;
/* -------------------------------------------------------------------------- */
#endif /* ifndef REED_SENSOR_H_ */

View File

@ -124,11 +124,11 @@ sht25_read(uint8_t variable)
return rd;
}
/*---------------------------------------------------------------------------*/
static int16_t
value(uint8_t type)
static int
value(int type)
{
return sht25_read(type);
}
/*---------------------------------------------------------------------------*/
SENSORS_SENSOR(sht25, "SHT25", value, configure, status);
SENSORS_SENSOR(sht25, SHT25_SENSOR, value, configure, status);
/*---------------------------------------------------------------------------*/

View File

@ -64,6 +64,8 @@
#define SHT25_VAL_HUM SHT25_HUM_HOLD
#define SHT25_ERROR -1
/* -------------------------------------------------------------------------- */
#define SHT25_SENSOR "SHT25 Sensor"
/* -------------------------------------------------------------------------- */
extern const struct sensors_sensor sht25;
/* -------------------------------------------------------------------------- */
#endif /* ifndef SHT25_H_ */