funkstuff/input.c

204 lines
6.2 KiB
C

/***************************************************************************
* Copyright (C) 11/2014 by Olaf Rempel *
* razzor@kopf-tisch.de *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; version 2 of the License, *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include <avr/io.h>
#include <avr/interrupt.h>
#include "event.h"
#include "input.h"
#include "timer.h"
/* *********************************************************************** */
#define INPUT_DEBOUNCE_TIMER 60
/* *********************************************************************** */
static uint8_t pcint_old;
static uint8_t quad_state;
static uint8_t quad_state_old;
static uint8_t debounce_timer[2];
static uint8_t input_old_state[2];
/* *********************************************************************** */
static void input_send_event(uint8_t type, uint8_t num, uint8_t state)
{
if ((debounce_timer[num] == 0) &&
(state != input_old_state[num])
)
{
debounce_timer[num] = 1;
event_queue(type, num, state);
event_queue(EVENT_TYPE_TIMER_SET, num, INPUT_DEBOUNCE_TIMER);
input_old_state[num] = state;
}
} /* input_send_event */
ISR(PCINT1_vect)
{
uint8_t pcint_new = PINC & PCMSK1;
uint8_t pcint = pcint_new ^ pcint_old;
uint8_t quad_state_new = quad_state;
/* quadruple decoder A changed */
if (pcint & (1<<PINC0))
{
/* positive edge */
if (pcint_new & (1<<PINC0))
{
if ((quad_state == 0) || (quad_state == 2))
{
quad_state_new++;
}
}
/* negative edge */
else
{
if ((quad_state == 1) || (quad_state == 3))
{
quad_state_new--;
}
}
}
/* quadruple decoder B changed */
else if (pcint & (1<<PINC1))
{
/* positive edge */
if (pcint_new & (1<<PINC1))
{
if ((quad_state == 0) || (quad_state == 1))
{
quad_state_new += 2;
}
}
/* negative edge */
else
{
if ((quad_state == 2) || (quad_state == 3))
{
quad_state_new -= 2;
}
}
}
/* quadruple decoder switch changed */
else if (pcint & (1<<PINC2))
{
input_send_event(EVENT_TYPE_INPUT_BUTTON,
EVENT_NUM_INPUT_BUTTON,
!!(pcint_new & (1<<PINC2)));
}
/* door switch changed */
else if (pcint & (1<<PINC3))
{
input_send_event(EVENT_TYPE_INPUT_SWITCH,
EVENT_NUM_INPUT_DOOR,
!(pcint_new & (1<<PINC3)));
}
/* one revolution completed? */
if ((quad_state_new == 3) && (quad_state_old == 0))
{
/* counter clock wise */
if (quad_state == 1)
{
event_queue(EVENT_TYPE_INPUT_INCDEC,
EVENT_NUM_INPUT_QUAD,
EVENT_VALUE_INPUT_QUAD_DEC);
}
/* clock wise */
else if (quad_state == 2)
{
event_queue(EVENT_TYPE_INPUT_INCDEC,
EVENT_NUM_INPUT_QUAD,
EVENT_VALUE_INPUT_QUAD_INC);
}
}
/* remember every two states */
if ((quad_state_new == 0) || (quad_state_new == 3))
{
quad_state_old = quad_state_new;
}
quad_state = quad_state_new;
pcint_old = pcint_new;
} /* PCINT1_vect */
void input_event_handler(struct event_entry *event)
{
if (event->type == EVENT_TYPE_TIMER_ELAPSED)
{
if (event->num == EVENT_NUM_TIMER_DEBOUNCE1)
{
debounce_timer[EVENT_NUM_INPUT_BUTTON] = 0;
input_send_event(EVENT_TYPE_INPUT_SWITCH,
EVENT_NUM_INPUT_BUTTON,
!!(PINC & (1<<PINC2)));
}
else if (event->num == EVENT_NUM_TIMER_DEBOUNCE2)
{
debounce_timer[EVENT_NUM_INPUT_DOOR] = 0;
input_send_event(EVENT_TYPE_INPUT_SWITCH,
EVENT_NUM_INPUT_DOOR,
!!(PINC & (1<<PINC3)));
}
}
} /* input_event_handler */
uint8_t input_get_sleep_mode(void)
{
return SLEEP_MODE_PWR_DOWN;
} /* input_get_sleep_mode */
void input_init(void)
{
/* pullup on all inputs */
DDRC &= ~((1<<PORTC3) | (1<<PORTC2) | (1<<PORTC1) | (1<<PORTC0));
PORTC |= (1<<PORTC3) | (1<<PORTC2) | (1<<PORTC1) | (1<<PORTC0);
/* Pinchange Interrupts enabled */
PCMSK1 = (1<<PCINT11) | (1<<PCINT10) | (1<<PCINT9) | (1<<PCINT8);
PCICR = (1<<PCIE1);
/* initial pin interrupt state */
pcint_old = PINC & PCMSK1;
/* initial quadruple decoder state */
quad_state = PINC & ((1<<PINC0) | (1<<PINC1));
/* send button state */
input_send_event(EVENT_TYPE_INPUT_BUTTON,
EVENT_NUM_INPUT_BUTTON,
!!(PINC & (1<<PINC2)));
/* send door state */
input_send_event(EVENT_TYPE_INPUT_SWITCH,
EVENT_NUM_INPUT_DOOR,
!(PINC & (1<<PINC3)));
} /* input_init */