2013-02-12 16:40:47 +01:00
|
|
|
/* Sensor routine */
|
|
|
|
|
2013-02-12 15:02:16 +01:00
|
|
|
#include "lib/sensors.h"
|
|
|
|
#include "dev/button-sensor.h"
|
|
|
|
|
|
|
|
#include <avr/interrupt.h>
|
2013-02-12 16:40:47 +01:00
|
|
|
#include "led.h" // debug
|
2013-02-12 15:02:16 +01:00
|
|
|
|
|
|
|
const struct sensors_sensor button_sensor;
|
|
|
|
|
|
|
|
static struct timer debouncetimer;
|
|
|
|
static int status(int type);
|
|
|
|
static int enabled = 0;
|
2013-02-12 16:40:47 +01:00
|
|
|
struct sensors_sensor *sensors[1];
|
|
|
|
unsigned char sensors_flags[1];
|
2013-02-12 15:02:16 +01:00
|
|
|
|
2013-02-13 15:07:49 +01:00
|
|
|
#define BUTTON_BIT INTF5
|
2013-02-12 15:02:16 +01:00
|
|
|
#define BUTTON_CHECK_IRQ() (EIFR & BUTTON_BIT) ? 0 : 1
|
|
|
|
|
|
|
|
#define PRINTF(...) printf(__VA_ARGS__)
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2013-02-13 15:07:49 +01:00
|
|
|
ISR(INT5_vect)
|
2013-02-12 15:02:16 +01:00
|
|
|
{
|
|
|
|
|
|
|
|
// leds_toggle(LEDS_YELLOW);
|
|
|
|
|
|
|
|
if(BUTTON_CHECK_IRQ()) {
|
|
|
|
if(timer_expired(&debouncetimer)) {
|
2013-02-12 16:40:47 +01:00
|
|
|
led1_on();
|
2013-02-12 15:02:16 +01:00
|
|
|
timer_set(&debouncetimer, CLOCK_SECOND / 4);
|
|
|
|
sensors_changed(&button_sensor);
|
2013-02-12 16:40:47 +01:00
|
|
|
led1_off();
|
2013-02-12 15:02:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2013-02-12 16:40:47 +01:00
|
|
|
|
|
|
|
static int
|
|
|
|
value(int type)
|
2013-02-12 15:02:16 +01:00
|
|
|
{
|
2013-02-13 15:07:49 +01:00
|
|
|
return (PORTE & _BV(PE5) ? 0 : 1) || !timer_expired(&debouncetimer);
|
2013-02-12 16:40:47 +01:00
|
|
|
//return 0;
|
2013-02-12 15:02:16 +01:00
|
|
|
}
|
2013-02-12 16:40:47 +01:00
|
|
|
|
|
|
|
static int
|
|
|
|
configure(int type, int c)
|
2013-02-12 15:02:16 +01:00
|
|
|
{
|
|
|
|
PRINTF("Sensor Button Configure called: %d, %d\n",type,c);
|
2013-02-12 16:40:47 +01:00
|
|
|
switch (type) {
|
|
|
|
case SENSORS_ACTIVE:
|
|
|
|
if (c) {
|
|
|
|
if(!status(SENSORS_ACTIVE)) {
|
|
|
|
led1_on();
|
|
|
|
timer_set(&debouncetimer, 0);
|
|
|
|
PRINTF("Setup sensor started\n");
|
2013-02-13 15:07:49 +01:00
|
|
|
DDRE |= (0<<DDE5); // Set pin as input
|
|
|
|
PORTE |= (1<<PORTE5); // Set port PORTE bint 5 with pullup resistor
|
|
|
|
EICRB |= (2<<ISC50); // For falling edge
|
|
|
|
EIMSK |= (1<<INT5); // Set int
|
2013-02-12 16:40:47 +01:00
|
|
|
enabled = 1;
|
|
|
|
sei();
|
|
|
|
led1_off();
|
|
|
|
}
|
|
|
|
PRINTF("Sensor EIMSK set\n");
|
|
|
|
} else {
|
|
|
|
enabled = 0;
|
2013-02-13 15:07:49 +01:00
|
|
|
EIMSK &= ~(1<<INT5); // clear int
|
2013-02-12 16:40:47 +01:00
|
|
|
PRINTF("Setup sensor failed\n");
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
2013-02-12 15:02:16 +01:00
|
|
|
}
|
2013-02-12 16:40:47 +01:00
|
|
|
|
2013-02-12 15:02:16 +01:00
|
|
|
static int
|
|
|
|
status(int type)
|
|
|
|
{
|
2013-02-12 16:40:47 +01:00
|
|
|
switch (type) {
|
|
|
|
case SENSORS_ACTIVE:
|
|
|
|
case SENSORS_READY:
|
2013-02-13 15:07:49 +01:00
|
|
|
return enabled;//(EIMSK & (1<<INT5) ? 0 : 1);//BUTTON_IRQ_ENABLED();
|
2013-02-12 16:40:47 +01:00
|
|
|
}
|
|
|
|
return 0;
|
2013-02-12 15:02:16 +01:00
|
|
|
}
|
2013-02-12 16:40:47 +01:00
|
|
|
|
|
|
|
SENSORS_SENSOR(button_sensor, BUTTON_SENSOR,
|
|
|
|
value, configure, status);
|
|
|
|
|