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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #ifndef _STATE_MACHINE_H_
  2. #define _STATE_MACHINE_H_
  3. #include <Array.h>
  4. struct StateMachineInput {
  5. StateMachineInput(int c, int e, int k, int m)
  6. : click(c), encoder(e), kill(k), motors_done(m) { };
  7. int click;
  8. int encoder;
  9. int kill;
  10. int motors_done;
  11. };
  12. class State {
  13. public:
  14. State(State *_parent = NULL);
  15. State *getParent(void) { return parent; }
  16. virtual void setChild(State *_child) { child = _child; }
  17. State *getChild(void) { return child; }
  18. void setTitle(const char *_title) { title = _title; }
  19. const char *getTitle(void) { return title; }
  20. virtual void enterState(void) = 0;
  21. virtual void inState(StateMachineInput smi) = 0;
  22. private:
  23. State *parent;
  24. State *child;
  25. const char *title;
  26. };
  27. class StateText : public State {
  28. public:
  29. StateText(State *_parent = NULL);
  30. void setHeading(const char *_heading);
  31. void setText(const char *_text);
  32. typedef void(*EnterFuncPtr)(void);
  33. typedef void(*InFuncPtr)(StateMachineInput smi);
  34. void onEnter(EnterFuncPtr func);
  35. void whenIn(InFuncPtr func);
  36. virtual void enterState(void);
  37. virtual void inState(StateMachineInput smi);
  38. private:
  39. const char *heading;
  40. const char *text;
  41. EnterFuncPtr onEnterFunc;
  42. InFuncPtr whenInFunc;
  43. };
  44. class StateMenu : public State {
  45. public:
  46. StateMenu(State *_parent = NULL);
  47. virtual void setChild(State *_child);
  48. void addChild(State *_child, int pos = -1);
  49. virtual void enterState(void);
  50. virtual void inState(StateMachineInput smi);
  51. private:
  52. int menuPos;
  53. Array<State *, 42> children;
  54. };
  55. class StateDynamicMenu : public State {
  56. public:
  57. StateDynamicMenu(State *_parent = NULL);
  58. typedef int(*CountFuncPtr)(void);
  59. typedef const char *(*GetFuncPtr)(int);
  60. typedef void(*CallFuncPtr)(int);
  61. void dataCount(CountFuncPtr count);
  62. void dataGet(GetFuncPtr get);
  63. void dataCall(CallFuncPtr call);
  64. virtual void enterState(void);
  65. virtual void inState(StateMachineInput smi);
  66. private:
  67. void display(void);
  68. CountFuncPtr countFunc;
  69. GetFuncPtr getFunc;
  70. CallFuncPtr callFunc;
  71. int count;
  72. Array<const char *, 42> contents;
  73. };
  74. #endif // _STATE_MACHINE_H_