-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDisplayManager.cpp
162 lines (149 loc) · 4.89 KB
/
DisplayManager.cpp
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
* © 2024 Peter Cole
*
* This is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* It is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this code. If not, see <https://www.gnu.org/licenses/>.
*/
#include "DisplayManager.h"
#ifndef PIO_UNIT_TESTING // Cannot create physical displays with Platform IO testing
#include "TFT_eSPIDisplay.h"
#endif // PIO_UNIT_TESTING
DisplayManager::DisplayManager() : _firstDisplay(nullptr), _logger(nullptr), _nextDisplayId(0) {}
void DisplayManager::addDisplay(DisplayInterface *display) {
LOG(LogLevel::LOG_DEBUG, "DisplayManager::addDisplay()");
if (display == nullptr) {
return;
}
display->setId(_nextDisplayId++);
if (_logger != nullptr) {
display->setLogger(_logger);
}
if (_firstDisplay == nullptr) {
_firstDisplay = display;
return;
}
DisplayInterface *current = _firstDisplay;
while (current->getNext() != nullptr) {
current = current->getNext();
}
current->setNext(display);
}
void DisplayManager::startDisplays() {
LOG(LogLevel::LOG_DEBUG, "DisplayManager::startDisplays()");
if (_firstDisplay == nullptr) {
return;
}
_setupAllSPICSPins();
for (auto *display = _firstDisplay; display; display = display->getNext()) {
display->begin();
}
}
void DisplayManager::clearAllDisplays() {
LOG(LogLevel::LOG_DEBUG, "DisplayManager::clearAllDisplays()");
if (_firstDisplay == nullptr) {
return;
}
for (auto *display = _firstDisplay; display; display = display->getNext()) {
// Select this SPI display if necessary
_selectSPIDisplay(display);
display->clearScreen();
}
}
void DisplayManager::displayStartupInfo(const char *version) {
LOG(LogLevel::LOG_DEBUG, "DisplayManager::displayStartupInfo(%s)", version);
// Do nothing if we don't have a DisplayManager or any displays
if (_firstDisplay == nullptr) {
return;
}
for (auto *display = _firstDisplay; display; display = display->getNext()) {
// Select this SPI display if necessary
_selectSPIDisplay(display);
display->displayStartupInfo(version);
}
}
void DisplayManager::update(ScreenManager *screenManager) {
// Do nothing if there is no ScreenManager with Screen info
if (screenManager == nullptr) {
return;
}
// Iterate through each display
for (auto *display = _firstDisplay; display; display = display->getNext()) {
// Select this SPI display if necessary
_selectSPIDisplay(display);
// If the screen ID for this display is invalid, default to the same ID
int screenId = display->getScreenId();
if (screenId == -1) {
screenId = display->getId();
display->setScreenId(screenId);
}
// Get the current screen for this display
Screen *screen = screenManager->getScreenById(screenId);
// If there is one, display it
if (screen) {
display->displayScreen(screen);
}
}
}
DisplayInterface *DisplayManager::getFirstDisplay() { return _firstDisplay; }
DisplayInterface *DisplayManager::getDisplayById(uint8_t displayId) {
for (DisplayInterface *display = _firstDisplay; display; display = display->getNext()) {
if (display->getId() == displayId) {
return display;
}
}
return nullptr;
}
void DisplayManager::setLogger(Logger *logger) { _logger = logger; }
DisplayManager::~DisplayManager() {
if (_firstDisplay != nullptr) {
DisplayInterface *display = _firstDisplay;
DisplayInterface *next = nullptr;
while (display) {
next = display->getNext();
delete display;
display = next;
}
_firstDisplay = nullptr;
}
_logger = nullptr;
}
void DisplayManager::_setupAllSPICSPins() {
for (DisplayInterface *display = _firstDisplay; display; display = display->getNext()) {
int csPin = display->getCSPin();
// If not set, don't do anything and move to the next display
if (csPin == -1) {
continue;
}
// Set the pin to output mode
pinMode(csPin, OUTPUT);
// Then enable the display ready for _tft->init()
digitalWrite(csPin, LOW);
}
}
void DisplayManager::_selectSPIDisplay(DisplayInterface *display) {
// If not set, don't do anything
int csPin = display->getCSPin();
if (csPin == -1) {
return;
}
// First every other display needs to be disabled
for (DisplayInterface *other = _firstDisplay; other; other = other->getNext()) {
if (other != display) {
if (other->getCSPin() != -1) {
digitalWrite(other->getCSPin(), HIGH);
}
}
}
// Otherwise set CS pin low to select this screen
digitalWrite(csPin, LOW);
}