123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- #ifndef _STATE_MACHINE_H_
- #define _STATE_MACHINE_H_
-
- #include <Array.h>
-
- #define STATE_ARRAY_SIZE 42
-
- template <typename T, size_t N>
- void array_print(Array<T, N> *arr);
-
- template <typename T, size_t N>
- void array_insert_at_pos(Array<T, N> *arr, T value, size_t pos);
-
- struct StateMachineInput {
- StateMachineInput(int c, int e, int r, int k, int m)
- : click(c), encoder(e), rpm(r), kill(k), motors_done(m) { };
-
- int click;
- int encoder;
- int rpm;
- int kill;
- int motors_done;
- };
-
- class State {
- public:
- 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; }
-
- virtual void enterState(void) = 0;
- virtual void inState(StateMachineInput smi) = 0;
-
- private:
- State *parent;
- State *child;
- const char *title;
- };
-
- class StateText : public State {
- public:
- StateText(State *_parent = NULL);
-
- void setHeading(const char *_heading);
- void setText(const char *_text);
-
- typedef void(*EnterFuncPtr)(void);
- typedef void(*InFuncPtr)(StateMachineInput smi);
-
- void onEnter(EnterFuncPtr func);
- void whenIn(InFuncPtr func);
-
- void updateText(void);
-
- virtual void enterState(void);
- virtual void inState(StateMachineInput smi);
-
- private:
- 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 setHeading(const char *_heading);
- void setText(const char *_text);
-
- typedef void(*EnterFuncPtr)(void);
- typedef void(*UpdateFuncPtr)(T value);
-
- void onEnter(EnterFuncPtr func);
- 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;
- const char *heading;
- const char *text;
- EnterFuncPtr onEnterFunc;
- 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);
-
- void setHeading(const char *_heading);
- void setText(const char *_text);
-
- typedef void(*EnterFuncPtr)(void);
- typedef void(*UpdateFuncPtr)(size_t index, T value);
-
- void onEnter(EnterFuncPtr func);
- 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;
- EnterFuncPtr onEnterFunc;
- UpdateFuncPtr updateFunc, updateLiveFunc;
- };
-
- #endif // _STATE_MACHINE_H_
|