/*
* fsm-events.c: Event-handling functions for SpaghettiMonster.
*
* Copyright (C) 2017 Selene ToyKeeper
*
* 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, either version 3 of the License, or
* (at your option) any later version.
*
* 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, see .
*/
#ifndef FSM_EVENTS_C
#define FSM_EVENTS_C
// TODO: maybe compare events by number instead of pointer?
// (number = index in event types array)
// (comparison would use full event content, but send off index to callbacks)
// (saves space by using uint8_t instead of a pointer)
// (also eliminates the need to duplicate single-entry events like for voltage or timer tick)
// return 1 if (a == b), 0 otherwise
uint8_t compare_event_sequences(uint8_t *a, const uint8_t *b) {
for(uint8_t i=0; (i= offset) return current_event[i-offset];
return 0;
}
*/
inline uint8_t last_event_num() {
uint8_t i;
for(i=0; current_event[i] && (i 0) {
#ifdef USE_DYNAMIC_UNDERCLOCKING
// underclock MCU to save power
CLKPR = 1< 0) {
// underclock MCU to save power
CLKPR = 1<=0; i--) {
uint8_t err = state_stack[i](event, arg);
if (! err) return 0;
}
return 1; // event not handled
}
void emit(EventPtr event, uint16_t arg) {
// add this event to the queue for later,
// so we won't use too much time during an interrupt
append_emission(event, arg);
}
// Search the pre-defined event list for one matching what the user just did,
// and emit it if one was found.
void emit_current_event(uint16_t arg) {
//uint8_t err = 1;
for (uint8_t i=0; i<(sizeof(event_sequences)/sizeof(EventPtr)); i++) {
if (events_match(current_event, event_sequences[i])) {
//DEBUG_FLASH;
//err = emit(event_sequences[i], arg);
//return err;
emit(event_sequences[i], arg);
return;
}
}
//return err;
}
#endif