Contiki 2.5
button-sensor.c
1 /*Button sensor routine
2 * Author: Georg von Zengen
3 */
4 
5 #include "lib/sensors.h"
6 #include "dev/button-sensor.h"
7 #include <avr/io.h>
8 #include <avr/interrupt.h>
9 const struct sensors_sensor button_sensor;
10 static int status(int type);
11 static struct timer debouncetimer;
12 ISR(INT2_vect){
13 
14  if(timer_expired(&debouncetimer)) {
15  timer_set(&debouncetimer, CLOCK_SECOND / 40);
16  sensors_changed(&button_sensor);
17  }
18 
19 }
20 
21 static int
22 value(int type)
23 {
24  return 1-((PINB & (1<<PB2))>>PB2) || !timer_expired(&debouncetimer);
25 }
26 
27 static int
28 configure(int type, int c)
29 {
30  switch (type) {
31  case SENSORS_ACTIVE:
32  if (c) {
33  if(!status(SENSORS_ACTIVE)) {
34  timer_set(&debouncetimer, CLOCK_SECOND / 40);
35  DDRB &= ~(1<<PB2);
36  PORTB &= ~(1<<PB2);
37  sei();
38  EICRA |= (1<<ISC20);
39  EIMSK |= (1<<INT2);
40  }
41  } else {
42  DDRB |= (1<<PB2);
43  PORTB |= (1<<PB2);
44  EIMSK &= ~(1<<INT2);
45  }
46  return 1;
47  }
48  return 0;
49 }
50 
51 static int
52 status(int type)
53 {
54  switch (type) {
55  case SENSORS_ACTIVE:
56  return 0;
57  case SENSORS_READY:
58  return ~(DDRB & (1<<PB2));
59  }
60 
61  return 0;
62 }
63 
64 SENSORS_SENSOR(button_sensor, BUTTON_SENSOR,
65  value, configure, status);
66