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.cpp 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #include <Arduino.h>
  2. #include "config.h"
  3. #include "config_pins.h"
  4. #include "lcd.h"
  5. #include "states.h"
  6. State::State(State *_parent) : parent(_parent), child(NULL), title("no title") {
  7. if (_parent != NULL) {
  8. _parent->setChild(this);
  9. }
  10. }
  11. // --------------------------------------
  12. StateText::StateText(State *_parent) : State(_parent) {
  13. heading = "";
  14. text = "";
  15. onEnterFunc = []() { };
  16. whenInFunc = [](StateMachineInput smi) {
  17. State *s = states_get();
  18. if (smi.click && (s != NULL) && (s->getChild() != NULL)) {
  19. states_go_to(s->getChild());
  20. }
  21. };
  22. }
  23. void StateText::setHeading(const char *_heading) {
  24. heading = _heading;
  25. }
  26. void StateText::setText(const char *_text) {
  27. text = _text;
  28. }
  29. void StateText::onEnter(EnterFuncPtr func) {
  30. onEnterFunc = func;
  31. }
  32. void StateText::whenIn(InFuncPtr func) {
  33. whenInFunc = func;
  34. }
  35. void StateText::enterState(void) {
  36. lcd_clear();
  37. lcd_set_heading(heading);
  38. lcd_set_text(text);
  39. onEnterFunc();
  40. }
  41. void StateText::inState(struct StateMachineInput smi) {
  42. whenInFunc(smi);
  43. }
  44. // --------------------------------------
  45. StateMenu::StateMenu(State *_parent) : State(_parent) {
  46. menuPos = 0;
  47. }
  48. void StateMenu::setChild(State *_child) {
  49. children.push_back(_child);
  50. }
  51. void StateMenu::addChild(State *_child, int pos) {
  52. if (pos < 0) {
  53. setChild(_child);
  54. } else {
  55. // TODO insert child at pos in children
  56. }
  57. }
  58. void StateMenu::enterState(void) {
  59. menuPos = 0;
  60. }
  61. void StateMenu::inState(struct StateMachineInput smi) {
  62. }
  63. // --------------------------------------
  64. StateDynamicMenu::StateDynamicMenu(State *_parent = NULL) : State(_parent) { }
  65. void StateDynamicMenu::dataCount(CountFuncPtr count) {
  66. countFunc = count;
  67. }
  68. void StateDynamicMenu::dataGet(GetFuncPtr get) {
  69. getFunc = get;
  70. }
  71. void StateDynamicMenu::dataCall(CallFuncPtr call) {
  72. callFunc = call;
  73. }
  74. void StateDynamicMenu::display(void) {
  75. }
  76. void StateDynamicMenu::enterState(void) {
  77. // cache all entries on entering state
  78. count = countFunc();
  79. contents.clear();
  80. for (int i = 0; i < count; i++) {
  81. contents.push_back(getFunc(i));
  82. }
  83. display();
  84. }
  85. void StateDynamicMenu::inState(StateMachineInput smi) {
  86. }