SAE Teensy ECU
IIT SAE Microcontroller programming
Loading...
Searching...
No Matches
State.cpp
Go to the documentation of this file.
1
12// @cond
13#include "State.h"
14#include "Log.h"
15#include "Pins.h"
16#include "WProgram.h"
17
18static LOG_TAG TAG = "State Manager";
19
20static State::State_t *lastState;
21static State::State_t *currentState;
22static int currentNotifyCode = 0;
23
24static void setNextState(State::State_t *state) {
25 lastState = currentState;
26 currentState = state;
27 currentState->notifyCode = currentNotifyCode;
28}
29
30static struct UnhandledState_t : State::State_t {
31 LOG_TAG ID = "UNHANDLED STATE";
32
33 State_t *run(void) {
34 Log.f(ID, "UNHANDLED STATE!");
36 return this;
37 };
38
39} UnhandledState;
40
42 return &UnhandledState;
43}
44
45void State::State_t::notify(int code) {
46 currentNotifyCode = code;
47}
48
50 return this->notifyCode;
51}
52
54 return lastState;
55}
56
58 return (LOG_TAG)420;
59}
60
61void State::begin(State_t &entry) {
62 State_t *startingState = &entry;
63 setNextState(&entry);
64
65 Log.d(TAG, "Starting State Machine");
66 while (currentNotifyCode != State::E_FATAL) {
67 currentNotifyCode = 0;
68 Log.d(TAG, "Start", TAG2NUM(currentState->getID())); // Note: cannot send initalizing state as pins are not defined yet to transmit message
69 Pins::setInternalValue(PINS_INTERNAL_STATE, TAG2NUM(currentState->getID()));
70
71 State_t *queuedState = currentState->run();
72
73 Log.d(TAG, "State returned code", currentNotifyCode);
74
75 if (currentNotifyCode == State::E_RESTART) {
76 setNextState(startingState);
77 } else {
78 setNextState(queuedState);
79 }
80
81 delay(1000);
82 }
83
84 Log.f(TAG, "STATE MACHINE STOPPED");
85}
86// @endcond
Special logging functionality.
uint32_t TAG2NUM(LOG_TAG tagValue)
Return the final numbervalue of a LOG_TAG.
const char * LOG_TAG
Type definition of logging tags This typedef is necessary to allow for easier manipulation of code by...
Definition Log.h:48
Logging::Log_t Log
The global logging object.
Update, set, and get predefined pin values.
State library.
void setInternalValue(uint8_t Internal_Pin, int value)
Set the value of an internal pin.
@ E_RESTART
Special code that makes the state machine start from the first state again.
Definition State.h:64
@ E_FATAL
Special code that makes the state machine completely stop.
Definition State.h:60
void begin(State_t &entry)
Begin the state machine with a pointer to a state.
void d(LOG_TAG TAG, LOG_MSG message)
Log a string using a debug tag.
void f(LOG_TAG TAG, LOG_MSG message)
Log a string using a fatal tag.
The parent state structure to extend from to create more states.
Definition State.h:70
int notifyCode
Used for receiving values from other states.
Definition State.h:90
void notify(int notify)
Send a code to the next state.
virtual LOG_TAG getID(void)
Returns the LOG_TAG of the state.
virtual State_t * run(void)
Runs the state.
State_t * getLastState()
Get a pointer of the last state.
int getNotify(void)
Get the code of the previous state.