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.9KB

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