fsm.trigger() not working
stanleyseow opened this issue · 1 comments
stanleyseow commented
I wanna trigger a state change every 10 sec but it is not working..
#define WRITE_EVENT 2
while ( millis() - timer1 > 10000 ) {
Serial.print("<<< ");
Serial.print( millis()/divider );
Serial.println(" triggered stateWriteSerial");
fsm.trigger(WRITE_EVENT);
timer1 = millis();
}
setup() {
fsm.add_transition(&stateIdle, &stateWriteSerial, WRITE_EVENT,NULL );
}
natehouk commented
You must call fsm.run_machine() at least once from setup() for fsm.trgger() to work due to the predicate in the trigger function preventing it from triggering if not initialized.
void Fsm::trigger(int event)
{
if (m_initialized)
{
// Find the transition with the current state and given event.
for (int i = 0; i < m_num_transitions; ++i)
{
if (m_transitions[i].state_from == m_current_state &&
m_transitions[i].event == event)
{
Fsm::make_transition(&(m_transitions[i]));
return;
}
}
}
}
Calling fsm.run_machine() will set m_initialized = true
void Fsm::run_machine()
{
// first run must exec first state "on_enter"
if (!m_initialized)
{
m_initialized = true;
if (m_current_state->on_enter != NULL)
m_current_state->on_enter();
}
if (m_current_state->on_state != NULL)
m_current_state->on_state();
Fsm::check_timed_transitions();
}