暫無描述
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 4.1KB

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