2
* fsm-pcint.c: PCINT (Pin Change Interrupt) functions for SpaghettiMonster.
4
* Copyright (C) 2017 Selene Scriven
6
* This program is free software: you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation, either version 3 of the License, or
9
* (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program. If not, see <http://www.gnu.org/licenses/>.
23
#include <avr/interrupt.h>
24
#include <util/delay_basic.h>
26
uint8_t button_is_pressed() {
27
uint8_t value = ((SWITCH_PORT & (1<<SWITCH_PIN)) == 0);
28
button_last_state = value;
32
inline void PCINT_on() {
33
#if (ATTINY == 25) || (ATTINY == 45) || (ATTINY == 85)
34
// enable pin change interrupt
36
// only pay attention to the e-switch pin
37
#if 0 // this is redundant; was already done in main()
38
PCMSK = (1 << SWITCH_PCINT);
40
// set bits 1:0 to 0b01 (interrupt on rising *and* falling edge) (default)
41
// MCUCR &= 0b11111101; MCUCR |= 0b00000001;
42
#elif (ATTINY == 1634)
43
// enable pin change interrupt
45
GIMSK |= ((1 << SWITCH_PCIE) | (1 << SWITCH2_PCIE));
47
GIMSK |= (1 << SWITCH_PCIE);
50
#error Unrecognized MCU type
54
inline void PCINT_off() {
55
#if (ATTINY == 25) || (ATTINY == 45) || (ATTINY == 85)
56
// disable all pin-change interrupts
57
GIMSK &= ~(1 << PCIE);
58
#elif (ATTINY == 1634)
59
// disable all pin-change interrupts
60
GIMSK &= ~(1 << SWITCH_PCIE);
62
#error Unrecognized MCU type
66
//void button_change_interrupt() {
67
#if (ATTINY == 25) || (ATTINY == 45) || (ATTINY == 85) || (ATTINY == 1634)
68
//EMPTY_INTERRUPT(PCINT0_vect);
77
#error Unrecognized MCU type
84
// as it turns out, it's more reliable to detect pin changes from WDT
85
// because PCINT itself tends to double-tap when connected to a
86
// noisy / bouncy switch (so the content of this function has been
87
// moved to a separate function, called from WDT only)
88
// PCINT_inner(button_is_pressed());
93
// should only be called from PCINT and/or WDT
94
// (is a separate function to reduce code duplication)
95
void PCINT_inner(uint8_t pressed) {
99
pushed = push_event(B_PRESS);
101
pushed = push_event(B_RELEASE);
104
// send event to the current state callback
106
button_last_state = pressed;
107
emit_current_event(0);