No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

statemachine.h 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #ifndef _STATE_MACHINE_H_
  2. #define _STATE_MACHINE_H_
  3. #include <Array.h>
  4. #define STATE_ARRAY_SIZE 42
  5. struct StateMachineInput {
  6. StateMachineInput(bool c, int e, int r, bool k, bool *m, bool am)
  7. : click(c), encoder(e), rpm(r), kill(k), all_motors_done(am) {
  8. for (int i = 0; i < AXIS_COUNT; i++) {
  9. motor_done[i] = m[i];
  10. }
  11. };
  12. bool click;
  13. int encoder;
  14. int rpm;
  15. bool kill;
  16. bool motor_done[AXIS_COUNT];
  17. bool all_motors_done;
  18. };
  19. class State {
  20. public:
  21. typedef void(*EnterFuncPtr)(void);
  22. typedef void(*InFuncPtr)(StateMachineInput smi);
  23. State(State *_parent = NULL);
  24. State *getParent(void) { return parent; }
  25. virtual void setChild(State *_child) { child = _child; }
  26. State *getChild(void) { return child; }
  27. void setTitle(const char *_title) { title = _title; }
  28. const char *getTitle(void) { return title; }
  29. void setHeading(const char *_heading) { heading = _heading; }
  30. const char *getHeading(void) { return heading; }
  31. void setText(const char *_text) { text = _text; }
  32. const char *getText(void) { return text; }
  33. void onEnter(EnterFuncPtr func) { onEnterFunc = func; }
  34. void whenIn(InFuncPtr func) { whenInFunc = func; }
  35. void updateText(void);
  36. virtual void enterState(void);
  37. virtual void inState(StateMachineInput smi);
  38. protected:
  39. State *parent;
  40. State *child;
  41. const char *title;
  42. const char *heading;
  43. const char *text;
  44. EnterFuncPtr onEnterFunc;
  45. InFuncPtr whenInFunc;
  46. };
  47. class StateMenu : public State {
  48. public:
  49. StateMenu(State *_parent = NULL, bool _show_parent = true);
  50. virtual void setChild(State *_child);
  51. void addChild(State *_child, int pos = -1);
  52. virtual void enterState(void);
  53. virtual void inState(StateMachineInput smi);
  54. private:
  55. void display(void);
  56. bool show_parent;
  57. int menuPos, menuOff;
  58. Array<State *, STATE_ARRAY_SIZE> children;
  59. };
  60. class StateDynamicMenu : public State {
  61. public:
  62. StateDynamicMenu(State *_parent = NULL);
  63. typedef int(*CountFuncPtr)(void);
  64. typedef const char *(*GetFuncPtr)(int);
  65. typedef void(*CallFuncPtr)(int);
  66. void dataCount(CountFuncPtr count);
  67. void dataGet(GetFuncPtr get);
  68. void dataCall(CallFuncPtr call);
  69. void setPrefix(String pre);
  70. virtual void enterState(void);
  71. virtual void inState(StateMachineInput smi);
  72. private:
  73. void display(void);
  74. CountFuncPtr countFunc;
  75. GetFuncPtr getFunc;
  76. CallFuncPtr callFunc;
  77. int menuPos, menuOff;
  78. int count;
  79. Array<String, STATE_ARRAY_SIZE> contents;
  80. String prefix;
  81. };
  82. template <typename T>
  83. class StateValue : public State {
  84. public:
  85. StateValue(State *_parent, T &_value, T _min, T _max);
  86. void setMax(T _max);
  87. typedef void(*UpdateFuncPtr)(T value);
  88. void onLiveUpdate(UpdateFuncPtr func);
  89. void onUpdate(UpdateFuncPtr func);
  90. virtual void enterState(void);
  91. virtual void inState(StateMachineInput smi);
  92. private:
  93. void display(void);
  94. T &value;
  95. T min, max;
  96. UpdateFuncPtr updateFunc, updateLiveFunc;
  97. };
  98. template <typename T, size_t N>
  99. class StateValues : public State {
  100. public:
  101. StateValues(State *_parent);
  102. void setData(size_t index, const char *name, T *value, T min, T max);
  103. typedef void(*UpdateFuncPtr)(size_t index, T value);
  104. void onLiveUpdate(UpdateFuncPtr func);
  105. void onUpdate(UpdateFuncPtr func);
  106. virtual void enterState(void);
  107. virtual void inState(StateMachineInput smi);
  108. private:
  109. void display(void);
  110. T *values[N];
  111. T mins[N], maxs[N];
  112. const char *texts[N];
  113. const char *heading;
  114. size_t pos;
  115. bool editing;
  116. UpdateFuncPtr updateFunc, updateLiveFunc;
  117. };
  118. #endif // _STATE_MACHINE_H_