Contiki-NG
reed-sensor.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015, Zolertia <http://www.zolertia.com>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the Institute nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * This file is part of the Contiki operating system.
30  *
31  */
32 /**
33  * \file
34  * Reed sensor driver file
35  * \author
36  * Antonio Lignan <alinan@zolertia.com>
37  */
38 
39 #include "contiki.h"
40 #include "lib/sensors.h"
41 #include "dev/reed-sensor.h"
42 #include "sys/process.h"
43 #include "sys/ctimer.h"
44 /*---------------------------------------------------------------------------*/
45 #ifndef REED_CHECK_PERIOD
46 #define REED_CHECK_PERIOD CLOCK_SECOND
47 #endif
48 /*---------------------------------------------------------------------------*/
49 static int current_status = -1;
50 static struct ctimer change_timer;
51 process_event_t reed_sensor_event_changed;
52 /*---------------------------------------------------------------------------*/
53 static int
54 status(int type)
55 {
56  switch(type) {
57  case SENSORS_ACTIVE:
58  case SENSORS_READY:
59  return ~(REED_PORT_DIR & REED_READ_PIN);
60  }
61  return REED_SENSOR_ERROR;
62 }
63 /*---------------------------------------------------------------------------*/
64 static int
65 value(int type)
66 {
67  if((!status(SENSORS_ACTIVE)) || (type != REED_SENSOR_VAL)) {
68  return REED_SENSOR_ERROR;
69  }
70  return (REED_PORT_READ & REED_READ_PIN) ? REED_CLOSED : REED_OPEN;
71 }
72 /*---------------------------------------------------------------------------*/
73 static void
74 check_callback(void *data)
75 {
76  static int new_status;
77  if(current_status == -1) {
78  ctimer_stop(&change_timer);
79  return;
80  }
81 
82  new_status = value(REED_SENSOR_VAL);
83  if(new_status != current_status) {
84  current_status = new_status;
85  process_post(PROCESS_BROADCAST, reed_sensor_event_changed, &current_status);
86  }
87  ctimer_reset(&change_timer);
88 }
89 /*---------------------------------------------------------------------------*/
90 static int
91 configure(int type, int c)
92 {
93  switch(type) {
94  case SENSORS_ACTIVE:
95  if(c) {
96  if(!status(SENSORS_ACTIVE)) {
97  REED_PORT_SEL |= REED_READ_PIN;
98  REED_PORT_DIR &= ~REED_READ_PIN;
99  REED_PORT_REN |= REED_READ_PIN;
100  REED_PORT_PRES |= REED_READ_PIN;
101  }
102  } else {
103  REED_PORT_DIR |= REED_READ_PIN;
104  REED_PORT_REN &= ~REED_READ_PIN;
105  }
106  return REED_SENSOR_SUCCESS;
107  case REED_SENSOR_MODE:
108  if(c == REED_SENSOR_EVENT_MODE) {
109  current_status = value(REED_SENSOR_VAL);
110  ctimer_set(&change_timer, REED_CHECK_PERIOD, check_callback, NULL);
111  } else if(c == REED_SENSOR_EVENT_POLL) {
112  current_status = -1;
113  ctimer_stop(&change_timer);
114  } else {
115  return REED_SENSOR_ERROR;
116  }
117  return REED_SENSOR_SUCCESS;
118  }
119  return REED_SENSOR_ERROR;
120 }
121 /*---------------------------------------------------------------------------*/
122 SENSORS_SENSOR(reed_sensor, REED_SENSOR, value, configure, status);
Header file for the reed sensor.
void ctimer_stop(struct ctimer *c)
Stop a pending callback timer.
Definition: ctimer.c:149
void ctimer_reset(struct ctimer *c)
Reset a callback timer with the same interval as was previously set.
Definition: ctimer.c:125
Header file for the callback timer
void ctimer_set(struct ctimer *c, clock_time_t t, void(*f)(void *), void *ptr)
Set a callback timer.
Definition: ctimer.c:99
Header file for the Contiki process interface.
int process_post(struct process *p, process_event_t ev, process_data_t data)
Post an asynchronous event.
Definition: process.c:322