暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

statemachine.cpp 1.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. }