-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathScreen.cpp
126 lines (112 loc) · 3.67 KB
/
Screen.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
/*
* © 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 "Screen.h"
Screen::Screen(uint8_t screenId) : _screenId(screenId), _next(nullptr), _firstScreenRow(nullptr), _logger(nullptr) {}
uint8_t Screen::getId() { return _screenId; }
void Screen::setNext(Screen *screen) { _next = screen; }
Screen *Screen::getNext() { return _next; }
void Screen::updateScreenRow(uint8_t screenRowId, const char *text) {
if (text == nullptr) {
return;
}
LOG(LogLevel::LOG_DEBUG, "Screen::updateScreenRow[%d](%d, %s)", _screenId, screenRowId, text);
// Check if it exists already
ScreenRow *updateRow = getScreenRowById(screenRowId);
// If not, create and add to the list unless this is supposed to delete it with ""
if (updateRow == nullptr && strcmp(text, "") != 0) {
_addScreenRow(screenRowId, text);
} else {
// If "" this row is to be deleted
if (strcmp(text, "") == 0) {
_deleteScreenRow(updateRow);
} else {
// Otherwise update the text
updateRow->setText(text);
}
}
}
ScreenRow *Screen::getFirstScreenRow() { return _firstScreenRow; }
ScreenRow *Screen::getScreenRowById(uint8_t screenRowId) {
if (_firstScreenRow == nullptr) {
return nullptr;
}
for (ScreenRow *row = _firstScreenRow; row; row = row->getNext()) {
if (row->getId() == screenRowId) {
return row;
}
}
return nullptr;
}
void Screen::setLogger(Logger *logger) { _logger = logger; }
Screen::~Screen() {
if (_firstScreenRow != nullptr) {
ScreenRow *row = _firstScreenRow;
ScreenRow *next = nullptr;
while (row) {
next = row->getNext();
delete row;
row = next;
}
_firstScreenRow = nullptr;
}
_next = nullptr;
}
void Screen::_addScreenRow(uint8_t screenRowId, const char *text) {
LOG(LogLevel::LOG_DEBUG, "Screen::_addScreenRow(%d, %s)", screenRowId, text);
ScreenRow *newRow = new ScreenRow(screenRowId);
newRow->setText(text);
if (_logger != nullptr) {
newRow->setLogger(_logger);
}
newRow->setText(text);
// If we don't have a first, this is it
if (_firstScreenRow == nullptr) {
_firstScreenRow = newRow;
} else {
// Otherwise, iterate through the list and add to the end
ScreenRow *row = _firstScreenRow;
while (row->getNext() != nullptr) {
row = row->getNext();
}
row->setNext(newRow);
}
}
void Screen::_deleteScreenRow(ScreenRow *screenRow) {
if (screenRow == nullptr) {
return;
}
LOG(LogLevel::LOG_DEBUG, "Screen::_deleteScreenRow() - ID: %d", screenRow->getId());
// If it's the first in the list, shuffle to next, delete, and done
if (_firstScreenRow == screenRow) {
ScreenRow *deleteRow = _firstScreenRow;
_firstScreenRow = deleteRow->getNext();
delete deleteRow;
return;
}
// Otherwise iterate through, shuffle, delete, and done
ScreenRow *currentRow = _firstScreenRow;
ScreenRow *previousRow = nullptr;
while (currentRow != nullptr) {
if (currentRow == screenRow) {
previousRow->setNext(currentRow->getNext());
delete currentRow;
return;
}
previousRow = currentRow;
currentRow = currentRow->getNext();
}
}