~toykeeper/flashlight-firmware/fsm

« back to all changes in this revision

Viewing changes to ToyKeeper/spaghetti-monster/fsm-events.h

  • 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-events.h: Event-handling 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-events.h: Event-handling functions for SpaghettiMonster.
 
2
// Copyright (C) 2017-2023 Selene ToyKeeper
 
3
// SPDX-License-Identifier: GPL-3.0-or-later
19
4
 
20
 
#ifndef FSM_EVENTS_H
21
 
#define FSM_EVENTS_H
 
5
#pragma once
22
6
 
23
7
#include <avr/pgmspace.h>
24
8
 
 
9
 
 
10
// timeout durations in ticks (each tick 1/62th s)
 
11
#ifndef HOLD_TIMEOUT
 
12
#define HOLD_TIMEOUT 24
 
13
#endif
 
14
#ifndef RELEASE_TIMEOUT
 
15
#define RELEASE_TIMEOUT 18
 
16
#endif
 
17
 
 
18
// return codes for Event handlers
 
19
// Indicates whether this handler consumed (handled) the Event, or
 
20
// if the Event should be sent to the next handler in the stack.
 
21
#define EVENT_HANDLED 0
 
22
#define EVENT_NOT_HANDLED 1
 
23
#define MISCHIEF_MANAGED EVENT_HANDLED
 
24
#define MISCHIEF_NOT_MANAGED EVENT_NOT_HANDLED
 
25
 
25
26
// typedefs
26
27
typedef uint8_t Event;
27
28
typedef struct Emission {
29
30
    uint16_t arg;
30
31
} Emission;
31
32
 
32
 
#define EVENT_HANDLED 0
33
 
#define EVENT_NOT_HANDLED 1
34
 
#define MISCHIEF_MANAGED EVENT_HANDLED
35
 
#define MISCHIEF_NOT_MANAGED EVENT_NOT_HANDLED
36
 
 
37
33
Event current_event;
38
34
// at 0.016 ms per tick, 255 ticks = 4.08 s
39
35
static volatile uint16_t ticks_since_last_event = 0;
40
36
 
41
 
// timeout durations in ticks (each tick 1/62th s)
42
 
#ifndef HOLD_TIMEOUT
43
 
#define HOLD_TIMEOUT 24
44
 
#endif
45
 
#ifndef RELEASE_TIMEOUT
46
 
#define RELEASE_TIMEOUT 18
47
 
#endif
 
37
// maximum number of events which can be waiting at one time
 
38
// (would probably be okay to reduce this to 4, but it's higher to be safe)
 
39
#define EMISSION_QUEUE_LEN 16
 
40
// was "volatile" before, changed to regular var since IRQ rewrites seem
 
41
// to have removed the need for it to be volatile
 
42
// no comment about "volatile emissions"
 
43
Emission emissions[EMISSION_QUEUE_LEN];
 
44
 
 
45
void append_emission(Event event, uint16_t arg);
 
46
void delete_first_emission();
 
47
void process_emissions();
 
48
uint8_t emit_now(Event event, uint16_t arg);
 
49
void emit(Event event, uint16_t arg);
 
50
void emit_current_event(uint16_t arg);
 
51
void empty_event_sequence();
 
52
uint8_t push_event(uint8_t ev_type);  // only for use by PCINT_inner()
 
53
 
 
54
 
 
55
// TODO: Maybe move these to their own file...
 
56
// ... this probably isn't the right place for delays.
 
57
#ifndef DELAY_FACTOR
 
58
    // adjust the timing of delays, lower = shorter delays
 
59
    // 90 = 90% delay, 10% for other things
 
60
    #define DELAY_FACTOR 92
 
61
#endif
 
62
inline void interrupt_nice_delays();
 
63
uint8_t nice_delay_ms(uint16_t ms);
 
64
//uint8_t nice_delay_s();
 
65
void delay_4ms(uint8_t ms);
 
66
 
48
67
 
49
68
/* Event structure
50
69
 * Bit  7: 1 for a button input event, 0 for all others.
200
219
#define EV_click15_hold         (B_CLICK|B_HOLD|B_PRESS|15)
201
220
#define EV_click15_hold_release (B_CLICK|B_HOLD|B_RELEASE|B_TIMEOUT|15)
202
221
 
203
 
 
204
 
void empty_event_sequence();
205
 
uint8_t push_event(uint8_t ev_type);
206
 
 
207
 
 
208
 
#define EMISSION_QUEUE_LEN 16
209
 
// no comment about "volatile emissions"
210
 
volatile Emission emissions[EMISSION_QUEUE_LEN];
211
 
 
212
 
void append_emission(Event event, uint16_t arg);
213
 
void delete_first_emission();
214
 
void process_emissions();
215
 
//#define emit_now emit
216
 
uint8_t emit_now(Event event, uint16_t arg);
217
 
void emit(Event event, uint16_t arg);
218
 
void emit_current_event(uint16_t arg);
219
 
 
220
 
uint8_t nice_delay_ms(uint16_t ms);
221
 
//uint8_t nice_delay_s();
222
 
inline void interrupt_nice_delays();
223
 
void delay_4ms(uint8_t ms);
224
 
 
225
 
#endif