~toykeeper/flashlight-firmware/trunk

« back to all changes in this revision

Viewing changes to ToyKeeper/spaghetti-monster/fsm-pcint.c

  • Committer: Selene Scriven
  • Date: 2015-03-17 08:56:50 UTC
  • mto: This revision was merged to the branch mainline in revision 124.
  • Revision ID: ubuntu@toykeeper.net-20150317085650-s89wr9h28n2co7z1
Added TheStar firmwares from _the_

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * fsm-pcint.c: PCINT (Pin Change Interrupt) functions for SpaghettiMonster.
3
 
 *
4
 
 * Copyright (C) 2017 Selene Scriven
5
 
 *
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.
10
 
 *
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.
15
 
 *
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/>.
18
 
 */
19
 
 
20
 
#ifndef FSM_PCINT_C
21
 
#define FSM_PCINT_C
22
 
 
23
 
#include <avr/interrupt.h>
24
 
#include <util/delay_basic.h>
25
 
 
26
 
uint8_t button_is_pressed() {
27
 
    uint8_t value = ((SWITCH_PORT & (1<<SWITCH_PIN)) == 0);
28
 
    button_last_state = value;
29
 
    return value;
30
 
}
31
 
 
32
 
inline void PCINT_on() {
33
 
    #if (ATTINY == 25) || (ATTINY == 45) || (ATTINY == 85)
34
 
        // enable pin change interrupt
35
 
        GIMSK |= (1 << PCIE);
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);
39
 
        #endif
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
44
 
        #ifdef SWITCH2_PCIE
45
 
        GIMSK |= ((1 << SWITCH_PCIE) | (1 << SWITCH2_PCIE));
46
 
        #else
47
 
        GIMSK |= (1 << SWITCH_PCIE);
48
 
        #endif
49
 
    #else
50
 
        #error Unrecognized MCU type
51
 
    #endif
52
 
}
53
 
 
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);
61
 
    #else
62
 
        #error Unrecognized MCU type
63
 
    #endif
64
 
}
65
 
 
66
 
//void button_change_interrupt() {
67
 
#if (ATTINY == 25) || (ATTINY == 45) || (ATTINY == 85) || (ATTINY == 1634)
68
 
//EMPTY_INTERRUPT(PCINT0_vect);
69
 
#ifdef PCINT_vect
70
 
ISR(PCINT_vect) {
71
 
#else
72
 
ISR(PCINT0_vect) {
73
 
#endif
74
 
    irq_pcint = 1;
75
 
}
76
 
#else
77
 
    #error Unrecognized MCU type
78
 
#endif
79
 
/*
80
 
ISR(PCINT0_vect) {
81
 
 
82
 
    //DEBUG_FLASH;
83
 
 
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());
89
 
 
90
 
}
91
 
*/
92
 
 
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) {
96
 
    uint8_t pushed;
97
 
 
98
 
    if (pressed) {
99
 
        pushed = push_event(B_PRESS);
100
 
    } else {
101
 
        pushed = push_event(B_RELEASE);
102
 
    }
103
 
 
104
 
    // send event to the current state callback
105
 
    if (pushed) {
106
 
        button_last_state = pressed;
107
 
        emit_current_event(0);
108
 
    }
109
 
}
110
 
#endif