~toykeeper/flashlight-firmware/trunk

188.1.11 by Selene Scriven
Completely reorganized SpaghettiMonster code into smaller logical pieces: fsm-*.c and fsm-*.h.
1
/*
2
 * fsm-states.c: State-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
 */
19
 
20
#ifndef FSM_STATES_C
21
#define FSM_STATES_C
22
23
#include "fsm-states.h"
24
#include "fsm-adc.h"
25
26
// TODO: if callback doesn't handle current event,
27
//       pass event to next state on stack?
28
//       Callback return values:
29
//       0: event handled normally
30
//       1: event not handled
31
//       255: error (not sure what this would even mean though, or what difference it would make)
32
// TODO: function to call stacked callbacks until one returns "handled"
33
188.1.44 by Selene Scriven
Ramp config mode actually works now...
34
void _set_state(StatePtr new_state, uint16_t arg,
35
                EventPtr exit_event, EventPtr enter_event) {
188.1.11 by Selene Scriven
Completely reorganized SpaghettiMonster code into smaller logical pieces: fsm-*.c and fsm-*.h.
36
    // call old state-exit hook (don't use stack)
188.1.44 by Selene Scriven
Ramp config mode actually works now...
37
    if (current_state != NULL) current_state(exit_event, arg);
188.1.11 by Selene Scriven
Completely reorganized SpaghettiMonster code into smaller logical pieces: fsm-*.c and fsm-*.h.
38
    // set new state
39
    current_state = new_state;
40
    // call new state-enter hook (don't use stack)
188.1.44 by Selene Scriven
Ramp config mode actually works now...
41
    if (new_state != NULL) current_state(enter_event, arg);
188.1.11 by Selene Scriven
Completely reorganized SpaghettiMonster code into smaller logical pieces: fsm-*.c and fsm-*.h.
42
}
43
44
int8_t push_state(StatePtr new_state, uint16_t arg) {
45
    if (state_stack_len < STATE_STACK_SIZE) {
46
        // TODO: call old state's exit hook?
47
        //       new hook for non-exit recursion into child?
48
        state_stack[state_stack_len] = new_state;
49
        state_stack_len ++;
188.1.44 by Selene Scriven
Ramp config mode actually works now...
50
        // FIXME: use EV_stacked_state?
51
        _set_state(new_state, arg, EV_leave_state, EV_enter_state);
188.1.11 by Selene Scriven
Completely reorganized SpaghettiMonster code into smaller logical pieces: fsm-*.c and fsm-*.h.
52
        return state_stack_len;
53
    } else {
54
        // TODO: um...  how is a flashlight supposed to handle a recursion depth error?
55
        return -1;
56
    }
57
}
58
59
StatePtr pop_state() {
60
    // TODO: how to handle pop from empty stack?
61
    StatePtr old_state = NULL;
62
    StatePtr new_state = NULL;
63
    if (state_stack_len > 0) {
64
        state_stack_len --;
65
        old_state = state_stack[state_stack_len];
66
    }
67
    if (state_stack_len > 0) {
68
        new_state = state_stack[state_stack_len-1];
69
    }
188.1.69 by Selene Scriven
Got the 4th PWM channel to work, ish. (channel 4 is inverted though)
70
    // FIXME: what should 'arg' be?  (maybe re-entry should be entry with arg+1?)
188.1.44 by Selene Scriven
Ramp config mode actually works now...
71
    _set_state(new_state, 0, EV_leave_state, EV_reenter_state);
188.1.11 by Selene Scriven
Completely reorganized SpaghettiMonster code into smaller logical pieces: fsm-*.c and fsm-*.h.
72
    return old_state;
73
}
74
75
uint8_t set_state(StatePtr new_state, uint16_t arg) {
76
    // FIXME: this calls exit/enter hooks it shouldn't
188.1.69 by Selene Scriven
Got the 4th PWM channel to work, ish. (channel 4 is inverted though)
77
    //        (for the layer underneath the top)
188.1.11 by Selene Scriven
Completely reorganized SpaghettiMonster code into smaller logical pieces: fsm-*.c and fsm-*.h.
78
    pop_state();
79
    return push_state(new_state, arg);
80
}
81
188.1.69 by Selene Scriven
Got the 4th PWM channel to work, ish. (channel 4 is inverted though)
82
#ifndef DONT_USE_DEFAULT_STATE
188.1.11 by Selene Scriven
Completely reorganized SpaghettiMonster code into smaller logical pieces: fsm-*.c and fsm-*.h.
83
// bottom state on stack
84
// handles default actions for LVP, thermal regulation, etc
85
uint8_t default_state(EventPtr event, uint16_t arg) {
188.1.69 by Selene Scriven
Got the 4th PWM channel to work, ish. (channel 4 is inverted though)
86
    if (0) {}  // this should get compiled out
188.1.11 by Selene Scriven
Completely reorganized SpaghettiMonster code into smaller logical pieces: fsm-*.c and fsm-*.h.
87
88
    #ifdef USE_LVP
89
    else if (event == EV_voltage_low) {
90
        low_voltage();
188.1.69 by Selene Scriven
Got the 4th PWM channel to work, ish. (channel 4 is inverted though)
91
        return EVENT_HANDLED;
188.1.11 by Selene Scriven
Completely reorganized SpaghettiMonster code into smaller logical pieces: fsm-*.c and fsm-*.h.
92
    }
93
    #endif
94
188.1.16 by Selene Scriven
Added thermal regulation to SpaghettiMonster / Baton.
95
    #if 0
188.1.11 by Selene Scriven
Completely reorganized SpaghettiMonster code into smaller logical pieces: fsm-*.c and fsm-*.h.
96
    #ifdef USE_THERMAL_REGULATION
97
    else if (event == EV_temperature_high) {
98
        high_temperature();
99
        return 0;
100
    }
101
102
    else if (event == EV_temperature_low) {
103
        low_temperature();
104
        return 0;
105
    }
106
    #endif
188.1.16 by Selene Scriven
Added thermal regulation to SpaghettiMonster / Baton.
107
    #endif
188.1.11 by Selene Scriven
Completely reorganized SpaghettiMonster code into smaller logical pieces: fsm-*.c and fsm-*.h.
108
109
    // event not handled
188.1.69 by Selene Scriven
Got the 4th PWM channel to work, ish. (channel 4 is inverted though)
110
    return EVENT_NOT_HANDLED;
188.1.11 by Selene Scriven
Completely reorganized SpaghettiMonster code into smaller logical pieces: fsm-*.c and fsm-*.h.
111
}
188.1.69 by Selene Scriven
Got the 4th PWM channel to work, ish. (channel 4 is inverted though)
112
#endif
188.1.11 by Selene Scriven
Completely reorganized SpaghettiMonster code into smaller logical pieces: fsm-*.c and fsm-*.h.
113
114
#endif