Contiki 3.x
button-sensor.c
Go to the documentation of this file.
1 /*
2  * Contiki SeedEye Platform project
3  *
4  * Copyright (c) 2012,
5  * Scuola Superiore Sant'Anna (http://www.sssup.it) and
6  * Consorzio Nazionale Interuniversitario per le Telecomunicazioni
7  * (http://www.cnit.it).
8  *
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  * notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in the
18  * documentation and/or other materials provided with the distribution.
19  * 3. Neither the name of the Institute nor the names of its contributors
20  * may be used to endorse or promote products derived from this software
21  * without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  */
36 
37 /**
38  * \addtogroup SeedEye Contiki SEEDEYE Platform
39  *
40  * @{
41  */
42 
43 /**
44  * \file platform/seedeye/dev/button-sensor.c
45  * \brief Button Sensor
46  * \author Giovanni Pellerano <giovanni.pellerano@evilaliv3.org>
47  * \date 2012-04-24
48  */
49 
50 #include <dev/button-sensor.h>
51 
52 #include <p32xxxx.h>
53 
54 #include <pic32_irq.h>
55 
56 #define button_read PORTDbits.RD5
57 
58 const struct sensors_sensor button_sensor;
59 
60 static struct timer debouncetimer;
61 
62 static int status(int type);
63 
64 static uint8_t sensor_status = 0;
65 
66 ISR(_CHANGE_NOTICE_VECTOR)
67 {
68  ENERGEST_ON(ENERGEST_TYPE_IRQ);
69 
70  if(timer_expired(&debouncetimer)) {
71  timer_set(&debouncetimer, CLOCK_SECOND / 4);
72  sensors_changed(&button_sensor);
73  }
74 
75  IFS1CLR = _IFS1_CNIF_MASK;
76 
77 
78  ENERGEST_OFF(ENERGEST_TYPE_IRQ);
79 }
80 
81 /*---------------------------------------------------------------------------*/
82 void
83 button_press(void)
84 {
85  sensors_changed(&button_sensor);
86 }
87 /*---------------------------------------------------------------------------*/
88 static int
89 value(int type)
90 {
91  return button_read || !timer_expired(&debouncetimer);
92 }
93 /*---------------------------------------------------------------------------*/
94 static int
95 configure(int type, int value)
96 {
97  switch (type) {
98  case SENSORS_ACTIVE:
99  if(value) {
100  if(!status(SENSORS_ACTIVE)) {
101  timer_set(&debouncetimer, 0);
102 
103  TRISDbits.TRISD5 = 1;
104 
105  CNCON = 0;
106  CNCONSET = 1 << _CNCON_ON_POSITION | 1 << _CNCON_SIDL_POSITION;
107  CNEN = 1 << _CNEN_CNEN14_POSITION;
108  CNPUE = 1 << _CNPUE_CNPUE14_POSITION;
109 
110  IEC1CLR = _IEC1_CNIE_MASK;
111  IFS1CLR = _IFS1_CNIF_MASK;
112 
113  IPC6CLR = _IPC6_CNIP_MASK;
114  IPC6SET = 6 << _IPC6_CNIP_POSITION;
115 
116  IEC1SET = 1 << _IEC1_CNIE_POSITION;
117 
118  sensor_status = 1;
119  }
120  }
121  return 1;
122  }
123 
124  sensor_status = 0;
125 
126  return 0;
127 }
128 /*---------------------------------------------------------------------------*/
129 static int
130 status(int type)
131 {
132  return sensor_status;
133 }
134 /*---------------------------------------------------------------------------*/
135 SENSORS_SENSOR(button_sensor, BUTTON_SENSOR, value, configure, status);
136 /*---------------------------------------------------------------------------*/
137 
138 /** @} */
A timer.
Definition: timer.h:86
INTERRUPT interface for PIC32MX (pic32mx795f512l)
void timer_set(struct timer *t, clock_time_t interval)
Set a timer.
Definition: timer.c:64
int timer_expired(struct timer *t)
Check if a timer has expired.
Definition: timer.c:121
#define CLOCK_SECOND
A second, measured in system clock time.
Definition: clock.h:82