182 lines
5.3 KiB
C
182 lines
5.3 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 "input.h"
|
||
|
#include "event.h"
|
||
|
|
||
|
/* *********************************************************************** */
|
||
|
|
||
|
static uint8_t pcint_old;
|
||
|
static uint8_t quad_state;
|
||
|
static uint8_t quad_state_old;
|
||
|
static uint8_t debounce_timer[2];
|
||
|
|
||
|
/* *********************************************************************** */
|
||
|
|
||
|
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))
|
||
|
{
|
||
|
if (debounce_timer[0] == 0)
|
||
|
{
|
||
|
debounce_timer[0] = 60; /* ~60ms */
|
||
|
|
||
|
event_queue(EVENT_TYPE_INPUT,
|
||
|
EVENT_NUM_INPUT_BUTTON,
|
||
|
!!(pcint_new & (1<<PINC2))
|
||
|
);
|
||
|
}
|
||
|
}
|
||
|
/* door switch changed */
|
||
|
else if (pcint & (1<<PINC3))
|
||
|
{
|
||
|
if (debounce_timer[1] == 0)
|
||
|
{
|
||
|
debounce_timer[1] = 60; /* ~60ms */
|
||
|
|
||
|
event_queue(EVENT_TYPE_INPUT,
|
||
|
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,
|
||
|
EVENT_NUM_INPUT_QUAD,
|
||
|
EVENT_VALUE_INPUT_QUAD_DEC
|
||
|
);
|
||
|
}
|
||
|
/* clock wise */
|
||
|
else if (quad_state == 2)
|
||
|
{
|
||
|
event_queue(EVENT_TYPE_INPUT,
|
||
|
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_tick(void)
|
||
|
{
|
||
|
if (debounce_timer[0] > 0)
|
||
|
{
|
||
|
debounce_timer[0]--;
|
||
|
}
|
||
|
|
||
|
if (debounce_timer[1] > 0)
|
||
|
{
|
||
|
debounce_timer[1]--;
|
||
|
}
|
||
|
} /* input_tick */
|
||
|
|
||
|
|
||
|
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 */
|
||
|
event_queue(EVENT_TYPE_INPUT,
|
||
|
EVENT_NUM_INPUT_BUTTON,
|
||
|
!!(pcint_old & (1<<PINC2))
|
||
|
);
|
||
|
|
||
|
/* send door state */
|
||
|
event_queue(EVENT_TYPE_INPUT,
|
||
|
EVENT_NUM_INPUT_DOOR,
|
||
|
!!(pcint_old & (1<<PINC3))
|
||
|
);
|
||
|
} /* input_init */
|