Allow on_state() functions
Opened this issue · 7 comments
Allow states to have a on_state()
function that will run while in that state.
Main program loop will need a call to run_machine()
to resolve than on_state()
calls and resolve timed transitions.
This example shows how it would work (not tested, can contain errors):
#include <Fsm.h>
/*
* FSM Library sample with user and timed
* transitions.
* Uses a button and Arduino builtin led,
* button can be replaced just grounding
* pin.
*/
// Used pins
#define LED_PIN 13
#define BUTTON_PIN 8
//Events
#define BUTTON_EVENT 0
int buttonState = 0;
/* state 1: led off
* state 2: led on
* transition from s1 to s2 when pusshing button
* transition back from s2 to s1 after 3 seconds
* SECOND PARAM IS NEW "on_state()" FUNCTION
*/
State state_led_off(&led_off, &check_button, null);
State state_led_on(&led_on, null, null);
Fsm fsm(&state_led_off);
// Transition functions
void led_off()
{
digitalWrite(LED_PIN, LOW);
}
void led_on()
{
digitalWrite(LED_PIN, HIGH);
}
void check_button()
{
buttonState = digitalRead(buttonPin);
if (buttonState == LOW) {
fsm.trigger(BUTTON_EVENT);
}
}
// standard arduino functions
void setup()
{
pinMode(LED_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT);
fsm.add_transition(&state_led_off, &state_led_on,
BUTTON_EVENT, null);
fsm.add_timed_transition(&state_led_on, &state_led_off, 3000, NULL);
}
void loop()
{
// Call fsm run
fsm.run_machine();
}
I am using this version of the library but I am now getting errors:
collect2.exe*:error: ld returned 1 exit status
Error creating .elf
Is there an include statement missing?
@DeeJee You'll need to post some code. That error can be for multiple reasons (e.g. missing namespace). Try to reproduce the error with as small a program as possible and post it here. Also worth making sure that by "this version" you mean the code in the pull request this issue references?
Also worth making sure that by "this version" you mean the code in the pull request this issue references?
Yes, I was referring to the code in the pull request.
I did what you suggested and made a tiny program with only the state machine code in it. It now compiles fine. That leaves me with a quest to find the code that caused the problem. The main version of the state machine works fine with my code but the code from the pull request does not. I think I made a mistake in my own code that I got away with.... until now.
Anyway, thank you for your tip. I should have thought about that myself :o
I currently have the state machine in use in my project. It is working fine and I an using all three functions. Thank you for creating this library.
I am referring to the three functions that are passed into the State objects. I believe they are called on_enter, on_state and on_exit.