osd-contiki/cpu/avr/dev/button-sensor.c

89 lines
1.9 KiB
C
Raw Normal View History

2013-02-19 10:22:48 +01:00
/* Sensor routine */
#include "lib/sensors.h"
#include "dev/button-sensor.h"
#include <avr/interrupt.h>
#include "led.h" // debug
const struct sensors_sensor button_sensor;
static struct timer debouncetimer;
static int status(int type);
static int enabled = 0;
2016-08-15 20:22:49 +02:00
volatile static int bstate;
2016-09-16 14:31:38 +02:00
volatile static int bstatei;
2013-02-19 10:22:48 +01:00
struct sensors_sensor *sensors[1];
unsigned char sensors_flags[1];
2013-04-17 13:44:17 +02:00
#define BUTTON_BIT INTF4
2013-02-19 10:22:48 +01:00
#define BUTTON_CHECK_IRQ() (EIFR & BUTTON_BIT) ? 0 : 1
#define PRINTF(...) printf(__VA_ARGS__)
/*---------------------------------------------------------------------------*/
2013-04-17 13:44:17 +02:00
ISR(INT4_vect)
2013-02-19 10:22:48 +01:00
{
if(BUTTON_CHECK_IRQ()) {
2016-09-16 14:31:38 +02:00
bstatei = (PINE & _BV(PE4) ? 0 : 1);
2013-02-19 10:22:48 +01:00
if(timer_expired(&debouncetimer)) {
2016-09-16 14:31:38 +02:00
// led1_on();
timer_set(&debouncetimer, CLOCK_SECOND / 16);
2016-08-15 20:22:49 +02:00
bstate = (PINE & _BV(PE4) ? 0 : 1);
2016-09-16 14:31:38 +02:00
if(bstate == bstatei){
sensors_changed(&button_sensor);
}
2013-04-17 13:44:17 +02:00
// led1_off();
2013-02-19 10:22:48 +01:00
}
}
}
/*---------------------------------------------------------------------------*/
static int
value(int type)
{
2016-08-15 20:22:49 +02:00
//return (PINE & _BV(PE4) ? 0 : 1) || !timer_expired(&debouncetimer);
2014-04-30 15:20:25 +02:00
2016-08-15 20:22:49 +02:00
return bstate;
2013-02-19 10:22:48 +01:00
}
static int
configure(int type, int c)
{
switch (type) {
case SENSORS_ACTIVE:
if (c) {
if(!status(SENSORS_ACTIVE)) {
2013-04-17 13:44:17 +02:00
// led1_on();
2013-02-19 10:22:48 +01:00
timer_set(&debouncetimer, 0);
2013-04-17 13:44:17 +02:00
DDRE |= (0<<DDE4); // Set pin as input
PORTE |= (1<<PORTE4); // Set port PORTE bint 5 with pullup resistor
EICRB |= (1<<ISC40); // For falling edge
EIMSK |= (1<<INT4); // Set int
2013-02-19 10:22:48 +01:00
enabled = 1;
sei();
2013-04-17 13:44:17 +02:00
// led1_off();
2013-02-19 10:22:48 +01:00
}
} else {
enabled = 0;
2013-04-17 13:44:17 +02:00
EIMSK &= ~(1<<INT4); // clear int
2013-02-19 10:22:48 +01:00
}
return 1;
}
return 0;
}
static int
status(int type)
{
switch (type) {
case SENSORS_ACTIVE:
case SENSORS_READY:
return enabled;//(EIMSK & (1<<INT5) ? 0 : 1);//BUTTON_IRQ_ENABLED();
}
return 0;
}
SENSORS_SENSOR(button_sensor, BUTTON_SENSOR,
value, configure, status);