forked from portavales/esp32-car-altimeter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig_screen.cpp
184 lines (159 loc) · 5.31 KB
/
config_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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include "config_screen.h"
#include "fonts.h"
#include "utils.h"
const char* AltimeterConfigScreen::version = "v0.0.2";
AltimeterConfigScreen::AltimeterConfigScreen() {
// Init to null the config
memset(&_config, 0, sizeof(_config));
_config.screen_rotation = 1;
_config.brightness = 2;
_config.plot_history_period = 1;
_config.debug = false;
}
bool AltimeterConfigScreen::begin(TFT_eSprite* screen_p, Preferences* prefs_p) {
_prefs = prefs_p;
_screen = screen_p;
showTitle();
return true;
}
void AltimeterConfigScreen::set_config(const altimeter_config_t& config) {
_config = config;
}
void AltimeterConfigScreen::saveState() {
_prefs->begin("car-altimeter", false);
_prefs->putInt("screen_rotation", _config.screen_rotation);
_prefs->putInt("brightness", _config.brightness);
_prefs->putInt("plot_history", _config.plot_history_period); // Preferences lib needs the name of the pref to be a short string and the int is a uint8 AFAICT
_prefs->putBool("debug", _config.debug);
_prefs->end();
}
void AltimeterConfigScreen::loadState() {
_prefs->begin("car-altimeter", false);
_config.screen_rotation = _prefs->getInt("screen_rotation", 1);
if (_config.screen_rotation != 1 && _config.screen_rotation != 3) {
_config.screen_rotation = 1;
}
_config.brightness = _prefs->getInt("brightness", 2);
if (_config.brightness < 1 || _config.brightness > 3) {
_config.brightness = 2;
}
_config.plot_history_period = _prefs->getInt("plot_history", 1);
if (_config.plot_history_period != 1 && _config.plot_history_period != 5 && _config.plot_history_period != 10) {
_config.plot_history_period = 1;
}
_config.debug = _prefs->getBool("debug", false);
_prefs->end();
}
void AltimeterConfigScreen::showTitle() {
_show_title_ts = millis() + SHOW_TITLE_PERIOD;
}
void AltimeterConfigScreen::button1() {
// _config.screen_rotation = (_config.screen_rotation + 1) % 4;
switch(_selected_field) {
case 0:
if (_config.screen_rotation == 1) {
_config.screen_rotation = 3;
} else {
_config.screen_rotation = 1;
}
break;
case 1:
_config.brightness = _config.brightness + 1;
if (_config.brightness > 3) {
_config.brightness = 1;
}
break;
case 2:
static int plot_history_period_values[] = {1, 5, 10};
for (int i = 0; i < 3; i++) {
if (plot_history_period_values[i] == _config.plot_history_period) {
_config.plot_history_period = plot_history_period_values[(i + 1) % 3];
break;
}
}
break;
case 3:
_config.debug = !_config.debug;
break;
case 4:
saveState();
_is_active = false;
break;
}
}
void AltimeterConfigScreen::button2() {
// _config.screen_rotation = (_config.screen_rotation + 1) % 4;
_selected_field = (_selected_field + 1) % 5;
}
void AltimeterConfigScreen::draw() {
static const String title = "Config";
if (millis() < _show_title_ts) {
_screen->loadFont(NotoSansMono_Bold_50);
// hackly measured for the NotoSansMono_Bold_50 font
#define PIXELS_PER_CHAR 30 //(int)(320/10.6)
_screen->setTextColor(COLOR_OFF_WHITE, TFT_BLACK);
_screen->setTextWrap(false, false);
const int x = (SCREEN_WIDTH - (title.length() * PIXELS_PER_CHAR)) / 2;
_screen->drawString(title, x, 70);
_screen->unloadFont();
} else {
_screen->setTextSize(2);
if (_selected_field == 0) {
_screen->setTextColor(COLOR_OFF_WHITE, TFT_BLUE);
} else {
_screen->setTextColor(COLOR_GREY, TFT_BLACK);
}
String screen_rotation_text = "Screen Rotation: ";
if (_config.screen_rotation == 1) {
screen_rotation_text += "[->]";
} else if (_config.screen_rotation == 3) {
screen_rotation_text += "[<-]";
}
_screen->drawString(screen_rotation_text, 10, 30);
if (_selected_field == 1) {
_screen->setTextColor(COLOR_OFF_WHITE, TFT_BLUE);
} else {
_screen->setTextColor(COLOR_GREY, TFT_BLACK);
}
_screen->drawString(String("Brightness: ") + String(_config.brightness), 10, 50);
if (_selected_field == 2) {
_screen->setTextColor(COLOR_OFF_WHITE, TFT_BLUE);
} else {
_screen->setTextColor(COLOR_GREY, TFT_BLACK);
}
String plot_history_text = "Plot History: ";
if (_config.plot_history_period == 1) {
plot_history_text += "5 mins";
} else if (_config.plot_history_period == 5) {
plot_history_text += "30 mins";
} else if (_config.plot_history_period == 10) {
plot_history_text += "1 hour";
}
_screen->drawString(plot_history_text, 10, 70);
if (_selected_field == 3) {
_screen->setTextColor(COLOR_OFF_WHITE, TFT_BLUE);
} else {
_screen->setTextColor(COLOR_GREY, TFT_BLACK);
}
String debug_text = "Debug Info: ";
if (_config.debug == true) {
debug_text += "enabled";
} else {
debug_text += "disabled";
}
_screen->drawString(debug_text, 10, 90);
if (_selected_field == 4) {
_screen->setTextColor(COLOR_OFF_WHITE, TFT_BLUE);
} else {
_screen->setTextColor(COLOR_GREY, TFT_BLACK);
}
_screen->drawString("SAVE", 10, 110);
_screen->setTextColor(COLOR_GREY, TFT_BLACK);
_screen->drawString("version: " + String(version), 10, 150);
}
}
void AltimeterConfigScreen::set_active() {
_is_active = true;
_selected_field = 0;
showTitle();
}