Contiki-Inga 3.x
button-sensor.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013, TU Braunschweig
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  * Button sensor routine
35  * @author:
36  * Georg von Zengen
37  * Enrico Joerns
38  */
39 
40 #include "lib/sensors.h"
41 #include "dev/button-sensor.h"
42 #include <avr/io.h>
43 #include <avr/interrupt.h>
44 
45 #define BUTTON_INT_vect INT2_vect
46 #define BUTTON_ISC ISC20
47 #define BUTTON_INT INT2
48 #define BUTTON_PORT PORTB
49 #define BUTTON_DDR DDRB
50 #define BUTION_PIN PINB
51 #define BUTTON_P PB2
52 #define BUTTON_REG_EICR EICRA
53 #define BUTTON_REG_EIMSK EIMSK
54 
55 #ifndef DEBOUNCE_TIME
56 #define DEBOUNCE_TIME CLOCK_SECOND / 40
57 #endif
58 
59 const struct sensors_sensor button_sensor;
60 static int status(int type);
61 static struct timer debouncetimer;
62 /*----------------------------------------------------------------------------*/
63 ISR(BUTTON_INT_vect) {
64  if (timer_expired(&debouncetimer)) {
65  timer_reset(&debouncetimer);
66  sensors_changed(&button_sensor);
67  }
68 }
69 static void
70 init() {
71  BUTTON_DDR &= ~(1 << BUTTON_P);
72  BUTTON_PORT &= ~(1 << BUTTON_P);
73  BUTTON_REG_EICR |= (0x1 << BUTTON_ISC); // 1 = any edge
74 }
75 /*----------------------------------------------------------------------------*/
76 static int
77 value(int type) {
78  return (1 - ((BUTION_PIN & (1 << BUTTON_P)) >> BUTTON_P))
79  || !timer_expired(&debouncetimer);
80 }
81 /*----------------------------------------------------------------------------*/
82 static int
83 configure(int type, int c) {
84  switch (type) {
85  case SENSORS_HW_INIT:
86  init();
87  return 1;
88  case SENSORS_ACTIVE:
89  // activate sensor
90  if (c) {
91  if (!status(SENSORS_ACTIVE)) {
92  timer_set(&debouncetimer, DEBOUNCE_TIME);
93  init();
94  BUTTON_REG_EIMSK |= (1 << BUTTON_INT);
95  sei();
96  }
97  // deactivate sensor
98  } else {
99  BUTTON_REG_EIMSK &= ~(1 << BUTTON_INT);
100  }
101  return 1;
102  }
103  return 0;
104 }
105 /*----------------------------------------------------------------------------*/
106 static int
107 status(int type) {
108  switch (type) {
109  case SENSORS_ACTIVE:
110  return (BUTTON_REG_EIMSK & (1 << BUTTON_INT));
111  case SENSORS_READY:
112  return ~(BUTTON_DDR & (1 << BUTTON_P));
113  }
114 
115  return 0;
116 }
117 /*----------------------------------------------------------------------------*/
118 SENSORS_SENSOR(button_sensor, BUTTON_SENSOR,
119  value, configure, status);
120