~toykeeper/flashlight-firmware/fsm

« back to all changes in this revision

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

  • Committer: Selene ToyKeeper
  • Date: 2023-11-04 15:09:10 UTC
  • mfrom: (483.1.175 anduril2)
  • Revision ID: bzr@toykeeper.net-20231104150910-ddd3afw4nhfvof2l
merged anduril2 branch -> fsm, with *years* of changes
(this also means this code is now Anduril 2 instead of Anduril 1)

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
 
 */
 
1
// fsm-pcint.c: PCINT (Pin Change Interrupt) functions for SpaghettiMonster.
 
2
// Copyright (C) 2017-2023 Selene ToyKeeper
 
3
// SPDX-License-Identifier: GPL-3.0-or-later
19
4
 
20
 
#ifndef FSM_PCINT_C
21
 
#define FSM_PCINT_C
 
5
#pragma once
22
6
 
23
7
#include <avr/interrupt.h>
24
8
#include <util/delay_basic.h>
46
30
        #else
47
31
        GIMSK |= (1 << SWITCH_PCIE);
48
32
        #endif
 
33
    #elif defined(AVRXMEGA3)  // ATTINY816, 817, etc)
 
34
        SWITCH_ISC_REG |= PORT_ISC_BOTHEDGES_gc;
49
35
    #else
50
36
        #error Unrecognized MCU type
51
37
    #endif
58
44
    #elif (ATTINY == 1634)
59
45
        // disable all pin-change interrupts
60
46
        GIMSK &= ~(1 << SWITCH_PCIE);
 
47
    #elif defined(AVRXMEGA3)  // ATTINY816, 817, etc)
 
48
        SWITCH_ISC_REG &= ~(PORT_ISC_gm);
61
49
    #else
62
50
        #error Unrecognized MCU type
63
51
    #endif
65
53
 
66
54
//void button_change_interrupt() {
67
55
#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
 
}
 
56
    #ifdef PCINT_vect
 
57
    ISR(PCINT_vect) {
 
58
    #else
 
59
    ISR(PCINT0_vect) {
 
60
    #endif
 
61
#elif defined(AVRXMEGA3)  // ATTINY816, 817, etc)
 
62
    ISR(SWITCH_VECT) {
 
63
        // Write a '1' to clear the interrupt flag
 
64
        SWITCH_INTFLG |= (1 << SWITCH_PIN);
76
65
#else
77
66
    #error Unrecognized MCU type
78
67
#endif
79
 
/*
80
 
ISR(PCINT0_vect) {
 
68
 
 
69
    irq_pcint = 1;  // let deferred code know an interrupt happened
81
70
 
82
71
    //DEBUG_FLASH;
83
72
 
86
75
    // noisy / bouncy switch (so the content of this function has been
87
76
    // moved to a separate function, called from WDT only)
88
77
    // PCINT_inner(button_is_pressed());
89
 
 
90
78
}
91
 
*/
92
79
 
93
80
// should only be called from PCINT and/or WDT
94
81
// (is a separate function to reduce code duplication)
95
82
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;
 
83
    button_last_state = pressed;
 
84
 
 
85
    // register the change, and send event to the current state callback
 
86
    if (pressed) {  // user pressed button
 
87
        push_event(B_PRESS);
107
88
        emit_current_event(0);
 
89
    } else {  // user released button
 
90
        // how long was the button held?
 
91
        push_event(B_RELEASE);
 
92
        emit_current_event(ticks_since_last_event);
108
93
    }
 
94
    ticks_since_last_event = 0;
109
95
}
110
 
#endif
 
96