-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStateManager.cpp
More file actions
78 lines (64 loc) · 2.18 KB
/
StateManager.cpp
File metadata and controls
78 lines (64 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
//
// Created by me on 21/04/2023.
//
#include "StateManager.h"
#include "BaseState.h"
StateManager::StateManager() {
this->currentState = nullptr;
leftButton = new AceButton(Pins::DIGITAL_PIN_BUTTON_L);
rightButton = new AceButton(Pins::DIGITAL_PIN_BUTTON_R);
leftButton->setButtonConfig(&buttonConfig);
rightButton->setButtonConfig(&buttonConfig);
buttonConfig.setIEventHandler(this);
buttonConfig.setFeature(ButtonConfig::kFeatureClick);
buttonConfig.setFeature(ButtonConfig::kFeatureDoubleClick);
buttonConfig.setFeature(ButtonConfig::kFeatureLongPress);
buttonConfig.setFeature(ButtonConfig::kFeatureSuppressAfterClick);
buttonConfig.setFeature(ButtonConfig::kFeatureSuppressAfterLongPress);
buttonConfig.setFeature(ButtonConfig::kFeatureSuppressAfterDoubleClick);
}
StateManager::~StateManager() {
if (this->currentState != nullptr) {
delete this->currentState;
}
delete leftButton;
delete rightButton;
}
void StateManager::loop() {
leftButton->check();
rightButton->check();
if (this->currentState != nullptr) {
this->currentState->loop();
}
}
void StateManager::changeState(BaseState *state) {
if (this->currentState != nullptr) {
this->currentState->exit();
delete this->currentState;
this->currentState = nullptr;
}
this->currentState = state;
if (this->currentState != nullptr) {
this->currentState->enter();
}
}
BaseState *StateManager::getState() {
return this->currentState;
}
void StateManager::handleEvent(AceButton *button, uint8_t eventType, uint8_t buttonState) {
DigitalInputs input = DigitalInputs::BUTTON_UNKNOWN;
if (button == leftButton) {
input = DigitalInputs::BUTTON_L;
} else if (button == rightButton) {
input = DigitalInputs::BUTTON_R;
} else {
Serial.print("Unknown button event: ");
Serial.print(eventType);
Serial.print(" on pin ");
Serial.println(button->getPin());
input = DigitalInputs::BUTTON_UNKNOWN;
}
if(this->currentState != nullptr && input != DigitalInputs::BUTTON_UNKNOWN) {
this->currentState->handleEvent(input, eventType);
}
}