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 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. virtual void enterState(void);
  72. virtual void inState(StateMachineInput smi);
  73. private:
  74. void display(void);
  75. CountFuncPtr countFunc;
  76. GetFuncPtr getFunc;
  77. CallFuncPtr callFunc;
  78. int menuPos, menuOff;
  79. int count;
  80. Array<const char *, STATE_ARRAY_SIZE> contents;
  81. };
  82. template <typename T>
  83. class StateValue : public State {
  84. public:
  85. StateValue(State *_parent, T &_value, T _min, T _max);
  86. typedef void(*UpdateFuncPtr)(T value);
  87. void onUpdate(UpdateFuncPtr func);
  88. virtual void enterState(void);
  89. virtual void inState(StateMachineInput smi);
  90. private:
  91. void display(void);
  92. T &value;
  93. T min, max;
  94. UpdateFuncPtr updateFunc;
  95. };
  96. #endif // _STATE_MACHINE_H_