-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy path__dd_misc_connect.h
More file actions
134 lines (128 loc) · 5.49 KB
/
__dd_misc_connect.h
File metadata and controls
134 lines (128 loc) · 5.49 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// #if __GNUC__ >= 8 && defined(ESP32)
// #define DD_CPP_FUNCTIONAL
// #endif
// #if defined(DD_CPP_FUNCTIONAL)
// #include <functional>
// #endif
#ifndef DD_NO_PASSIVE_CONNECT
/// @brief
/// Helper class to manage passive connection to DumbDisplay.
/// The only method that should be called in `void loop() {}` is DDPassiveConnectionHelper::loop(),
/// passing to it some callbacks
/// Note that it will call DumbDisplay::masterReset() when reconnected (i.e. lost previous connection)
/// @since v0.9.9
class DDMasterResetPassiveConnectionHelper {
public:
/// @param saveAndPlaybackWhenInit if true, will call DumbDisplay::recordLayerCommands() / DumbDisplay::playbackLayerCommands() before and after calling `initializeCallback`
DDMasterResetPassiveConnectionHelper(DumbDisplay& dumbdisplay, bool saveAndPlaybackWhenInit = false) : dumbdisplay(dumbdisplay), saveAndPlaybackWhenInit(saveAndPlaybackWhenInit) {
this->initState = -2;
}
public:
/// @param initializeCallback called after DumbDisplay is connected (or reconnected)
/// @param updateCallback called to update DumbDisplay components
/// @param disconnectedCallback called after "master reset" DumbDisplay, i.e. lost previous connection
bool loop(void (*initializeCallback)(), void (*updateCallback)(), void (*disconnectedCallback)() = NULL) {
// #if defined(DD_CPP_FUNCTIONAL)
// bool loop(std::function<void()> initializeCallback, std::function<void()> updateCallback, std::function<void()> disconnectedCallback = NULL) {
// #else
// bool loop(void (*initializeCallback)(), void (*updateCallback)(), void (*disconnectedCallback)() = NULL) {
// #endif
DDConnectPassiveStatus connectStatus;
dumbdisplay.connectPassive(&connectStatus);
if (connectStatus.connected) {
if (connectStatus.reconnecting) {
// if reconnecting (i.e. lost previous connection, "master reset" DumbDisplay)
dumbdisplay.masterReset();
this->initState = 0;
if (disconnectedCallback != NULL) disconnectedCallback();
return false;
}
if (this->initState <= 0) {
if (initializeCallback != NULL) {
if (this->saveAndPlaybackWhenInit) {
dumbdisplay.recordLayerCommands();
}
initializeCallback();
if (this->saveAndPlaybackWhenInit) {
dumbdisplay.playbackLayerCommands();
}
}
this->initState = 1;
}
if (updateCallback != NULL) updateCallback();
if (this->initState == -3) {
// just masterReset
this->initState = 0;
} else {
this->initState = 2;
}
return true;
} else {
if (this->initState == -2) {
this->initState = 0; // so initially will go to 0 first
} else if (this->initState == 0) {
this->initState = -1;
}
}
return false;
}
inline bool initialized() { return this->initState > 0; }
inline bool firstUpdated() { return this->initState > 1; }
inline bool isIdle() { return this->initState <= 0; }
inline bool justBecameIdle() { return this->initState == 0; }
/// normally, "master reset" will be called automatically when lost connection; but can be called explicitly;
/// note that if called explicitly, will not call disconnectedCallback;
/// IMPORTANT: should only call it in updateCallback, and after calling it, should return immediately
void masterReset() {
dumbdisplay.masterReset();
this->initState = -3;
}
public:
DumbDisplay& dumbdisplay;
private:
bool saveAndPlaybackWhenInit;
int8_t initState;
};
/// @brief
/// Helper class to manage passive connection to DumbDisplay.
/// The only method that should be called in `void loop() {}` is DDPassiveConnectionHelper::loop(),
/// passing to it some callbacks
/// Note that it will surround call to `initializeCallback` with calls to DumbDisplay::recordLayerSetupCommands() and DumbDisplay::playbackLayerSetupCommands()
/// @since v0.9.9
class DDReconnectPassiveConnectionHelper {
public:
/// @param layerSetupPersistId used when calling DumbDisplay::playbackLayerSetupCommands()
DDReconnectPassiveConnectionHelper(DumbDisplay& dumbdisplay, const String& layerSetupPersistId) : dumbdisplay(dumbdisplay) {
this->layerSetupPersistId = layerSetupPersistId;
this->init = false;
}
public:
/// @param initializeCallback called after DumbDisplay is connected (or reconnected)
/// @param updateCallback called to update DumbDisplay components
/// @param disconnectedCallback called after "master reset" DumbDisplay, i.e. lost previous connection
bool loop(void (*initializeCallback)(), void (*updateCallback)()) {
// #if defined(DD_CPP_FUNCTIONAL)
// bool loop(std::function<void()> initializeCallback, std::function<void()> updateCallback) {
// #else
// bool loop(void (*initializeCallback)(), void (*updateCallback)()) {
// #endif
if (dumbdisplay.connectPassive()) {
if (!this->init) {
this->dumbdisplay.recordLayerSetupCommands();
if (initializeCallback != NULL) initializeCallback();
this->dumbdisplay.playbackLayerSetupCommands(this->layerSetupPersistId);
this->init = true;
}
if (updateCallback != NULL) updateCallback();
return true;
}
return false;
}
inline bool initialized() { return this->init; }
public:
DumbDisplay& dumbdisplay;
private:
String layerSetupPersistId;
bool init;
};
#endif