123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- #ifndef _STATE_MACHINE_H_
- #define _STATE_MACHINE_H_
-
- #include <Array.h>
-
- #define STATE_ARRAY_SIZE 42
-
- struct StateMachineInput {
- StateMachineInput(bool c, int e, int r, bool k, bool *m, bool am)
- : click(c), encoder(e), rpm(r), kill(k), all_motors_done(am) {
- for (int i = 0; i < AXIS_COUNT; i++) {
- motor_done[i] = m[i];
- }
- };
-
- bool click;
- int encoder;
- int rpm;
- bool kill;
- bool motor_done[AXIS_COUNT];
- bool all_motors_done;
- };
-
- class State {
- public:
- typedef void(*EnterFuncPtr)(void);
- typedef void(*InFuncPtr)(StateMachineInput smi);
-
- State(State *_parent = NULL);
- State *getParent(void) { return parent; }
-
- virtual void setChild(State *_child) { child = _child; }
- State *getChild(void) { return child; }
-
- void setTitle(const char *_title) { title = _title; }
- const char *getTitle(void) { return title; }
-
- void setHeading(const char *_heading) { heading = _heading; }
- const char *getHeading(void) { return heading; }
-
- void setText(const char *_text) { text = _text; }
- const char *getText(void) { return text; }
-
- void onEnter(EnterFuncPtr func) { onEnterFunc = func; }
- void whenIn(InFuncPtr func) { whenInFunc = func; }
-
- void updateText(void);
-
- virtual void enterState(void);
- virtual void inState(StateMachineInput smi);
-
- protected:
- State *parent;
- State *child;
- const char *title;
- const char *heading;
- const char *text;
- EnterFuncPtr onEnterFunc;
- InFuncPtr whenInFunc;
- };
-
- class StateMenu : public State {
- public:
- StateMenu(State *_parent = NULL, bool _show_parent = true);
- virtual void setChild(State *_child);
-
- void addChild(State *_child, int pos = -1);
-
- virtual void enterState(void);
- virtual void inState(StateMachineInput smi);
-
- private:
- void display(void);
-
- bool show_parent;
- int menuPos, menuOff;
- Array<State *, STATE_ARRAY_SIZE> children;
- };
-
- class StateDynamicMenu : public State {
- public:
- StateDynamicMenu(State *_parent = NULL);
-
- typedef int(*CountFuncPtr)(void);
- typedef const char *(*GetFuncPtr)(int);
- typedef void(*CallFuncPtr)(int);
-
- void dataCount(CountFuncPtr count);
- void dataGet(GetFuncPtr get);
- void dataCall(CallFuncPtr call);
-
- void setPrefix(String pre);
-
- virtual void enterState(void);
- virtual void inState(StateMachineInput smi);
-
- private:
- void display(void);
-
- CountFuncPtr countFunc;
- GetFuncPtr getFunc;
- CallFuncPtr callFunc;
-
- int menuPos, menuOff;
- int count;
- Array<String, STATE_ARRAY_SIZE> contents;
- String prefix;
- };
-
- template <typename T>
- class StateValue : public State {
- public:
- StateValue(State *_parent, T &_value, T _min, T _max);
-
- void setMax(T _max);
-
- typedef void(*UpdateFuncPtr)(T value);
-
- void onLiveUpdate(UpdateFuncPtr func);
- void onUpdate(UpdateFuncPtr func);
-
- virtual void enterState(void);
- virtual void inState(StateMachineInput smi);
-
- private:
- void display(void);
-
- T &value;
- T min, max;
- UpdateFuncPtr updateFunc, updateLiveFunc;
- };
-
- template <typename T, size_t N>
- class StateValues : public State {
- public:
- StateValues(State *_parent);
-
- void setData(size_t index, const char *name, T *value, T min, T max);
-
- typedef void(*UpdateFuncPtr)(size_t index, T value);
-
- void onLiveUpdate(UpdateFuncPtr func);
- void onUpdate(UpdateFuncPtr func);
-
- virtual void enterState(void);
- virtual void inState(StateMachineInput smi);
-
- private:
- void display(void);
-
- T *values[N];
- T mins[N], maxs[N];
- const char *texts[N];
- const char *heading;
- size_t pos;
- bool editing;
- UpdateFuncPtr updateFunc, updateLiveFunc;
- };
-
- #endif // _STATE_MACHINE_H_
|